Skip to content

Commit

Permalink
structure refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeneas Rekkas (arekkas) committed May 18, 2016
1 parent a94481a commit 3faf695
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 133 deletions.
36 changes: 5 additions & 31 deletions client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
19 changes: 8 additions & 11 deletions cmd/cli/handler.go
Original file line number Diff line number Diff line change
@@ -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),
}
}
31 changes: 16 additions & 15 deletions policy/handler_cli.go → cmd/cli/handler_cli.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package policy
package cli

import (
"fmt"
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down
17 changes: 9 additions & 8 deletions client/handler_cli.go → cmd/cli/handler_client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cli

import (
"fmt"
Expand All @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 10 additions & 9 deletions connection/handler_cli.go → cmd/cli/handler_connection.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package connection
package cli

import (
"fmt"
Expand All @@ -7,29 +7,30 @@ 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 {
fmt.Print(cmd.UsageString())
return
}

err := h.M.Create(&Connection{
err := h.M.Create(&connection.Connection{
ID: uuid.New(),
Provider: args[0],
LocalSubject: args[1],
Expand All @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions cmd/cli/handler_jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions cmd/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")
Expand Down
34 changes: 34 additions & 0 deletions cmd/server/handler_client_factory.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 3faf695

Please sign in to comment.