-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
edb587a
commit d9a8d20
Showing
3 changed files
with
116 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters