Skip to content

Commit

Permalink
wip: account
Browse files Browse the repository at this point in the history
katallaxie authored Apr 24, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 9283acf commit e315ca5
Showing 11 changed files with 2,817 additions and 122 deletions.
167 changes: 167 additions & 0 deletions api/api.yml
Original file line number Diff line number Diff line change
@@ -136,6 +136,52 @@ paths:
"204":
description: Successful

/operators/{operatorId}/token:
get:
summary: Gets a token for an operator
operationId: getOperatorToken
parameters:
- $ref: "#/components/parameters/operatorId"
responses:
"200":
description: Successful
content:
application/json:
schema:
$ref: "#/components/schemas/JWTToken"
post:
summary: Creates a new token for an operator
operationId: createOperatorToken
parameters:
- $ref: "#/components/parameters/operatorId"
responses:
"201":
description: Created
content:
application/json:
schema:
$ref: "#/components/schemas/JWTToken"
put:
summary: Updates a token for an operator
operationId: updateOperatorToken
parameters:
- $ref: "#/components/parameters/operatorId"
responses:
"200":
description: Successful
content:
application/json:
schema:
$ref: "#/components/schemas/JWTToken"
delete:
summary: Deletes a token for an operator
operationId: deleteOperatorToken
parameters:
- $ref: "#/components/parameters/operatorId"
responses:
"204":
description: Successful

/operators/{operatorId}/signing-keys:
get:
summary: List all signing keys for an operator
@@ -240,6 +286,121 @@ paths:
operatorId: "$response.body#/operatorId"
accountId: "$response.body#/id"

/operators/{operatorId}/accounts/{accountId}:
get:
summary: Gets an account by ID
operationId: getOperatorAccount
parameters:
- $ref: "#/components/parameters/operatorId"
- $ref: "#/components/parameters/accountId"
responses:
"200":
description: Successful
content:
application/json:
schema:
$ref: "#/components/schemas/Account"
delete:
summary: Deletes an account by ID
operationId: deleteOperatorAccount
parameters:
- $ref: "#/components/parameters/operatorId"
- $ref: "#/components/parameters/accountId"
responses:
"204":
description: Successful

/operators/{operatorId}/accounts/{accountId}/signing-keys:
get:
summary: List all signing keys for an account
operationId: listOperatorAccountSigningKeys
tags:
- Operator
produces:
- "application/json"
parameters:
- $ref: "#/components/parameters/operatorId"
- $ref: "#/components/parameters/accountId"
- $ref: "#/components/parameters/offsetParam"
- $ref: "#/components/parameters/limitParam"
security:
- bearerAuth: ["read:signing-keys"]
responses:
"200":
description: Successfull response
content:
application/json:
schema:
allOf:
- $ref: "#/components/schemas/PaginatedResult"
- type: object
properties:
results:
type: array
items:
$ref: "#/components/schemas/KeyPair"
post:
summary: Creates a new signing key
operationId: createOperatorAccountSigningKey
tags:
- Operator
produces:
- "application/json"
parameters:
- $ref: "#/components/parameters/operatorId"
- $ref: "#/components/parameters/accountId"
responses:
"201":
description: Created
content:
application/json:
schema:
$ref: "#/components/schemas/KeyPair"
links:
GetSigningKeyById:
operationId: getSigningKey
parameters:
operatorId: "$response.body#/operatorId"
accountId: "$response.body#/accountId"
signingKeyId: "$response#body#/id"

/operators/{operatorId}/accounts/{accountId}/token:
get:
summary: Gets a token for an account
operationId: getOperatorAccountToken
parameters:
- $ref: "#/components/parameters/operatorId"
- $ref: "#/components/parameters/accountId"
responses:
"200":
description: Successful
content:
application/json:
schema:
$ref: "#/components/schemas/JWTToken"
post:
summary: Creates a new token for an account
operationId: createOperatorAccountToken
parameters:
- $ref: "#/components/parameters/operatorId"
- $ref: "#/components/parameters/accountId"
responses:
"201":
description: Created
content:
application/json:
schema:
$ref: "#/components/schemas/JWTToken"
delete:
summary: Deletes a token for an account
operationId: deleteOperatorAccountToken
parameters:
- $ref: "#/components/parameters/operatorId"
- $ref: "#/components/parameters/accountId"
responses:
"204":
description: Successful

/teams:
get:
summary: List all teams
@@ -801,6 +962,12 @@ components:
type: string
format: base32

JWTToken:
description: |
A JWT token is a JSON Web Token.
type: string
format: JWT

KeyPair:
descriptio: |
A KeyPair is a public/private key pair.
81 changes: 80 additions & 1 deletion internal/api/adapters/db.go
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ func (db *DB) RunMigrations() error {
// GetOperator ...
func (db *DB) GetOperator(ctx context.Context, id uuid.UUID) (*models.Operator, error) {
operator := &models.Operator{}
if err := db.conn.Where("id = ?", id).First(operator).Error; err != nil {
if err := db.conn.Where("id = ?", id).Preload("SigningKeys").Preload("Key").First(operator).Error; err != nil {
return nil, err
}

@@ -74,6 +74,85 @@ func (db *DB) CreateOperatorSigningKey(ctx context.Context, operatorID uuid.UUID
})
}

// CreateOperatorAccountSigningKey ...
func (db *DB) CreateOperatorAccountSigningKey(ctx context.Context, accountID uuid.UUID, key *models.NKey) error {
return db.conn.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var account models.Account
if err := tx.Where("id = ?", accountID).First(&account).Error; err != nil {
return err
}

err := tx.Model(&account).Association("SigningKeys").Append(key)
if err != nil {
return err
}

return nil
})
}

// ListOperatorAccountsSigningKey ...
func (db *DB) ListOperatorAccountsSigningKey(ctx context.Context, operatorID uuid.UUID, pagination models.Pagination[*models.Account]) (*models.Pagination[*models.Account], error) {
accounts := []*models.Account{}

err := db.conn.WithContext(ctx).Scopes(models.Paginate(&accounts, &pagination, db.conn)).Limit(pagination.Limit).Offset(pagination.Offset).Find(&accounts).Error
if err != nil {
return nil, err
}
pagination.Rows = accounts

return &pagination, nil
}

// CreateOperatorAccount ...
func (db *DB) CreateOperatorAccount(ctx context.Context, account *models.Account) error {
return db.conn.WithContext(ctx).Create(account).Error
}

// CreateOperatorAccountToken ...
func (db *DB) CreateOperatorAccountToken(ctx context.Context, accountID uuid.UUID, token *models.Token) error {
return db.conn.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var account models.Account
if err := tx.Where("id = ?", accountID).First(&account).Error; err != nil {
return err
}

err := tx.Model(&account).Association("Token").Replace(token)
if err != nil {
return err
}

return nil
})
}

// GetOperatorAccount ...
func (db *DB) GetOperatorAccount(ctx context.Context, id uuid.UUID) (*models.Account, error) {
account := &models.Account{}
if err := db.conn.Where("id = ?", id).Preload("SigningKeys").First(account).Error; err != nil {
return nil, err
}

return account, nil
}

// CreateOperatorToken ...
func (db *DB) CreateOperatorToken(ctx context.Context, operatorID uuid.UUID, token *models.Token) error {
return db.conn.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var operator models.Operator
if err := tx.Where("id = ?", operatorID).First(&operator).Error; err != nil {
return err
}

err := tx.Model(&operator).Association("Token").Replace(token)
if err != nil {
return err
}

return nil
})
}

// ListOperator ...
func (db *DB) ListOperator(ctx context.Context, pagination models.Pagination[*models.Operator]) (*models.Pagination[*models.Operator], error) {
operators := []*models.Operator{}
Loading

0 comments on commit e315ca5

Please sign in to comment.