Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(transaction): check transaction identifier #68

Merged
merged 4 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions server/api/services/transaction_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ func (ts *transactionService) Initialize(userId string, transaction *models.Tran
return nil, echo.NewHTTPError(http.StatusNotFound, "unable to find user")
}

foundTransaction, err := ts.transactionPersister.GetByIdentifier(transaction.Identifier, ts.tenant.ID)
if err != nil {
ts.logger.Error(err)
return nil, echo.NewHTTPError(http.StatusInternalServerError, "unable to search for transaction")
}

if foundTransaction != nil && len(*foundTransaction) > 0 {
ts.logger.Error("transaction already exists")
return nil, echo.NewHTTPError(http.StatusConflict, "transaction already exists")
}

// check for better error handling as BeginLogin can throw a BadRequestError AND normal errors (but same type)
if len(webauthnUser.WebauthnCredentials) == 0 {
return nil, echo.NewHTTPError(
Expand Down
14 changes: 14 additions & 0 deletions server/persistence/persisters/transaction_persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

type TransactionPersister interface {
Create(transaction *models.Transaction) error
GetByIdentifier(identifier string, tenantID uuid.UUID) (*models.Transactions, error)
ListByUserId(userId uuid.UUID, tenantId uuid.UUID) (*models.Transactions, error)
GetByUserId(userId uuid.UUID, tenantId uuid.UUID) (*models.Transaction, error)
GetByChallenge(challenge string, tenantId uuid.UUID) (*models.Transaction, error)
Expand Down Expand Up @@ -65,6 +66,19 @@ func (p *transactionPersister) ListByUserId(userId uuid.UUID, tenantId uuid.UUID
return &transactions, nil
}

func (p *transactionPersister) GetByIdentifier(identifier string, tenantId uuid.UUID) (*models.Transactions, error) {
transactions := models.Transactions{}
err := p.database.Eager().Where("identifier = ? AND tenant_id = ?", identifier, tenantId).All(&transactions)
if err != nil && errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("failed to list transactions by user id: %w", err)
}

return &transactions, nil
}

func (p *transactionPersister) GetByChallenge(challenge string, tenantId uuid.UUID) (*models.Transaction, error) {
transaction := models.Transaction{}
err := p.database.Eager().Where("challenge = ? AND tenant_id = ?", challenge, tenantId).First(&transaction)
Expand Down
5 changes: 4 additions & 1 deletion spec/passkey-server.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
openapi: 3.1.0
info:
version: '1.1'
version: '1.2'
title: passkey-server
description: 'This API shall represent the private and public endpoints for passkey registration, management and authentication'
termsOfService: 'https://www.hanko.io/terms'
Expand Down Expand Up @@ -263,6 +263,8 @@ paths:
$ref: '#/components/responses/error'
'404':
$ref: '#/components/responses/error'
'409':
$ref: '#/components/responses/error'
'500':
$ref: '#/components/responses/error'
servers:
Expand Down Expand Up @@ -578,6 +580,7 @@ components:
transaction_id:
type: string
maxLength: 128
description: Needs to be a tenant-wide unique identifier
transaction_data:
type: object
required:
Expand Down
Loading