| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
"net/http/httptest" |
| 6 |
"sync" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/Masterminds/semver/v3" |
| 10 |
"github.com/bradenaw/juniper/xslices" |
| 11 |
"github.com/gin-gonic/gin" |
| 12 |
"github.com/henrybear327/go-proton-api" |
| 13 |
"github.com/henrybear327/go-proton-api/server/backend" |
| 14 |
) |
| 15 |
|
| 16 |
type AuthCacher interface { |
| 17 |
GetAuthInfo(username string) (proton.AuthInfo, bool) |
| 18 |
SetAuthInfo(username string, info proton.AuthInfo) |
| 19 |
GetAuth(username string) (proton.Auth, bool) |
| 20 |
SetAuth(username string, auth proton.Auth) |
| 21 |
} |
| 22 |
|
| 23 |
// StatusHook is a function that can be used to modify the response code of a call. |
| 24 |
type StatusHook func(*http.Request) (int, bool) |
| 25 |
|
| 26 |
type Server struct { |
| 27 |
// r is the gin router. |
| 28 |
r *gin.Engine |
| 29 |
|
| 30 |
// s is the underlying server. |
| 31 |
s *httptest.Server |
| 32 |
|
| 33 |
// b is the server backend, which manages accounts, messages, attachments, etc. |
| 34 |
b *backend.Backend |
| 35 |
|
| 36 |
// callWatchers records callWatchers received by the server. |
| 37 |
callWatchers []callWatcher |
| 38 |
callWatchersLock sync.RWMutex |
| 39 |
|
| 40 |
// statusHooks are hooks that can be used to modify the response code of a call. |
| 41 |
statusHooks []StatusHook |
| 42 |
statusHooksLock sync.RWMutex |
| 43 |
|
| 44 |
// domain is the test server domain. |
| 45 |
domain string |
| 46 |
|
| 47 |
// minAppVersion is the minimum app version that the server will accept. |
| 48 |
minAppVersion *semver.Version |
| 49 |
|
| 50 |
// proxyOrigin is the URL of the origin server when the server is a proxy. |
| 51 |
proxyOrigin string |
| 52 |
|
| 53 |
// proxyTransport is the transport to use when the server is a proxy. |
| 54 |
proxyTransport *http.Transport |
| 55 |
|
| 56 |
// authCacher can optionally be set to cache proxied auth calls. |
| 57 |
authCacher AuthCacher |
| 58 |
|
| 59 |
// offline is whether to pretend the server is offline and return 5xx errors. |
| 60 |
offline bool |
| 61 |
|
| 62 |
// rateLimit is the rate limiter for the server. |
| 63 |
rateLimit *rateLimiter |
| 64 |
} |
| 65 |
|
| 66 |
func New(opts ...Option) *Server { |
| 67 |
builder := newServerBuilder() |
| 68 |
|
| 69 |
for _, opt := range opts { |
| 70 |
opt.config(builder) |
| 71 |
} |
| 72 |
|
| 73 |
return builder.build() |
| 74 |
} |
| 75 |
|
| 76 |
// GetHostURL returns the API root to make calls to. |
| 77 |
func (s *Server) GetHostURL() string { |
| 78 |
return s.s.URL |
| 79 |
} |
| 80 |
|
| 81 |
// GetProxyURL returns the API root to make calls to which should be proxied. |
| 82 |
func (s *Server) GetProxyURL() string { |
| 83 |
return s.s.URL + "/proxy" |
| 84 |
} |
| 85 |
|
| 86 |
// GetDomain returns the domain of the server (e.g. "proton.local"). |
| 87 |
func (s *Server) GetDomain() string { |
| 88 |
return s.domain |
| 89 |
} |
| 90 |
|
| 91 |
// AddCallWatcher adds a call watcher to the server. |
| 92 |
func (s *Server) AddCallWatcher(fn func(Call), paths ...string) { |
| 93 |
s.callWatchersLock.Lock() |
| 94 |
defer s.callWatchersLock.Unlock() |
| 95 |
|
| 96 |
s.callWatchers = append(s.callWatchers, newCallWatcher(fn, paths...)) |
| 97 |
} |
| 98 |
|
| 99 |
// AddStatusHook adds a status hook to the server. |
| 100 |
func (s *Server) AddStatusHook(fn StatusHook) { |
| 101 |
s.statusHooksLock.Lock() |
| 102 |
defer s.statusHooksLock.Unlock() |
| 103 |
|
| 104 |
s.statusHooks = append(s.statusHooks, fn) |
| 105 |
} |
| 106 |
|
| 107 |
// CreateUser creates a new server user with the given username and password. |
| 108 |
// A single address will be created for the user, derived from the username and the server's domain. |
| 109 |
func (s *Server) CreateUser(username string, password []byte) (string, string, error) { |
| 110 |
userID, err := s.b.CreateUser(username, password) |
| 111 |
if err != nil { |
| 112 |
return "", "", err |
| 113 |
} |
| 114 |
|
| 115 |
addrID, err := s.b.CreateAddress(userID, username+"@"+s.domain, password, true, proton.AddressStatusEnabled, proton.AddressTypeOriginal) |
| 116 |
if err != nil { |
| 117 |
return "", "", err |
| 118 |
} |
| 119 |
|
| 120 |
return userID, addrID, nil |
| 121 |
} |
| 122 |
|
| 123 |
func (s *Server) RemoveUser(userID string) error { |
| 124 |
return s.b.RemoveUser(userID) |
| 125 |
} |
| 126 |
|
| 127 |
func (s *Server) RefreshUser(userID string, refresh proton.RefreshFlag) error { |
| 128 |
return s.b.RefreshUser(userID, refresh) |
| 129 |
} |
| 130 |
|
| 131 |
func (s *Server) GetUserKeyIDs(userID string) ([]string, error) { |
| 132 |
user, err := s.b.GetUser(userID) |
| 133 |
if err != nil { |
| 134 |
return nil, err |
| 135 |
} |
| 136 |
|
| 137 |
return xslices.Map(user.Keys, func(key proton.Key) string { |
| 138 |
return key.ID |
| 139 |
}), nil |
| 140 |
} |
| 141 |
|
| 142 |
func (s *Server) CreateUserKey(userID string, password []byte) error { |
| 143 |
return s.b.CreateUserKey(userID, password) |
| 144 |
} |
| 145 |
|
| 146 |
func (s *Server) RemoveUserKey(userID, keyID string) error { |
| 147 |
return s.b.RemoveUserKey(userID, keyID) |
| 148 |
} |
| 149 |
|
| 150 |
func (s *Server) CreateAddress(userID, email string, password []byte) (string, error) { |
| 151 |
return s.b.CreateAddress(userID, email, password, true, proton.AddressStatusEnabled, proton.AddressTypeOriginal) |
| 152 |
} |
| 153 |
|
| 154 |
func (s *Server) CreateAddressAsUpdate(userID, email string, password []byte) (string, error) { |
| 155 |
return s.b.CreateAddressAsUpdate(userID, email, password, true, proton.AddressStatusEnabled, proton.AddressTypeOriginal) |
| 156 |
} |
| 157 |
|
| 158 |
func (s *Server) ChangeAddressType(userID, addrId string, addrType proton.AddressType) error { |
| 159 |
return s.b.ChangeAddressType(userID, addrId, addrType) |
| 160 |
} |
| 161 |
|
| 162 |
func (s *Server) RemoveAddress(userID, addrID string) error { |
| 163 |
return s.b.RemoveAddress(userID, addrID) |
| 164 |
} |
| 165 |
|
| 166 |
func (s *Server) CreateAddressKey(userID, addrID string, password []byte) error { |
| 167 |
return s.b.CreateAddressKey(userID, addrID, password) |
| 168 |
} |
| 169 |
|
| 170 |
func (s *Server) RemoveAddressKey(userID, addrID, keyID string) error { |
| 171 |
return s.b.RemoveAddressKey(userID, addrID, keyID) |
| 172 |
} |
| 173 |
|
| 174 |
func (s *Server) CreateLabel(userID, name, parentID string, labelType proton.LabelType) (string, error) { |
| 175 |
label, err := s.b.CreateLabel(userID, name, parentID, labelType) |
| 176 |
if err != nil { |
| 177 |
return "", err |
| 178 |
} |
| 179 |
|
| 180 |
return label.ID, nil |
| 181 |
} |
| 182 |
|
| 183 |
func (s *Server) GetLabels(userID string) ([]proton.Label, error) { |
| 184 |
return s.b.GetLabels(userID) |
| 185 |
} |
| 186 |
|
| 187 |
func (s *Server) LabelMessage(userID, msgID, labelID string) error { |
| 188 |
return s.b.LabelMessages(userID, labelID, msgID) |
| 189 |
} |
| 190 |
|
| 191 |
func (s *Server) UnlabelMessage(userID, msgID, labelID string) error { |
| 192 |
return s.b.UnlabelMessages(userID, labelID, msgID) |
| 193 |
} |
| 194 |
|
| 195 |
func (s *Server) AddAddressCreatedEvent(userID, addrID string) error { |
| 196 |
return s.b.AddAddressCreatedUpdate(userID, addrID) |
| 197 |
} |
| 198 |
|
| 199 |
func (s *Server) AddLabelCreatedEvent(userID, labelID string) error { |
| 200 |
return s.b.AddLabelCreatedUpdate(userID, labelID) |
| 201 |
} |
| 202 |
|
| 203 |
func (s *Server) AddMessageCreatedEvent(userID, messageID string) error { |
| 204 |
return s.b.AddMessageCreatedUpdate(userID, messageID) |
| 205 |
} |
| 206 |
|
| 207 |
// SetMaxUpdatesPerEvent |
| 208 |
func (s *Server) SetMaxUpdatesPerEvent(max int) { |
| 209 |
s.b.SetMaxUpdatesPerEvent(max) |
| 210 |
} |
| 211 |
|
| 212 |
func (s *Server) SetAuthLife(authLife time.Duration) { |
| 213 |
s.b.SetAuthLife(authLife) |
| 214 |
} |
| 215 |
|
| 216 |
func (s *Server) SetMinAppVersion(minAppVersion *semver.Version) { |
| 217 |
s.minAppVersion = minAppVersion |
| 218 |
} |
| 219 |
|
| 220 |
func (s *Server) SetOffline(offline bool) { |
| 221 |
s.offline = offline |
| 222 |
} |
| 223 |
|
| 224 |
func (s *Server) RevokeUser(userID string) error { |
| 225 |
sessions, err := s.b.GetSessions(userID) |
| 226 |
if err != nil { |
| 227 |
return err |
| 228 |
} |
| 229 |
|
| 230 |
for _, session := range sessions { |
| 231 |
if err := s.b.DeleteSession(userID, session.UID); err != nil { |
| 232 |
return err |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
return nil |
| 237 |
} |
| 238 |
|
| 239 |
func (s *Server) Close() { |
| 240 |
s.proxyTransport.CloseIdleConnections() |
| 241 |
s.s.Close() |
| 242 |
} |