auth
auth/context.go
Update CheckEndpoints for strict checking, add CountEndpoints. Create a "strict" mode for CheckEndpoints that will only return true on an exact match, and update the memstore implementation accordingly. Add tests to make sure that the strict mode is adhered to. We need this mode because in certain situations (e.g., the client has more than one endpoint registered), the spec demands a full-string comparison. Add a CountEndpoints method to the ClientStore that will return the number of endpoints registered for a specific client. As we just mentioned, the rules for how a redirect URI is validated depend upon the number of endpoints a client has registered, so we need to be able to get at that number.
| paddy@50 | 1 package auth |
| paddy@50 | 2 |
| paddy@50 | 3 import ( |
| paddy@50 | 4 "errors" |
| paddy@50 | 5 "html/template" |
| paddy@50 | 6 "io" |
| paddy@50 | 7 "time" |
| paddy@50 | 8 |
| paddy@50 | 9 "code.secondbit.org/uuid" |
| paddy@50 | 10 ) |
| paddy@50 | 11 |
| paddy@50 | 12 var ( |
| paddy@50 | 13 ErrNoTemplate = errors.New("no Template specified for the Context") |
| paddy@50 | 14 ) |
| paddy@50 | 15 |
| paddy@50 | 16 type Context struct { |
| paddy@50 | 17 template *template.Template |
| paddy@50 | 18 clients ClientStore |
| paddy@50 | 19 grants GrantStore |
| paddy@50 | 20 profiles ProfileStore |
| paddy@50 | 21 tokens TokenStore |
| paddy@50 | 22 } |
| paddy@50 | 23 |
| paddy@50 | 24 func (c Context) Render(out io.Writer, name string, data interface{}) error { |
| paddy@50 | 25 if c.template == nil { |
| paddy@50 | 26 return ErrNoTemplate |
| paddy@50 | 27 } |
| paddy@50 | 28 return c.template.ExecuteTemplate(out, name, data) |
| paddy@50 | 29 } |
| paddy@50 | 30 |
| paddy@50 | 31 func (c Context) GetClient(id uuid.ID) (Client, error) { |
| paddy@50 | 32 if c.clients == nil { |
| paddy@50 | 33 return Client{}, ErrNoClientStore |
| paddy@50 | 34 } |
| paddy@50 | 35 return c.clients.GetClient(id) |
| paddy@50 | 36 } |
| paddy@50 | 37 |
| paddy@50 | 38 func (c Context) SaveClient(client Client) error { |
| paddy@50 | 39 if c.clients == nil { |
| paddy@50 | 40 return ErrNoClientStore |
| paddy@50 | 41 } |
| paddy@50 | 42 return c.clients.SaveClient(client) |
| paddy@50 | 43 } |
| paddy@50 | 44 |
| paddy@50 | 45 func (c Context) UpdateClient(id uuid.ID, change ClientChange) error { |
| paddy@50 | 46 if c.clients == nil { |
| paddy@50 | 47 return ErrNoClientStore |
| paddy@50 | 48 } |
| paddy@50 | 49 return c.clients.UpdateClient(id, change) |
| paddy@50 | 50 } |
| paddy@50 | 51 |
| paddy@50 | 52 func (c Context) DeleteClient(id uuid.ID) error { |
| paddy@50 | 53 if c.clients == nil { |
| paddy@50 | 54 return ErrNoClientStore |
| paddy@50 | 55 } |
| paddy@50 | 56 return c.clients.DeleteClient(id) |
| paddy@50 | 57 } |
| paddy@50 | 58 |
| paddy@50 | 59 func (c Context) ListClientsByOwner(ownerID uuid.ID, num, offset int) ([]Client, error) { |
| paddy@50 | 60 if c.clients == nil { |
| paddy@50 | 61 return []Client{}, ErrNoClientStore |
| paddy@50 | 62 } |
| paddy@50 | 63 return c.clients.ListClientsByOwner(ownerID, num, offset) |
| paddy@50 | 64 } |
| paddy@50 | 65 |
| paddy@50 | 66 func (c Context) AddEndpoint(client uuid.ID, endpoint Endpoint) error { |
| paddy@50 | 67 if c.clients == nil { |
| paddy@50 | 68 return ErrNoClientStore |
| paddy@50 | 69 } |
| paddy@50 | 70 return c.clients.AddEndpoint(client, endpoint) |
| paddy@50 | 71 } |
| paddy@50 | 72 |
| paddy@50 | 73 func (c Context) RemoveEndpoint(client, endpoint uuid.ID) error { |
| paddy@50 | 74 if c.clients == nil { |
| paddy@50 | 75 return ErrNoClientStore |
| paddy@50 | 76 } |
| paddy@50 | 77 return c.clients.RemoveEndpoint(client, endpoint) |
| paddy@50 | 78 } |
| paddy@50 | 79 |
| paddy@50 | 80 func (c Context) CheckEndpoint(client uuid.ID, URI string) (bool, error) { |
| paddy@50 | 81 if c.clients == nil { |
| paddy@50 | 82 return false, ErrNoClientStore |
| paddy@50 | 83 } |
| paddy@50 | 84 return c.clients.CheckEndpoint(client, URI) |
| paddy@50 | 85 } |
| paddy@50 | 86 |
| paddy@50 | 87 func (c Context) ListEndpoints(client uuid.ID, num, offset int) ([]Endpoint, error) { |
| paddy@50 | 88 if c.clients == nil { |
| paddy@50 | 89 return []Endpoint{}, ErrNoClientStore |
| paddy@50 | 90 } |
| paddy@50 | 91 return c.clients.ListEndpoints(client, num, offset) |
| paddy@50 | 92 } |
| paddy@50 | 93 |
| paddy@50 | 94 func (c Context) GetGrant(code string) (Grant, error) { |
| paddy@50 | 95 if c.grants == nil { |
| paddy@50 | 96 return Grant{}, ErrNoGrantStore |
| paddy@50 | 97 } |
| paddy@50 | 98 return c.grants.GetGrant(code) |
| paddy@50 | 99 } |
| paddy@50 | 100 |
| paddy@50 | 101 func (c Context) SaveGrant(grant Grant) error { |
| paddy@50 | 102 if c.grants == nil { |
| paddy@50 | 103 return ErrNoGrantStore |
| paddy@50 | 104 } |
| paddy@50 | 105 return c.grants.SaveGrant(grant) |
| paddy@50 | 106 } |
| paddy@50 | 107 |
| paddy@50 | 108 func (c Context) DeleteGrant(code string) error { |
| paddy@50 | 109 if c.grants == nil { |
| paddy@50 | 110 return ErrNoGrantStore |
| paddy@50 | 111 } |
| paddy@50 | 112 return c.grants.DeleteGrant(code) |
| paddy@50 | 113 } |
| paddy@50 | 114 |
| paddy@50 | 115 func (c Context) GetProfileByID(id uuid.ID) (Profile, error) { |
| paddy@50 | 116 if c.profiles == nil { |
| paddy@50 | 117 return Profile{}, ErrNoProfileStore |
| paddy@50 | 118 } |
| paddy@50 | 119 return c.profiles.GetProfileByID(id) |
| paddy@50 | 120 } |
| paddy@50 | 121 |
| paddy@50 | 122 func (c Context) GetProfileByLogin(loginType, value string) (Profile, error) { |
| paddy@50 | 123 if c.profiles == nil { |
| paddy@50 | 124 return Profile{}, ErrNoProfileStore |
| paddy@50 | 125 } |
| paddy@50 | 126 return c.profiles.GetProfileByLogin(loginType, value) |
| paddy@50 | 127 } |
| paddy@50 | 128 |
| paddy@50 | 129 func (c Context) SaveProfile(profile Profile) error { |
| paddy@50 | 130 if c.profiles == nil { |
| paddy@50 | 131 return ErrNoProfileStore |
| paddy@50 | 132 } |
| paddy@50 | 133 return c.profiles.SaveProfile(profile) |
| paddy@50 | 134 } |
| paddy@50 | 135 |
| paddy@50 | 136 func (c Context) UpdateProfile(id uuid.ID, change ProfileChange) error { |
| paddy@50 | 137 if c.profiles == nil { |
| paddy@50 | 138 return ErrNoProfileStore |
| paddy@50 | 139 } |
| paddy@50 | 140 return c.profiles.UpdateProfile(id, change) |
| paddy@50 | 141 } |
| paddy@50 | 142 |
| paddy@50 | 143 func (c Context) UpdateProfiles(ids []uuid.ID, change BulkProfileChange) error { |
| paddy@50 | 144 if c.profiles == nil { |
| paddy@50 | 145 return ErrNoProfileStore |
| paddy@50 | 146 } |
| paddy@50 | 147 return c.profiles.UpdateProfiles(ids, change) |
| paddy@50 | 148 } |
| paddy@50 | 149 |
| paddy@50 | 150 func (c Context) DeleteProfile(id uuid.ID) error { |
| paddy@50 | 151 if c.profiles == nil { |
| paddy@50 | 152 return ErrNoProfileStore |
| paddy@50 | 153 } |
| paddy@50 | 154 return c.profiles.DeleteProfile(id) |
| paddy@50 | 155 } |
| paddy@50 | 156 |
| paddy@50 | 157 func (c Context) AddLogin(login Login) error { |
| paddy@50 | 158 if c.profiles == nil { |
| paddy@50 | 159 return ErrNoProfileStore |
| paddy@50 | 160 } |
| paddy@50 | 161 return c.profiles.AddLogin(login) |
| paddy@50 | 162 } |
| paddy@50 | 163 |
| paddy@50 | 164 func (c Context) RemoveLogin(loginType, value string, profile uuid.ID) error { |
| paddy@50 | 165 if c.profiles == nil { |
| paddy@50 | 166 return ErrNoProfileStore |
| paddy@50 | 167 } |
| paddy@50 | 168 return c.profiles.RemoveLogin(loginType, value, profile) |
| paddy@50 | 169 } |
| paddy@50 | 170 |
| paddy@50 | 171 func (c Context) RecordLoginUse(loginType, value string, when time.Time) error { |
| paddy@50 | 172 if c.profiles == nil { |
| paddy@50 | 173 return ErrNoProfileStore |
| paddy@50 | 174 } |
| paddy@50 | 175 return c.profiles.RecordLoginUse(loginType, value, when) |
| paddy@50 | 176 } |
| paddy@50 | 177 |
| paddy@50 | 178 func (c Context) ListLogins(profile uuid.ID, num, offset int) ([]Login, error) { |
| paddy@50 | 179 if c.profiles == nil { |
| paddy@50 | 180 return []Login{}, ErrNoProfileStore |
| paddy@50 | 181 } |
| paddy@50 | 182 return c.profiles.ListLogins(profile, num, offset) |
| paddy@50 | 183 } |
| paddy@50 | 184 |
| paddy@50 | 185 func (c Context) GetToken(token string, refresh bool) (Token, error) { |
| paddy@50 | 186 if c.tokens == nil { |
| paddy@50 | 187 return Token{}, ErrNoTokenStore |
| paddy@50 | 188 } |
| paddy@50 | 189 return c.tokens.GetToken(token, refresh) |
| paddy@50 | 190 } |
| paddy@50 | 191 |
| paddy@50 | 192 func (c Context) SaveToken(token Token) error { |
| paddy@50 | 193 if c.tokens == nil { |
| paddy@50 | 194 return ErrNoTokenStore |
| paddy@50 | 195 } |
| paddy@50 | 196 return c.tokens.SaveToken(token) |
| paddy@50 | 197 } |
| paddy@50 | 198 |
| paddy@50 | 199 func (c Context) RemoveToken(token string) error { |
| paddy@50 | 200 if c.tokens == nil { |
| paddy@50 | 201 return ErrNoTokenStore |
| paddy@50 | 202 } |
| paddy@50 | 203 return c.tokens.RemoveToken(token) |
| paddy@50 | 204 } |
| paddy@50 | 205 |
| paddy@50 | 206 func (c Context) GetTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) { |
| paddy@50 | 207 if c.tokens == nil { |
| paddy@50 | 208 return []Token{}, ErrNoTokenStore |
| paddy@50 | 209 } |
| paddy@50 | 210 return c.tokens.GetTokensByProfileID(profileID, num, offset) |
| paddy@50 | 211 } |