auth

Paddy 2015-07-15 Parent:807d20a0b197 Child:b7e685839a1b

178:0a2c3d677161 Go to Latest

auth/config.go

Update to use a generic event emitter. Rather can creating a purpose-built event emitter for each and every event we need to emit (I'm looking at you, login verification event) which is _downright silly_, we're now using a generic event publisher that's based on saying "HEY A MODEL UPDATED". This means we need to change all our setup code in authd to use events.NewNSQPublisher or events.NewStdoutPublisher instead of our homegrown solutions. Which also means updating our config to take an events.Publisher instead of our LoginVerificationNotifier (blergh). Our Context also now uses an events.Publisher instead of a LoginVerificationNotifier. Party all around! We also replaced our SendLoginVerification helper method on Context with a SendModelEvent helper method on Context, which is just a light wrapper around events.PublishModelEvent. Of course, all this means we need to update our email_verification listener to listen to the correct channel (based on the model we want updates about) and filter down to a Created action or our new custom action for "the customer wants their verification resent", which I'm OK making a special case and not generic, because c'mon. But we had a subtle change to all our constants, some of which are unofficial constants now. I'm unsure how I feel about this. We also updated our email_verification listener so that we're unmarshalling to a custom loginEvent, which is just an events.Event that overwrites the Data property to be an auth.Login instance. This is to make sure we don't need to wrangle a map[string]interface{}, which is no fun. I'm also OK with special-casing like this, because it's 1) a tiny amount of code, 2) properly utilising composition, and 3) the only way I can think of to cleanly accomplish what I want. I also added a note about GetLogin's deficient handling of logins, namely that it doesn't recognise admins and return Verification codes to them, which would be a useful property for internal tools to take advantage of. Ah well. I updated the Profile and Login implementations so they're now event.Model instances, mainly by just exporting some strings from them through getters that will let us automatically build an Event from them. This lets us use the PublishModelEvent helper. I updated our CreateProfileHandler to properly mangle the login Verification property, and to fire off the ActionCreated events for the new Login and the new Profile. I updated our GetLoginHandler and UpdateLoginHandler to properly mangle the loginVerification property. God that's annoying. :-/ You'll note I didn't start publishing the events.ActionUpdated or events.ActionDeleted events for Profiles or Logins yet, and didn't bother publishing any events for literally any other type. That's because I'm a lazy piece of crap and will end up publishing them when I absolutely have to. Part of that is because if a channel isn't created/being read for a topic, the messages will just stack up in NSQ, and I don't want that. But mostly I'm lazy. Finally, I got to delete the entire profile_verification.go file, because we're no longer special-casing that. Hooray!

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 }