Completing Helper interface and integrating it into the handlers
2 files changed, 79 insertions(+), 37 deletions(-)

M interfaces.go
M routes.go
M interfaces.go +21 -1
@@ 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
 }

          
M routes.go +58 -36
@@ 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.Co
 			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.Con
 		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.
 		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.
 		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.Con
 	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.Conte
 		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.Conte
 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.Co
 	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 ech
 		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 ech
 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.C
 	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.Conte
 	}
 
 	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
 
 // 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"
 	}