Skip to content

Commit

Permalink
wip: account download token
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie committed May 28, 2024
1 parent a7b01ba commit dd2b736
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 6 deletions.
1 change: 1 addition & 0 deletions cmd/web/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.R
app.Post("/accounts/create", handlers.CreateAccount())
app.Get("/accounts/:id", handlers.ShowAccount())
app.Delete("/accounts/:id", handlers.DeleteAccount())
app.Get("/accounts/:id/token", handlers.GetAccountToken())
app.Get("/accounts/partials/operator-skgs", handlers.OperatorSkgsOptions())

// Users handler
Expand Down
7 changes: 7 additions & 0 deletions internal/web/adapters/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,10 @@ func (h *handlers) UpdateSystemAccount() fiber.Handler {
return operators.NewUpdateSystemAccountController(h.db)
})
}

// GetAccountToken ...
func (h *handlers) GetAccountToken() fiber.Handler {
return htmx.NewHxControllerHandler(func() htmx.Controller {
return accounts.NewGetAccountTokenController(h.db)
})
}
11 changes: 11 additions & 0 deletions internal/web/controllers/accounts/controllers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package accounts

import (
"github.com/zeiss/typhoon/internal/web/controllers/accounts/tokens"
"github.com/zeiss/typhoon/internal/web/ports"
)

// NewGetAccountTokenController ...
func NewGetAccountTokenController(db ports.Accounts) *tokens.GetAccountTokenControllerImpl {
return tokens.NewGetAccountTokenController(db)
}
7 changes: 7 additions & 0 deletions internal/web/controllers/accounts/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ func (l *ShowAccountControllerImpl) Get() error {
),
dropdowns.DropdownMenuItems(
dropdowns.DropdownMenuItemsProps{},
dropdowns.DropdownMenuItem(
dropdowns.DropdownMenuItemProps{},
htmx.A(
htmx.Href(fmt.Sprintf("/accounts/%s/token", acc.ID)),
htmx.Text("Download JWT Token"),
),
),
dropdowns.DropdownMenuItem(
dropdowns.DropdownMenuItemProps{},
buttons.Error(
Expand Down
53 changes: 53 additions & 0 deletions internal/web/controllers/accounts/tokens/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package tokens

import (
"fmt"
"strings"

"github.com/google/uuid"
htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/typhoon/internal/api/models"
"github.com/zeiss/typhoon/internal/web/ports"
)

// GetAccountTokenControllerParams ...
type GetAccountTokenControllerParams struct {
ID uuid.UUID `json:"id" form:"id" param:"id" validate:"required,uuid"`
}

// GetAccountTokenControllerImpl ...
type GetAccountTokenControllerImpl struct {
Params GetAccountTokenControllerParams
ports.Accounts
htmx.DefaultController
}

// NewGetAccountTokenController ...
func NewGetAccountTokenController(db ports.Accounts) *GetAccountTokenControllerImpl {
return &GetAccountTokenControllerImpl{
Params: GetAccountTokenControllerParams{},
Accounts: db,
DefaultController: htmx.DefaultController{},
}
}

// Get ...
func (l *GetAccountTokenControllerImpl) Get() error {
err := l.BindParams(l)
if err != nil {
return err
}

account := models.Account{ID: l.Params.ID}

err = l.GetAccount(l.Context(), &account)
if err != nil {
return err
}

r := strings.NewReader(account.Token.Token)

l.Ctx().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s.jwt", account.Name))

return l.Ctx().SendStream(r)
}
12 changes: 6 additions & 6 deletions internal/web/controllers/operators/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ package operators
import (
"fmt"

"github.com/zeiss/typhoon/internal/api/models"
"github.com/zeiss/typhoon/internal/utils"
"github.com/zeiss/typhoon/internal/web/components"
"github.com/zeiss/typhoon/internal/web/components/operators"
"github.com/zeiss/typhoon/internal/web/ports"

"github.com/google/uuid"
htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/cards"
"github.com/zeiss/fiber-htmx/components/dropdowns"
"github.com/zeiss/fiber-htmx/components/forms"
"github.com/zeiss/fiber-htmx/components/icons"
"github.com/zeiss/fiber-htmx/components/tooltips"
"github.com/zeiss/typhoon/internal/api/models"
"github.com/zeiss/typhoon/internal/utils"
"github.com/zeiss/typhoon/internal/web/components"
"github.com/zeiss/typhoon/internal/web/components/operators"
"github.com/zeiss/typhoon/internal/web/ports"
)

// ShowOperatorControllerImpl ...
Expand Down Expand Up @@ -140,7 +141,6 @@ func (l *ShowOperatorControllerImpl) Get() error {
htmx.Text("Download JWT Token"),
),
),

dropdowns.DropdownMenuItem(
dropdowns.DropdownMenuItemProps{},
htmx.A(
Expand Down
2 changes: 2 additions & 0 deletions internal/web/ports/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ type Handlers interface {
DeleteUser() fiber.Handler
// UpdateSystemAccount ...
UpdateSystemAccount() fiber.Handler
// GetAccountToken ...
GetAccountToken() fiber.Handler
}

0 comments on commit dd2b736

Please sign in to comment.