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.
4 "code.secondbit.org/pqarrays.hg"
7 "github.com/secondbit/pan"
10 func (s Scope) GetSQLTableName() string {
14 func (s Scopes) Value() (driver.Value, error) {
15 ids := make(pqarrays.StringArray, 0, len(s))
16 for _, scope := range s {
17 ids = append(ids, scope.ID)
22 func (s *Scopes) Scan(value interface{}) error {
24 var ids pqarrays.StringArray
25 err := ids.Scan(value)
29 for _, id := range ids {
30 *s = append(*s, Scope{ID: id})
35 func (p *postgres) createScopesSQL(scopes []Scope) *pan.Query {
36 fields, _ := pan.GetFields(scopes[0])
37 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(scopes[0]))
38 query.Include("(" + pan.QueryList(fields) + ")")
39 query.Include("VALUES")
40 query.FlushExpressions(" ")
41 for _, scope := range scopes {
42 _, values := pan.GetFields(scope)
43 query.Include("("+pan.VariableList(len(values))+")", values...)
45 return query.FlushExpressions(", ")
48 func (p *postgres) createScopes(scopes []Scope) error {
52 query := p.createScopesSQL(scopes)
53 _, err := p.db.Exec(query.String(), query.Args...)
54 if e, ok := err.(*pq.Error); ok && e.Constraint == "scopes_pkey" {
55 err = ErrScopeAlreadyExists
60 func (p *postgres) getScopesSQL(ids []string) *pan.Query {
62 intids := make([]interface{}, len(ids))
63 for pos, id := range ids {
66 fields, _ := pan.GetFields(scope)
67 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(scope))
69 query.Include(pan.GetUnquotedColumn(scope, "ID") + " IN")
70 query.Include("("+pan.VariableList(len(ids))+")", intids...)
71 return query.FlushExpressions(" ")
74 func (p *postgres) getScopes(ids []string) ([]Scope, error) {
75 query := p.getScopesSQL(ids)
76 rows, err := p.db.Query(query.String(), query.Args...)
83 err := pan.Unmarshal(rows, &scope)
87 scopes = append(scopes, scope)
89 if err = rows.Err(); err != nil {
95 func (p *postgres) updateScopeSQL(id string, change ScopeChange) *pan.Query {
97 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(scope)+" SET ")
98 query.IncludeIfNotNil(pan.GetUnquotedColumn(scope, "Name")+" = ?", change.Name)
99 query.IncludeIfNotNil(pan.GetUnquotedColumn(scope, "Description")+" = ?", change.Description)
100 query.FlushExpressions(", ")
102 query.Include(pan.GetUnquotedColumn(scope, "ID")+" = ?", id)
103 return query.FlushExpressions(" ")
106 func (p *postgres) updateScope(id string, change ScopeChange) error {
110 query := p.updateScopeSQL(id, change)
111 res, err := p.db.Exec(query.String(), query.Args...)
115 rows, err := res.RowsAffected()
120 return ErrScopeNotFound
125 func (p *postgres) removeScopesSQL(ids []string) *pan.Query {
127 intids := make([]interface{}, len(ids))
128 for pos, id := range ids {
131 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(scope))
133 query.Include(pan.GetUnquotedColumn(scope, "ID") + " IN")
134 query.Include("("+pan.VariableList(len(ids))+")", intids...)
135 return query.FlushExpressions(" ")
138 func (p *postgres) removeScopes(ids []string) error {
139 query := p.removeScopesSQL(ids)
140 res, err := p.db.Exec(query.String(), query.Args...)
144 rows, err := res.RowsAffected()
149 return ErrScopeNotFound
154 func (p *postgres) listScopesSQL() *pan.Query {
156 fields, _ := pan.GetFields(scope)
157 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(scope))
158 return query.FlushExpressions(" ")
161 func (p *postgres) listScopes() ([]Scope, error) {
162 query := p.listScopesSQL()
163 rows, err := p.db.Query(query.String(), query.Args...)
165 return []Scope{}, err
170 err = pan.Unmarshal(rows, &scope)
174 scopes = append(scopes, scope)
176 if err = rows.Err(); err != nil {