auth

Paddy 2015-03-24 Parent:77db7c65216c Child:202e991accc2

152:de5e09680f6b Go to Latest

auth/authd/server.go

Implement postgres version of scopeStore. Update the authd server to use postgres as its scopeStore, instead of memstore. panic when starting the authd server if the CreateScopes call fails. This should, ideally, ignore ErrScopeAlreadyExists errors, but does not as of this commit. Update the simple.gotmpl template to properly display scopes, after switching to the Scope type instead of simply passing around the string the client supplied broke the template and I never bothered fixing it. Update the updateScopes method on the scopeStore (and the corresponding UpdateScopes method on the Context type) to be updateScope/UpdateScope. Operating on several scopes at a time like that is simply too challenging in SQL and I can't justify the complexity with a use case. Add a helper method to ScopeChange called Empty(), which returns true if the ScopeChange is full of nil values. Remove the ID from the ScopeChange type, because we're no longer accepting multiple ScopeChange types in UpdateScope, so we can supply that information outside the ScopeChange, which matches the rest of our update* methods. Correct our tests in scope_test.go to correctly use the updateScope method instead of the old updateScopes method. This generally just resulted in calling updateScope multiple times, as opposed to just once. Add a scope table initialization to the sql/postgres_init.sql script.

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 }