auth

Paddy 2015-05-17 Parent:37a42585660e

172:8ecb60d29b0d Go to Latest

auth/profile_verification.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
paddy@169 1 package auth
paddy@169 2
paddy@169 3 import (
paddy@169 4 "log"
paddy@169 5 "time"
paddy@169 6
paddy@169 7 "code.secondbit.org/events.hg"
paddy@169 8 )
paddy@169 9
paddy@169 10 const (
paddy@169 11 EventSystem = "authd"
paddy@169 12 EventModelLogin = "login"
paddy@169 13 EventActionSendVerification = "send_verification"
paddy@169 14 EventTopicLoginVerification = "login_verification"
paddy@169 15 )
paddy@169 16
paddy@169 17 type loginVerificationNotifier interface {
paddy@169 18 SendLoginVerification(login Login)
paddy@169 19 }
paddy@169 20
paddy@169 21 type stdoutNotifier struct{}
paddy@169 22
paddy@169 23 func NewStdoutNotifier() stdoutNotifier {
paddy@169 24 return stdoutNotifier{}
paddy@169 25 }
paddy@169 26
paddy@169 27 func (s stdoutNotifier) SendLoginVerification(login Login) {
paddy@169 28 log.Printf("Use \"%s\" as the verification code for \"%s\"\n", login.Verification, login.Value)
paddy@169 29 }
paddy@169 30
paddy@169 31 type nsqNotifier struct {
paddy@169 32 *events.NSQPublisher
paddy@169 33 }
paddy@169 34
paddy@169 35 func NewNSQNotifier(address string) (*nsqNotifier, error) {
paddy@169 36 p, err := events.NewNSQPublisher(EventSystem+"/"+Version, address)
paddy@169 37 return &nsqNotifier{p}, err
paddy@169 38 }
paddy@169 39
paddy@169 40 func (n *nsqNotifier) SendLoginVerification(login Login) {
paddy@169 41 evt := events.Event{
paddy@169 42 System: EventSystem,
paddy@169 43 Model: EventModelLogin,
paddy@169 44 ID: login.Value,
paddy@169 45 Action: EventActionSendVerification,
paddy@169 46 Timestamp: time.Now(),
paddy@169 47 Data: map[string]string{
paddy@169 48 "verification": login.Verification,
paddy@169 49 },
paddy@169 50 }
paddy@169 51 err := n.Publish(EventTopicLoginVerification, evt)
paddy@169 52 if err != nil {
paddy@169 53 log.Printf("Error sending event: %#+v\n", err)
paddy@169 54 }
paddy@169 55 }