auth

Paddy 2015-04-07 Parent:de5e09680f6b Child:202e991accc2

155:762953f6a7f2 Go to Latest

auth/authd/server.go

Implement postgres version of the tokenStore. Create a postgres implementation for the tokenStore. Note that because pq doesn't support Postgres' array types (see https://github.com/lib/pq/issues/49), we couldn't just store the token.Scopes field as a Postgres array of varchars, which would have been the ideal. Instead, we need a many-to-many table that maps tokens to scopes. This meant we needed a special tokenScope type for our database mapping, and we needed to complicate the token storage/retrieval functions a little bit. It's kind of ugly, I'm not a huge fan of it, and I'd much rather be using the Postgres array types, but... well, here we are. We also added the postgres tokenStore to our slice of tokenStores to test when the correct environment variables are present. We wrote initialization SQL for the tables required by the postgres tokenStore. Also, added a helper script for emptying the test database, because I got tired of doing it by hand. We should be doing that in an automated fashion in the tests themselves, but that would mean extending the *Store interfaces.

History
paddy@100 1 package main
paddy@100 2
paddy@100 3 import (
paddy@100 4 "html/template"
paddy@100 5 "log"
paddy@100 6 "net/http"
paddy@100 7
paddy@107 8 "code.secondbit.org/auth.hg"
paddy@100 9 "github.com/gorilla/mux"
paddy@100 10 )
paddy@100 11
paddy@100 12 func main() {
paddy@151 13 log.SetFlags(log.LstdFlags | log.Llongfile)
paddy@149 14 p, err := auth.NewPostgres("dbname=testdb sslmode=disable")
paddy@149 15 if err != nil {
paddy@149 16 panic(err)
paddy@149 17 }
paddy@100 18 store := auth.NewMemstore()
paddy@149 19 if err != nil {
paddy@149 20 panic(err)
paddy@149 21 }
paddy@100 22 config := auth.Config{
paddy@151 23 ClientStore: &p,
paddy@100 24 AuthCodeStore: store,
paddy@149 25 ProfileStore: &p,
paddy@100 26 TokenStore: store,
paddy@100 27 SessionStore: store,
paddy@152 28 ScopeStore: &p,
paddy@100 29 Template: template.Must(template.New("base").ParseGlob("./templates/*.gotmpl")),
paddy@100 30 LoginURI: "/login",
paddy@100 31 }
paddy@149 32 err = config.Init()
paddy@106 33 if err != nil {
paddy@106 34 log.Fatal(err)
paddy@106 35 }
paddy@100 36 context, err := auth.NewContext(config)
paddy@100 37 if err != nil {
paddy@100 38 panic(err)
paddy@100 39 }
paddy@149 40 err = context.CreateScopes([]auth.Scope{
paddy@149 41 {ID: "testscope", Name: "Test Scope"},
paddy@149 42 })
paddy@152 43 if err != nil {
paddy@152 44 panic(err)
paddy@152 45 }
paddy@100 46
paddy@100 47 router := mux.NewRouter()
paddy@100 48 auth.RegisterOAuth2(router, context)
paddy@100 49 auth.RegisterSessionHandlers(router, context)
paddy@106 50 auth.RegisterProfileHandlers(router, context)
paddy@108 51 auth.RegisterClientHandlers(router, context)
paddy@100 52 http.Handle("/", router)
paddy@100 53 log.Fatal(http.ListenAndServe(":8080", nil))
paddy@100 54 }