Switch to a JWT approach.
We're going to use a JWT as our access tokens (as discussed in &yet's excellent
post https://blog.andyet.com/2015/05/12/micro-services-user-info-and-auth and my
ensuing conversation with Fritzy).
The benefit of this approach is that we can do authentication and even some
authorization without touching the database at all.
The drawback is that we can no longer revoke access tokens, only the refresh
tokens that grant the access tokens.
We need a new config variable to set our private key, used to sign the JWT.
We get to remove our token handlers, as we no longer can revoke tokens, so
there's no purpose in getting information about it or listing them.
Our tokenStore revokeToken gets to be simplified, as it will only ever be used
for refresh tokens now. We also updated our postgres and memstore
implementations.
We added a helper method for generating the signed "access token" (our JWT) and
started using it in the places where we're creating a Token.
We get to remove the `revoked` SQL column for the tokens table, and rename the
`refresh_revoked` column to just be `revoked`.
We shortened our access token expiration to 15 minutes instead of an hour, to
deal with the token not being revokable.
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 deleteAuthorizationCodesByClientID(clientID uuid.ID) error
53 useAuthorizationCode(code string) error
56 func (m *memstore) getAuthorizationCode(code string) (AuthorizationCode, error) {
57 m.authCodeLock.RLock()
58 defer m.authCodeLock.RUnlock()
59 authCode, ok := m.authCodes[code]
61 return AuthorizationCode{}, ErrAuthorizationCodeNotFound
66 func (m *memstore) saveAuthorizationCode(authCode AuthorizationCode) error {
68 defer m.authCodeLock.Unlock()
69 _, ok := m.authCodes[authCode.Code]
71 return ErrAuthorizationCodeAlreadyExists
73 m.authCodes[authCode.Code] = authCode
77 func (m *memstore) deleteAuthorizationCode(code string) error {
79 defer m.authCodeLock.Unlock()
80 _, ok := m.authCodes[code]
82 return ErrAuthorizationCodeNotFound
84 delete(m.authCodes, code)
88 func (m *memstore) deleteAuthorizationCodesByProfileID(profileID uuid.ID) error {
90 defer m.authCodeLock.Unlock()
92 for _, code := range m.authCodes {
93 if code.ProfileID.Equal(profileID) {
94 codes = append(codes, code.Code)
98 return ErrProfileNotFound
100 for _, code := range codes {
101 delete(m.authCodes, code)
106 func (m *memstore) deleteAuthorizationCodesByClientID(clientID uuid.ID) error {
107 m.authCodeLock.Lock()
108 defer m.authCodeLock.Unlock()
110 for _, code := range m.authCodes {
111 if code.ClientID.Equal(clientID) {
112 codes = append(codes, code.Code)
116 return ErrClientNotFound
118 for _, code := range codes {
119 delete(m.authCodes, code)
124 func (m *memstore) useAuthorizationCode(code string) error {
125 m.authCodeLock.Lock()
126 defer m.authCodeLock.Unlock()
127 a, ok := m.authCodes[code]
129 return ErrAuthorizationCodeNotFound
132 m.authCodes[code] = a
136 func authCodeGrantValidate(w http.ResponseWriter, r *http.Request, context Context) (scopes Scopes, profileID uuid.ID, valid bool) {
137 enc := json.NewEncoder(w)
138 code := r.PostFormValue("code")
140 w.WriteHeader(http.StatusBadRequest)
141 renderJSONError(enc, "invalid_request")
144 clientID, _, ok := getClientAuth(w, r, true)
148 authCode, err := context.GetAuthorizationCode(code)
150 if err == ErrAuthorizationCodeNotFound {
151 w.WriteHeader(http.StatusBadRequest)
152 renderJSONError(enc, "invalid_grant")
155 w.WriteHeader(http.StatusInternalServerError)
156 renderJSONError(enc, "server_error")
159 redirectURI := r.PostFormValue("redirect_uri")
160 if authCode.RedirectURI != redirectURI {
161 w.WriteHeader(http.StatusBadRequest)
162 renderJSONError(enc, "invalid_grant")
165 if !authCode.ClientID.Equal(clientID) {
166 w.WriteHeader(http.StatusBadRequest)
167 renderJSONError(enc, "invalid_grant")
170 return authCode.Scopes, authCode.ProfileID, true
173 func authCodeGrantInvalidate(r *http.Request, context Context) error {
174 code := r.PostFormValue("code")
176 return ErrAuthorizationCodeNotFound
178 return context.UseAuthorizationCode(code)
181 func authCodeGrantAuditString(r *http.Request) string {
182 return "authcode:" + r.PostFormValue("code")