auth

Paddy 2015-05-17 Parent:cf1aef6eb81f

172:8ecb60d29b0d Go to Latest

auth/authcode_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"
5 "github.com/lib/pq"
6 "github.com/secondbit/pan"
7 )
9 func (ac AuthorizationCode) GetSQLTableName() string {
10 return "authorization_codes"
11 }
13 func (p *postgres) getAuthorizationCodeSQL(code string) *pan.Query {
14 var ac AuthorizationCode
15 fields, _ := pan.GetFields(ac)
16 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(ac))
17 query.IncludeWhere()
18 query.Include(pan.GetUnquotedColumn(ac, "Code")+" = ?", code)
19 return query.FlushExpressions(" ")
20 }
22 func (p *postgres) getAuthorizationCode(code string) (AuthorizationCode, error) {
23 query := p.getAuthorizationCodeSQL(code)
24 rows, err := p.db.Query(query.String(), query.Args...)
25 if err != nil {
26 return AuthorizationCode{}, err
27 }
28 var ac AuthorizationCode
29 var found bool
30 for rows.Next() {
31 err := pan.Unmarshal(rows, &ac)
32 if err != nil {
33 return ac, err
34 }
35 found = true
36 }
37 if err = rows.Err(); err != nil {
38 return ac, err
39 }
40 if !found {
41 return ac, ErrAuthorizationCodeNotFound
42 }
43 return ac, nil
44 }
46 func (p *postgres) saveAuthorizationCodeSQL(authCode AuthorizationCode) *pan.Query {
47 fields, values := pan.GetFields(authCode)
48 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(authCode))
49 query.Include("(" + pan.QueryList(fields) + ")")
50 query.Include("VALUES")
51 query.Include("("+pan.VariableList(len(values))+")", values...)
52 return query.FlushExpressions(" ")
53 }
55 func (p *postgres) saveAuthorizationCode(authCode AuthorizationCode) error {
56 query := p.saveAuthorizationCodeSQL(authCode)
57 _, err := p.db.Exec(query.String(), query.Args...)
58 if e, ok := err.(*pq.Error); ok && e.Constraint == "authorization_codes_pkey" {
59 err = ErrAuthorizationCodeAlreadyExists
60 }
61 return err
62 }
64 func (p *postgres) deleteAuthorizationCodeSQL(code string) *pan.Query {
65 var authCode AuthorizationCode
66 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(authCode))
67 query.IncludeWhere()
68 query.Include(pan.GetUnquotedColumn(authCode, "Code")+" = ?", code)
69 return query.FlushExpressions(" ")
70 }
72 func (p *postgres) deleteAuthorizationCode(code string) error {
73 query := p.deleteAuthorizationCodeSQL(code)
74 res, err := p.db.Exec(query.String(), query.Args...)
75 if err != nil {
76 return err
77 }
78 rows, err := res.RowsAffected()
79 if err != nil {
80 return err
81 }
82 if rows == 0 {
83 return ErrAuthorizationCodeNotFound
84 }
85 return nil
86 }
88 func (p *postgres) deleteAuthorizationCodesByProfileIDSQL(profileID uuid.ID) *pan.Query {
89 var authCode AuthorizationCode
90 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(authCode))
91 query.IncludeWhere()
92 query.Include(pan.GetUnquotedColumn(authCode, "ProfileID")+" = ?", profileID)
93 return query.FlushExpressions(" ")
94 }
96 func (p *postgres) deleteAuthorizationCodesByProfileID(profileID uuid.ID) error {
97 query := p.deleteAuthorizationCodesByProfileIDSQL(profileID)
98 res, err := p.db.Exec(query.String(), query.Args...)
99 if err != nil {
100 return err
101 }
102 rows, err := res.RowsAffected()
103 if err != nil {
104 return err
105 }
106 if rows == 0 {
107 return ErrProfileNotFound
108 }
109 return nil
110 }
112 func (p *postgres) deleteAuthorizationCodesByClientIDSQL(clientID uuid.ID) *pan.Query {
113 var authCode AuthorizationCode
114 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(authCode))
115 query.IncludeWhere()
116 query.Include(pan.GetUnquotedColumn(authCode, "ClientID")+" = ?", clientID)
117 return query.FlushExpressions(" ")
118 }
120 func (p *postgres) deleteAuthorizationCodesByClientID(clientID uuid.ID) error {
121 query := p.deleteAuthorizationCodesByClientIDSQL(clientID)
122 res, err := p.db.Exec(query.String(), query.Args...)
123 if err != nil {
124 return err
125 }
126 rows, err := res.RowsAffected()
127 if err != nil {
128 return err
129 }
130 if rows == 0 {
131 return ErrClientNotFound
132 }
133 return nil
134 }
136 func (p *postgres) useAuthorizationCodeSQL(code string) *pan.Query {
137 var authCode AuthorizationCode
138 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(authCode)+" SET ")
139 query.Include(pan.GetUnquotedColumn(authCode, "Used")+" = ?", true)
140 query.IncludeWhere()
141 query.Include(pan.GetUnquotedColumn(authCode, "Code")+" = ?", code)
142 return query.FlushExpressions(" ")
143 }
145 func (p *postgres) useAuthorizationCode(code string) error {
146 query := p.useAuthorizationCodeSQL(code)
147 res, err := p.db.Exec(query.String(), query.Args...)
148 if err != nil {
149 return err
150 }
151 rows, err := res.RowsAffected()
152 if err != nil {
153 return err
154 }
155 if rows == 0 {
156 return ErrAuthorizationCodeNotFound
157 }
158 return nil
159 }