auth

Paddy 2015-03-22 Parent:8267e1c8bcd1 Child:de5e09680f6b

151:77db7c65216c Go to Latest

auth/authd/server.go

Implement postgres clientStore. Stop requiring the client ID be passed to clientStore.addEndpoints and context.AddEndpoints. The Endpoints themselves contain the client ID. When using the authd server, set the log flags to include the file path and line number. Add an ErrEndpointAlreadyExists error, to return when creating an endpoint and its ID already exists in the database. Add a Deleted property to Clients and remove the clientStore.deleteClient and context.DeleteClient methods. We're not going to actually remove that data, and we want to be able to restore it, so include it in the ClientChange type and call it using UpdateClient. Create a ClientChange.Empty helper method that will return whether the ClientChange has any changes to perform. Return ErrClientNotFound from clientStore.getClient if the Client's Deleted property is set to true. This also requires us to ignore ErrClientNotFound errors when calling memstore.listClientsByOwner, as they should just be skipped instead of returning an error. Add the postgres type methods needed to implement clientStore. Include postgres as a clientStore if the testing.Short() flag is not set. Generate a new ID for the Client on every run in the tests, now that we can't actually remove it from the database/memstore in code. We really just need a *Store.Reset() function that erases all the data and starts over again, to give the tests a clean execution environment (and so they can clean up after themselves). Add the CREATE TABLE statements for the Clients table and the Endpoints table to sql/postgres_init.sql.

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@149 28 ScopeStore: store,
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@100 43
paddy@100 44 router := mux.NewRouter()
paddy@100 45 auth.RegisterOAuth2(router, context)
paddy@100 46 auth.RegisterSessionHandlers(router, context)
paddy@106 47 auth.RegisterProfileHandlers(router, context)
paddy@108 48 auth.RegisterClientHandlers(router, context)
paddy@100 49 http.Handle("/", router)
paddy@100 50 log.Fatal(http.ListenAndServe(":8080", nil))
paddy@100 51 }