auth

Paddy 2015-04-25 Parent:202e991accc2 Child:0ff23f3a4ede

164:cf1aef6eb81f Go to Latest

auth/authd/server.go

Clean up after Client deletion, finish cleaning up after Profile deletion. 6f473576c6ae started cleaning up after Profiles when they're deleted, but didn't clean up the Clients created by that Profile. This fixes that, and also fixes a BUG note about cleaning up after a Client when it's deleted. Extend the authorizationCodeStore to have a deleteAuthorizationCodesByClientID method that will delete the AuthorizationCodes that have been granted by the Client specified by the passed ID. We also implemented this in memstore and postgres, so tests continue to pass. Extend the clientStore to have a deleteClientsByOwner method that will delete the Clients that were created by the Profile specified by the passed ID. We also implemented this in memstore and postgres, so tests continue to pass. Extend the clientStore to have a removeEndpointsByClientID method that will delete the Endpoints that belong(ed) to a the Client specified by the passed ID. We also implemented this in memstore and postgres, so tests continue to pass. Extend the tokenStore to have a revokeTokensByClientID method that will revoke all the Tokens that were granted to the Client specified by the passed ID. We also implemented this in memstore and postgres, so tests continue to pass. When listing Clients by their owner, allow setting the num argument (which controls how many to return) to 0 or lower, and using that to signal "return all Clients belonging to this owner", instead of paging. This is useful when deleting the Clients belonging to a Profile as part of the cleanup after deleting the Profile. Create a cleanUpAfterClientDeletion helper function that will delete the Endpoints and AuthorizationCodes belonging to a Client, and revoke the Tokens belonging to a Client, as part of cleaning up after a Client has been deleted. Add a check in the handler for listing Clients owned by a Profile to disallow the num argument to be lower than 1, because the API should be forced to page. Call our cleanUpAfterClientDeletion once the Client has been deleted in the appropriate handler. Fill out our Context with new methods to wrap all the new methods we're adding to our *Stores. In cleanUpAfterProfileDeletion, obtain a list of clients belonging to the owner, use our new DeleteClientsByOwner method to remove all of them, and then use the list to run our new cleanUpAfterClientDeletion function to clear away the final remnants of a Profile when it's deleted.

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@157 7 "os"
paddy@100 8
paddy@107 9 "code.secondbit.org/auth.hg"
paddy@100 10 "github.com/gorilla/mux"
paddy@100 11 )
paddy@100 12
paddy@100 13 func main() {
paddy@151 14 log.SetFlags(log.LstdFlags | log.Llongfile)
paddy@157 15 var config auth.Config
paddy@157 16 if os.Getenv("AUTH_PG_DB") != "" {
paddy@157 17 p, err := auth.NewPostgres(os.Getenv("AUTH_PG_DB"))
paddy@157 18 if err != nil {
paddy@157 19 panic(err)
paddy@157 20 }
paddy@157 21 config.ClientStore = &p
paddy@157 22 config.AuthCodeStore = &p
paddy@157 23 config.ProfileStore = &p
paddy@157 24 config.TokenStore = &p
paddy@157 25 config.SessionStore = &p
paddy@157 26 config.ScopeStore = &p
paddy@157 27 } else {
paddy@157 28 store := auth.NewMemstore()
paddy@157 29 config.ClientStore = store
paddy@157 30 config.AuthCodeStore = store
paddy@157 31 config.ProfileStore = store
paddy@157 32 config.TokenStore = store
paddy@157 33 config.SessionStore = store
paddy@157 34 config.ScopeStore = store
paddy@149 35 }
paddy@157 36 config.Template = template.Must(template.New("base").ParseGlob("./templates/*.gotmpl"))
paddy@157 37 config.LoginURI = "/login"
paddy@157 38 err := config.Init()
paddy@106 39 if err != nil {
paddy@106 40 log.Fatal(err)
paddy@106 41 }
paddy@100 42 context, err := auth.NewContext(config)
paddy@100 43 if err != nil {
paddy@100 44 panic(err)
paddy@100 45 }
paddy@149 46 err = context.CreateScopes([]auth.Scope{
paddy@149 47 {ID: "testscope", Name: "Test Scope"},
paddy@149 48 })
paddy@157 49 if err != nil && err != auth.ErrScopeAlreadyExists {
paddy@157 50 log.Fatal(err)
paddy@152 51 }
paddy@100 52
paddy@100 53 router := mux.NewRouter()
paddy@100 54 auth.RegisterOAuth2(router, context)
paddy@100 55 auth.RegisterSessionHandlers(router, context)
paddy@106 56 auth.RegisterProfileHandlers(router, context)
paddy@108 57 auth.RegisterClientHandlers(router, context)
paddy@100 58 http.Handle("/", router)
paddy@100 59 log.Fatal(http.ListenAndServe(":8080", nil))
paddy@100 60 }