Skip to content

Commit

Permalink
wip: create account
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Apr 26, 2024
1 parent edb587a commit d9a8d20
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 3 deletions.
3 changes: 2 additions & 1 deletion cmd/api/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.R
sc := controllers.NewSystemsController(db)
vc := controllers.NewVersionController(build)
oc := controllers.NewOperatorsController(db)
ac := controllers.NewAccountsController(db)

handlers := services.NewApiHandlers(sc, tc, vc, oc)
handlers := services.NewApiHandlers(sc, tc, vc, oc, ac)

handler := openapi.NewStrictHandler(handlers, nil)
openapi.RegisterHandlers(app, handler)
Expand Down
91 changes: 91 additions & 0 deletions internal/api/controllers/accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package controllers

import (
"context"

"github.com/google/uuid"
"github.com/nats-io/jwt"
"github.com/nats-io/nkeys"
"github.com/zeiss/typhoon/internal/api/models"
"github.com/zeiss/typhoon/internal/api/ports"
)

// AccountsController ...
type AccountsController struct {
db ports.Accounts
}

// NewAccountsController ...
func NewAccountsController(db ports.Accounts) *AccountsController {
return &AccountsController{db}
}

// CreateAccount ...
func (c *AccountsController) CreateAccount(ctx context.Context, name string, operatorID uuid.UUID) (*models.Account, error) {
pk, err := nkeys.CreateAccount()
if err != nil {
return nil, err
}

id, err := pk.PublicKey()
if err != nil {
return nil, err
}

seed, err := pk.Seed()
if err != nil {
return nil, err
}

// Create a signing key for the account
sk, err := nkeys.CreateAccount()
if err != nil {
return nil, err
}

spk, err := sk.PublicKey()
if err != nil {
return nil, err
}

skSeed, err := sk.Seed()
if err != nil {
return nil, err
}

// Create a token for the account
oc := jwt.NewAccountClaims(id)
oc.Name = name
oc.SigningKeys.Add(spk)

token, err := oc.Encode(pk)
if err != nil {
return nil, err
}

ac := &models.Account{
Name: name,
OperatorID: operatorID,
Key: models.NKey{
ID: id,
Seed: seed,
},
SigningKeys: []models.NKey{
{
ID: spk,
Seed: skSeed,
},
},
Token: models.Token{
ID: id,
Token: token,
},
}

err = c.db.CreateAccount(ctx, ac)
if err != nil {
return nil, err
}

return ac, nil
}
25 changes: 23 additions & 2 deletions internal/api/services/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ type ApiHandlers struct {
teams *controllers.TeamsController
version *controllers.VersionController
operators *controllers.OperatorsController
accounts *controllers.AccountsController

openapi.Unimplemented
}

// NewApiHandlers ...
func NewApiHandlers(systems *controllers.SystemsController, teams *controllers.TeamsController, version *controllers.VersionController, operators *controllers.OperatorsController) *ApiHandlers {
return &ApiHandlers{systems: systems, teams: teams, version: version, operators: operators}
func NewApiHandlers(systems *controllers.SystemsController, teams *controllers.TeamsController, version *controllers.VersionController, operators *controllers.OperatorsController, accounts *controllers.AccountsController) *ApiHandlers {
return &ApiHandlers{systems: systems, teams: teams, version: version, operators: operators, accounts: accounts}
}

// CreateOperator ...
Expand All @@ -34,6 +35,26 @@ func (a *ApiHandlers) CreateOperator(ctx context.Context, req openapi.CreateOper
return openapi.CreateOperator201JSONResponse(openapi.Operator{Id: &operator.ID, Name: operator.Name}), nil
}

// CreateOperatorAccount ...
func (a *ApiHandlers) CreateOperatorAccount(ctx context.Context, req openapi.CreateOperatorAccountRequestObject) (openapi.CreateOperatorAccountResponseObject, error) {
account, err := a.accounts.CreateAccount(ctx, req.Body.Name, req.OperatorId)
if err != nil {
return nil, err
}

resp := openapi.CreateOperatorAccount201JSONResponse(
openapi.Account{
Id: &account.ID,
Name: account.Name,
CreatedAt: &account.CreatedAt,
UpdatedAt: &account.UpdatedAt,
DeletedAt: &account.DeletedAt.Time,
},
)

return openapi.CreateOperatorAccount201JSONResponse(resp), nil
}

// // GetOperator ...
// func (a *ApiHandlers) GetOperator(ctx context.Context, req openapi.GetOperatorRequestObject) (openapi.GetOperatorResponseObject, error) {
// operator, err := a.operators.GetOperator(ctx, req.OperatorId)
Expand Down

0 comments on commit d9a8d20

Please sign in to comment.