Skip to content

Commit

Permalink
feat: add get credential endpoint
Browse files Browse the repository at this point in the history
Add an endpoint to only get one credential.
  • Loading branch information
FreddyDevelop committed Nov 18, 2024
1 parent 9afe292 commit d582e26
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
6 changes: 5 additions & 1 deletion server/api/dto/request/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type CredentialRequests interface {
ListCredentialsDto | DeleteCredentialsDto | UpdateCredentialsDto
ListCredentialsDto | GetCredentialsDto | DeleteCredentialsDto | UpdateCredentialsDto
}

type TenantDto struct {
Expand All @@ -20,6 +20,10 @@ type ListCredentialsDto struct {
UserId string `query:"user_id" validate:"required"`
}

type GetCredentialsDto struct {
CredentialId string `param:"credential_id" validate:"required"`
}

type DeleteCredentialsDto struct {
CredentialId string `param:"credential_id" validate:"required"`
}
Expand Down
26 changes: 26 additions & 0 deletions server/api/handler/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/gobuffalo/pop/v6"
"github.com/labstack/echo/v4"
"github.com/teamhanko/passkey-server/api/dto/request"
"github.com/teamhanko/passkey-server/api/dto/response"
"github.com/teamhanko/passkey-server/api/helper"
"github.com/teamhanko/passkey-server/api/services"
"github.com/teamhanko/passkey-server/persistence"
Expand All @@ -13,6 +14,7 @@ import (

type CredentialsHandler interface {
List(ctx echo.Context) error
Get(ctx echo.Context) error
Update(ctx echo.Context) error
Delete(ctx echo.Context) error
}
Expand Down Expand Up @@ -51,6 +53,30 @@ func (credHandler *credentialsHandler) List(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, dtos)
}

func (credHandler *credentialsHandler) Get(ctx echo.Context) error {
requestDto, err := BindAndValidateRequest[request.GetCredentialsDto](ctx)
if err != nil {
ctx.Logger().Error(err)
return err
}

h, err := helper.GetHandlerContext(ctx)
if err != nil {
ctx.Logger().Error(err)
return err
}

service := services.NewCredentialService(ctx, *h.Tenant, credHandler.persister.GetWebauthnCredentialPersister(nil))
credential, err := service.Get(*requestDto)
if err != nil {
return err
}

credentialDto := response.CredentialDtoFromModel(*credential)

return ctx.JSON(http.StatusOK, credentialDto)
}

func (credHandler *credentialsHandler) Update(ctx echo.Context) error {
requestDto, err := BindAndValidateRequest[request.UpdateCredentialsDto](ctx)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions server/api/router/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func RouteCredentials(parent *echo.Group, persister persistence.Persister) {

group := parent.Group("/credentials", passkeyMiddleware.ApiKeyMiddleware())
group.GET("", credentialsHandler.List)
group.GET("/:credential_id", credentialsHandler.Get)
group.PATCH("/:credential_id", credentialsHandler.Update)
group.DELETE("/:credential_id", credentialsHandler.Delete)

Expand Down
15 changes: 15 additions & 0 deletions server/api/services/credential_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

type CredentialService interface {
List(dto request.ListCredentialsDto) (response.CredentialDtoList, error)
Get(dto request.GetCredentialsDto) (*models.WebauthnCredential, error)
Update(dto request.UpdateCredentialsDto) (*models.WebauthnCredential, error)
Delete(dto request.DeleteCredentialsDto) error
}
Expand Down Expand Up @@ -45,6 +46,20 @@ func (cs *credentialService) List(dto request.ListCredentialsDto) (response.Cred
return dtos, nil
}

func (cs *credentialService) Get(dto request.GetCredentialsDto) (*models.WebauthnCredential, error) {
credential, err := cs.credentialPersister.Get(dto.CredentialId, cs.tenant.ID)
if err != nil {
cs.logger.Error(err)
return nil, echo.NewHTTPError(http.StatusInternalServerError, err)
}

if credential == nil {
return nil, echo.NewHTTPError(http.StatusNotFound, fmt.Errorf("credential with id '%s' not found", dto.CredentialId))
}

return credential, nil
}

func (cs *credentialService) Update(dto request.UpdateCredentialsDto) (*models.WebauthnCredential, error) {

credential, err := cs.credentialPersister.Get(dto.CredentialId, cs.tenant.ID)
Expand Down

0 comments on commit d582e26

Please sign in to comment.