auth

Paddy 2015-05-11 Parent:202e991accc2 Child:0ff23f3a4ede

165:04c8edf89e3b Go to Latest

auth/authd/server.go

Support CORS. Add support for the OPTIONS method on certain endpoints. Automatically add headers (and respond to) OPTIONS requests. Note that these headers are full of deceit and mendacity, and we should switch to trout soon so we can actually be honest about what methods we support.

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 var config auth.Config
16 if os.Getenv("AUTH_PG_DB") != "" {
17 p, err := auth.NewPostgres(os.Getenv("AUTH_PG_DB"))
18 if err != nil {
19 panic(err)
20 }
21 config.ClientStore = &p
22 config.AuthCodeStore = &p
23 config.ProfileStore = &p
24 config.TokenStore = &p
25 config.SessionStore = &p
26 config.ScopeStore = &p
27 } else {
28 store := auth.NewMemstore()
29 config.ClientStore = store
30 config.AuthCodeStore = store
31 config.ProfileStore = store
32 config.TokenStore = store
33 config.SessionStore = store
34 config.ScopeStore = store
35 }
36 config.Template = template.Must(template.New("base").ParseGlob("./templates/*.gotmpl"))
37 config.LoginURI = "/login"
38 err := config.Init()
39 if err != nil {
40 log.Fatal(err)
41 }
42 context, err := auth.NewContext(config)
43 if err != nil {
44 panic(err)
45 }
46 err = context.CreateScopes([]auth.Scope{
47 {ID: "testscope", Name: "Test Scope"},
48 })
49 if err != nil && err != auth.ErrScopeAlreadyExists {
50 log.Fatal(err)
51 }
53 router := mux.NewRouter()
54 auth.RegisterOAuth2(router, context)
55 auth.RegisterSessionHandlers(router, context)
56 auth.RegisterProfileHandlers(router, context)
57 auth.RegisterClientHandlers(router, context)
58 http.Handle("/", router)
59 log.Fatal(http.ListenAndServe(":8080", nil))
60 }