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.
6 "code.secondbit.org/uuid.hg"
8 "github.com/secondbit/pan"
11 func (p Profile) GetSQLTableName() string {
15 func (l Login) GetSQLTableName() string {
19 func (p *postgres) getProfileByIDSQL(id uuid.ID) *pan.Query {
21 fields, _ := pan.GetFields(profile)
22 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(profile))
24 query.Include(pan.GetUnquotedColumn(profile, "ID")+" = ?", id)
25 return query.FlushExpressions(" ")
28 func (p *postgres) getProfileByID(id uuid.ID) (Profile, error) {
29 query := p.getProfileByIDSQL(id)
30 rows, err := p.db.Query(query.String(), query.Args...)
37 err := pan.Unmarshal(rows, &profile)
43 if err := rows.Err(); err != nil {
47 return profile, ErrProfileNotFound
52 func (p *postgres) getProfileByLoginSQL(value string) *pan.Query {
55 fields, _ := pan.GetUnquotedAbsoluteFields(profile)
56 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(profile))
57 query.Include("INNER JOIN " + pan.GetTableName(login))
58 query.Include("ON " + pan.GetUnquotedAbsoluteColumn(profile, "ID") + " = " + pan.GetUnquotedAbsoluteColumn(login, "ProfileID"))
60 query.Include(pan.GetUnquotedAbsoluteColumn(login, "Value")+" = ?", value)
61 return query.FlushExpressions(" ")
64 func (p *postgres) getProfileByLogin(value string) (Profile, error) {
65 query := p.getProfileByLoginSQL(value)
66 rows, err := p.db.Query(query.String(), query.Args...)
73 err := pan.Unmarshal(rows, &profile)
79 if err := rows.Err(); err != nil {
83 return profile, ErrProfileNotFound
88 func (p *postgres) saveProfileSQL(profile Profile) *pan.Query {
89 fields, values := pan.GetFields(profile)
90 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(profile))
91 query.Include("(" + pan.QueryList(fields) + ")")
92 query.Include("VALUES")
93 query.Include("("+pan.VariableList(len(values))+")", values...)
94 return query.FlushExpressions(" ")
97 func (p *postgres) saveProfile(profile Profile) error {
98 query := p.saveProfileSQL(profile)
99 _, err := p.db.Exec(query.String(), query.Args...)
100 if e, ok := err.(*pq.Error); ok && e.Constraint == "profiles_pkey" {
101 err = ErrProfileAlreadyExists
106 func (p *postgres) updateProfileSQL(id uuid.ID, change ProfileChange) *pan.Query {
108 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(profile)+" SET ")
109 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Name")+" = ?", change.Name)
110 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Passphrase")+" = ?", change.Passphrase)
111 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Iterations")+" = ?", change.Iterations)
112 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Salt")+" = ?", change.Salt)
113 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "PassphraseScheme")+" = ?", change.PassphraseScheme)
114 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Compromised")+" = ?", change.Compromised)
115 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "LockedUntil")+" = ?", change.LockedUntil)
116 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "PassphraseReset")+" = ?", change.PassphraseReset)
117 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "PassphraseResetCreated")+" = ?", change.PassphraseResetCreated)
118 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "LastSeen")+" = ?", change.LastSeen)
119 query.FlushExpressions(", ")
121 query.Include(pan.GetUnquotedColumn(profile, "ID")+" = ?", id)
122 return query.FlushExpressions(" ")
125 func (p *postgres) updateProfile(id uuid.ID, change ProfileChange) error {
129 query := p.updateProfileSQL(id, change)
130 _, err := p.db.Exec(query.String(), query.Args...)
134 func (p *postgres) updateProfilesSQL(ids []uuid.ID, change BulkProfileChange) *pan.Query {
139 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(profile)+" SET ")
140 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Compromised")+" = ?", change.Compromised)
141 query.FlushExpressions(", ")
143 intids := make([]interface{}, len(ids))
144 for pos, id := range ids {
147 query.Include(pan.GetUnquotedColumn(profile, "ID")+" IN ("+pan.VariableList(len(ids))+")", intids...)
148 return query.FlushExpressions(" ")
151 func (p *postgres) updateProfiles(ids []uuid.ID, change BulkProfileChange) error {
152 query := p.updateProfilesSQL(ids, change)
153 _, err := p.db.Exec(query.String(), query.Args...)
157 func (p *postgres) deleteProfileSQL(id uuid.ID) *pan.Query {
159 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(profile))
161 query.Include(pan.GetUnquotedColumn(profile, "ID")+" = ?", id)
162 return query.FlushExpressions(" ")
165 func (p *postgres) deleteProfile(id uuid.ID) error {
166 query := p.deleteProfileSQL(id)
167 res, err := p.db.Exec(query.String(), query.Args...)
171 rows, err := res.RowsAffected()
176 return ErrProfileNotFound
181 func (p *postgres) addLoginSQL(login Login) *pan.Query {
182 fields, values := pan.GetFields(login)
183 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(login))
184 query.Include("(" + pan.QueryList(fields) + ")")
185 query.Include("VALUES")
186 query.Include("("+pan.VariableList(len(values))+")", values...)
187 return query.FlushExpressions(" ")
190 func (p *postgres) addLogin(login Login) error {
191 query := p.addLoginSQL(login)
192 _, err := p.db.Exec(query.String(), query.Args...)
193 if e, ok := err.(*pq.Error); ok && e.Constraint == "logins_pkey" {
194 return ErrLoginAlreadyExists
199 func (p *postgres) removeLoginsByProfileSQL(profile uuid.ID) *pan.Query {
201 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(login))
203 query.Include(pan.GetUnquotedColumn(login, "ProfileID")+" = ?", profile)
204 return query.FlushExpressions(" ")
207 func (p *postgres) removeLoginsByProfile(profile uuid.ID) error {
208 query := p.removeLoginsByProfileSQL(profile)
209 res, err := p.db.Exec(query.String(), query.Args...)
213 rows, err := res.RowsAffected()
218 return ErrProfileNotFound
223 func (p *postgres) removeLoginSQL(value string, profile uuid.ID) *pan.Query {
225 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(login))
227 query.Include(pan.GetUnquotedColumn(login, "Value")+" = ? AND "+pan.GetUnquotedColumn(login, "ProfileID")+" = ?", value, profile)
228 return query.FlushExpressions(" ")
231 func (p *postgres) removeLogin(value string, profile uuid.ID) error {
232 query := p.removeLoginSQL(value, profile)
233 res, err := p.db.Exec(query.String(), query.Args...)
237 rows, err := res.RowsAffected()
242 return ErrLoginNotFound
247 func (p *postgres) recordLoginUseSQL(value string, when time.Time) *pan.Query {
249 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(login)+" SET ")
250 query.Include(pan.GetUnquotedColumn(login, "LastUsed")+" = ?", when)
252 query.Include(pan.GetUnquotedColumn(login, "Value")+" = ?", value)
253 return query.FlushExpressions(" ")
256 func (p *postgres) recordLoginUse(value string, when time.Time) error {
257 query := p.recordLoginUseSQL(value, when)
258 _, err := p.db.Exec(query.String(), query.Args...)
262 func (p *postgres) verifyLoginSQL(value, verification string) *pan.Query {
264 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(login)+" SET ")
265 query.Include(pan.GetUnquotedColumn(login, "Verified")+" = ?", true)
267 query.FlushExpressions(" ")
268 query.Include(pan.GetUnquotedColumn(login, "Value")+" = ?", value)
269 query.Include(pan.GetUnquotedColumn(login, "Verification")+" = ?", verification)
270 return query.FlushExpressions(" AND ")
273 func (p *postgres) verifyLogin(value, verification string) error {
274 query := p.verifyLoginSQL(value, verification)
275 res, err := p.db.Exec(query.String(), query.Args...)
279 rows, err := res.RowsAffected()
284 return ErrLoginVerificationInvalid
289 func (p *postgres) getLoginSQL(value string) *pan.Query {
291 fields, _ := pan.GetFields(login)
292 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(login))
294 query.Include(pan.GetUnquotedColumn(login, "Value")+" = ?", value)
295 return query.FlushExpressions(" ")
298 func (p *postgres) getLogin(value string) (Login, error) {
299 query := p.getLoginSQL(value)
300 rows, err := p.db.Query(query.String(), query.Args...)
307 err := pan.Unmarshal(rows, &login)
313 if err := rows.Err(); err != nil {
317 return login, ErrLoginNotFound
322 func (p *postgres) listLoginsSQL(profile uuid.ID, num, offset int) *pan.Query {
324 fields, _ := pan.GetFields(login)
325 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(login))
327 query.Include(pan.GetUnquotedColumn(login, "ProfileID")+" = ?", profile)
328 query.IncludeLimit(int64(num))
329 query.IncludeOffset(int64(offset))
330 return query.FlushExpressions(" ")
333 func (p *postgres) listLogins(profile uuid.ID, num, offset int) ([]Login, error) {
334 query := p.listLoginsSQL(profile, num, offset)
335 rows, err := p.db.Query(query.String(), query.Args...)
337 return []Login{}, err
342 err = pan.Unmarshal(rows, &login)
346 logins = append(logins, login)
348 if err := rows.Err(); err != nil {