auth
auth/authd/server.go
Instantiate a login verification handler. When starting up the authd binary, get our login verification handler set up. We're going to use environment variables to detect if NSQ is configured. If it is, that'll be used. Otherwise, fall back on logging to stdout.
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 }