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