Skip to content

Commit

Permalink
Separate post register/login hooks (ory#150)
Browse files Browse the repository at this point in the history
Closes ory#149
  • Loading branch information
nmlc authored and Khoa Nguyen committed Dec 31, 2019
1 parent 4330819 commit a38627c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
14 changes: 4 additions & 10 deletions driver/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,10 @@ type Registry interface {
registration.StrategyProvider
}

type (
selfServiceStrategy interface {
login.Strategy
registration.Strategy
}
postHooks []interface {
login.PostHookExecutor
registration.PostHookExecutor
}
)
type selfServiceStrategy interface {
login.Strategy
registration.Strategy
}

func NewRegistry(c configuration.Provider) (Registry, error) {
driver, err := dbal.GetDriverFor(c.DSN())
Expand Down
6 changes: 3 additions & 3 deletions driver/registry_default_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/ory/kratos/selfservice/hook"
)

func (m *RegistryDefault) hooksPost(credentialsType identity.CredentialsType, configs []configuration.SelfServiceHook) postHooks {
var i postHooks
func (m *RegistryDefault) getHooks(credentialsType identity.CredentialsType, configs []configuration.SelfServiceHook) []interface{} {
var i []interface{}

for _, h := range configs {
switch h.Run {
Expand Down Expand Up @@ -67,7 +67,7 @@ func (m *RegistryDefault) hooksPost(credentialsType identity.CredentialsType, co
m.l.
WithField("type", credentialsType).
WithField("hook", h.Run).
Errorf("A unknown post login hook was requested and can therefore not be used.")
Errorf("A unknown hook was requested and can therefore not be used")
}
}

Expand Down
13 changes: 9 additions & 4 deletions driver/registry_default_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ func (m *RegistryDefault) PreLoginHooks() []login.PreHookExecutor {
}

func (m *RegistryDefault) PostLoginHooks(credentialsType identity.CredentialsType) []login.PostHookExecutor {
a := m.hooksPost(credentialsType, m.c.SelfServiceLoginAfterHooks(string(credentialsType)))
b := make([]login.PostHookExecutor, len(a))
for k, v := range a {
b[k] = v
a := m.getHooks(credentialsType, m.c.SelfServiceLoginAfterHooks(string(credentialsType)))

var b []login.PostHookExecutor

for _, v := range a {
if hook, ok := v.(login.PostHookExecutor); ok {
b = append(b, hook)
}
}

return b
}

Expand Down
13 changes: 9 additions & 4 deletions driver/registry_default_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import (
)

func (m *RegistryDefault) PostRegistrationHooks(credentialsType identity.CredentialsType) []registration.PostHookExecutor {
a := m.hooksPost(credentialsType, m.c.SelfServiceRegistrationAfterHooks(string(credentialsType)))
b := make([]registration.PostHookExecutor, len(a))
for k, v := range a {
b[k] = v
a := m.getHooks(credentialsType, m.c.SelfServiceRegistrationAfterHooks(string(credentialsType)))

var b []registration.PostHookExecutor

for _, v := range a {
if hook, ok := v.(registration.PostHookExecutor); ok {
b = append(b, hook)
}
}

return b
}

Expand Down
5 changes: 0 additions & 5 deletions selfservice/hook/session_destroyer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/http"

"github.com/ory/kratos/selfservice/flow/login"
"github.com/ory/kratos/selfservice/flow/registration"
"github.com/ory/kratos/session"
)

Expand All @@ -23,10 +22,6 @@ func NewSessionDestroyer(r sessionDestroyerDependencies) *SessionDestroyer {
return &SessionDestroyer{r: r}
}

func (e *SessionDestroyer) ExecuteRegistrationPostHook(w http.ResponseWriter, r *http.Request, a *registration.Request, s *session.Session) error {
panic("This should not have been called. See https:/ory/kratos/issues/149")
}

func (e *SessionDestroyer) ExecuteLoginPostHook(_ http.ResponseWriter, r *http.Request, _ *login.Request, s *session.Session) error {
if err := e.r.SessionPersister().DeleteSessionsFor(r.Context(), s.Identity.ID); err != nil {
return err
Expand Down

0 comments on commit a38627c

Please sign in to comment.