diff --git a/appview/oauth/handler/handler.go b/appview/oauth/handler/handler.go index 62aff886..e24d00a8 100644 --- a/appview/oauth/handler/handler.go +++ b/appview/oauth/handler/handler.go @@ -14,6 +14,7 @@ import ( "github.com/lestrrat-go/jwx/v2/jwk" "tangled.sh/tangled.sh/core/appview" "tangled.sh/tangled.sh/core/appview/db" + "tangled.sh/tangled.sh/core/appview/middleware" "tangled.sh/tangled.sh/core/appview/oauth" "tangled.sh/tangled.sh/core/appview/oauth/client" "tangled.sh/tangled.sh/core/appview/pages" @@ -38,6 +39,8 @@ func (o *OAuthHandler) Router() http.Handler { r.Get("/login", o.login) r.Post("/login", o.login) + r.With(middleware.AuthMiddleware(o.OAuth)).Post("/logout", o.logout) + r.Get("/oauth/client-metadata.json", o.clientMetadata) r.Get("/oauth/jwks.json", o.jwks) r.Get("/oauth/callback", o.callback) @@ -61,8 +64,6 @@ func (o *OAuthHandler) clientMetadata(w http.ResponseWriter, r *http.Request) { "token_endpoint_auth_signing_alg": "ES256", } - fmt.Println("clientMetadata", metadata) - w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(metadata) @@ -246,6 +247,18 @@ func (o *OAuthHandler) callback(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/", http.StatusFound) } +func (o *OAuthHandler) logout(w http.ResponseWriter, r *http.Request) { + err := o.OAuth.ClearSession(r, w) + if err != nil { + log.Println("failed to clear session:", err) + http.Redirect(w, r, "/", http.StatusFound) + return + } + + log.Println("session cleared successfully") + http.Redirect(w, r, "/", http.StatusFound) +} + func pubKeyFromJwk(jwks string) (jwk.Key, error) { k, err := helpers.ParseJWKFromBytes([]byte(jwks)) if err != nil { diff --git a/appview/pages/templates/user/login.html b/appview/pages/templates/user/login.html index b251f433..1c157fc2 100644 --- a/appview/pages/templates/user/login.html +++ b/appview/pages/templates/user/login.html @@ -27,9 +27,9 @@