auth

Paddy 2015-07-18 Parent:0a2c3d677161 Child:b7e685839a1b

180:4b68bac597b7 Go to Latest

auth/authd/server.go

Update client to detect errors. The client doesn't treat non-200 responses as errors automatically, so we need to detect when the response.Errors property is set, and use that to return an error. To avoid the boilerplate and an extensive error system, I just wrapped them in an httpErrors type that implements the error interface. That way the errors can be returned, and callers can type-cast and interrogate them. I also updated the GetLogin function to return an auth.ErrLoginNotFound error when the httpErrors response indicates that's the reason the request failed.

History
1 package main
3 import (
4 "encoding/base64"
5 "html/template"
6 "log"
7 "net/http"
8 "os"
10 "code.secondbit.org/auth.hg"
11 "code.secondbit.org/events.hg"
12 "github.com/gorilla/mux"
13 )
15 func main() {
16 log.SetFlags(log.LstdFlags | log.Llongfile)
17 log.Printf("Running version '%s'\n", auth.Version)
18 var config auth.Config
19 var jwtSecret string
20 var err error
21 if os.Getenv("JWT_SECRET") == "" {
22 log.Fatal("JWT_SECRET must be set.")
23 } else {
24 jwtSecret = os.Getenv("JWT_SECRET")
25 }
26 if os.Getenv("JWT_SECRET_IS_BASE64_ENCODED") == "true" {
27 config.JWTPrivateKey, err = base64.StdEncoding.DecodeString(jwtSecret)
28 if err != nil {
29 panic(err)
30 }
31 } else {
32 config.JWTPrivateKey = []byte(jwtSecret)
33 }
34 if os.Getenv("AUTH_PG_DB") != "" {
35 p, err := auth.NewPostgres(os.Getenv("AUTH_PG_DB"))
36 if err != nil {
37 panic(err)
38 }
39 config.ClientStore = &p
40 config.AuthCodeStore = &p
41 config.ProfileStore = &p
42 config.TokenStore = &p
43 config.SessionStore = &p
44 config.ScopeStore = &p
45 } else {
46 store := auth.NewMemstore()
47 config.ClientStore = store
48 config.AuthCodeStore = store
49 config.ProfileStore = store
50 config.TokenStore = store
51 config.SessionStore = store
52 config.ScopeStore = store
53 }
54 config.Template = template.Must(template.New("base").ParseGlob("./templates/*.gotmpl"))
55 config.LoginURI = "/login"
56 if os.Getenv("AUTH_NSQD_ADDR") != "" {
57 publisher, err := events.NewNSQPublisher("code.secondbit.org/auth/authd-"+auth.Version, os.Getenv("AUTH_NSQD_ADDR"))
58 if err != nil {
59 log.Fatal(err)
60 }
61 config.EventsPublisher = publisher
62 } else {
63 config.EventsPublisher = events.NewStdoutPublisher()
64 }
65 err = config.Init()
66 if err != nil {
67 log.Fatal(err)
68 }
69 context, err := auth.NewContext(config)
70 if err != nil {
71 panic(err)
72 }
73 err = context.CreateScopes([]auth.Scope{
74 auth.ScopeLoginAdmin,
75 {ID: "subscriptions", Name: "Manage subscriptions", Description: "Create, view, edit, and cancel your subscriptions."},
76 })
77 if err != nil && err != auth.ErrScopeAlreadyExists {
78 log.Fatal(err)
79 }
81 router := mux.NewRouter()
82 auth.RegisterOAuth2(router, context)
83 auth.RegisterSessionHandlers(router, context)
84 auth.RegisterProfileHandlers(router, context)
85 auth.RegisterClientHandlers(router, context)
86 http.Handle("/", router)
87 log.Println("Listening on port 9000")
88 log.Fatal(http.ListenAndServe("0.0.0.0:9000", nil))
89 }