Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Logout hook plugin #611

Merged
merged 5 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions auth/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
// PreRedirectHookError is the error interface which allows the user to set correct http status code and Message to be set in case the function returns an error
// without which the current usage in GetCallbackHandler will set this to InternalServerError
type PreRedirectHookFunc func(ctx context.Context, authCtx interfaces.AuthenticationContext, request *http.Request, w http.ResponseWriter) *PreRedirectHookError
type LogoutHookFunc func(ctx context.Context, authCtx interfaces.AuthenticationContext, request *http.Request, w http.ResponseWriter) error
type HTTPRequestToMetadataAnnotator func(ctx context.Context, request *http.Request) metadata.MD
type UserInfoForwardResponseHandler func(ctx context.Context, w http.ResponseWriter, m protoiface.MessageV1) error

Expand All @@ -68,7 +69,7 @@
handler.HandleFunc(fmt.Sprintf("/%s", OIdCMetadataEndpoint), GetOIdCMetadataEndpointRedirectHandler(ctx, authCtx))

// These endpoints require authentication
handler.HandleFunc("/logout", GetLogoutEndpointHandler(ctx, authCtx))
handler.HandleFunc("/logout", GetLogoutEndpointHandler(ctx, authCtx, pluginRegistry))

Check warning on line 72 in auth/handlers.go

View check run for this annotation

Codecov / codecov/patch

auth/handlers.go#L72

Added line #L72 was not covered by tests
}

// Look for access token and refresh token, if both are present and the access token is expired, then attempt to
Expand Down Expand Up @@ -466,9 +467,19 @@
}
}

func GetLogoutEndpointHandler(ctx context.Context, authCtx interfaces.AuthenticationContext) http.HandlerFunc {
func GetLogoutEndpointHandler(ctx context.Context, authCtx interfaces.AuthenticationContext, pluginRegistry *plugins.Registry) http.HandlerFunc {

Check warning on line 470 in auth/handlers.go

View check run for this annotation

Codecov / codecov/patch

auth/handlers.go#L470

Added line #L470 was not covered by tests
return func(writer http.ResponseWriter, request *http.Request) {
logger.Debugf(ctx, "Deleting auth cookies")
hook := plugins.Get[LogoutHookFunc](pluginRegistry, plugins.PluginIDLogoutHook)
if hook != nil {
if err := hook(ctx, authCtx, request, writer); err != nil {
logger.Errorf(ctx, "logout hook failed: %v", err)
writer.WriteHeader(http.StatusInternalServerError)
return
}
logger.Debugf(ctx, "logout hook called")

Check warning on line 479 in auth/handlers.go

View check run for this annotation

Codecov / codecov/patch

auth/handlers.go#L472-L479

Added lines #L472 - L479 were not covered by tests
}

logger.Debugf(ctx, "deleting auth cookies")

Check warning on line 482 in auth/handlers.go

View check run for this annotation

Codecov / codecov/patch

auth/handlers.go#L482

Added line #L482 was not covered by tests
authCtx.CookieManager().DeleteCookies(ctx, writer)

// Redirect if one was given
Expand Down
1 change: 1 addition & 0 deletions plugins/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
PluginIDDataProxy PluginID = "DataProxy"
PluginIDUnaryServiceMiddleware PluginID = "UnaryServiceMiddleware"
PluginIDPreRedirectHook PluginID = "PreRedirectHook"
PluginIDLogoutHook PluginID = "LogoutHook"
)

type AtomicRegistry struct {
Expand Down
20 changes: 20 additions & 0 deletions plugins/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ func TestRedirectHook(t *testing.T) {
assert.Equal(t, fmt.Errorf("redirect hook error"), err)
}

type LogoutHook func(context.Context) error

func TestLogoutHook(t *testing.T) {
ar := NewAtomicRegistry(nil)
r := NewRegistry()

hook := LogoutHook(func(ctx context.Context) error {
return fmt.Errorf("redirect hook error")
})
err := r.Register(PluginIDLogoutHook, hook)
assert.NoError(t, err)

ar.Store(r)
r = ar.Load()
fn := Get[LogoutHook](r, PluginIDLogoutHook)
err = fn(context.Background())

assert.Equal(t, fmt.Errorf("redirect hook error"), err)
}

func TestRegistry_RegisterDefault(t *testing.T) {
r := NewRegistry()
r.RegisterDefault("hello", 5)
Expand Down
Loading