auth
auth/context.go
Update client to detect errors. The client doesn't treat non-200 responses as errors automatically, so we need to detect when the response.Errors property is set, and use that to return an error. To avoid the boilerplate and an extensive error system, I just wrapped them in an httpErrors type that implements the error interface. That way the errors can be returned, and callers can type-cast and interrogate them. I also updated the GetLogin function to return an auth.ErrLoginNotFound error when the httpErrors response indicates that's the reason the request failed.
| paddy@50 | 1 package auth |
| paddy@50 | 2 |
| paddy@50 | 3 import ( |
| paddy@50 | 4 "html/template" |
| paddy@50 | 5 "io" |
| paddy@55 | 6 "log" |
| paddy@77 | 7 "net/url" |
| paddy@50 | 8 "time" |
| paddy@50 | 9 |
| paddy@178 | 10 "code.secondbit.org/events.hg" |
| paddy@107 | 11 "code.secondbit.org/uuid.hg" |
| paddy@50 | 12 ) |
| paddy@50 | 13 |
| paddy@57 | 14 // Context wraps the different storage interfaces and should |
| paddy@57 | 15 // be used as the main point of interaction for the data storage |
| paddy@57 | 16 // layer. |
| paddy@50 | 17 type Context struct { |
| paddy@178 | 18 template *template.Template |
| paddy@178 | 19 loginURI *url.URL |
| paddy@178 | 20 clients clientStore |
| paddy@178 | 21 authCodes authorizationCodeStore |
| paddy@178 | 22 profiles profileStore |
| paddy@178 | 23 tokens tokenStore |
| paddy@178 | 24 sessions sessionStore |
| paddy@178 | 25 scopes scopeStore |
| paddy@178 | 26 eventsPublisher events.Publisher |
| paddy@178 | 27 config Config |
| paddy@96 | 28 } |
| paddy@96 | 29 |
| paddy@96 | 30 // NewContext takes a Config instance and uses it to bootstrap a Context |
| paddy@96 | 31 // using the information provided in the Config variable. |
| paddy@96 | 32 func NewContext(config Config) (Context, error) { |
| paddy@102 | 33 if config.iterations == 0 { |
| paddy@102 | 34 return Context{}, ErrConfigNotInitialized |
| paddy@102 | 35 } |
| paddy@96 | 36 context := Context{ |
| paddy@178 | 37 clients: config.ClientStore, |
| paddy@178 | 38 authCodes: config.AuthCodeStore, |
| paddy@178 | 39 profiles: config.ProfileStore, |
| paddy@178 | 40 tokens: config.TokenStore, |
| paddy@178 | 41 sessions: config.SessionStore, |
| paddy@178 | 42 scopes: config.ScopeStore, |
| paddy@178 | 43 eventsPublisher: config.EventsPublisher, |
| paddy@178 | 44 template: config.Template, |
| paddy@178 | 45 config: config, |
| paddy@96 | 46 } |
| paddy@96 | 47 var err error |
| paddy@96 | 48 context.loginURI, err = url.Parse(config.LoginURI) |
| paddy@96 | 49 if err != nil { |
| paddy@96 | 50 log.Println(err) |
| paddy@96 | 51 return Context{}, ErrInvalidLoginURI |
| paddy@96 | 52 } |
| paddy@96 | 53 return context, nil |
| paddy@50 | 54 } |
| paddy@50 | 55 |
| paddy@57 | 56 // Render uses the HTML templates associated with the Context to render the |
| paddy@57 | 57 // template specified by name to out using data to fill any template variables. |
| paddy@55 | 58 func (c Context) Render(out io.Writer, name string, data interface{}) { |
| paddy@50 | 59 if c.template == nil { |
| paddy@57 | 60 log.Println("No template set on Context, can't render anything!") |
| paddy@57 | 61 return |
| paddy@50 | 62 } |
| paddy@55 | 63 err := c.template.ExecuteTemplate(out, name, data) |
| paddy@55 | 64 if err != nil { |
| paddy@55 | 65 log.Println("Error executing template", name, ":", err) |
| paddy@55 | 66 } |
| paddy@50 | 67 } |
| paddy@50 | 68 |
| paddy@57 | 69 // GetClient returns a single Client by its ID from the |
| paddy@57 | 70 // clientStore associated with the Context. |
| paddy@50 | 71 func (c Context) GetClient(id uuid.ID) (Client, error) { |
| paddy@50 | 72 if c.clients == nil { |
| paddy@50 | 73 return Client{}, ErrNoClientStore |
| paddy@50 | 74 } |
| paddy@57 | 75 return c.clients.getClient(id) |
| paddy@50 | 76 } |
| paddy@50 | 77 |
| paddy@57 | 78 // SaveClient stores the passed Client in the clientStore |
| paddy@57 | 79 // associated with the Context. |
| paddy@50 | 80 func (c Context) SaveClient(client Client) error { |
| paddy@50 | 81 if c.clients == nil { |
| paddy@50 | 82 return ErrNoClientStore |
| paddy@50 | 83 } |
| paddy@57 | 84 return c.clients.saveClient(client) |
| paddy@50 | 85 } |
| paddy@50 | 86 |
| paddy@57 | 87 // UpdateClient applies the specified ClientChange to the Client |
| paddy@57 | 88 // with the specified ID in the clientStore associated with the |
| paddy@57 | 89 // Context. |
| paddy@50 | 90 func (c Context) UpdateClient(id uuid.ID, change ClientChange) error { |
| paddy@50 | 91 if c.clients == nil { |
| paddy@50 | 92 return ErrNoClientStore |
| paddy@50 | 93 } |
| paddy@57 | 94 return c.clients.updateClient(id, change) |
| paddy@50 | 95 } |
| paddy@50 | 96 |
| paddy@57 | 97 // ListClientsByOwner returns a slice of up to num Clients, starting at offset (inclusive) |
| paddy@57 | 98 // that have the specified OwnerID in the clientStore associated with the Context. |
| paddy@50 | 99 func (c Context) ListClientsByOwner(ownerID uuid.ID, num, offset int) ([]Client, error) { |
| paddy@50 | 100 if c.clients == nil { |
| paddy@50 | 101 return []Client{}, ErrNoClientStore |
| paddy@50 | 102 } |
| paddy@57 | 103 return c.clients.listClientsByOwner(ownerID, num, offset) |
| paddy@50 | 104 } |
| paddy@50 | 105 |
| paddy@164 | 106 func (c Context) DeleteClientsByOwner(ownerID uuid.ID) error { |
| paddy@164 | 107 if c.clients == nil { |
| paddy@164 | 108 return ErrNoClientStore |
| paddy@164 | 109 } |
| paddy@164 | 110 return c.clients.deleteClientsByOwner(ownerID) |
| paddy@164 | 111 } |
| paddy@164 | 112 |
| paddy@151 | 113 // AddEndpoints stores the specified Endpoints in the clientStore associated with the Context. |
| paddy@151 | 114 func (c Context) AddEndpoints(endpoints []Endpoint) error { |
| paddy@50 | 115 if c.clients == nil { |
| paddy@50 | 116 return ErrNoClientStore |
| paddy@50 | 117 } |
| paddy@116 | 118 for pos, endpoint := range endpoints { |
| paddy@116 | 119 u, err := normalizeURIString(endpoint.URI) |
| paddy@116 | 120 if err != nil { |
| paddy@116 | 121 return err |
| paddy@116 | 122 } |
| paddy@116 | 123 endpoint.NormalizedURI = u |
| paddy@116 | 124 endpoints[pos] = endpoint |
| paddy@116 | 125 } |
| paddy@151 | 126 return c.clients.addEndpoints(endpoints) |
| paddy@50 | 127 } |
| paddy@50 | 128 |
| paddy@143 | 129 // GetEndpoint retrieves the Endpoint with the specified ID from the clientStore associated |
| paddy@143 | 130 // with the Context, if and only if it belongs to the Client with the specified ID. |
| paddy@143 | 131 func (c Context) GetEndpoint(client, endpoint uuid.ID) (Endpoint, error) { |
| paddy@143 | 132 if c.clients == nil { |
| paddy@143 | 133 return Endpoint{}, ErrNoClientStore |
| paddy@143 | 134 } |
| paddy@143 | 135 return c.clients.getEndpoint(client, endpoint) |
| paddy@143 | 136 } |
| paddy@143 | 137 |
| paddy@57 | 138 // RemoveEndpoint deletes the Endpoint with the specified ID from the clientStore associated |
| paddy@57 | 139 // with the Context, and disassociates the Endpoint from the specified Client. |
| paddy@50 | 140 func (c Context) RemoveEndpoint(client, endpoint uuid.ID) error { |
| paddy@50 | 141 if c.clients == nil { |
| paddy@50 | 142 return ErrNoClientStore |
| paddy@50 | 143 } |
| paddy@57 | 144 return c.clients.removeEndpoint(client, endpoint) |
| paddy@50 | 145 } |
| paddy@50 | 146 |
| paddy@57 | 147 // CheckEndpoint finds Endpoints in the clientStore associated with the Context that belong |
| paddy@58 | 148 // to the Client specified by the passed ID and match the URI passed. URI matches must be |
| paddy@58 | 149 // performed according to RFC 3986 Section 6. |
| paddy@58 | 150 func (c Context) CheckEndpoint(client uuid.ID, URI string) (bool, error) { |
| paddy@50 | 151 if c.clients == nil { |
| paddy@50 | 152 return false, ErrNoClientStore |
| paddy@50 | 153 } |
| paddy@116 | 154 u, err := normalizeURIString(URI) |
| paddy@116 | 155 if err != nil { |
| paddy@116 | 156 return false, err |
| paddy@116 | 157 } |
| paddy@116 | 158 return c.clients.checkEndpoint(client, u) |
| paddy@50 | 159 } |
| paddy@50 | 160 |
| paddy@57 | 161 // ListEndpoints finds Endpoints in the clientStore associated with the Context that belong |
| paddy@57 | 162 // to the Client specified by the passed ID. It returns up to num endpoints, starting at offset, |
| paddy@57 | 163 // exclusive. |
| paddy@50 | 164 func (c Context) ListEndpoints(client uuid.ID, num, offset int) ([]Endpoint, error) { |
| paddy@50 | 165 if c.clients == nil { |
| paddy@50 | 166 return []Endpoint{}, ErrNoClientStore |
| paddy@50 | 167 } |
| paddy@57 | 168 return c.clients.listEndpoints(client, num, offset) |
| paddy@50 | 169 } |
| paddy@50 | 170 |
| paddy@164 | 171 func (c Context) RemoveEndpointsByClientID(client uuid.ID) error { |
| paddy@164 | 172 if c.clients == nil { |
| paddy@164 | 173 return ErrNoClientStore |
| paddy@164 | 174 } |
| paddy@164 | 175 return c.clients.removeEndpointsByClientID(client) |
| paddy@164 | 176 } |
| paddy@164 | 177 |
| paddy@57 | 178 // CountEndpoints returns the number of Endpoints the are associated with the Client specified by the |
| paddy@57 | 179 // passed ID in the clientStore associated with the Context. |
| paddy@55 | 180 func (c Context) CountEndpoints(client uuid.ID) (int64, error) { |
| paddy@55 | 181 if c.clients == nil { |
| paddy@55 | 182 return 0, ErrNoClientStore |
| paddy@55 | 183 } |
| paddy@57 | 184 return c.clients.countEndpoints(client) |
| paddy@55 | 185 } |
| paddy@55 | 186 |
| paddy@87 | 187 // GetAuthorizationCode returns the AuthorizationCode specified by the provided code from the authorizationCodeStore associated with the |
| paddy@57 | 188 // Context. |
| paddy@87 | 189 func (c Context) GetAuthorizationCode(code string) (AuthorizationCode, error) { |
| paddy@87 | 190 if c.authCodes == nil { |
| paddy@87 | 191 return AuthorizationCode{}, ErrNoAuthorizationCodeStore |
| paddy@50 | 192 } |
| paddy@87 | 193 return c.authCodes.getAuthorizationCode(code) |
| paddy@50 | 194 } |
| paddy@50 | 195 |
| paddy@87 | 196 // SaveAuthorizationCode stores the passed AuthorizationCode in the authorizationCodeStore associated with the Context. |
| paddy@87 | 197 func (c Context) SaveAuthorizationCode(authCode AuthorizationCode) error { |
| paddy@87 | 198 if c.authCodes == nil { |
| paddy@87 | 199 return ErrNoAuthorizationCodeStore |
| paddy@50 | 200 } |
| paddy@87 | 201 return c.authCodes.saveAuthorizationCode(authCode) |
| paddy@50 | 202 } |
| paddy@50 | 203 |
| paddy@87 | 204 // DeleteAuthorizationCode removes the AuthorizationCode specified by the provided code from the authorizationCodeStore associated with |
| paddy@57 | 205 // the Context. |
| paddy@87 | 206 func (c Context) DeleteAuthorizationCode(code string) error { |
| paddy@87 | 207 if c.authCodes == nil { |
| paddy@87 | 208 return ErrNoAuthorizationCodeStore |
| paddy@50 | 209 } |
| paddy@87 | 210 return c.authCodes.deleteAuthorizationCode(code) |
| paddy@50 | 211 } |
| paddy@50 | 212 |
| paddy@163 | 213 // DeleteAuthorizationCodesByProfileID removes the AuthorizationCodes associated with the Profile specified by the provided ID from the |
| paddy@163 | 214 // authorizationCodeStore associated with the Context. |
| paddy@163 | 215 func (c Context) DeleteAuthorizationCodesByProfileID(profileID uuid.ID) error { |
| paddy@163 | 216 if c.authCodes == nil { |
| paddy@163 | 217 return ErrNoAuthorizationCodeStore |
| paddy@163 | 218 } |
| paddy@163 | 219 return c.authCodes.deleteAuthorizationCodesByProfileID(profileID) |
| paddy@163 | 220 } |
| paddy@163 | 221 |
| paddy@164 | 222 func (c Context) DeleteAuthorizationCodesByClientID(clientID uuid.ID) error { |
| paddy@164 | 223 if c.authCodes == nil { |
| paddy@164 | 224 return ErrNoAuthorizationCodeStore |
| paddy@164 | 225 } |
| paddy@164 | 226 return c.authCodes.deleteAuthorizationCodesByClientID(clientID) |
| paddy@164 | 227 } |
| paddy@164 | 228 |
| paddy@94 | 229 // UseAuthorizationCode marks the AuthorizationCode specified by the provided code as used in the authorizationCodeStore associated with |
| paddy@94 | 230 // the Context. Once an AuthorizationCode is marked as used, its Used property will be set to true when retrieved from the authorizationCodeStore. |
| paddy@94 | 231 func (c Context) UseAuthorizationCode(code string) error { |
| paddy@94 | 232 if c.authCodes == nil { |
| paddy@94 | 233 return ErrNoAuthorizationCodeStore |
| paddy@94 | 234 } |
| paddy@94 | 235 return c.authCodes.useAuthorizationCode(code) |
| paddy@94 | 236 } |
| paddy@94 | 237 |
| paddy@57 | 238 // GetProfileByID returns the Profile specified by the provided ID from the profileStore associated with |
| paddy@57 | 239 // the Context. |
| paddy@50 | 240 func (c Context) GetProfileByID(id uuid.ID) (Profile, error) { |
| paddy@50 | 241 if c.profiles == nil { |
| paddy@50 | 242 return Profile{}, ErrNoProfileStore |
| paddy@50 | 243 } |
| paddy@57 | 244 return c.profiles.getProfileByID(id) |
| paddy@50 | 245 } |
| paddy@50 | 246 |
| paddy@57 | 247 // GetProfileByLogin returns the Profile associated with the specified Login from the profileStore associated |
| paddy@57 | 248 // with the Context. |
| paddy@69 | 249 func (c Context) GetProfileByLogin(value string) (Profile, error) { |
| paddy@50 | 250 if c.profiles == nil { |
| paddy@50 | 251 return Profile{}, ErrNoProfileStore |
| paddy@50 | 252 } |
| paddy@69 | 253 return c.profiles.getProfileByLogin(value) |
| paddy@50 | 254 } |
| paddy@50 | 255 |
| paddy@57 | 256 // SaveProfile inserts the passed Profile into the profileStore associated with the Context. |
| paddy@50 | 257 func (c Context) SaveProfile(profile Profile) error { |
| paddy@50 | 258 if c.profiles == nil { |
| paddy@50 | 259 return ErrNoProfileStore |
| paddy@50 | 260 } |
| paddy@57 | 261 return c.profiles.saveProfile(profile) |
| paddy@50 | 262 } |
| paddy@50 | 263 |
| paddy@57 | 264 // UpdateProfile applies the supplied ProfileChange to the Profile that matches the specified ID |
| paddy@57 | 265 // in the profileStore associated with the Context. |
| paddy@50 | 266 func (c Context) UpdateProfile(id uuid.ID, change ProfileChange) error { |
| paddy@50 | 267 if c.profiles == nil { |
| paddy@50 | 268 return ErrNoProfileStore |
| paddy@50 | 269 } |
| paddy@57 | 270 return c.profiles.updateProfile(id, change) |
| paddy@50 | 271 } |
| paddy@50 | 272 |
| paddy@57 | 273 // UpdateProfiles applies the supplied BulkProfileChange to every Profile that matches one of the |
| paddy@57 | 274 // specified IDs in the profileStore associated with the Context. |
| paddy@50 | 275 func (c Context) UpdateProfiles(ids []uuid.ID, change BulkProfileChange) error { |
| paddy@50 | 276 if c.profiles == nil { |
| paddy@50 | 277 return ErrNoProfileStore |
| paddy@50 | 278 } |
| paddy@57 | 279 return c.profiles.updateProfiles(ids, change) |
| paddy@50 | 280 } |
| paddy@50 | 281 |
| paddy@161 | 282 // DeleteProfile removes the specified Profile from the profileStore associated with the Context. |
| paddy@161 | 283 func (c Context) DeleteProfile(id uuid.ID) error { |
| paddy@161 | 284 if c.profiles == nil { |
| paddy@161 | 285 return ErrNoProfileStore |
| paddy@161 | 286 } |
| paddy@161 | 287 return c.profiles.deleteProfile(id) |
| paddy@161 | 288 } |
| paddy@161 | 289 |
| paddy@57 | 290 // AddLogin stores the passed Login in the profileStore associated with the Context. It also associates |
| paddy@57 | 291 // the newly-created Login with the Orofile in login.ProfileID. |
| paddy@50 | 292 func (c Context) AddLogin(login Login) error { |
| paddy@50 | 293 if c.profiles == nil { |
| paddy@50 | 294 return ErrNoProfileStore |
| paddy@50 | 295 } |
| paddy@57 | 296 return c.profiles.addLogin(login) |
| paddy@50 | 297 } |
| paddy@50 | 298 |
| paddy@172 | 299 // GetLogin returns the Login specified from the profileStore associated with the Context. |
| paddy@172 | 300 func (c Context) GetLogin(value string) (Login, error) { |
| paddy@172 | 301 if c.profiles == nil { |
| paddy@172 | 302 return Login{}, ErrNoProfileStore |
| paddy@172 | 303 } |
| paddy@172 | 304 return c.profiles.getLogin(value) |
| paddy@172 | 305 } |
| paddy@172 | 306 |
| paddy@57 | 307 // RemoveLogin removes the specified Login from the profileStore associated with the Context, provided |
| paddy@57 | 308 // the Login has a ProfileID property that matches the profile ID passed in. It also disassociates the |
| paddy@57 | 309 // deleted Login from the Profile in login.ProfileID. |
| paddy@69 | 310 func (c Context) RemoveLogin(value string, profile uuid.ID) error { |
| paddy@50 | 311 if c.profiles == nil { |
| paddy@50 | 312 return ErrNoProfileStore |
| paddy@50 | 313 } |
| paddy@69 | 314 return c.profiles.removeLogin(value, profile) |
| paddy@50 | 315 } |
| paddy@50 | 316 |
| paddy@160 | 317 // RemoveLoginsByProfile removes all Logins connected to the specified Profile in the profileStore |
| paddy@160 | 318 // associated with the Context and disassociates them from the Profile. |
| paddy@160 | 319 func (c Context) RemoveLoginsByProfile(profile uuid.ID) error { |
| paddy@160 | 320 if c.profiles == nil { |
| paddy@160 | 321 return ErrNoProfileStore |
| paddy@160 | 322 } |
| paddy@160 | 323 return c.profiles.removeLoginsByProfile(profile) |
| paddy@160 | 324 } |
| paddy@160 | 325 |
| paddy@57 | 326 // RecordLoginUse sets the LastUsed property of the Login specified in the profileStore associated with |
| paddy@57 | 327 // the Context to the value passed in as when. |
| paddy@69 | 328 func (c Context) RecordLoginUse(value string, when time.Time) error { |
| paddy@50 | 329 if c.profiles == nil { |
| paddy@50 | 330 return ErrNoProfileStore |
| paddy@50 | 331 } |
| paddy@69 | 332 return c.profiles.recordLoginUse(value, when) |
| paddy@50 | 333 } |
| paddy@50 | 334 |
| paddy@172 | 335 // VerifyLogin sets the Verified property of the Login specied to true in the profileStore associated with |
| paddy@172 | 336 // the Context, assuming the Verification property of the Login in the profileStore matches the verification |
| paddy@172 | 337 // passed. If the verifications do not match, an ErrLoginVerificationInvalid error is returned. |
| paddy@172 | 338 func (c Context) VerifyLogin(value, verification string) error { |
| paddy@172 | 339 if c.profiles == nil { |
| paddy@172 | 340 return ErrNoProfileStore |
| paddy@172 | 341 } |
| paddy@172 | 342 return c.profiles.verifyLogin(value, verification) |
| paddy@172 | 343 } |
| paddy@172 | 344 |
| paddy@57 | 345 // ListLogins returns a slice of up to num Logins associated with the specified Profile from the profileStore |
| paddy@57 | 346 // associated with the Context, skipping offset Profiles. |
| paddy@50 | 347 func (c Context) ListLogins(profile uuid.ID, num, offset int) ([]Login, error) { |
| paddy@50 | 348 if c.profiles == nil { |
| paddy@50 | 349 return []Login{}, ErrNoProfileStore |
| paddy@50 | 350 } |
| paddy@57 | 351 return c.profiles.listLogins(profile, num, offset) |
| paddy@50 | 352 } |
| paddy@50 | 353 |
| paddy@57 | 354 // GetToken returns the Token specified from the tokenStore associated with the Context. |
| paddy@57 | 355 // If refresh is true, the token input should be compared against the refresh tokens, not the |
| paddy@57 | 356 // access tokens. |
| paddy@50 | 357 func (c Context) GetToken(token string, refresh bool) (Token, error) { |
| paddy@50 | 358 if c.tokens == nil { |
| paddy@50 | 359 return Token{}, ErrNoTokenStore |
| paddy@50 | 360 } |
| paddy@57 | 361 return c.tokens.getToken(token, refresh) |
| paddy@50 | 362 } |
| paddy@50 | 363 |
| paddy@57 | 364 // SaveToken stores the passed Token in the tokenStore associated with the Context. |
| paddy@50 | 365 func (c Context) SaveToken(token Token) error { |
| paddy@50 | 366 if c.tokens == nil { |
| paddy@50 | 367 return ErrNoTokenStore |
| paddy@50 | 368 } |
| paddy@57 | 369 return c.tokens.saveToken(token) |
| paddy@50 | 370 } |
| paddy@50 | 371 |
| paddy@93 | 372 // RevokeToken revokes the Token identfied by the passed token string from the tokenStore associated |
| paddy@168 | 373 // with the context. |
| paddy@168 | 374 func (c Context) RevokeToken(token string) error { |
| paddy@93 | 375 if c.tokens == nil { |
| paddy@93 | 376 return ErrNoTokenStore |
| paddy@93 | 377 } |
| paddy@168 | 378 return c.tokens.revokeToken(token) |
| paddy@93 | 379 } |
| paddy@93 | 380 |
| paddy@162 | 381 // RevokeTokensByProfileID revokes the Tokens associated with the Profile identified by the passed ID in |
| paddy@162 | 382 // the tokenStore associated with the Context. |
| paddy@162 | 383 func (c Context) RevokeTokensByProfileID(profileID uuid.ID) error { |
| paddy@162 | 384 if c.tokens == nil { |
| paddy@162 | 385 return ErrNoTokenStore |
| paddy@162 | 386 } |
| paddy@162 | 387 return c.tokens.revokeTokensByProfileID(profileID) |
| paddy@162 | 388 } |
| paddy@162 | 389 |
| paddy@164 | 390 func (c Context) RevokeTokensByClientID(clientID uuid.ID) error { |
| paddy@164 | 391 if c.tokens == nil { |
| paddy@164 | 392 return ErrNoTokenStore |
| paddy@164 | 393 } |
| paddy@164 | 394 return c.tokens.revokeTokensByClientID(clientID) |
| paddy@164 | 395 } |
| paddy@164 | 396 |
| paddy@57 | 397 // GetTokensByProfileID returns a slice of up to num Tokens with a ProfileID that matches the specified |
| paddy@57 | 398 // profileID from the tokenStore associated with the Context, skipping offset Tokens. |
| paddy@50 | 399 func (c Context) GetTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) { |
| paddy@50 | 400 if c.tokens == nil { |
| paddy@50 | 401 return []Token{}, ErrNoTokenStore |
| paddy@50 | 402 } |
| paddy@57 | 403 return c.tokens.getTokensByProfileID(profileID, num, offset) |
| paddy@50 | 404 } |
| paddy@69 | 405 |
| paddy@69 | 406 // CreateSession stores the passed Session in the sessionStore associated with the Context. |
| paddy@69 | 407 func (c Context) CreateSession(session Session) error { |
| paddy@69 | 408 if c.sessions == nil { |
| paddy@69 | 409 return ErrNoSessionStore |
| paddy@69 | 410 } |
| paddy@69 | 411 return c.sessions.createSession(session) |
| paddy@69 | 412 } |
| paddy@69 | 413 |
| paddy@69 | 414 // GetSession returns the Session specified from the sessionStore associated with the Context. |
| paddy@69 | 415 func (c Context) GetSession(id string) (Session, error) { |
| paddy@69 | 416 if c.sessions == nil { |
| paddy@69 | 417 return Session{}, ErrNoSessionStore |
| paddy@69 | 418 } |
| paddy@69 | 419 return c.sessions.getSession(id) |
| paddy@69 | 420 } |
| paddy@69 | 421 |
| paddy@159 | 422 // TerminateSession sets the Session identified by the passed ID as inactive in the sessionStore assocated |
| paddy@159 | 423 // with the Context. |
| paddy@159 | 424 func (c Context) TerminateSession(id string) error { |
| paddy@159 | 425 if c.sessions == nil { |
| paddy@159 | 426 return ErrNoSessionStore |
| paddy@159 | 427 } |
| paddy@159 | 428 return c.sessions.terminateSession(id) |
| paddy@159 | 429 } |
| paddy@159 | 430 |
| paddy@162 | 431 // TerminateSessionsByProfile sets the Sessions associated with the passed Profile ID as inactive in the |
| paddy@162 | 432 // sessionStore associated with the Context. |
| paddy@162 | 433 func (c Context) TerminateSessionsByProfile(profile uuid.ID) error { |
| paddy@162 | 434 if c.sessions == nil { |
| paddy@162 | 435 return ErrNoSessionStore |
| paddy@162 | 436 } |
| paddy@162 | 437 return c.sessions.terminateSessionsByProfile(profile) |
| paddy@162 | 438 } |
| paddy@162 | 439 |
| paddy@69 | 440 // RemoveSession removes the Session identified by the passed ID from the sessionStore associated with |
| paddy@69 | 441 // the Context. |
| paddy@69 | 442 func (c Context) RemoveSession(id string) error { |
| paddy@69 | 443 if c.sessions == nil { |
| paddy@69 | 444 return ErrNoSessionStore |
| paddy@69 | 445 } |
| paddy@69 | 446 return c.sessions.removeSession(id) |
| paddy@69 | 447 } |
| paddy@69 | 448 |
| paddy@69 | 449 // ListSessions returns a slice of up to num Sessions from the sessionStore associated with the Context, |
| paddy@69 | 450 // ordered by the date they were created, descending. If before.IsZero() returns false, only Sessions |
| paddy@69 | 451 // that were created before that time will be returned. If profile is not nil, only Sessions belonging to |
| paddy@69 | 452 // that Profile will be returned. |
| paddy@69 | 453 func (c Context) ListSessions(profile uuid.ID, before time.Time, num int64) ([]Session, error) { |
| paddy@116 | 454 if c.sessions == nil { |
| paddy@69 | 455 return []Session{}, ErrNoSessionStore |
| paddy@69 | 456 } |
| paddy@69 | 457 return c.sessions.listSessions(profile, before, num) |
| paddy@69 | 458 } |
| paddy@134 | 459 |
| paddy@134 | 460 func (c Context) CreateScopes(scopes []Scope) error { |
| paddy@134 | 461 if c.scopes == nil { |
| paddy@134 | 462 return ErrNoScopeStore |
| paddy@134 | 463 } |
| paddy@134 | 464 return c.scopes.createScopes(scopes) |
| paddy@134 | 465 } |
| paddy@134 | 466 |
| paddy@134 | 467 func (c Context) GetScopes(ids []string) ([]Scope, error) { |
| paddy@134 | 468 if c.scopes == nil { |
| paddy@134 | 469 return []Scope{}, ErrNoScopeStore |
| paddy@134 | 470 } |
| paddy@134 | 471 return c.scopes.getScopes(ids) |
| paddy@134 | 472 } |
| paddy@134 | 473 |
| paddy@152 | 474 func (c Context) UpdateScope(id string, change ScopeChange) error { |
| paddy@134 | 475 if c.scopes == nil { |
| paddy@152 | 476 return ErrNoScopeStore |
| paddy@134 | 477 } |
| paddy@152 | 478 return c.scopes.updateScope(id, change) |
| paddy@134 | 479 } |
| paddy@134 | 480 |
| paddy@134 | 481 func (c Context) RemoveScopes(ids []string) error { |
| paddy@134 | 482 if c.scopes == nil { |
| paddy@134 | 483 return ErrNoScopeStore |
| paddy@134 | 484 } |
| paddy@134 | 485 return c.scopes.removeScopes(ids) |
| paddy@134 | 486 } |
| paddy@134 | 487 |
| paddy@134 | 488 func (c Context) ListScopes() ([]Scope, error) { |
| paddy@134 | 489 if c.scopes == nil { |
| paddy@134 | 490 return []Scope{}, ErrNoScopeStore |
| paddy@134 | 491 } |
| paddy@134 | 492 return c.scopes.listScopes() |
| paddy@134 | 493 } |
| paddy@172 | 494 |
| paddy@178 | 495 func (c Context) SendModelEvent(m events.Model, action string) error { |
| paddy@178 | 496 if c.eventsPublisher == nil { |
| paddy@178 | 497 log.Println("Event publisher not set!") |
| paddy@172 | 498 } |
| paddy@178 | 499 return events.PublishModelEvent(c.eventsPublisher, m, action) |
| paddy@172 | 500 } |