-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mfa): add multi factor authentication endpoints
* add config for mfa passkeys * add endpoints for mfa passkeys TODO: add docs Closes: #45
- Loading branch information
Stefan Jacobi
committed
Mar 5, 2024
1 parent
ff48313
commit 6f8b064
Showing
24 changed files
with
466 additions
and
138 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
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
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
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,115 @@ | ||
package handler | ||
|
||
import ( | ||
"fmt" | ||
"github.com/go-webauthn/webauthn/protocol" | ||
"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" | ||
auditlog "github.com/teamhanko/passkey-server/audit_log" | ||
"github.com/teamhanko/passkey-server/persistence" | ||
"github.com/teamhanko/passkey-server/persistence/models" | ||
"net/http" | ||
) | ||
|
||
type mfaLoginHandler struct { | ||
*webauthnHandler | ||
} | ||
|
||
func NewMfaLoginHandler(persister persistence.Persister) WebauthnHandler { | ||
webauthnHandler := newWebAuthnHandler(persister, true) | ||
|
||
return &mfaLoginHandler{ | ||
webauthnHandler, | ||
} | ||
} | ||
|
||
func (lh *mfaLoginHandler) Init(ctx echo.Context) error { | ||
h, err := helper.GetMfaHandlerContext(ctx) | ||
if err != nil { | ||
ctx.Logger().Error(err) | ||
return err | ||
} | ||
|
||
dto, err := BindAndValidateRequest[request.InitMfaLoginDto](ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return lh.persister.GetConnection().Transaction(func(tx *pop.Connection) error { | ||
userPersister := lh.persister.GetWebauthnUserPersister(tx) | ||
sessionPersister := lh.persister.GetWebauthnSessionDataPersister(tx) | ||
credentialPersister := lh.persister.GetWebauthnCredentialPersister(tx) | ||
|
||
service := services.NewLoginService(services.WebauthnServiceCreateParams{ | ||
Ctx: ctx, | ||
Tenant: *h.Tenant, | ||
WebauthnClient: *h.WebauthnClient, | ||
UserId: dto.UserId, | ||
UserPersister: userPersister, | ||
SessionPersister: sessionPersister, | ||
CredentialPersister: credentialPersister, | ||
}) | ||
|
||
credentialAssertion, err := service.Initialize() | ||
err = lh.handleError(h.AuditLog, models.AuditLogWebAuthnAuthenticationInitFailed, tx, ctx, nil, nil, err) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
auditErr := h.AuditLog.CreateWithConnection(tx, models.AuditLogWebAuthnAuthenticationInitSucceeded, nil, nil, nil) | ||
if auditErr != nil { | ||
ctx.Logger().Error(auditErr) | ||
return fmt.Errorf(auditlog.CreationFailureFormat, auditErr) | ||
} | ||
|
||
return ctx.JSON(http.StatusOK, credentialAssertion) | ||
}) | ||
} | ||
|
||
func (lh *mfaLoginHandler) Finish(ctx echo.Context) error { | ||
parsedRequest, err := protocol.ParseCredentialRequestResponse(ctx.Request()) | ||
if err != nil { | ||
ctx.Logger().Error(err) | ||
return echo.NewHTTPError(http.StatusBadRequest, "unable to finish login").SetInternal(err) | ||
} | ||
|
||
h, err := helper.GetMfaHandlerContext(ctx) | ||
if err != nil { | ||
ctx.Logger().Error(err) | ||
return err | ||
} | ||
|
||
return lh.persister.Transaction(func(tx *pop.Connection) error { | ||
userPersister := lh.persister.GetWebauthnUserPersister(tx) | ||
sessionPersister := lh.persister.GetWebauthnSessionDataPersister(tx) | ||
credentialPersister := lh.persister.GetWebauthnCredentialPersister(tx) | ||
|
||
service := services.NewLoginService(services.WebauthnServiceCreateParams{ | ||
Ctx: ctx, | ||
Tenant: *h.Tenant, | ||
WebauthnClient: *h.WebauthnClient, | ||
UserPersister: userPersister, | ||
SessionPersister: sessionPersister, | ||
CredentialPersister: credentialPersister, | ||
Generator: h.Generator, | ||
}) | ||
|
||
token, userId, err := service.Finalize(parsedRequest) | ||
err = lh.handleError(h.AuditLog, models.AuditLogWebAuthnAuthenticationFinalFailed, tx, ctx, &userId, nil, err) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
auditErr := h.AuditLog.CreateWithConnection(tx, models.AuditLogWebAuthnAuthenticationFinalSucceeded, &userId, nil, nil) | ||
if auditErr != nil { | ||
ctx.Logger().Error(auditErr) | ||
return fmt.Errorf(auditlog.CreationFailureFormat, auditErr) | ||
} | ||
|
||
return ctx.JSON(http.StatusOK, &response.TokenDto{Token: token}) | ||
}) | ||
} |
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
Oops, something went wrong.