auth
163:73e12d5a1124
Go to Latest
auth/authd/server.go
Use postgres arrays for scope associations.
Use the new pqarrays library I wrote to store Scope associations for Tokens and
AuthorizationCodes, instead of using our hacky and abstraction-breaking
many-to-many code.
We also created the authStore.deleteAuthorizationCodesByProfileID method, to
clear out the AuthorizationCodes that belong to a Profile (used when the Profile
is deleted). So we added the implementation for memstore and for our postgres
store.
Call Context.DeleteAuthorizationCodesByProfileID when deleting a Profile to
clean up after it.
Rename sortedScopes to Scopes, which we use pqarrays.StringArray's methods on to
fulfill the sql.Scanner and driver.Valuer interfaces. This lets us store Scopes
in postgres arrays.
Create a stringsToScopes helper function that creates Scope objects, with their
IDs filled by the strings specified.
Update our GrantType.Validate function signature to return Scopes instead of
[]string.
Create a Scopes.Strings() helper method that returns a []string of the IDs of
the Scopes.
Update our SQL init file to use the new postgres array definition, instead of
the many-to-many definition.
9 "code.secondbit.org/auth.hg"
10 "github.com/gorilla/mux"
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"))
21 config.ClientStore = &p
22 config.AuthCodeStore = &p
23 config.ProfileStore = &p
24 config.TokenStore = &p
25 config.SessionStore = &p
26 config.ScopeStore = &p
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
36 config.Template = template.Must(template.New("base").ParseGlob("./templates/*.gotmpl"))
37 config.LoginURI = "/login"
42 context, err := auth.NewContext(config)
46 err = context.CreateScopes([]auth.Scope{
47 {ID: "testscope", Name: "Test Scope"},
49 if err != nil && err != auth.ErrScopeAlreadyExists {
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))