auth

Paddy 2015-12-14 Parent:0a2c3d677161

181:b7e685839a1b Go to Latest

auth/authd/server.go

Break out scopes and events. This repo has gotten unwieldy, and there are portions of it that need to be imported by a large number of other packages. For example, scopes will be used in almost every API we write. Rather than importing the entirety of this codebase into every API we write, I've opted to move the scope logic out into a scopes package, with a subpackage for the defined types, which is all most projects actually want to import. We also define some event type constants, and importing those shouldn't require a project to import all our dependencies, either. So I made an events subpackage that just holds those constants. This package has become a little bit of a red-headed stepchild and is do for a refactor, but I'm trying to put that off as long as I can. The refactoring of our scopes stuff has left a bug wherein a token can be granted for scopes that don't exist. I'm going to need to revisit that, and also how to limit scopes to only be granted to the users that should be able to request them. But that's a battle for another day.

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