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.
4 "code.secondbit.org/uuid.hg"
7 "github.com/secondbit/pan"
10 func (t Token) GetSQLTableName() string {
14 func (p *postgres) getTokenSQL(token string, refresh bool) *pan.Query {
16 fields, _ := pan.GetFields(t)
17 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(t))
20 query.Include(pan.GetUnquotedColumn(t, "AccessToken")+" = ?", token)
22 query.Include(pan.GetUnquotedColumn(t, "RefreshToken")+" = ?", token)
24 return query.FlushExpressions(" ")
27 func (p *postgres) getToken(token string, refresh bool) (Token, error) {
28 query := p.getTokenSQL(token, refresh)
29 rows, err := p.db.Query(query.String(), query.Args...)
36 err := pan.Unmarshal(rows, &t)
42 if err = rows.Err(); err != nil {
46 return t, ErrTokenNotFound
51 func (p *postgres) saveTokenSQL(token Token) *pan.Query {
52 fields, values := pan.GetFields(token)
53 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(token))
54 query.Include("(" + pan.QueryList(fields) + ")")
55 query.Include("VALUES")
56 query.Include("("+pan.VariableList(len(values))+")", values...)
57 return query.FlushExpressions(" ")
60 func (p *postgres) saveToken(token Token) error {
61 query := p.saveTokenSQL(token)
62 _, err := p.db.Exec(query.String(), query.Args...)
63 if e, ok := err.(*pq.Error); ok && e.Constraint == "tokens_pkey" {
64 err = ErrTokenAlreadyExists
66 if err != nil || len(token.Scopes) < 1 {
72 func (p *postgres) revokeTokenSQL(token string) *pan.Query {
74 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(t)+" SET ")
75 query.Include(pan.GetUnquotedColumn(t, "Revoked")+" = ?", true)
77 query.Include(pan.GetUnquotedColumn(t, "RefreshToken")+" = ?", token)
78 return query.FlushExpressions(" ")
81 func (p *postgres) revokeToken(token string) error {
82 query := p.revokeTokenSQL(token)
83 res, err := p.db.Exec(query.String(), query.Args...)
87 rows, err := res.RowsAffected()
92 return ErrTokenNotFound
97 func (p *postgres) revokeTokensByProfileIDSQL(profileID uuid.ID) *pan.Query {
99 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(t)+" SET ")
100 query.Include(pan.GetUnquotedColumn(t, "Revoked")+" = ?", true)
102 query.Include(pan.GetUnquotedColumn(t, "ProfileID")+" = ?", profileID)
103 return query.FlushExpressions(" ")
106 func (p *postgres) revokeTokensByProfileID(profileID uuid.ID) error {
107 query := p.revokeTokensByProfileIDSQL(profileID)
108 res, err := p.db.Exec(query.String(), query.Args...)
112 rows, err := res.RowsAffected()
117 return ErrProfileNotFound
122 func (p *postgres) revokeTokensByClientIDSQL(clientID uuid.ID) *pan.Query {
124 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(t)+" SET ")
125 query.Include(pan.GetUnquotedColumn(t, "Revoked")+" = ?", true)
127 query.Include(pan.GetUnquotedColumn(t, "ClientID")+" = ?", clientID)
128 return query.FlushExpressions(" ")
131 func (p *postgres) revokeTokensByClientID(clientID uuid.ID) error {
132 query := p.revokeTokensByClientIDSQL(clientID)
133 res, err := p.db.Exec(query.String(), query.Args...)
137 rows, err := res.RowsAffected()
142 return ErrClientNotFound
147 func (p *postgres) getTokensByProfileIDSQL(profileID uuid.ID, num, offset int) *pan.Query {
149 fields, _ := pan.GetFields(token)
150 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(token))
152 query.Include(pan.GetUnquotedColumn(token, "ProfileID")+" = ?", profileID)
153 query.IncludeLimit(int64(num))
154 query.IncludeOffset(int64(offset))
155 return query.FlushExpressions(" ")
158 func (p *postgres) getTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) {
159 query := p.getTokensByProfileIDSQL(profileID, num, offset)
160 rows, err := p.db.Query(query.String(), query.Args...)
162 return []Token{}, err
165 var tokenIDs []string
168 err = pan.Unmarshal(rows, &token)
172 tokens = append(tokens, token)
173 tokenIDs = append(tokenIDs, token.AccessToken)
175 if err = rows.Err(); err != nil {