auth

Paddy 2015-05-15 Parent:d103a598548c Child:807d20a0b197

168:581c60f8dd23 Go to Latest

auth/config.go

Switch to a JWT approach. We're going to use a JWT as our access tokens (as discussed in &yet's excellent post https://blog.andyet.com/2015/05/12/micro-services-user-info-and-auth and my ensuing conversation with Fritzy). The benefit of this approach is that we can do authentication and even some authorization without touching the database at all. The drawback is that we can no longer revoke access tokens, only the refresh tokens that grant the access tokens. We need a new config variable to set our private key, used to sign the JWT. We get to remove our token handlers, as we no longer can revoke tokens, so there's no purpose in getting information about it or listing them. Our tokenStore revokeToken gets to be simplified, as it will only ever be used for refresh tokens now. We also updated our postgres and memstore implementations. We added a helper method for generating the signed "access token" (our JWT) and started using it in the places where we're creating a Token. We get to remove the `revoked` SQL column for the tokens table, and rename the `refresh_revoked` column to just be `revoked`. We shortened our access token expiration to 15 minutes instead of an hour, to deal with the token not being revokable.

History
1 package auth
3 import (
4 "errors"
5 "html/template"
6 "log"
7 )
9 var (
10 // ErrInvalidLoginURI is returned when a Context is instantiated with a Config object that specifies a LoginURI that can't be parsed as a URL.
11 ErrInvalidLoginURI = errors.New("invalid login URI")
12 // ErrConfigNotInitialized is returned when a Context is instantiated with a Config object that hasn't had its Init function called.
13 ErrConfigNotInitialized = errors.New("config not initialized")
14 )
16 // Config holds the configuration values necessary to run a server. A Config
17 // instance is the only way to instantiate a Context variable.
18 type Config struct {
19 ClientStore clientStore
20 AuthCodeStore authorizationCodeStore
21 ProfileStore profileStore
22 TokenStore tokenStore
23 SessionStore sessionStore
24 ScopeStore scopeStore
25 Template *template.Template
26 LoginURI string
27 JWTPrivateKey []byte
28 iterations int
29 secureCookie bool
30 }
32 // Init is a function that preps the Config object to be used for Context creation, setting variables
33 // that are determined at the beginning of program execution.
34 func (c *Config) Init() error {
35 scheme, ok := passphraseSchemes[CurPassphraseScheme]
36 if !ok {
37 return ErrInvalidPassphraseScheme
38 }
39 var err error
40 c.iterations, err = scheme.calculateIterations()
41 if err != nil {
42 return err
43 }
44 log.Printf("Generating passphrases with %d iterations...\n", c.iterations)
45 return nil
46 }