auth

Paddy 2015-05-17 Parent:581c60f8dd23

172:8ecb60d29b0d Go to Latest

auth/token_postgres.go

Support email verification. The bulk of this commit is auto-modifying files to export variables (mostly our request error types and our response type) so that they can be reused in a Go client for that API. We also implement the beginnings of a Go client for that API, implementing the bare minimum we need for our immediate purposes: the ability to retrieve information about a Login. This, of course, means we need an API endpoint that will return information about a Login, which in turn required us to implement a GetLogin method in our profileStore. Which got in-memory and postgres implementations. That done, we could add the Verification field and Verified field to the Login type, to keep track of whether we've verified the user's ownership of those communication methods (if the Login is, in fact, a communication method). This required us to update sql/postgres_init.sql to account for the new fields we're tracking. It also means that when creating a Login, we had to generate a UUID to use as the Verification field. To make things complete, we needed a verifyLogin method on the profileStore to mark a Login as verified. That, in turn, required an endpoint to control this through the API. While doing so, I lumped things together in an UpdateLogin handler just so we could reuse the endpoint and logic when resending a verification email that may have never reached the user, for whatever reason (the quintessential "send again" button). Finally, we implemented an email_verification listener that will pull email_verification events off NSQ, check for the requisite data integrity, and use mailgun to email out a verification/welcome email.

History
1 package auth
3 import (
4 "code.secondbit.org/uuid.hg"
6 "github.com/lib/pq"
7 "github.com/secondbit/pan"
8 )
10 func (t Token) GetSQLTableName() string {
11 return "tokens"
12 }
14 func (p *postgres) getTokenSQL(token string, refresh bool) *pan.Query {
15 var t Token
16 fields, _ := pan.GetFields(t)
17 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(t))
18 query.IncludeWhere()
19 if !refresh {
20 query.Include(pan.GetUnquotedColumn(t, "AccessToken")+" = ?", token)
21 } else {
22 query.Include(pan.GetUnquotedColumn(t, "RefreshToken")+" = ?", token)
23 }
24 return query.FlushExpressions(" ")
25 }
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...)
30 if err != nil {
31 return Token{}, err
32 }
33 var t Token
34 var found bool
35 for rows.Next() {
36 err := pan.Unmarshal(rows, &t)
37 if err != nil {
38 return t, err
39 }
40 found = true
41 }
42 if err = rows.Err(); err != nil {
43 return t, err
44 }
45 if !found {
46 return t, ErrTokenNotFound
47 }
48 return t, nil
49 }
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(" ")
58 }
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
65 }
66 if err != nil || len(token.Scopes) < 1 {
67 return err
68 }
69 return err
70 }
72 func (p *postgres) revokeTokenSQL(token string) *pan.Query {
73 var t Token
74 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(t)+" SET ")
75 query.Include(pan.GetUnquotedColumn(t, "Revoked")+" = ?", true)
76 query.IncludeWhere()
77 query.Include(pan.GetUnquotedColumn(t, "RefreshToken")+" = ?", token)
78 return query.FlushExpressions(" ")
79 }
81 func (p *postgres) revokeToken(token string) error {
82 query := p.revokeTokenSQL(token)
83 res, err := p.db.Exec(query.String(), query.Args...)
84 if err != nil {
85 return err
86 }
87 rows, err := res.RowsAffected()
88 if err != nil {
89 return err
90 }
91 if rows == 0 {
92 return ErrTokenNotFound
93 }
94 return nil
95 }
97 func (p *postgres) revokeTokensByProfileIDSQL(profileID uuid.ID) *pan.Query {
98 var t Token
99 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(t)+" SET ")
100 query.Include(pan.GetUnquotedColumn(t, "Revoked")+" = ?", true)
101 query.IncludeWhere()
102 query.Include(pan.GetUnquotedColumn(t, "ProfileID")+" = ?", profileID)
103 return query.FlushExpressions(" ")
104 }
106 func (p *postgres) revokeTokensByProfileID(profileID uuid.ID) error {
107 query := p.revokeTokensByProfileIDSQL(profileID)
108 res, err := p.db.Exec(query.String(), query.Args...)
109 if err != nil {
110 return err
111 }
112 rows, err := res.RowsAffected()
113 if err != nil {
114 return err
115 }
116 if rows == 0 {
117 return ErrProfileNotFound
118 }
119 return nil
120 }
122 func (p *postgres) revokeTokensByClientIDSQL(clientID uuid.ID) *pan.Query {
123 var t Token
124 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(t)+" SET ")
125 query.Include(pan.GetUnquotedColumn(t, "Revoked")+" = ?", true)
126 query.IncludeWhere()
127 query.Include(pan.GetUnquotedColumn(t, "ClientID")+" = ?", clientID)
128 return query.FlushExpressions(" ")
129 }
131 func (p *postgres) revokeTokensByClientID(clientID uuid.ID) error {
132 query := p.revokeTokensByClientIDSQL(clientID)
133 res, err := p.db.Exec(query.String(), query.Args...)
134 if err != nil {
135 return err
136 }
137 rows, err := res.RowsAffected()
138 if err != nil {
139 return err
140 }
141 if rows == 0 {
142 return ErrClientNotFound
143 }
144 return nil
145 }
147 func (p *postgres) getTokensByProfileIDSQL(profileID uuid.ID, num, offset int) *pan.Query {
148 var token Token
149 fields, _ := pan.GetFields(token)
150 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(token))
151 query.IncludeWhere()
152 query.Include(pan.GetUnquotedColumn(token, "ProfileID")+" = ?", profileID)
153 query.IncludeLimit(int64(num))
154 query.IncludeOffset(int64(offset))
155 return query.FlushExpressions(" ")
156 }
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...)
161 if err != nil {
162 return []Token{}, err
163 }
164 var tokens []Token
165 var tokenIDs []string
166 for rows.Next() {
167 var token Token
168 err = pan.Unmarshal(rows, &token)
169 if err != nil {
170 return tokens, err
171 }
172 tokens = append(tokens, token)
173 tokenIDs = append(tokenIDs, token.AccessToken)
174 }
175 if err = rows.Err(); err != nil {
176 return tokens, err
177 }
178 return tokens, nil
179 }