auth

Paddy 2015-07-15 Parent:cf1aef6eb81f

178:0a2c3d677161 Go to Latest

auth/authcode_postgres.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@156 1 package auth
paddy@156 2
paddy@156 3 import (
paddy@163 4 "code.secondbit.org/uuid.hg"
paddy@156 5 "github.com/lib/pq"
paddy@156 6 "github.com/secondbit/pan"
paddy@156 7 )
paddy@156 8
paddy@156 9 func (ac AuthorizationCode) GetSQLTableName() string {
paddy@156 10 return "authorization_codes"
paddy@156 11 }
paddy@156 12
paddy@156 13 func (p *postgres) getAuthorizationCodeSQL(code string) *pan.Query {
paddy@156 14 var ac AuthorizationCode
paddy@156 15 fields, _ := pan.GetFields(ac)
paddy@156 16 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(ac))
paddy@156 17 query.IncludeWhere()
paddy@156 18 query.Include(pan.GetUnquotedColumn(ac, "Code")+" = ?", code)
paddy@156 19 return query.FlushExpressions(" ")
paddy@156 20 }
paddy@156 21
paddy@156 22 func (p *postgres) getAuthorizationCode(code string) (AuthorizationCode, error) {
paddy@156 23 query := p.getAuthorizationCodeSQL(code)
paddy@156 24 rows, err := p.db.Query(query.String(), query.Args...)
paddy@156 25 if err != nil {
paddy@156 26 return AuthorizationCode{}, err
paddy@156 27 }
paddy@156 28 var ac AuthorizationCode
paddy@156 29 var found bool
paddy@156 30 for rows.Next() {
paddy@156 31 err := pan.Unmarshal(rows, &ac)
paddy@156 32 if err != nil {
paddy@156 33 return ac, err
paddy@156 34 }
paddy@156 35 found = true
paddy@156 36 }
paddy@156 37 if err = rows.Err(); err != nil {
paddy@156 38 return ac, err
paddy@156 39 }
paddy@156 40 if !found {
paddy@156 41 return ac, ErrAuthorizationCodeNotFound
paddy@156 42 }
paddy@156 43 return ac, nil
paddy@156 44 }
paddy@156 45
paddy@156 46 func (p *postgres) saveAuthorizationCodeSQL(authCode AuthorizationCode) *pan.Query {
paddy@156 47 fields, values := pan.GetFields(authCode)
paddy@156 48 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(authCode))
paddy@156 49 query.Include("(" + pan.QueryList(fields) + ")")
paddy@156 50 query.Include("VALUES")
paddy@156 51 query.Include("("+pan.VariableList(len(values))+")", values...)
paddy@156 52 return query.FlushExpressions(" ")
paddy@156 53 }
paddy@156 54
paddy@156 55 func (p *postgres) saveAuthorizationCode(authCode AuthorizationCode) error {
paddy@156 56 query := p.saveAuthorizationCodeSQL(authCode)
paddy@156 57 _, err := p.db.Exec(query.String(), query.Args...)
paddy@156 58 if e, ok := err.(*pq.Error); ok && e.Constraint == "authorization_codes_pkey" {
paddy@156 59 err = ErrAuthorizationCodeAlreadyExists
paddy@156 60 }
paddy@156 61 return err
paddy@156 62 }
paddy@156 63
paddy@156 64 func (p *postgres) deleteAuthorizationCodeSQL(code string) *pan.Query {
paddy@156 65 var authCode AuthorizationCode
paddy@156 66 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(authCode))
paddy@156 67 query.IncludeWhere()
paddy@156 68 query.Include(pan.GetUnquotedColumn(authCode, "Code")+" = ?", code)
paddy@156 69 return query.FlushExpressions(" ")
paddy@156 70 }
paddy@156 71
paddy@156 72 func (p *postgres) deleteAuthorizationCode(code string) error {
paddy@156 73 query := p.deleteAuthorizationCodeSQL(code)
paddy@156 74 res, err := p.db.Exec(query.String(), query.Args...)
paddy@156 75 if err != nil {
paddy@156 76 return err
paddy@156 77 }
paddy@156 78 rows, err := res.RowsAffected()
paddy@156 79 if err != nil {
paddy@156 80 return err
paddy@156 81 }
paddy@156 82 if rows == 0 {
paddy@156 83 return ErrAuthorizationCodeNotFound
paddy@156 84 }
paddy@163 85 return nil
paddy@163 86 }
paddy@163 87
paddy@163 88 func (p *postgres) deleteAuthorizationCodesByProfileIDSQL(profileID uuid.ID) *pan.Query {
paddy@163 89 var authCode AuthorizationCode
paddy@163 90 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(authCode))
paddy@163 91 query.IncludeWhere()
paddy@163 92 query.Include(pan.GetUnquotedColumn(authCode, "ProfileID")+" = ?", profileID)
paddy@163 93 return query.FlushExpressions(" ")
paddy@163 94 }
paddy@163 95
paddy@163 96 func (p *postgres) deleteAuthorizationCodesByProfileID(profileID uuid.ID) error {
paddy@163 97 query := p.deleteAuthorizationCodesByProfileIDSQL(profileID)
paddy@163 98 res, err := p.db.Exec(query.String(), query.Args...)
paddy@163 99 if err != nil {
paddy@163 100 return err
paddy@163 101 }
paddy@163 102 rows, err := res.RowsAffected()
paddy@163 103 if err != nil {
paddy@163 104 return err
paddy@163 105 }
paddy@163 106 if rows == 0 {
paddy@163 107 return ErrProfileNotFound
paddy@163 108 }
paddy@163 109 return nil
paddy@156 110 }
paddy@156 111
paddy@164 112 func (p *postgres) deleteAuthorizationCodesByClientIDSQL(clientID uuid.ID) *pan.Query {
paddy@164 113 var authCode AuthorizationCode
paddy@164 114 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(authCode))
paddy@164 115 query.IncludeWhere()
paddy@164 116 query.Include(pan.GetUnquotedColumn(authCode, "ClientID")+" = ?", clientID)
paddy@164 117 return query.FlushExpressions(" ")
paddy@164 118 }
paddy@164 119
paddy@164 120 func (p *postgres) deleteAuthorizationCodesByClientID(clientID uuid.ID) error {
paddy@164 121 query := p.deleteAuthorizationCodesByClientIDSQL(clientID)
paddy@164 122 res, err := p.db.Exec(query.String(), query.Args...)
paddy@164 123 if err != nil {
paddy@164 124 return err
paddy@164 125 }
paddy@164 126 rows, err := res.RowsAffected()
paddy@164 127 if err != nil {
paddy@164 128 return err
paddy@164 129 }
paddy@164 130 if rows == 0 {
paddy@164 131 return ErrClientNotFound
paddy@164 132 }
paddy@164 133 return nil
paddy@164 134 }
paddy@164 135
paddy@156 136 func (p *postgres) useAuthorizationCodeSQL(code string) *pan.Query {
paddy@156 137 var authCode AuthorizationCode
paddy@156 138 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(authCode)+" SET ")
paddy@156 139 query.Include(pan.GetUnquotedColumn(authCode, "Used")+" = ?", true)
paddy@156 140 query.IncludeWhere()
paddy@156 141 query.Include(pan.GetUnquotedColumn(authCode, "Code")+" = ?", code)
paddy@156 142 return query.FlushExpressions(" ")
paddy@156 143 }
paddy@156 144
paddy@156 145 func (p *postgres) useAuthorizationCode(code string) error {
paddy@156 146 query := p.useAuthorizationCodeSQL(code)
paddy@156 147 res, err := p.db.Exec(query.String(), query.Args...)
paddy@156 148 if err != nil {
paddy@156 149 return err
paddy@156 150 }
paddy@156 151 rows, err := res.RowsAffected()
paddy@156 152 if err != nil {
paddy@156 153 return err
paddy@156 154 }
paddy@156 155 if rows == 0 {
paddy@156 156 return ErrAuthorizationCodeNotFound
paddy@156 157 }
paddy@156 158 return nil
paddy@156 159 }