From 55b7b69522dd72f22976f553d43b68367f2cf945 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Thu, 7 Sep 2023 13:17:47 -0600 Subject: [PATCH] Completing Helper interface and integrating it into the handlers --- interfaces.go | 22 +++++++++++- routes.go | 94 +++++++++++++++++++++++++++++++-------------------- 2 files changed, 79 insertions(+), 37 deletions(-) diff --git a/interfaces.go b/interfaces.go index 0fb26ce..5f3d676 100644 --- a/interfaces.go +++ b/interfaces.go @@ -1,9 +1,29 @@ package oauth2 -import "github.com/labstack/echo/v4" +import ( + "github.com/labstack/echo/v4" + "hg.code.netlandish.com/~netlandish/gobwebs" +) // Helper is an interface to allow you to customize behavior // after a specific action from the application routes type Helper interface { + // Personal access and authorized client tokens + ListPersonalTemplateVars(c echo.Context) gobwebs.Map + AddPersonalTemplateVars(c echo.Context) gobwebs.Map + RevokePersonalTemplateVars(c echo.Context) gobwebs.Map + ProcessSuccessfulPersonalAdd(c echo.Context) error + ProcessSuccessfulPersonalRevoke(c echo.Context) error + + // OAuth2 Clients + ListClientsTemplateVars(c echo.Context) gobwebs.Map + AddClientTemplateVars(c echo.Context) gobwebs.Map + DetailClientTemplateVars(c echo.Context) gobwebs.Map + ReissueClientTemplateVars(c echo.Context) gobwebs.Map ProcessSuccessfulClientAdd(c echo.Context) error + ProcessSuccessfulClientUnregister(c echo.Context) error + ProcessSuccessfulClientReissue(c echo.Context) error + + // Authoirze process + AuthorizeTemplateVars(c echo.Context) gobwebs.Map } diff --git a/routes.go b/routes.go index bf218b5..9c88223 100644 --- a/routes.go +++ b/routes.go @@ -60,6 +60,7 @@ func (s *Service) RegisterRoutes() { // ListPersonal ... func (s *Service) ListPersonal(c echo.Context) error { var tokens, clients []*Grant + gmap := s.config.Helper.ListPersonalTemplateVars(c) gctx := c.(*server.Context) opts := &database.FilterOptions{ Filter: sq.And{ @@ -78,19 +79,18 @@ func (s *Service) ListPersonal(c echo.Context) error { clients = append(clients, grant) } } - return gctx.Render(http.StatusOK, "oauth2_personal_list.html", gobwebs.Map{ - "tokens": tokens, - "clients": clients, - }) + + gmap["tokens"] = tokens + gmap["clients"] = clients + return gctx.Render(http.StatusOK, "oauth2_personal_list.html", gmap) } // AddPersonal ... func (s *Service) AddPersonal(c echo.Context) error { gctx := c.(*server.Context) form := &AddPersonalTokenForm{} - gmap := gobwebs.Map{ - "form": form, - } + gmap := s.config.Helper.AddPersonalTemplateVars(c) + gmap["form"] = form req := c.Request() if req.Method == "POST" { @@ -126,6 +126,12 @@ func (s *Service) AddPersonal(c echo.Context) error { if err := dbgrant.Store(c.Request().Context()); err != nil { return err } + + c.Set("grant", grant) + if err := s.config.Helper.ProcessSuccessfulPersonalAdd(c); err != nil { + return err + } + gmap["token"] = token return gctx.Render(http.StatusOK, "oauth2_add_personal_done.html", gmap) } @@ -140,6 +146,7 @@ func (s *Service) RevokePersonal(c echo.Context) error { return echo.NotFoundHandler(c) } gctx := c.(*server.Context) + gmap := s.config.Helper.RevokePersonalTemplateVars(c) opts := &database.FilterOptions{ Filter: sq.And{ sq.Eq{"g.id": id}, @@ -161,19 +168,23 @@ func (s *Service) RevokePersonal(c echo.Context) error { if err := token.Revoke(req.Context()); err != nil { return err } + c.Set("grant", token) + if err := s.config.Helper.ProcessSuccessfulPersonalRevoke(c); err != nil { + return err + } + return c.Redirect(http.StatusMovedPermanently, c.Echo().Reverse(s.RouteName("list_personal"))) } - return gctx.Render(http.StatusOK, "oauth2_revoke_confirm.html", gobwebs.Map{ - "token": token, - }) - + gmap["token"] = token + return gctx.Render(http.StatusOK, "oauth2_revoke_confirm.html", gmap) } // ListClients ... func (s *Service) ListClients(c echo.Context) error { gctx := c.(*server.Context) + gmap := s.config.Helper.ListClientsTemplateVars(c) opts := &database.FilterOptions{ Filter: sq.And{ sq.Eq{"owner_id": gctx.User.GetID()}, @@ -184,18 +195,16 @@ func (s *Service) ListClients(c echo.Context) error { if err != nil { return err } - return gctx.Render(http.StatusOK, "oauth2_clients_list.html", gobwebs.Map{ - "clients": clients, - }) + gmap["clients"] = clients + return gctx.Render(http.StatusOK, "oauth2_clients_list.html", gmap) } // AddClient ... func (s *Service) AddClient(c echo.Context) error { gctx := c.(*server.Context) form := &AddClientForm{} - gmap := gobwebs.Map{ - "form": form, - } + gmap := s.config.Helper.AddClientTemplateVars(c) + gmap["form"] = form req := c.Request() if req.Method == "POST" { @@ -214,12 +223,12 @@ func (s *Service) AddClient(c echo.Context) error { if err := client.Store(c.Request().Context()); err != nil { return err } - if s.config.Helper != nil { - c.Set("client", client) - if err := s.config.Helper.ProcessSuccessfulClientAdd(c); err != nil { - return err - } + + c.Set("client", client) + if err := s.config.Helper.ProcessSuccessfulClientAdd(c); err != nil { + return err } + gmap["client"] = client gmap["token"] = token return gctx.Render(http.StatusOK, "oauth2_add_client_done.html", gmap) @@ -231,6 +240,7 @@ func (s *Service) AddClient(c echo.Context) error { func (s *Service) DetailClient(c echo.Context) error { id := c.Param("id") gctx := c.(*server.Context) + gmap := s.config.Helper.DetailClientTemplateVars(c) client, err := GetClientByID(c.Request().Context(), id) if err != nil { return err @@ -238,9 +248,8 @@ func (s *Service) DetailClient(c echo.Context) error { if client == nil || client.OwnerID != int(gctx.User.GetID()) { return echo.NotFoundHandler(c) } - return gctx.Render(http.StatusOK, "oauth2_client_detail.html", gobwebs.Map{ - "client": client, - }) + gmap["client"] = client + return gctx.Render(http.StatusOK, "oauth2_client_detail.html", gmap) } // UnregisterClient ... @@ -260,6 +269,10 @@ func (s *Service) UnregisterClient(c echo.Context) error { return err } + c.Set("client", client) + if err := s.config.Helper.ProcessSuccessfulClientUnregister(c); err != nil { + return err + } return c.Redirect(http.StatusMovedPermanently, c.Echo().Reverse(s.RouteName("list_clients"))) } @@ -268,6 +281,7 @@ func (s *Service) UnregisterClient(c echo.Context) error { func (s *Service) ReissueClient(c echo.Context) error { id := c.Param("id") gctx := c.(*server.Context) + gmap := s.config.Helper.ReissueClientTemplateVars(c) client, err := GetClientByID(c.Request().Context(), id) if err != nil { return err @@ -292,10 +306,15 @@ func (s *Service) ReissueClient(c echo.Context) error { if err := newClient.Store(c.Request().Context()); err != nil { return err } - return gctx.Render(http.StatusOK, "oauth2_add_client_done.html", gobwebs.Map{ - "client": newClient, - "token": token, - }) + + c.Set("client", newClient) + if err := s.config.Helper.ProcessSuccessfulClientReissue(c); err != nil { + return err + } + + gmap["client"] = newClient + gmap["token"] = token + return gctx.Render(http.StatusOK, "oauth2_add_client_done.html", gmap) } func oauth2Redirect(c echo.Context, redirectURI string, params gobwebs.Map) error { @@ -370,13 +389,13 @@ func (s *Service) Authorize(c echo.Context) error { } gctx := c.(*server.Context) - return gctx.Render(http.StatusOK, "oauth2_authorization.html", gobwebs.Map{ - "client": client, - "grants": grants.List(), - "client_id": clientID, - "redirect_uri": redirectURL, - "state": state, - }) + gmap := s.config.Helper.AuthorizeTemplateVars(c) + gmap["client"] = client + gmap["grants"] = grants.List() + gmap["client_id"] = clientID + gmap["redirect_uri"] = redirectURL + gmap["state"] = state + return gctx.Render(http.StatusOK, "oauth2_authorization.html", gmap) } // AuthorisePOST ... @@ -737,6 +756,9 @@ func (s *Service) RouteName(value string) string { // NewService return service func NewService(eg *echo.Group, name string, config *ServiceConfig) *Service { + if config.Helper == nil { + panic(fmt.Errorf("No oauth2 Helper interface provided via ServiceConfig")) + } if name == "" { name = "oauth2" } -- 2.45.2