auth
auth/token_postgres.go
Implement postgres version of the tokenStore. Create a postgres implementation for the tokenStore. Note that because pq doesn't support Postgres' array types (see https://github.com/lib/pq/issues/49), we couldn't just store the token.Scopes field as a Postgres array of varchars, which would have been the ideal. Instead, we need a many-to-many table that maps tokens to scopes. This meant we needed a special tokenScope type for our database mapping, and we needed to complicate the token storage/retrieval functions a little bit. It's kind of ugly, I'm not a huge fan of it, and I'd much rather be using the Postgres array types, but... well, here we are. We also added the postgres tokenStore to our slice of tokenStores to test when the correct environment variables are present. We wrote initialization SQL for the tables required by the postgres tokenStore. Also, added a helper script for emptying the test database, because I got tired of doing it by hand. We should be doing that in an automated fashion in the tests themselves, but that would mean extending the *Store interfaces.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/token_postgres.go Tue Apr 07 01:00:26 2015 -0400 1.3 @@ -0,0 +1,214 @@ 1.4 +package auth 1.5 + 1.6 +import ( 1.7 + "code.secondbit.org/uuid.hg" 1.8 + 1.9 + "github.com/lib/pq" 1.10 + "github.com/secondbit/pan" 1.11 +) 1.12 + 1.13 +type tokenScope struct { 1.14 + Token string 1.15 + Scope string 1.16 +} 1.17 + 1.18 +func (t tokenScope) GetSQLTableName() string { 1.19 + return "scopes_tokens" 1.20 +} 1.21 + 1.22 +func (t Token) GetSQLTableName() string { 1.23 + return "tokens" 1.24 +} 1.25 + 1.26 +func (p *postgres) getTokenSQL(token string, refresh bool) *pan.Query { 1.27 + var t Token 1.28 + fields, _ := pan.GetFields(t) 1.29 + query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(t)) 1.30 + query.IncludeWhere() 1.31 + if !refresh { 1.32 + query.Include(pan.GetUnquotedColumn(t, "AccessToken")+" = ?", token) 1.33 + } else { 1.34 + query.Include(pan.GetUnquotedColumn(t, "RefreshToken")+" = ?", token) 1.35 + } 1.36 + return query.FlushExpressions(" ") 1.37 +} 1.38 + 1.39 +func (p *postgres) getToken(token string, refresh bool) (Token, error) { 1.40 + query := p.getTokenSQL(token, refresh) 1.41 + rows, err := p.db.Query(query.String(), query.Args...) 1.42 + if err != nil { 1.43 + return Token{}, err 1.44 + } 1.45 + var t Token 1.46 + var found bool 1.47 + for rows.Next() { 1.48 + err := pan.Unmarshal(rows, &t) 1.49 + if err != nil { 1.50 + return t, err 1.51 + } 1.52 + found = true 1.53 + } 1.54 + if err = rows.Err(); err != nil { 1.55 + return t, err 1.56 + } 1.57 + if !found { 1.58 + return t, ErrTokenNotFound 1.59 + } 1.60 + query = p.getTokenScopesSQL([]string{t.AccessToken}) 1.61 + rows, err = p.db.Query(query.String(), query.Args...) 1.62 + if err != nil { 1.63 + return t, err 1.64 + } 1.65 + for rows.Next() { 1.66 + var ts tokenScope 1.67 + err = pan.Unmarshal(rows, &ts) 1.68 + if err != nil { 1.69 + return t, err 1.70 + } 1.71 + t.Scopes = append(t.Scopes, ts.Scope) 1.72 + } 1.73 + if err = rows.Err(); err != nil { 1.74 + return t, err 1.75 + } 1.76 + return t, nil 1.77 +} 1.78 + 1.79 +func (p *postgres) saveTokenSQL(token Token) *pan.Query { 1.80 + fields, values := pan.GetFields(token) 1.81 + query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(token)) 1.82 + query.Include("(" + pan.QueryList(fields) + ")") 1.83 + query.Include("VALUES") 1.84 + query.Include("("+pan.VariableList(len(values))+")", values...) 1.85 + return query.FlushExpressions(" ") 1.86 +} 1.87 + 1.88 +func (p *postgres) saveTokenScopesSQL(ts []tokenScope) *pan.Query { 1.89 + fields, _ := pan.GetFields(ts[0]) 1.90 + query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(ts[0])) 1.91 + query.Include("(" + pan.QueryList(fields) + ")") 1.92 + query.Include("VALUES") 1.93 + query.FlushExpressions(" ") 1.94 + for _, t := range ts { 1.95 + _, values := pan.GetFields(t) 1.96 + query.Include("("+pan.VariableList(len(values))+")", values...) 1.97 + } 1.98 + return query.FlushExpressions(", ") 1.99 +} 1.100 + 1.101 +func (p *postgres) saveToken(token Token) error { 1.102 + query := p.saveTokenSQL(token) 1.103 + _, err := p.db.Exec(query.String(), query.Args...) 1.104 + if e, ok := err.(*pq.Error); ok && e.Constraint == "tokens_pkey" { 1.105 + err = ErrTokenAlreadyExists 1.106 + } 1.107 + if err != nil || len(token.Scopes) < 1 { 1.108 + return err 1.109 + } 1.110 + var ts []tokenScope 1.111 + for _, scope := range token.Scopes { 1.112 + ts = append(ts, tokenScope{Token: token.AccessToken, Scope: scope}) 1.113 + } 1.114 + query = p.saveTokenScopesSQL(ts) 1.115 + _, err = p.db.Exec(query.String(), query.Args...) 1.116 + return err 1.117 +} 1.118 + 1.119 +func (p *postgres) revokeTokenSQL(token string, refresh bool) *pan.Query { 1.120 + var t Token 1.121 + query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(t)+" SET ") 1.122 + query.Include(pan.GetUnquotedColumn(t, "Revoked")+" = ?", true) 1.123 + query.IncludeWhere() 1.124 + if !refresh { 1.125 + query.Include(pan.GetUnquotedColumn(t, "AccessToken")+" = ?", token) 1.126 + } else { 1.127 + query.Include(pan.GetUnquotedColumn(t, "RefreshToken")+" = ?", token) 1.128 + } 1.129 + return query.FlushExpressions(" ") 1.130 +} 1.131 + 1.132 +func (p *postgres) revokeToken(token string, refresh bool) error { 1.133 + query := p.revokeTokenSQL(token, refresh) 1.134 + res, err := p.db.Exec(query.String(), query.Args...) 1.135 + if err != nil { 1.136 + return err 1.137 + } 1.138 + rows, err := res.RowsAffected() 1.139 + if err != nil { 1.140 + return err 1.141 + } 1.142 + if rows == 0 { 1.143 + return ErrTokenNotFound 1.144 + } 1.145 + return nil 1.146 +} 1.147 + 1.148 +func (p *postgres) getTokensByProfileIDSQL(profileID uuid.ID, num, offset int) *pan.Query { 1.149 + var token Token 1.150 + fields, _ := pan.GetFields(token) 1.151 + query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(token)) 1.152 + query.IncludeWhere() 1.153 + query.Include(pan.GetUnquotedColumn(token, "ProfileID")+" = ?", profileID) 1.154 + query.IncludeLimit(int64(num)) 1.155 + query.IncludeOffset(int64(offset)) 1.156 + return query.FlushExpressions(" ") 1.157 +} 1.158 + 1.159 +func (p *postgres) getTokenScopesSQL(tokens []string) *pan.Query { 1.160 + var t tokenScope 1.161 + fields, _ := pan.GetFields(t) 1.162 + tokensI := make([]interface{}, len(tokens)) 1.163 + for pos, token := range tokens { 1.164 + tokensI[pos] = token 1.165 + } 1.166 + query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(t)) 1.167 + query.IncludeWhere() 1.168 + query.Include(pan.GetUnquotedColumn(t, "Token")+" IN ("+pan.VariableList(len(tokensI))+")", tokensI...) 1.169 + return query.FlushExpressions(" ") 1.170 +} 1.171 + 1.172 +func (p *postgres) getTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) { 1.173 + query := p.getTokensByProfileIDSQL(profileID, num, offset) 1.174 + rows, err := p.db.Query(query.String(), query.Args...) 1.175 + if err != nil { 1.176 + return []Token{}, err 1.177 + } 1.178 + var tokens []Token 1.179 + var tokenIDs []string 1.180 + for rows.Next() { 1.181 + var token Token 1.182 + err = pan.Unmarshal(rows, &token) 1.183 + if err != nil { 1.184 + return tokens, err 1.185 + } 1.186 + tokens = append(tokens, token) 1.187 + tokenIDs = append(tokenIDs, token.AccessToken) 1.188 + } 1.189 + if err = rows.Err(); err != nil { 1.190 + return tokens, err 1.191 + } 1.192 + if len(tokenIDs) < 1 { 1.193 + return tokens, nil 1.194 + } 1.195 + scopes := map[string][]string{} 1.196 + query = p.getTokenScopesSQL(tokenIDs) 1.197 + rows, err = p.db.Query(query.String(), query.Args...) 1.198 + if err != nil { 1.199 + return tokens, err 1.200 + } 1.201 + for rows.Next() { 1.202 + var t tokenScope 1.203 + err = pan.Unmarshal(rows, &t) 1.204 + if err != nil { 1.205 + return tokens, err 1.206 + } 1.207 + scopes[t.Token] = append(scopes[t.Token], t.Scope) 1.208 + } 1.209 + if err = rows.Err(); err != nil { 1.210 + return tokens, err 1.211 + } 1.212 + for pos, token := range tokens { 1.213 + token.Scopes = scopes[token.AccessToken] 1.214 + tokens[pos] = token 1.215 + } 1.216 + return tokens, nil 1.217 +}