Use postgres arrays for scope associations.
Use the new pqarrays library I wrote to store Scope associations for Tokens and
AuthorizationCodes, instead of using our hacky and abstraction-breaking
many-to-many code.
We also created the authStore.deleteAuthorizationCodesByProfileID method, to
clear out the AuthorizationCodes that belong to a Profile (used when the Profile
is deleted). So we added the implementation for memstore and for our postgres
store.
Call Context.DeleteAuthorizationCodesByProfileID when deleting a Profile to
clean up after it.
Rename sortedScopes to Scopes, which we use pqarrays.StringArray's methods on to
fulfill the sql.Scanner and driver.Valuer interfaces. This lets us store Scopes
in postgres arrays.
Create a stringsToScopes helper function that creates Scope objects, with their
IDs filled by the strings specified.
Update our GrantType.Validate function signature to return Scopes instead of
[]string.
Create a Scopes.Strings() helper method that returns a []string of the IDs of
the Scopes.
Update our SQL init file to use the new postgres array definition, instead of
the many-to-many definition.
9 "code.secondbit.org/uuid.hg"
13 RegisterGrantType("authorization_code", GrantType{
14 Validate: authCodeGrantValidate,
15 Invalidate: authCodeGrantInvalidate,
17 ReturnToken: RenderJSONToken,
19 AuditString: authCodeGrantAuditString,
24 // ErrNoAuthorizationCodeStore is returned when a Context tries to act on a authorizationCodeStore without setting one first.
25 ErrNoAuthorizationCodeStore = errors.New("no authorizationCodeStore was specified for the Context")
26 // ErrAuthorizationCodeNotFound is returned when an AuthorizationCode is requested but not found in the authorizationCodeStore.
27 ErrAuthorizationCodeNotFound = errors.New("authorization code not found in authorizationCodeStore")
28 // ErrAuthorizationCodeAlreadyExists is returned when an AuthorizationCode is added to a authorizationCodeStore, but another AuthorizationCode with the
29 // same Code already exists in the authorizationCodeStore.
30 ErrAuthorizationCodeAlreadyExists = errors.New("authorization code already exists in authorizationCodeStore")
33 // AuthorizationCode represents an authorization grant made by a user to a Client, to
34 // access user data within a defined Scope for a limited amount of time.
35 type AuthorizationCode struct {
47 type authorizationCodeStore interface {
48 getAuthorizationCode(code string) (AuthorizationCode, error)
49 saveAuthorizationCode(authCode AuthorizationCode) error
50 deleteAuthorizationCode(code string) error
51 deleteAuthorizationCodesByProfileID(profileID uuid.ID) error
52 useAuthorizationCode(code string) error
55 func (m *memstore) getAuthorizationCode(code string) (AuthorizationCode, error) {
56 m.authCodeLock.RLock()
57 defer m.authCodeLock.RUnlock()
58 authCode, ok := m.authCodes[code]
60 return AuthorizationCode{}, ErrAuthorizationCodeNotFound
65 func (m *memstore) saveAuthorizationCode(authCode AuthorizationCode) error {
67 defer m.authCodeLock.Unlock()
68 _, ok := m.authCodes[authCode.Code]
70 return ErrAuthorizationCodeAlreadyExists
72 m.authCodes[authCode.Code] = authCode
76 func (m *memstore) deleteAuthorizationCode(code string) error {
78 defer m.authCodeLock.Unlock()
79 _, ok := m.authCodes[code]
81 return ErrAuthorizationCodeNotFound
83 delete(m.authCodes, code)
87 func (m *memstore) deleteAuthorizationCodesByProfileID(profileID uuid.ID) error {
89 defer m.authCodeLock.Unlock()
91 for _, code := range m.authCodes {
92 if code.ProfileID.Equal(profileID) {
93 codes = append(codes, code.Code)
97 return ErrProfileNotFound
99 for _, code := range codes {
100 delete(m.authCodes, code)
105 func (m *memstore) useAuthorizationCode(code string) error {
106 m.authCodeLock.Lock()
107 defer m.authCodeLock.Unlock()
108 a, ok := m.authCodes[code]
110 return ErrAuthorizationCodeNotFound
113 m.authCodes[code] = a
117 func authCodeGrantValidate(w http.ResponseWriter, r *http.Request, context Context) (scopes Scopes, profileID uuid.ID, valid bool) {
118 enc := json.NewEncoder(w)
119 code := r.PostFormValue("code")
121 w.WriteHeader(http.StatusBadRequest)
122 renderJSONError(enc, "invalid_request")
125 clientID, _, ok := getClientAuth(w, r, true)
129 authCode, err := context.GetAuthorizationCode(code)
131 if err == ErrAuthorizationCodeNotFound {
132 w.WriteHeader(http.StatusBadRequest)
133 renderJSONError(enc, "invalid_grant")
136 w.WriteHeader(http.StatusInternalServerError)
137 renderJSONError(enc, "server_error")
140 redirectURI := r.PostFormValue("redirect_uri")
141 if authCode.RedirectURI != redirectURI {
142 w.WriteHeader(http.StatusBadRequest)
143 renderJSONError(enc, "invalid_grant")
146 if !authCode.ClientID.Equal(clientID) {
147 w.WriteHeader(http.StatusBadRequest)
148 renderJSONError(enc, "invalid_grant")
151 return authCode.Scopes, authCode.ProfileID, true
154 func authCodeGrantInvalidate(r *http.Request, context Context) error {
155 code := r.PostFormValue("code")
157 return ErrAuthorizationCodeNotFound
159 return context.UseAuthorizationCode(code)
162 func authCodeGrantAuditString(r *http.Request) string {
163 return "authcode:" + r.PostFormValue("code")