auth

Paddy 2015-07-18 Parent:0a2c3d677161 Child:b7e685839a1b

180:4b68bac597b7 Go to Latest

auth/config.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.

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