diff --git a/client/handler.go b/client/handler.go index e0f4cbf9c27..6084e84b85d 100644 --- a/client/handler.go +++ b/client/handler.go @@ -9,7 +9,6 @@ import ( "github.com/julienschmidt/httprouter" "github.com/ory-am/common/rand/sequence" "github.com/ory-am/fosite" - "github.com/ory-am/hydra/config" "github.com/ory-am/hydra/firewall" "github.com/ory-am/hydra/herodot" "github.com/ory-am/ladon" @@ -27,40 +26,15 @@ const ( const ( ClientsResource = "rn:hydra:clients" - ClientResource = "rn:hydra:clients:%s" - Scope = "hydra.clients" + ClientResource = "rn:hydra:clients:%s" + Scope = "hydra.clients" ) func (h *Handler) SetRoutes(r *httprouter.Router) { r.GET(ClientsHandlerPath, h.GetAll) r.POST(ClientsHandlerPath, h.Create) - r.GET(ClientsHandlerPath+"/:id", h.Get) - r.DELETE(ClientsHandlerPath+"/:id", h.Delete) -} - -func NewManager(c *config.Config) Manager { - ctx := c.Context() - - switch ctx.Connection.(type) { - case *config.MemoryConnection: - return &MemoryManager{ - Clients: map[string]*fosite.DefaultClient{}, - Hasher: ctx.Hasher, - } - default: - panic("Unknown connection type.") - } -} - -func NewHandler(c *config.Config, router *httprouter.Router, manager Manager) *Handler { - ctx := c.Context() - h := &Handler{ - H: &herodot.JSON{}, - W: ctx.Warden, Manager: manager, - } - - h.SetRoutes(router) - return h + r.GET(ClientsHandlerPath + "/:id", h.Get) + r.DELETE(ClientsHandlerPath + "/:id", h.Delete) } func (h *Handler) Create(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { @@ -95,7 +69,7 @@ func (h *Handler) Create(w http.ResponseWriter, r *http.Request, _ httprouter.Pa return } - h.H.WriteCreated(ctx, w, r, ClientsHandlerPath+"/"+c.GetID(), &c) + h.H.WriteCreated(ctx, w, r, ClientsHandlerPath + "/" + c.GetID(), &c) } func (h *Handler) GetAll(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { diff --git a/cmd/cli/handler.go b/cmd/cli/handler.go index 79f6b2f460d..45d37cbd372 100644 --- a/cmd/cli/handler.go +++ b/cmd/cli/handler.go @@ -1,24 +1,21 @@ package cli import ( - "github.com/ory-am/hydra/client" "github.com/ory-am/hydra/config" - "github.com/ory-am/hydra/connection" - "github.com/ory-am/hydra/policy" ) type Handler struct { - Clients *client.CLIHandler - Connections *connection.CLIHandler - Policies *policy.CLIHandler - Keys *CLIHandler + Clients *ClientHandler + Connections *ConnectionHandler + Policies *PolicyHandler + Keys *JWKHandler } func NewHandler(c *config.Config) *Handler { return &Handler{ - Clients: client.NewCLIHandler(c), - Connections: connection.NewCLIHandler(c), - Policies: policy.NewCLIHandler(c), - Keys: newJWKCLIHandler(c), + Clients: newClientHandler(c), + Connections: newConnectionHandler(c), + Policies: newPolicHandler(c), + Keys: newJWKHandler(c), } } diff --git a/policy/handler_cli.go b/cmd/cli/handler_cli.go similarity index 81% rename from policy/handler_cli.go rename to cmd/cli/handler_cli.go index 0f5a411ba40..64e17cc9d65 100644 --- a/policy/handler_cli.go +++ b/cmd/cli/handler_cli.go @@ -1,4 +1,4 @@ -package policy +package cli import ( "fmt" @@ -9,21 +9,22 @@ import ( "github.com/ory-am/ladon" "github.com/spf13/cobra" "github.com/square/go-jose/json" + "github.com/ory-am/hydra/policy" ) -type CLIHandler struct { +type PolicyHandler struct { Config *config.Config - M *HTTPManager + M *policy.HTTPManager } -func NewCLIHandler(c *config.Config) *CLIHandler { - return &CLIHandler{ +func newPolicHandler(c *config.Config) *PolicyHandler { + return &PolicyHandler{ Config: c, - M: &HTTPManager{}, + M: &policy.HTTPManager{}, } } -func (h *CLIHandler) CreatePolicy(cmd *cobra.Command, args []string) { +func (h *PolicyHandler) CreatePolicy(cmd *cobra.Command, args []string) { h.M.Endpoint = h.Config.Resolve("/policies") h.M.Client = h.Config.OAuth2Client(cmd) @@ -73,7 +74,7 @@ func (h *CLIHandler) CreatePolicy(cmd *cobra.Command, args []string) { } -func (h *CLIHandler) AddResourceToPolicy(cmd *cobra.Command, args []string) { +func (h *PolicyHandler) AddResourceToPolicy(cmd *cobra.Command, args []string) { h.M.Endpoint = h.Config.Resolve("/policies") h.M.Client = h.Config.OAuth2Client(cmd) @@ -96,11 +97,11 @@ func (h *CLIHandler) AddResourceToPolicy(cmd *cobra.Command, args []string) { fmt.Printf("Added resources to policy %s", p.ID) } -func (h *CLIHandler) RemoveResourceFromPolicy(cmd *cobra.Command, args []string) { +func (h *PolicyHandler) RemoveResourceFromPolicy(cmd *cobra.Command, args []string) { fmt.Println("Not yet implemented.") } -func (h *CLIHandler) AddSubjectToPolicy(cmd *cobra.Command, args []string) { +func (h *PolicyHandler) AddSubjectToPolicy(cmd *cobra.Command, args []string) { h.M.Endpoint = h.Config.Resolve("/policies") h.M.Client = h.Config.OAuth2Client(cmd) @@ -123,11 +124,11 @@ func (h *CLIHandler) AddSubjectToPolicy(cmd *cobra.Command, args []string) { fmt.Printf("Added subjects to policy %s", p.ID) } -func (h *CLIHandler) RemoveSubjectFromPolicy(cmd *cobra.Command, args []string) { +func (h *PolicyHandler) RemoveSubjectFromPolicy(cmd *cobra.Command, args []string) { fmt.Println("Not yet implemented.") } -func (h *CLIHandler) AddActionToPolicy(cmd *cobra.Command, args []string) { +func (h *PolicyHandler) AddActionToPolicy(cmd *cobra.Command, args []string) { h.M.Endpoint = h.Config.Resolve("/policies") h.M.Client = h.Config.OAuth2Client(cmd) @@ -150,11 +151,11 @@ func (h *CLIHandler) AddActionToPolicy(cmd *cobra.Command, args []string) { fmt.Printf("Added actions to policy %s", p.ID) } -func (h *CLIHandler) RemoveActionFromPolicy(cmd *cobra.Command, args []string) { +func (h *PolicyHandler) RemoveActionFromPolicy(cmd *cobra.Command, args []string) { fmt.Println("Not yet implemented.") } -func (h *CLIHandler) GetPolicy(cmd *cobra.Command, args []string) { +func (h *PolicyHandler) GetPolicy(cmd *cobra.Command, args []string) { h.M.Endpoint = h.Config.Resolve("/policies") h.M.Client = h.Config.OAuth2Client(cmd) @@ -172,7 +173,7 @@ func (h *CLIHandler) GetPolicy(cmd *cobra.Command, args []string) { fmt.Printf("%s\n", out) } -func (h *CLIHandler) DeletePolicy(cmd *cobra.Command, args []string) { +func (h *PolicyHandler) DeletePolicy(cmd *cobra.Command, args []string) { h.M.Endpoint = h.Config.Resolve("/policies") h.M.Client = h.Config.OAuth2Client(cmd) diff --git a/client/handler_cli.go b/cmd/cli/handler_client.go similarity index 80% rename from client/handler_cli.go rename to cmd/cli/handler_client.go index ed3bef13954..8e080c15cb1 100644 --- a/client/handler_cli.go +++ b/cmd/cli/handler_client.go @@ -1,4 +1,4 @@ -package client +package cli import ( "fmt" @@ -7,21 +7,22 @@ import ( "github.com/ory-am/hydra/config" "github.com/ory-am/hydra/pkg" "github.com/spf13/cobra" + "github.com/ory-am/hydra/client" ) -type CLIHandler struct { +type ClientHandler struct { Config *config.Config - M *HTTPManager + M *client.HTTPManager } -func NewCLIHandler(c *config.Config) *CLIHandler { - return &CLIHandler{ +func newClientHandler(c *config.Config) *ClientHandler { + return &ClientHandler{ Config: c, - M: &HTTPManager{}, + M: &client.HTTPManager{}, } } -func (h *CLIHandler) CreateClient(cmd *cobra.Command, args []string) { +func (h *ClientHandler) CreateClient(cmd *cobra.Command, args []string) { var err error h.M.Endpoint = h.Config.Resolve("/clients") @@ -57,7 +58,7 @@ func (h *CLIHandler) CreateClient(cmd *cobra.Command, args []string) { fmt.Printf("Client Secret: %s\n", secret) } -func (h *CLIHandler) DeleteClient(cmd *cobra.Command, args []string) { +func (h *ClientHandler) DeleteClient(cmd *cobra.Command, args []string) { h.M.Endpoint = h.Config.Resolve("/clients") h.M.Client = h.Config.OAuth2Client(cmd) if len(args) == 0 { diff --git a/connection/handler_cli.go b/cmd/cli/handler_connection.go similarity index 64% rename from connection/handler_cli.go rename to cmd/cli/handler_connection.go index 182e805cfab..216e98c5de9 100644 --- a/connection/handler_cli.go +++ b/cmd/cli/handler_connection.go @@ -1,4 +1,4 @@ -package connection +package cli import ( "fmt" @@ -7,21 +7,22 @@ import ( "github.com/ory-am/hydra/pkg" "github.com/pborman/uuid" "github.com/spf13/cobra" + "github.com/ory-am/hydra/connection" ) -type CLIHandler struct { +type ConnectionHandler struct { Config *config.Config - M *HTTPManager + M *connection.HTTPManager } -func NewCLIHandler(c *config.Config) *CLIHandler { - return &CLIHandler{ +func newConnectionHandler(c *config.Config) *ConnectionHandler { + return &ConnectionHandler{ Config: c, - M: &HTTPManager{}, + M: &connection.HTTPManager{}, } } -func (h *CLIHandler) CreateConnection(cmd *cobra.Command, args []string) { +func (h *ConnectionHandler) CreateConnection(cmd *cobra.Command, args []string) { h.M.Client = h.Config.OAuth2Client(cmd) h.M.Endpoint = h.Config.Resolve("/connections") if len(args) != 3 { @@ -29,7 +30,7 @@ func (h *CLIHandler) CreateConnection(cmd *cobra.Command, args []string) { return } - err := h.M.Create(&Connection{ + err := h.M.Create(&connection.Connection{ ID: uuid.New(), Provider: args[0], LocalSubject: args[1], @@ -38,7 +39,7 @@ func (h *CLIHandler) CreateConnection(cmd *cobra.Command, args []string) { pkg.Must(err, "Could not create connection: %s", err) } -func (h *CLIHandler) DeleteConnection(cmd *cobra.Command, args []string) { +func (h *ConnectionHandler) DeleteConnection(cmd *cobra.Command, args []string) { h.M.Client = h.Config.OAuth2Client(cmd) h.M.Endpoint = h.Config.Resolve("/connections") if len(args) == 0 { diff --git a/cmd/cli/handler_jwk.go b/cmd/cli/handler_jwk.go index 00a8e413863..205229565b3 100644 --- a/cmd/cli/handler_jwk.go +++ b/cmd/cli/handler_jwk.go @@ -10,19 +10,19 @@ import ( "github.com/spf13/cobra" ) -type CLIHandler struct { +type JWKHandler struct { Config *config.Config M *jwk.HTTPManager } -func newJWKCLIHandler(c *config.Config) *CLIHandler { - return &CLIHandler{ +func newJWKHandler(c *config.Config) *JWKHandler { + return &JWKHandler{ Config: c, M: &jwk.HTTPManager{}, } } -func (h *CLIHandler) CreateKeys(cmd *cobra.Command, args []string) { +func (h *JWKHandler) CreateKeys(cmd *cobra.Command, args []string) { h.M.Endpoint = h.Config.Resolve("/keys") h.M.Client = h.Config.OAuth2Client(cmd) if len(args) == 0 { @@ -40,7 +40,7 @@ func (h *CLIHandler) CreateKeys(cmd *cobra.Command, args []string) { fmt.Printf("%s\n", out) } -func (h *CLIHandler) GetKeys(cmd *cobra.Command, args []string) { +func (h *JWKHandler) GetKeys(cmd *cobra.Command, args []string) { h.M.Endpoint = h.Config.Resolve("/keys") h.M.Client = h.Config.OAuth2Client(cmd) if len(args) == 0 { @@ -57,7 +57,7 @@ func (h *CLIHandler) GetKeys(cmd *cobra.Command, args []string) { fmt.Printf("%s\n", out) } -func (h *CLIHandler) DeleteKeys(cmd *cobra.Command, args []string) { +func (h *JWKHandler) DeleteKeys(cmd *cobra.Command, args []string) { h.M.Endpoint = h.Config.Resolve("/keys") h.M.Client = h.Config.OAuth2Client(cmd) if len(args) == 0 { diff --git a/cmd/server/handler.go b/cmd/server/handler.go index 69999b98de9..7b1797c5c5f 100644 --- a/cmd/server/handler.go +++ b/cmd/server/handler.go @@ -29,8 +29,8 @@ func (h *Handler) Start(c *config.Config, router *httprouter.Router) { ctx := c.Context() // Set up warden - clientsManager := client.NewManager(c) - InjectFositeStore(c, clientsManager) + clientsManager := newClientManager(c) + injectFositeStore(c, clientsManager) ctx.Warden = &warden.LocalWarden{ Warden: &ladon.Ladon{ Manager: ctx.LadonManager, @@ -43,11 +43,11 @@ func (h *Handler) Start(c *config.Config, router *httprouter.Router) { } // Set up handlers - h.Clients = client.NewHandler(c, router, clientsManager) - h.Keys = NewJWKHandler(c, router) - h.Connections = connection.NewHandler(c, router) - h.Policy = policy.NewHandler(c, router) - h.OAuth2 = NewOAuth2Handler(c, router, h.Keys.Manager) + h.Clients = newClientHandler(c, router, clientsManager) + h.Keys = newJWKHandler(c, router) + h.Connections = newConnectionHandler(c, router) + h.Policy = newPolicyHandler(c, router) + h.OAuth2 = newOAuth2Handler(c, router, h.Keys.Manager) // Create root account if new install h.createRS256KeysIfNotExist(c, oauth2.ConsentEndpointKey, "private") diff --git a/cmd/server/handler_client_factory.go b/cmd/server/handler_client_factory.go new file mode 100644 index 00000000000..5a4c427cdfc --- /dev/null +++ b/cmd/server/handler_client_factory.go @@ -0,0 +1,34 @@ +package server + +import ( + "github.com/julienschmidt/httprouter" + "github.com/ory-am/fosite" + "github.com/ory-am/hydra/config" + "github.com/ory-am/hydra/herodot" + "github.com/ory-am/hydra/client" +) + +func newClientManager(c *config.Config) client.Manager { + ctx := c.Context() + + switch ctx.Connection.(type) { + case *config.MemoryConnection: + return &client.MemoryManager{ + Clients: map[string]*fosite.DefaultClient{}, + Hasher: ctx.Hasher, + } + default: + panic("Unknown connection type.") + } +} + +func newClientHandler(c *config.Config, router *httprouter.Router, manager client.Manager) *client.Handler { + ctx := c.Context() + h := &client.Handler{ + H: &herodot.JSON{}, + W: ctx.Warden, Manager: manager, + } + + h.SetRoutes(router) + return h +} \ No newline at end of file diff --git a/cmd/server/handler_connection_factory.go b/cmd/server/handler_connection_factory.go new file mode 100644 index 00000000000..bf7eb41feb9 --- /dev/null +++ b/cmd/server/handler_connection_factory.go @@ -0,0 +1,27 @@ +package server + +import ( + "github.com/julienschmidt/httprouter" + "github.com/ory-am/hydra/herodot" + "github.com/ory-am/hydra/config" + "github.com/ory-am/hydra/connection" +) + +func newConnectionHandler(c *config.Config, router *httprouter.Router) *connection.Handler { + ctx := c.Context() + + h := &connection.Handler{} + h.H = &herodot.JSON{} + h.W = ctx.Warden + h.SetRoutes(router) + + switch ctx.Connection.(type) { + case *config.MemoryConnection: + h.Manager = connection.NewMemoryManager() + break + default: + panic("Unknown connection type.") + } + + return h +} diff --git a/cmd/server/jwk_factory.go b/cmd/server/handler_jwk_factory.go similarity index 88% rename from cmd/server/jwk_factory.go rename to cmd/server/handler_jwk_factory.go index c1e66eff41d..38387a4cf68 100644 --- a/cmd/server/jwk_factory.go +++ b/cmd/server/handler_jwk_factory.go @@ -7,7 +7,7 @@ import ( "github.com/ory-am/hydra/jwk" ) -func NewJWKHandler(c *config.Config, router *httprouter.Router) *jwk.Handler { +func newJWKHandler(c *config.Config, router *httprouter.Router) *jwk.Handler { ctx := c.Context() h := &jwk.Handler{ H: &herodot.JSON{}, diff --git a/cmd/server/handler_factory.go b/cmd/server/handler_oauth2_factory.go similarity index 97% rename from cmd/server/handler_factory.go rename to cmd/server/handler_oauth2_factory.go index 9d3cb05c3d9..c9f045b2c4b 100644 --- a/cmd/server/handler_factory.go +++ b/cmd/server/handler_oauth2_factory.go @@ -29,7 +29,7 @@ import ( "github.com/ory-am/hydra/internal" ) -func InjectFositeStore(c *config.Config, clients client.Manager) { +func injectFositeStore(c *config.Config, clients client.Manager) { var ctx = c.Context() var store pkg.FositeStorer @@ -51,7 +51,7 @@ func InjectFositeStore(c *config.Config, clients client.Manager) { ctx.FositeStore = store } -func NewOAuth2Handler(c *config.Config, router *httprouter.Router, keys jwk.Manager) *oauth2.Handler { +func newOAuth2Handler(c *config.Config, router *httprouter.Router, keys jwk.Manager) *oauth2.Handler { var ctx = c.Context() var store = ctx.FositeStore diff --git a/cmd/server/handler_policy_factory.go b/cmd/server/handler_policy_factory.go new file mode 100644 index 00000000000..88f4fd89849 --- /dev/null +++ b/cmd/server/handler_policy_factory.go @@ -0,0 +1,19 @@ +package server + +import ( + "github.com/julienschmidt/httprouter" + "github.com/ory-am/hydra/herodot" + "github.com/ory-am/hydra/config" + "github.com/ory-am/hydra/policy" +) + +func newPolicyHandler(c *config.Config, router *httprouter.Router) *policy.Handler { + ctx := c.Context() + h := &policy.Handler{ + H: &herodot.JSON{}, + W: ctx.Warden, + Manager: ctx.LadonManager, + } + h.SetRoutes(router) + return h +} diff --git a/connection/handler.go b/connection/handler.go index 8d4b917f2a8..e2ee1350b9a 100644 --- a/connection/handler.go +++ b/connection/handler.go @@ -8,7 +8,6 @@ import ( "github.com/asaskevich/govalidator" "github.com/go-errors/errors" "github.com/julienschmidt/httprouter" - "github.com/ory-am/hydra/config" "github.com/ory-am/hydra/firewall" "github.com/ory-am/hydra/herodot" "github.com/ory-am/ladon" @@ -18,8 +17,8 @@ import ( const ( connectionsResource = "rn:hydra:connections" - connectionResource = "rn:hydra:connections:%s" - scope = "hydra.connections" + connectionResource = "rn:hydra:connections:%s" + scope = "hydra.connections" ) type Handler struct { @@ -28,25 +27,6 @@ type Handler struct { W firewall.Firewall } -func NewHandler(c *config.Config, router *httprouter.Router) *Handler { - ctx := c.Context() - - h := &Handler{} - h.H = &herodot.JSON{} - h.W = ctx.Warden - h.SetRoutes(router) - - switch ctx.Connection.(type) { - case *config.MemoryConnection: - h.Manager = NewMemoryManager() - break - default: - panic("Unknown connection type.") - } - - return h -} - func (h *Handler) SetRoutes(r *httprouter.Router) { r.GET("/connections", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { if r.URL.Query().Get("local_subject") != "" { @@ -99,7 +79,7 @@ func (h *Handler) Create(w http.ResponseWriter, r *http.Request, ps httprouter.P return } - h.H.WriteCreated(ctx, w, r, "/oauth2/connections/"+conn.ID, &conn) + h.H.WriteCreated(ctx, w, r, "/oauth2/connections/" + conn.ID, &conn) } func (h *Handler) FindLocal(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { diff --git a/herodot/herodot_test.go b/herodot/herodot_test.go new file mode 100644 index 00000000000..e30db717741 --- /dev/null +++ b/herodot/herodot_test.go @@ -0,0 +1,15 @@ +package herodot + +import ( + "testing" + "github.com/stretchr/testify/assert" + "golang.org/x/net/context" +) + +func TestNewContext(t *testing.T) { + ctx := NewContext() + assert.NotNil(t, ctx.Value(RequestIDKey)) + + ctx = Context(context.Background()) + assert.NotNil(t, ctx.Value(RequestIDKey)) +} \ No newline at end of file diff --git a/policy/handler.go b/policy/handler.go index 979eb91856a..0c4ea139dcb 100644 --- a/policy/handler.go +++ b/policy/handler.go @@ -7,7 +7,6 @@ import ( "github.com/go-errors/errors" "github.com/julienschmidt/httprouter" - "github.com/ory-am/hydra/config" "github.com/ory-am/hydra/firewall" "github.com/ory-am/hydra/herodot" "github.com/ory-am/ladon" @@ -15,9 +14,9 @@ import ( ) const ( - endpoint = "/policies" - scope = "hydra.policies" - policyResource = "rn:hydra:policies" + endpoint = "/policies" + scope = "hydra.policies" + policyResource = "rn:hydra:policies" policiesResource = "rn:hydra:policies:%s" ) @@ -27,24 +26,11 @@ type Handler struct { W firewall.Firewall } -func NewHandler(c *config.Config, router *httprouter.Router) *Handler { - ctx := c.Context() - - h := &Handler{ - H: &herodot.JSON{}, - W: ctx.Warden, - Manager: ctx.LadonManager, - } - h.SetRoutes(router) - - return h -} - func (h *Handler) SetRoutes(r *httprouter.Router) { r.POST(endpoint, h.Create) r.GET(endpoint, h.Find) - r.GET(endpoint+"/:id", h.Get) - r.DELETE(endpoint+"/:id", h.Delete) + r.GET(endpoint + "/:id", h.Get) + r.DELETE(endpoint + "/:id", h.Delete) } func (h *Handler) Find(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { @@ -95,7 +81,7 @@ func (h *Handler) Create(w http.ResponseWriter, r *http.Request, _ httprouter.Pa return } - h.H.WriteCreated(ctx, w, r, "/policies/"+p.ID, p) + h.H.WriteCreated(ctx, w, r, "/policies/" + p.ID, p) } func (h *Handler) Get(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {