auth

Paddy 2015-05-17 Parent:bdb83e88e1da Child:b0d1b3e39fc8

172:8ecb60d29b0d Go to Latest

auth/authd/server.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@100 1 package main
paddy@100 2
paddy@100 3 import (
paddy@100 4 "html/template"
paddy@100 5 "log"
paddy@100 6 "net/http"
paddy@157 7 "os"
paddy@100 8
paddy@107 9 "code.secondbit.org/auth.hg"
paddy@100 10 "github.com/gorilla/mux"
paddy@100 11 )
paddy@100 12
paddy@100 13 func main() {
paddy@151 14 log.SetFlags(log.LstdFlags | log.Llongfile)
paddy@170 15 log.Printf("Running version '%s'\n", auth.Version)
paddy@157 16 var config auth.Config
paddy@157 17 if os.Getenv("AUTH_PG_DB") != "" {
paddy@157 18 p, err := auth.NewPostgres(os.Getenv("AUTH_PG_DB"))
paddy@157 19 if err != nil {
paddy@157 20 panic(err)
paddy@157 21 }
paddy@157 22 config.ClientStore = &p
paddy@157 23 config.AuthCodeStore = &p
paddy@157 24 config.ProfileStore = &p
paddy@157 25 config.TokenStore = &p
paddy@157 26 config.SessionStore = &p
paddy@157 27 config.ScopeStore = &p
paddy@157 28 } else {
paddy@157 29 store := auth.NewMemstore()
paddy@157 30 config.ClientStore = store
paddy@157 31 config.AuthCodeStore = store
paddy@157 32 config.ProfileStore = store
paddy@157 33 config.TokenStore = store
paddy@157 34 config.SessionStore = store
paddy@157 35 config.ScopeStore = store
paddy@149 36 }
paddy@157 37 config.Template = template.Must(template.New("base").ParseGlob("./templates/*.gotmpl"))
paddy@157 38 config.LoginURI = "/login"
paddy@168 39 config.JWTPrivateKey = []byte(`secret`)
paddy@170 40 if os.Getenv("AUTH_NSQD_ADDR") != "" {
paddy@170 41 n, err := auth.NewNSQNotifier(os.Getenv("AUTH_NSQD_ADDR"))
paddy@170 42 if err != nil {
paddy@170 43 log.Fatal(err)
paddy@170 44 }
paddy@170 45 config.LoginVerificationNotifier = n
paddy@170 46 } else {
paddy@170 47 config.LoginVerificationNotifier = auth.NewStdoutNotifier()
paddy@170 48 }
paddy@157 49 err := config.Init()
paddy@106 50 if err != nil {
paddy@106 51 log.Fatal(err)
paddy@106 52 }
paddy@100 53 context, err := auth.NewContext(config)
paddy@100 54 if err != nil {
paddy@100 55 panic(err)
paddy@100 56 }
paddy@149 57 err = context.CreateScopes([]auth.Scope{
paddy@149 58 {ID: "testscope", Name: "Test Scope"},
paddy@149 59 })
paddy@157 60 if err != nil && err != auth.ErrScopeAlreadyExists {
paddy@157 61 log.Fatal(err)
paddy@152 62 }
paddy@100 63
paddy@100 64 router := mux.NewRouter()
paddy@100 65 auth.RegisterOAuth2(router, context)
paddy@100 66 auth.RegisterSessionHandlers(router, context)
paddy@106 67 auth.RegisterProfileHandlers(router, context)
paddy@108 68 auth.RegisterClientHandlers(router, context)
paddy@100 69 http.Handle("/", router)
paddy@100 70 log.Fatal(http.ListenAndServe(":8080", nil))
paddy@100 71 }