Start to support deleting profiles through the API.
Create a removeLoginsByProfile method on the profileStore, to allow an easy way
to bulk-delete logins associated with a Profile after the Profile has been
deleted.
Create postgres and memstore implementations of the removeLoginsByProfile
method.
Create a cleanUpAfterProfileDeletion helper method that will clean up the child
objects of a Profile (its Sessions, Tokens, Clients, etc.). The intended usage
is to call this in a goroutine after a Profile has been deleted, to try and get
things back in order.
Detect when the UpdateProfileHandler API is used to set the Deleted flag of a
Profile to true, and clean up after the Profile when that's the case.
Add a DeleteProfileHandler API endpoint that is a shortcut to setting the
Deleted flag of a Profile to true and cleaning up after the Profile.
The problem with our approach thus far is that some of it is reversible and some
is not. If a Profile is maliciously/accidentally deleted, it's simple enough to
use the API as a superuser to restore the Profile. But doing that will not (and
cannot) restore the Logins associated with that Profile, for example. While it
would be nice to add a Deleted flag to our Logins that we could simply toggle,
that would wreak havoc with our database constraints and ensuring uniqueness of
Login values. I still don't have a solution for this, outside the superuser
manually restoring a Login for the Profile, after which the user can
authenticate themselves and add more Logins as desired. But there has to be a
better way.
I suppose since the passphrase is being stored with the Profile and not the
Login, we could offer an endpoint that would automate this, but... well, that
would be tricky. It would require the user remembering their Profile ID, and
let's be honest, nobody's going to remember a UUID.
Maybe such an endpoint would help from a customer service standpoint: we
identify their Profile manually, then send them to /profiles/ID/restorelogin or
something, and that lets them add a Login back to the Profile.
I'll figure it out later. For now, we know we at least have enough information
to identify a user is who they say they are and resolve the situation manually.
10 "code.secondbit.org/uuid.hg"
14 defaultTokenExpiration = 3600 // one hour
18 RegisterGrantType("refresh_token", GrantType{
19 Validate: refreshTokenValidate,
20 Invalidate: refreshTokenInvalidate,
22 ReturnToken: RenderJSONToken,
23 AuditString: refreshTokenAuditString,
28 // ErrNoTokenStore is returned when a Context tries to act on a tokenStore without setting one first.
29 ErrNoTokenStore = errors.New("no tokenStore was specified for the Context")
30 // ErrTokenNotFound is returned when a Token is requested but not found in a tokenStore.
31 ErrTokenNotFound = errors.New("token not found in tokenStore")
32 // ErrTokenAlreadyExists is returned when a Token is added to a tokenStore, but another Token with
33 // the same AccessToken property already exists in the tokenStore.
34 ErrTokenAlreadyExists = errors.New("token already exists in tokenStore")
37 // Token represents an access and/or refresh token that the Client can use to access user data
38 // or obtain a new access token.
46 Scopes []string `sql_column:"-"`
53 type tokenStore interface {
54 getToken(token string, refresh bool) (Token, error)
55 saveToken(token Token) error
56 revokeToken(token string, refresh bool) error
57 getTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error)
60 func (m *memstore) getToken(token string, refresh bool) (Token, error) {
62 t, err := m.lookupTokenByRefresh(token)
69 defer m.tokenLock.RUnlock()
70 result, ok := m.tokens[token]
72 return Token{}, ErrTokenNotFound
77 func (m *memstore) saveToken(token Token) error {
79 defer m.tokenLock.Unlock()
80 _, ok := m.tokens[token.AccessToken]
82 return ErrTokenAlreadyExists
84 m.tokens[token.AccessToken] = token
85 if token.RefreshToken != "" {
86 m.refreshTokenLookup[token.RefreshToken] = token.AccessToken
88 if _, ok = m.profileTokenLookup[token.ProfileID.String()]; ok {
89 m.profileTokenLookup[token.ProfileID.String()] = append(m.profileTokenLookup[token.ProfileID.String()], token.AccessToken)
91 m.profileTokenLookup[token.ProfileID.String()] = []string{token.AccessToken}
96 func (m *memstore) revokeToken(token string, refresh bool) error {
98 t, err := m.lookupTokenByRefresh(token)
105 defer m.tokenLock.Unlock()
106 t, ok := m.tokens[token]
108 return ErrTokenNotFound
111 t.RefreshRevoked = true
119 func (m *memstore) getTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) {
120 ids, err := m.lookupTokensByProfileID(profileID.String())
122 return []Token{}, err
124 if len(ids) > num+offset {
125 ids = ids[offset : num+offset]
126 } else if len(ids) > offset {
129 return []Token{}, nil
132 for _, id := range ids {
133 token, err := m.getToken(id, false)
135 return []Token{}, err
137 tokens = append(tokens, token)
142 func refreshTokenValidate(w http.ResponseWriter, r *http.Request, context Context) (scopes []string, profileID uuid.ID, valid bool) {
143 enc := json.NewEncoder(w)
144 refresh := r.PostFormValue("refresh_token")
146 w.WriteHeader(http.StatusBadRequest)
147 renderJSONError(enc, "invalid_request")
150 token, err := context.GetToken(refresh, true)
152 if err == ErrTokenNotFound {
153 w.WriteHeader(http.StatusBadRequest)
154 renderJSONError(enc, "invalid_grant")
157 log.Println("Error exchanging refresh token:", err)
158 w.WriteHeader(http.StatusInternalServerError)
159 renderJSONError(enc, "server_error")
162 clientID, _, ok := getClientAuth(w, r, true)
166 if !token.ClientID.Equal(clientID) {
167 w.WriteHeader(http.StatusBadRequest)
168 renderJSONError(enc, "invalid_grant")
171 if token.RefreshRevoked {
172 w.WriteHeader(http.StatusBadRequest)
173 renderJSONError(enc, "invalid_grant")
176 return token.Scopes, token.ProfileID, true
179 func refreshTokenInvalidate(r *http.Request, context Context) error {
180 refresh := r.PostFormValue("refresh_token")
182 return ErrTokenNotFound
184 return context.RevokeToken(refresh, true)
187 func refreshTokenAuditString(r *http.Request) string {
188 return "refresh_token:" + r.PostFormValue("refresh_token")
191 // BUG(paddy): We need to implement a handler for revoking a token.
192 // BUG(paddy): We need to implement a handler for listing active tokens.