Break out scopes and events.
This repo has gotten unwieldy, and there are portions of it that need to be
imported by a large number of other packages.
For example, scopes will be used in almost every API we write. Rather than
importing the entirety of this codebase into every API we write, I've opted to
move the scope logic out into a scopes package, with a subpackage for the
defined types, which is all most projects actually want to import.
We also define some event type constants, and importing those shouldn't require
a project to import all our dependencies, either. So I made an events subpackage
that just holds those constants.
This package has become a little bit of a red-headed stepchild and is do for a
refactor, but I'm trying to put that off as long as I can.
The refactoring of our scopes stuff has left a bug wherein a token can be
granted for scopes that don't exist. I'm going to need to revisit that, and also
how to limit scopes to only be granted to the users that should be able to
request them. But that's a battle for another day.
9 "code.secondbit.org/scopes.hg/types"
10 "code.secondbit.org/uuid.hg"
14 RegisterGrantType("authorization_code", GrantType{
15 Validate: authCodeGrantValidate,
16 Invalidate: authCodeGrantInvalidate,
18 ReturnToken: RenderJSONToken,
20 AuditString: authCodeGrantAuditString,
25 // ErrNoAuthorizationCodeStore is returned when a Context tries to act on a authorizationCodeStore without setting one first.
26 ErrNoAuthorizationCodeStore = errors.New("no authorizationCodeStore was specified for the Context")
27 // ErrAuthorizationCodeNotFound is returned when an AuthorizationCode is requested but not found in the authorizationCodeStore.
28 ErrAuthorizationCodeNotFound = errors.New("authorization code not found in authorizationCodeStore")
29 // ErrAuthorizationCodeAlreadyExists is returned when an AuthorizationCode is added to a authorizationCodeStore, but another AuthorizationCode with the
30 // same Code already exists in the authorizationCodeStore.
31 ErrAuthorizationCodeAlreadyExists = errors.New("authorization code already exists in authorizationCodeStore")
34 // AuthorizationCode represents an authorization grant made by a user to a Client, to
35 // access user data within a defined Scope for a limited amount of time.
36 type AuthorizationCode struct {
41 Scopes scopeTypes.Scopes
48 type authorizationCodeStore interface {
49 getAuthorizationCode(code string) (AuthorizationCode, error)
50 saveAuthorizationCode(authCode AuthorizationCode) error
51 deleteAuthorizationCode(code string) error
52 deleteAuthorizationCodesByProfileID(profileID uuid.ID) error
53 deleteAuthorizationCodesByClientID(clientID uuid.ID) error
54 useAuthorizationCode(code string) error
57 func (m *memstore) getAuthorizationCode(code string) (AuthorizationCode, error) {
58 m.authCodeLock.RLock()
59 defer m.authCodeLock.RUnlock()
60 authCode, ok := m.authCodes[code]
62 return AuthorizationCode{}, ErrAuthorizationCodeNotFound
67 func (m *memstore) saveAuthorizationCode(authCode AuthorizationCode) error {
69 defer m.authCodeLock.Unlock()
70 _, ok := m.authCodes[authCode.Code]
72 return ErrAuthorizationCodeAlreadyExists
74 m.authCodes[authCode.Code] = authCode
78 func (m *memstore) deleteAuthorizationCode(code string) error {
80 defer m.authCodeLock.Unlock()
81 _, ok := m.authCodes[code]
83 return ErrAuthorizationCodeNotFound
85 delete(m.authCodes, code)
89 func (m *memstore) deleteAuthorizationCodesByProfileID(profileID uuid.ID) error {
91 defer m.authCodeLock.Unlock()
93 for _, code := range m.authCodes {
94 if code.ProfileID.Equal(profileID) {
95 codes = append(codes, code.Code)
99 return ErrProfileNotFound
101 for _, code := range codes {
102 delete(m.authCodes, code)
107 func (m *memstore) deleteAuthorizationCodesByClientID(clientID uuid.ID) error {
108 m.authCodeLock.Lock()
109 defer m.authCodeLock.Unlock()
111 for _, code := range m.authCodes {
112 if code.ClientID.Equal(clientID) {
113 codes = append(codes, code.Code)
117 return ErrClientNotFound
119 for _, code := range codes {
120 delete(m.authCodes, code)
125 func (m *memstore) useAuthorizationCode(code string) error {
126 m.authCodeLock.Lock()
127 defer m.authCodeLock.Unlock()
128 a, ok := m.authCodes[code]
130 return ErrAuthorizationCodeNotFound
133 m.authCodes[code] = a
137 func authCodeGrantValidate(w http.ResponseWriter, r *http.Request, context Context) (scopes scopeTypes.Scopes, profileID uuid.ID, valid bool) {
138 enc := json.NewEncoder(w)
139 code := r.PostFormValue("code")
141 w.WriteHeader(http.StatusBadRequest)
142 renderJSONError(enc, "invalid_request")
145 clientID, _, ok := getClientAuth(w, r, true)
149 authCode, err := context.GetAuthorizationCode(code)
151 if err == ErrAuthorizationCodeNotFound {
152 w.WriteHeader(http.StatusBadRequest)
153 renderJSONError(enc, "invalid_grant")
156 w.WriteHeader(http.StatusInternalServerError)
157 renderJSONError(enc, "server_error")
160 redirectURI := r.PostFormValue("redirect_uri")
161 if authCode.RedirectURI != redirectURI {
162 w.WriteHeader(http.StatusBadRequest)
163 renderJSONError(enc, "invalid_grant")
166 if !authCode.ClientID.Equal(clientID) {
167 w.WriteHeader(http.StatusBadRequest)
168 renderJSONError(enc, "invalid_grant")
171 return authCode.Scopes, authCode.ProfileID, true
174 func authCodeGrantInvalidate(r *http.Request, context Context) error {
175 code := r.PostFormValue("code")
177 return ErrAuthorizationCodeNotFound
179 return context.UseAuthorizationCode(code)
182 func authCodeGrantAuditString(r *http.Request) string {
183 return "authcode:" + r.PostFormValue("code")