mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-21 12:28:46 +00:00
feat: add WebAuthN support for passwordless login and 2fa (#966)
* at least registration prompt works * in memory test for login * buttons to start webauthn process * begin eventstore impl * begin eventstore impl * serialize into bytes * fix: u2f, passwordless types * fix for localhost * fix script * fix: u2f, passwordless types * fix: add u2f * fix: verify u2f * fix: session data in event store * fix: u2f credentials in eventstore * fix: webauthn pkg handles business models * feat: tests * feat: append events * fix: test * fix: check only ready webauthn creds * fix: move u2f methods to authrepo * frontend improvements * fix return * feat: add passwordless * feat: add passwordless * improve ui / error handling * separate call for login * fix login * js * feat: u2f login methods * feat: remove unused session id * feat: error handling * feat: error handling * feat: refactor user eventstore * feat: finish webauthn * feat: u2f and passwordlss in auth.proto * u2f step * passwordless step * cleanup js * EndpointPasswordLessLogin * migration * update mfaChecked test * next step test * token name * cleanup * attribute * passwordless as tokens * remove sms as otp type * add "user" to amr for webauthn * error handling * fixes * fix tests * naming * naming * fixes * session handler * i18n * error handling in login * Update internal/ui/login/static/i18n/de.yaml Co-authored-by: Fabi <[email protected]> * Update internal/ui/login/static/i18n/en.yaml Co-authored-by: Fabi <[email protected]> * improvements * merge fixes * fixes * fixes Co-authored-by: Fabiennne <[email protected]> Co-authored-by: Fabi <[email protected]>
This commit is contained in:
co-authored by
Fabi
Fabiennne
parent
184e79be97
commit
300ade66a7
@@ -2,7 +2,6 @@ package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/caos/zitadel/internal/config/systemdefaults"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
@@ -16,6 +15,7 @@ import (
|
||||
"github.com/caos/zitadel/internal/api/http/middleware"
|
||||
auth_repository "github.com/caos/zitadel/internal/auth/repository"
|
||||
"github.com/caos/zitadel/internal/auth/repository/eventsourcing"
|
||||
"github.com/caos/zitadel/internal/config/systemdefaults"
|
||||
"github.com/caos/zitadel/internal/crypto"
|
||||
"github.com/caos/zitadel/internal/form"
|
||||
"github.com/caos/zitadel/internal/id"
|
||||
|
||||
@@ -7,15 +7,15 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
tmplMfaInitDone = "mfainitdone"
|
||||
tmplMFAInitDone = "mfainitdone"
|
||||
)
|
||||
|
||||
type mfaInitDoneData struct {
|
||||
}
|
||||
|
||||
func (l *Login) renderMfaInitDone(w http.ResponseWriter, r *http.Request, authReq *model.AuthRequest, data *mfaDoneData) {
|
||||
func (l *Login) renderMFAInitDone(w http.ResponseWriter, r *http.Request, authReq *model.AuthRequest, data *mfaDoneData) {
|
||||
var errType, errMessage string
|
||||
data.baseData = l.getBaseData(r, authReq, "Mfa Init Done", errType, errMessage)
|
||||
data.baseData = l.getBaseData(r, authReq, "MFA Init Done", errType, errMessage)
|
||||
data.profileData = l.getProfileData(authReq)
|
||||
l.renderer.RenderTemplate(w, r, l.renderer.Templates[tmplMfaInitDone], data, nil)
|
||||
l.renderer.RenderTemplate(w, r, l.renderer.Templates[tmplMFAInitDone], data, nil)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
|
||||
"github.com/caos/zitadel/internal/auth_request/model"
|
||||
user_model "github.com/caos/zitadel/internal/user/model"
|
||||
)
|
||||
|
||||
const (
|
||||
tmplMFAU2FInit = "mfainitu2f"
|
||||
)
|
||||
|
||||
func (l *Login) renderRegisterU2F(w http.ResponseWriter, r *http.Request, authReq *model.AuthRequest, err error) {
|
||||
var errType, errMessage, credentialData string
|
||||
var u2f *user_model.WebAuthNToken
|
||||
if err == nil {
|
||||
u2f, err = l.authRepo.AddMFAU2F(setContext(r.Context(), authReq.UserOrgID), authReq.UserID)
|
||||
}
|
||||
if err != nil {
|
||||
errMessage = l.getErrorMessage(r, err)
|
||||
}
|
||||
if u2f != nil {
|
||||
credentialData = base64.RawURLEncoding.EncodeToString(u2f.CredentialCreationData)
|
||||
}
|
||||
data := &webAuthNData{
|
||||
userData: l.getUserData(r, authReq, "Register WebAuthNToken", errType, errMessage),
|
||||
CredentialCreationData: credentialData,
|
||||
}
|
||||
l.renderer.RenderTemplate(w, r, l.renderer.Templates[tmplMFAU2FInit], data, nil)
|
||||
}
|
||||
|
||||
func (l *Login) handleRegisterU2F(w http.ResponseWriter, r *http.Request) {
|
||||
data := new(webAuthNFormData)
|
||||
authReq, err := l.getAuthRequestAndParseData(r, data)
|
||||
if err != nil {
|
||||
l.renderError(w, r, authReq, err)
|
||||
return
|
||||
}
|
||||
if data.Recreate {
|
||||
l.renderRegisterU2F(w, r, authReq, nil)
|
||||
return
|
||||
}
|
||||
credData, err := base64.URLEncoding.DecodeString(data.CredentialData)
|
||||
if err != nil {
|
||||
l.renderRegisterU2F(w, r, authReq, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = l.authRepo.VerifyMFAU2FSetup(setContext(r.Context(), authReq.UserOrgID), authReq.UserID, data.Name, credData); err != nil {
|
||||
l.renderRegisterU2F(w, r, authReq, err)
|
||||
return
|
||||
}
|
||||
done := &mfaDoneData{
|
||||
MFAType: model.MFATypeU2F,
|
||||
}
|
||||
l.renderMFAInitDone(w, r, authReq, done)
|
||||
}
|
||||
@@ -12,17 +12,17 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
tmplMfaInitVerify = "mfainitverify"
|
||||
tmplMFAInitVerify = "mfainitverify"
|
||||
)
|
||||
|
||||
type mfaInitVerifyData struct {
|
||||
MfaType model.MFAType `schema:"mfaType"`
|
||||
MFAType model.MFAType `schema:"mfaType"`
|
||||
Code string `schema:"code"`
|
||||
URL string `schema:"url"`
|
||||
Secret string `schema:"secret"`
|
||||
}
|
||||
|
||||
func (l *Login) handleMfaInitVerify(w http.ResponseWriter, r *http.Request) {
|
||||
func (l *Login) handleMFAInitVerify(w http.ResponseWriter, r *http.Request) {
|
||||
data := new(mfaInitVerifyData)
|
||||
authReq, err := l.getAuthRequestAndParseData(r, data)
|
||||
if err != nil {
|
||||
@@ -30,29 +30,29 @@ func (l *Login) handleMfaInitVerify(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
var verifyData *mfaVerifyData
|
||||
switch data.MfaType {
|
||||
switch data.MFAType {
|
||||
case model.MFATypeOTP:
|
||||
verifyData = l.handleOtpVerify(w, r, authReq, data)
|
||||
verifyData = l.handleOTPVerify(w, r, authReq, data)
|
||||
}
|
||||
|
||||
if verifyData != nil {
|
||||
l.renderMfaInitVerify(w, r, authReq, verifyData, err)
|
||||
l.renderMFAInitVerify(w, r, authReq, verifyData, err)
|
||||
return
|
||||
}
|
||||
|
||||
done := &mfaDoneData{
|
||||
MfaType: data.MfaType,
|
||||
MFAType: data.MFAType,
|
||||
}
|
||||
l.renderMfaInitDone(w, r, authReq, done)
|
||||
l.renderMFAInitDone(w, r, authReq, done)
|
||||
}
|
||||
|
||||
func (l *Login) handleOtpVerify(w http.ResponseWriter, r *http.Request, authReq *model.AuthRequest, data *mfaInitVerifyData) *mfaVerifyData {
|
||||
err := l.authRepo.VerifyMfaOTPSetup(setContext(r.Context(), authReq.UserOrgID), authReq.UserID, data.Code)
|
||||
func (l *Login) handleOTPVerify(w http.ResponseWriter, r *http.Request, authReq *model.AuthRequest, data *mfaInitVerifyData) *mfaVerifyData {
|
||||
err := l.authRepo.VerifyMFAOTPSetup(setContext(r.Context(), authReq.UserOrgID), authReq.UserID, data.Code)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
mfadata := &mfaVerifyData{
|
||||
MfaType: data.MfaType,
|
||||
MFAType: data.MFAType,
|
||||
otpData: otpData{
|
||||
Secret: data.Secret,
|
||||
Url: data.URL,
|
||||
@@ -62,21 +62,21 @@ func (l *Login) handleOtpVerify(w http.ResponseWriter, r *http.Request, authReq
|
||||
return mfadata
|
||||
}
|
||||
|
||||
func (l *Login) renderMfaInitVerify(w http.ResponseWriter, r *http.Request, authReq *model.AuthRequest, data *mfaVerifyData, err error) {
|
||||
func (l *Login) renderMFAInitVerify(w http.ResponseWriter, r *http.Request, authReq *model.AuthRequest, data *mfaVerifyData, err error) {
|
||||
var errType, errMessage string
|
||||
if err != nil {
|
||||
errMessage = l.getErrorMessage(r, err)
|
||||
}
|
||||
data.baseData = l.getBaseData(r, authReq, "Mfa Init Verify", errType, errMessage)
|
||||
data.baseData = l.getBaseData(r, authReq, "MFA Init Verify", errType, errMessage)
|
||||
data.profileData = l.getProfileData(authReq)
|
||||
if data.MfaType == model.MFATypeOTP {
|
||||
if data.MFAType == model.MFATypeOTP {
|
||||
code, err := generateQrCode(data.otpData.Url)
|
||||
if err == nil {
|
||||
data.otpData.QrCode = code
|
||||
}
|
||||
}
|
||||
|
||||
l.renderer.RenderTemplate(w, r, l.renderer.Templates[tmplMfaInitVerify], data, nil)
|
||||
l.renderer.RenderTemplate(w, r, l.renderer.Templates[tmplMFAInitVerify], data, nil)
|
||||
}
|
||||
|
||||
func generateQrCode(url string) (string, error) {
|
||||
|
||||
@@ -8,15 +8,15 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
tmplMfaPrompt = "mfaprompt"
|
||||
tmplMFAPrompt = "mfaprompt"
|
||||
)
|
||||
|
||||
type mfaPromptData struct {
|
||||
MfaProvider model.MFAType `schema:"provider"`
|
||||
MFAProvider model.MFAType `schema:"provider"`
|
||||
Skip bool `schema:"skip"`
|
||||
}
|
||||
|
||||
func (l *Login) handleMfaPrompt(w http.ResponseWriter, r *http.Request) {
|
||||
func (l *Login) handleMFAPrompt(w http.ResponseWriter, r *http.Request) {
|
||||
data := new(mfaPromptData)
|
||||
authReq, err := l.getAuthRequestAndParseData(r, data)
|
||||
if err != nil {
|
||||
@@ -25,11 +25,11 @@ func (l *Login) handleMfaPrompt(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
if !data.Skip {
|
||||
mfaVerifyData := new(mfaVerifyData)
|
||||
mfaVerifyData.MfaType = data.MfaProvider
|
||||
l.handleMfaCreation(w, r, authReq, mfaVerifyData)
|
||||
mfaVerifyData.MFAType = data.MFAProvider
|
||||
l.handleMFACreation(w, r, authReq, mfaVerifyData)
|
||||
return
|
||||
}
|
||||
err = l.authRepo.SkipMfaInit(setContext(r.Context(), authReq.UserOrgID), authReq.UserID)
|
||||
err = l.authRepo.SkipMFAInit(setContext(r.Context(), authReq.UserOrgID), authReq.UserID)
|
||||
if err != nil {
|
||||
l.renderError(w, r, authReq, err)
|
||||
return
|
||||
@@ -37,7 +37,7 @@ func (l *Login) handleMfaPrompt(w http.ResponseWriter, r *http.Request) {
|
||||
l.handleLogin(w, r)
|
||||
}
|
||||
|
||||
func (l *Login) handleMfaPromptSelection(w http.ResponseWriter, r *http.Request) {
|
||||
func (l *Login) handleMFAPromptSelection(w http.ResponseWriter, r *http.Request) {
|
||||
data := new(mfaPromptData)
|
||||
authReq, err := l.getAuthRequestAndParseData(r, data)
|
||||
if err != nil {
|
||||
@@ -48,45 +48,48 @@ func (l *Login) handleMfaPromptSelection(w http.ResponseWriter, r *http.Request)
|
||||
l.renderNextStep(w, r, authReq)
|
||||
}
|
||||
|
||||
func (l *Login) renderMfaPrompt(w http.ResponseWriter, r *http.Request, authReq *model.AuthRequest, mfaPromptData *model.MfaPromptStep, err error) {
|
||||
func (l *Login) renderMFAPrompt(w http.ResponseWriter, r *http.Request, authReq *model.AuthRequest, mfaPromptData *model.MFAPromptStep, err error) {
|
||||
var errType, errMessage string
|
||||
if err != nil {
|
||||
errMessage = l.getErrorMessage(r, err)
|
||||
}
|
||||
data := mfaData{
|
||||
baseData: l.getBaseData(r, authReq, "Mfa Prompt", errType, errMessage),
|
||||
baseData: l.getBaseData(r, authReq, "MFA Prompt", errType, errMessage),
|
||||
profileData: l.getProfileData(authReq),
|
||||
}
|
||||
|
||||
if mfaPromptData == nil {
|
||||
l.renderError(w, r, authReq, caos_errs.ThrowPreconditionFailed(nil, "APP-XU0tj", "Errors.User.Mfa.NoProviders"))
|
||||
l.renderError(w, r, authReq, caos_errs.ThrowPreconditionFailed(nil, "APP-XU0tj", "Errors.User.MFA.NoProviders"))
|
||||
return
|
||||
}
|
||||
|
||||
data.MfaProviders = mfaPromptData.MfaProviders
|
||||
data.MfaRequired = mfaPromptData.Required
|
||||
data.MFAProviders = mfaPromptData.MFAProviders
|
||||
data.MFARequired = mfaPromptData.Required
|
||||
|
||||
if len(mfaPromptData.MfaProviders) == 1 && mfaPromptData.Required {
|
||||
if len(mfaPromptData.MFAProviders) == 1 && mfaPromptData.Required {
|
||||
data := &mfaVerifyData{
|
||||
MfaType: mfaPromptData.MfaProviders[0],
|
||||
MFAType: mfaPromptData.MFAProviders[0],
|
||||
}
|
||||
l.handleMfaCreation(w, r, authReq, data)
|
||||
l.handleMFACreation(w, r, authReq, data)
|
||||
return
|
||||
}
|
||||
l.renderer.RenderTemplate(w, r, l.renderer.Templates[tmplMfaPrompt], data, nil)
|
||||
l.renderer.RenderTemplate(w, r, l.renderer.Templates[tmplMFAPrompt], data, nil)
|
||||
}
|
||||
|
||||
func (l *Login) handleMfaCreation(w http.ResponseWriter, r *http.Request, authReq *model.AuthRequest, data *mfaVerifyData) {
|
||||
switch data.MfaType {
|
||||
func (l *Login) handleMFACreation(w http.ResponseWriter, r *http.Request, authReq *model.AuthRequest, data *mfaVerifyData) {
|
||||
switch data.MFAType {
|
||||
case model.MFATypeOTP:
|
||||
l.handleOtpCreation(w, r, authReq, data)
|
||||
l.handleOTPCreation(w, r, authReq, data)
|
||||
return
|
||||
case model.MFATypeU2F:
|
||||
l.renderRegisterU2F(w, r, authReq, nil)
|
||||
return
|
||||
}
|
||||
l.renderError(w, r, authReq, caos_errs.ThrowPreconditionFailed(nil, "APP-Or3HO", "Errors.User.Mfa.NoProviders"))
|
||||
l.renderError(w, r, authReq, caos_errs.ThrowPreconditionFailed(nil, "APP-Or3HO", "Errors.User.MFA.NoProviders"))
|
||||
}
|
||||
|
||||
func (l *Login) handleOtpCreation(w http.ResponseWriter, r *http.Request, authReq *model.AuthRequest, data *mfaVerifyData) {
|
||||
otp, err := l.authRepo.AddMfaOTP(setContext(r.Context(), authReq.UserOrgID), authReq.UserID)
|
||||
func (l *Login) handleOTPCreation(w http.ResponseWriter, r *http.Request, authReq *model.AuthRequest, data *mfaVerifyData) {
|
||||
otp, err := l.authRepo.AddMFAOTP(setContext(r.Context(), authReq.UserOrgID), authReq.UserID)
|
||||
if err != nil {
|
||||
l.renderError(w, r, authReq, err)
|
||||
return
|
||||
@@ -96,5 +99,5 @@ func (l *Login) handleOtpCreation(w http.ResponseWriter, r *http.Request, authRe
|
||||
Secret: otp.SecretString,
|
||||
Url: otp.Url,
|
||||
}
|
||||
l.renderMfaInitVerify(w, r, authReq, data, nil)
|
||||
l.renderMFAInitVerify(w, r, authReq, data, nil)
|
||||
}
|
||||
|
||||
@@ -8,24 +8,24 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
tmplMfaVerify = "mfaverify"
|
||||
tmplMFAVerify = "mfaverify"
|
||||
)
|
||||
|
||||
type mfaVerifyFormData struct {
|
||||
MfaType model.MFAType `schema:"mfaType"`
|
||||
MFAType model.MFAType `schema:"mfaType"`
|
||||
Code string `schema:"code"`
|
||||
}
|
||||
|
||||
func (l *Login) handleMfaVerify(w http.ResponseWriter, r *http.Request) {
|
||||
func (l *Login) handleMFAVerify(w http.ResponseWriter, r *http.Request) {
|
||||
data := new(mfaVerifyFormData)
|
||||
authReq, err := l.getAuthRequestAndParseData(r, data)
|
||||
if err != nil {
|
||||
l.renderError(w, r, authReq, err)
|
||||
return
|
||||
}
|
||||
if data.MfaType == model.MFATypeOTP {
|
||||
if data.MFAType == model.MFATypeOTP {
|
||||
userAgentID, _ := http_mw.UserAgentIDFromCtx(r.Context())
|
||||
err = l.authRepo.VerifyMfaOTP(setContext(r.Context(), authReq.UserOrgID), authReq.ID, authReq.UserID, data.Code, userAgentID, model.BrowserInfoFromRequest(r))
|
||||
err = l.authRepo.VerifyMFAOTP(setContext(r.Context(), authReq.UserOrgID), authReq.ID, authReq.UserID, data.Code, userAgentID, model.BrowserInfoFromRequest(r))
|
||||
}
|
||||
if err != nil {
|
||||
l.renderError(w, r, authReq, err)
|
||||
@@ -34,15 +34,23 @@ func (l *Login) handleMfaVerify(w http.ResponseWriter, r *http.Request) {
|
||||
l.renderNextStep(w, r, authReq)
|
||||
}
|
||||
|
||||
func (l *Login) renderMfaVerify(w http.ResponseWriter, r *http.Request, authReq *model.AuthRequest, verificationStep *model.MfaVerificationStep, err error) {
|
||||
func (l *Login) renderMFAVerify(w http.ResponseWriter, r *http.Request, authReq *model.AuthRequest, verificationStep *model.MFAVerificationStep, err error) {
|
||||
var errType, errMessage string
|
||||
if err != nil {
|
||||
errMessage = l.getErrorMessage(r, err)
|
||||
}
|
||||
data := l.getUserData(r, authReq, "Mfa Verify", errType, errMessage)
|
||||
if verificationStep != nil {
|
||||
data.MfaProviders = verificationStep.MfaProviders
|
||||
data.SelectedMfaProvider = verificationStep.MfaProviders[0]
|
||||
data := l.getUserData(r, authReq, "MFA Verify", errType, errMessage)
|
||||
if verificationStep == nil {
|
||||
l.renderError(w, r, authReq, err)
|
||||
return
|
||||
}
|
||||
l.renderer.RenderTemplate(w, r, l.renderer.Templates[tmplMfaVerify], data, nil)
|
||||
switch verificationStep.MFAProviders[len(verificationStep.MFAProviders)-1] {
|
||||
case model.MFATypeU2F:
|
||||
l.renderU2FVerification(w, r, authReq, nil)
|
||||
return
|
||||
case model.MFATypeOTP:
|
||||
data.MFAProviders = verificationStep.MFAProviders
|
||||
data.SelectedMFAProvider = model.MFATypeOTP
|
||||
}
|
||||
l.renderer.RenderTemplate(w, r, l.renderer.Templates[tmplMFAVerify], data, nil)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
|
||||
http_mw "github.com/caos/zitadel/internal/api/http/middleware"
|
||||
"github.com/caos/zitadel/internal/auth_request/model"
|
||||
user_model "github.com/caos/zitadel/internal/user/model"
|
||||
)
|
||||
|
||||
const (
|
||||
tmplU2FVerification = "u2fverification"
|
||||
)
|
||||
|
||||
func (l *Login) renderU2FVerification(w http.ResponseWriter, r *http.Request, authReq *model.AuthRequest, err error) {
|
||||
var errType, errMessage, credentialData string
|
||||
var webAuthNLogin *user_model.WebAuthNLogin
|
||||
if err == nil {
|
||||
userAgentID, _ := http_mw.UserAgentIDFromCtx(r.Context())
|
||||
webAuthNLogin, err = l.authRepo.BeginMFAU2FLogin(setContext(r.Context(), authReq.UserOrgID), authReq.UserID, authReq.ID, userAgentID)
|
||||
}
|
||||
if err != nil {
|
||||
errMessage = l.getErrorMessage(r, err)
|
||||
}
|
||||
if webAuthNLogin != nil {
|
||||
credentialData = base64.RawURLEncoding.EncodeToString(webAuthNLogin.CredentialAssertionData)
|
||||
}
|
||||
data := &webAuthNData{
|
||||
userData: l.getUserData(r, authReq, "Login WebAuthNToken", errType, errMessage),
|
||||
CredentialCreationData: credentialData,
|
||||
}
|
||||
l.renderer.RenderTemplate(w, r, l.renderer.Templates[tmplU2FVerification], data, nil)
|
||||
}
|
||||
|
||||
func (l *Login) handleU2FVerification(w http.ResponseWriter, r *http.Request) {
|
||||
formData := new(webAuthNFormData)
|
||||
authReq, err := l.getAuthRequestAndParseData(r, formData)
|
||||
if err != nil {
|
||||
l.renderError(w, r, authReq, err)
|
||||
return
|
||||
}
|
||||
if formData.Recreate {
|
||||
l.renderU2FVerification(w, r, authReq, nil)
|
||||
return
|
||||
}
|
||||
credData, err := base64.URLEncoding.DecodeString(formData.CredentialData)
|
||||
if err != nil {
|
||||
l.renderU2FVerification(w, r, authReq, err)
|
||||
return
|
||||
}
|
||||
userAgentID, _ := http_mw.UserAgentIDFromCtx(r.Context())
|
||||
err = l.authRepo.VerifyMFAU2F(setContext(r.Context(), authReq.UserOrgID), authReq.UserID, authReq.ID, userAgentID, credData, model.BrowserInfoFromRequest(r))
|
||||
if err != nil {
|
||||
l.renderU2FVerification(w, r, authReq, err)
|
||||
return
|
||||
}
|
||||
l.renderNextStep(w, r, authReq)
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
|
||||
http_mw "github.com/caos/zitadel/internal/api/http/middleware"
|
||||
"github.com/caos/zitadel/internal/auth_request/model"
|
||||
user_model "github.com/caos/zitadel/internal/user/model"
|
||||
)
|
||||
|
||||
const (
|
||||
tmplPasswordlessVerification = "passwordlessverification"
|
||||
)
|
||||
|
||||
func (l *Login) renderPasswordlessVerification(w http.ResponseWriter, r *http.Request, authReq *model.AuthRequest, err error) {
|
||||
var errType, errMessage, credentialData string
|
||||
var webAuthNLogin *user_model.WebAuthNLogin
|
||||
if err == nil {
|
||||
userAgentID, _ := http_mw.UserAgentIDFromCtx(r.Context())
|
||||
webAuthNLogin, err = l.authRepo.BeginPasswordlessLogin(setContext(r.Context(), authReq.UserOrgID), authReq.UserID, authReq.ID, userAgentID)
|
||||
}
|
||||
if err != nil {
|
||||
errMessage = l.getErrorMessage(r, err)
|
||||
}
|
||||
if webAuthNLogin != nil {
|
||||
credentialData = base64.RawURLEncoding.EncodeToString(webAuthNLogin.CredentialAssertionData)
|
||||
}
|
||||
data := &webAuthNData{
|
||||
userData: l.getUserData(r, authReq, "Login Passwordless", errType, errMessage),
|
||||
CredentialCreationData: credentialData,
|
||||
}
|
||||
l.renderer.RenderTemplate(w, r, l.renderer.Templates[tmplPasswordlessVerification], data, nil)
|
||||
}
|
||||
|
||||
func (l *Login) handlePasswordlessVerification(w http.ResponseWriter, r *http.Request) {
|
||||
formData := new(webAuthNFormData)
|
||||
authReq, err := l.getAuthRequestAndParseData(r, formData)
|
||||
if err != nil {
|
||||
l.renderError(w, r, authReq, err)
|
||||
return
|
||||
}
|
||||
if formData.Recreate {
|
||||
l.renderPasswordlessVerification(w, r, authReq, nil)
|
||||
return
|
||||
}
|
||||
credData, err := base64.URLEncoding.DecodeString(formData.CredentialData)
|
||||
if err != nil {
|
||||
l.renderPasswordlessVerification(w, r, authReq, err)
|
||||
return
|
||||
}
|
||||
userAgentID, _ := http_mw.UserAgentIDFromCtx(r.Context())
|
||||
err = l.authRepo.VerifyPasswordless(setContext(r.Context(), authReq.UserOrgID), authReq.UserID, authReq.ID, userAgentID, credData, model.BrowserInfoFromRequest(r))
|
||||
if err != nil {
|
||||
l.renderPasswordlessVerification(w, r, authReq, err)
|
||||
return
|
||||
}
|
||||
l.renderNextStep(w, r, authReq)
|
||||
}
|
||||
@@ -3,18 +3,19 @@ package handler
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/caos/logging"
|
||||
iam_model "github.com/caos/zitadel/internal/iam/model"
|
||||
"github.com/gorilla/csrf"
|
||||
"golang.org/x/text/language"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
"github.com/caos/logging"
|
||||
"github.com/gorilla/csrf"
|
||||
"golang.org/x/text/language"
|
||||
|
||||
http_mw "github.com/caos/zitadel/internal/api/http/middleware"
|
||||
"github.com/caos/zitadel/internal/auth_request/model"
|
||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/i18n"
|
||||
iam_model "github.com/caos/zitadel/internal/iam/model"
|
||||
"github.com/caos/zitadel/internal/renderer"
|
||||
)
|
||||
|
||||
@@ -32,31 +33,34 @@ func CreateRenderer(pathPrefix string, staticDir http.FileSystem, cookieName str
|
||||
pathPrefix: pathPrefix,
|
||||
}
|
||||
tmplMapping := map[string]string{
|
||||
tmplError: "error.html",
|
||||
tmplLogin: "login.html",
|
||||
tmplUserSelection: "select_user.html",
|
||||
tmplPassword: "password.html",
|
||||
tmplMfaVerify: "mfa_verify.html",
|
||||
tmplMfaPrompt: "mfa_prompt.html",
|
||||
tmplMfaInitVerify: "mfa_init_verify.html",
|
||||
tmplMfaInitDone: "mfa_init_done.html",
|
||||
tmplMailVerification: "mail_verification.html",
|
||||
tmplMailVerified: "mail_verified.html",
|
||||
tmplInitPassword: "init_password.html",
|
||||
tmplInitPasswordDone: "init_password_done.html",
|
||||
tmplInitUser: "init_user.html",
|
||||
tmplInitUserDone: "init_user_done.html",
|
||||
tmplPasswordResetDone: "password_reset_done.html",
|
||||
tmplChangePassword: "change_password.html",
|
||||
tmplChangePasswordDone: "change_password_done.html",
|
||||
tmplRegisterOption: "register_option.html",
|
||||
tmplRegister: "register.html",
|
||||
tmplLogoutDone: "logout_done.html",
|
||||
tmplRegisterOrg: "register_org.html",
|
||||
tmplChangeUsername: "change_username.html",
|
||||
tmplChangeUsernameDone: "change_username_done.html",
|
||||
tmplLinkUsersDone: "link_users_done.html",
|
||||
tmplExternalNotFoundOption: "external_not_found_option.html",
|
||||
tmplError: "error.html",
|
||||
tmplLogin: "login.html",
|
||||
tmplUserSelection: "select_user.html",
|
||||
tmplPassword: "password.html",
|
||||
tmplPasswordlessVerification: "passwordless.html",
|
||||
tmplMFAVerify: "mfa_verify.html",
|
||||
tmplMFAPrompt: "mfa_prompt.html",
|
||||
tmplMFAInitVerify: "mfa_init_verify.html",
|
||||
tmplMFAU2FInit: "mfa_init_u2f.html",
|
||||
tmplU2FVerification: "mfa_verification_u2f.html",
|
||||
tmplMFAInitDone: "mfa_init_done.html",
|
||||
tmplMailVerification: "mail_verification.html",
|
||||
tmplMailVerified: "mail_verified.html",
|
||||
tmplInitPassword: "init_password.html",
|
||||
tmplInitPasswordDone: "init_password_done.html",
|
||||
tmplInitUser: "init_user.html",
|
||||
tmplInitUserDone: "init_user_done.html",
|
||||
tmplPasswordResetDone: "password_reset_done.html",
|
||||
tmplChangePassword: "change_password.html",
|
||||
tmplChangePasswordDone: "change_password_done.html",
|
||||
tmplRegisterOption: "register_option.html",
|
||||
tmplRegister: "register.html",
|
||||
tmplLogoutDone: "logout_done.html",
|
||||
tmplRegisterOrg: "register_org.html",
|
||||
tmplChangeUsername: "change_username.html",
|
||||
tmplChangeUsernameDone: "change_username_done.html",
|
||||
tmplLinkUsersDone: "link_users_done.html",
|
||||
tmplExternalNotFoundOption: "external_not_found_option.html",
|
||||
}
|
||||
funcs := map[string]interface{}{
|
||||
"resourceUrl": func(file string) string {
|
||||
@@ -86,6 +90,9 @@ func CreateRenderer(pathPrefix string, staticDir http.FileSystem, cookieName str
|
||||
"userSelectionUrl": func() string {
|
||||
return path.Join(r.pathPrefix, EndpointUserSelection)
|
||||
},
|
||||
"passwordLessVerificationUrl": func() string {
|
||||
return path.Join(r.pathPrefix, EndpointPasswordlessLogin)
|
||||
},
|
||||
"passwordResetUrl": func(id string) string {
|
||||
return path.Join(r.pathPrefix, fmt.Sprintf("%s?%s=%s", EndpointPasswordReset, queryAuthRequestID, id))
|
||||
},
|
||||
@@ -93,16 +100,22 @@ func CreateRenderer(pathPrefix string, staticDir http.FileSystem, cookieName str
|
||||
return path.Join(r.pathPrefix, EndpointPassword)
|
||||
},
|
||||
"mfaVerifyUrl": func() string {
|
||||
return path.Join(r.pathPrefix, EndpointMfaVerify)
|
||||
return path.Join(r.pathPrefix, EndpointMFAVerify)
|
||||
},
|
||||
"mfaPromptUrl": func() string {
|
||||
return path.Join(r.pathPrefix, EndpointMfaPrompt)
|
||||
return path.Join(r.pathPrefix, EndpointMFAPrompt)
|
||||
},
|
||||
"mfaPromptChangeUrl": func(id string, provider model.MFAType) string {
|
||||
return path.Join(r.pathPrefix, fmt.Sprintf("%s?%s=%s;%s=%v", EndpointMfaPrompt, queryAuthRequestID, id, "provider", provider))
|
||||
return path.Join(r.pathPrefix, fmt.Sprintf("%s?%s=%s;%s=%v", EndpointMFAPrompt, queryAuthRequestID, id, "provider", provider))
|
||||
},
|
||||
"mfaInitVerifyUrl": func() string {
|
||||
return path.Join(r.pathPrefix, EndpointMfaInitVerify)
|
||||
return path.Join(r.pathPrefix, EndpointMFAInitVerify)
|
||||
},
|
||||
"mfaInitU2FVerifyUrl": func() string {
|
||||
return path.Join(r.pathPrefix, EndpointMFAInitU2FVerify)
|
||||
},
|
||||
"mfaInitU2FLoginUrl": func() string {
|
||||
return path.Join(r.pathPrefix, EndpointU2FVerification)
|
||||
},
|
||||
"mailVerificationUrl": func() string {
|
||||
return path.Join(r.pathPrefix, EndpointMailVerification)
|
||||
@@ -190,8 +203,10 @@ func (l *Login) chooseNextStep(w http.ResponseWriter, r *http.Request, authReq *
|
||||
l.renderInitPassword(w, r, authReq, authReq.UserID, "", err)
|
||||
case *model.PasswordStep:
|
||||
l.renderPassword(w, r, authReq, nil)
|
||||
case *model.MfaVerificationStep:
|
||||
l.renderMfaVerify(w, r, authReq, step, err)
|
||||
case *model.PasswordlessStep:
|
||||
l.renderPasswordlessVerification(w, r, authReq, nil)
|
||||
case *model.MFAVerificationStep:
|
||||
l.renderMFAVerify(w, r, authReq, step, err)
|
||||
case *model.RedirectToCallbackStep:
|
||||
if len(authReq.PossibleSteps) > 1 {
|
||||
l.chooseNextStep(w, r, authReq, 1, err)
|
||||
@@ -202,8 +217,8 @@ func (l *Login) chooseNextStep(w http.ResponseWriter, r *http.Request, authReq *
|
||||
l.renderChangePassword(w, r, authReq, err)
|
||||
case *model.VerifyEMailStep:
|
||||
l.renderMailVerification(w, r, authReq, "", err)
|
||||
case *model.MfaPromptStep:
|
||||
l.renderMfaPrompt(w, r, authReq, step, err)
|
||||
case *model.MFAPromptStep:
|
||||
l.renderMFAPrompt(w, r, authReq, step, err)
|
||||
case *model.InitUserStep:
|
||||
l.renderInitUser(w, r, authReq, "", "", step.PasswordSet, nil)
|
||||
case *model.ChangeUsernameStep:
|
||||
@@ -356,8 +371,8 @@ type userData struct {
|
||||
baseData
|
||||
profileData
|
||||
PasswordChecked string
|
||||
MfaProviders []model.MFAType
|
||||
SelectedMfaProvider model.MFAType
|
||||
MFAProviders []model.MFAType
|
||||
SelectedMFAProvider model.MFAType
|
||||
Linking bool
|
||||
}
|
||||
|
||||
@@ -386,21 +401,21 @@ type userSelectionData struct {
|
||||
type mfaData struct {
|
||||
baseData
|
||||
profileData
|
||||
MfaProviders []model.MFAType
|
||||
MfaRequired bool
|
||||
MFAProviders []model.MFAType
|
||||
MFARequired bool
|
||||
}
|
||||
|
||||
type mfaVerifyData struct {
|
||||
baseData
|
||||
profileData
|
||||
MfaType model.MFAType
|
||||
MFAType model.MFAType
|
||||
otpData
|
||||
}
|
||||
|
||||
type mfaDoneData struct {
|
||||
baseData
|
||||
profileData
|
||||
MfaType model.MFAType
|
||||
MFAType model.MFAType
|
||||
}
|
||||
|
||||
type otpData struct {
|
||||
|
||||
@@ -13,6 +13,7 @@ const (
|
||||
EndpointLogin = "/login"
|
||||
EndpointExternalLogin = "/login/externalidp"
|
||||
EndpointExternalLoginCallback = "/login/externalidp/callback"
|
||||
EndpointPasswordlessLogin = "/login/passwordless"
|
||||
EndpointLoginName = "/loginname"
|
||||
EndpointUserSelection = "/userselection"
|
||||
EndpointChangeUsername = "/username/change"
|
||||
@@ -21,9 +22,11 @@ const (
|
||||
EndpointChangePassword = "/password/change"
|
||||
EndpointPasswordReset = "/password/reset"
|
||||
EndpointInitUser = "/user/init"
|
||||
EndpointMfaVerify = "/mfa/verify"
|
||||
EndpointMfaPrompt = "/mfa/prompt"
|
||||
EndpointMfaInitVerify = "/mfa/init/verify"
|
||||
EndpointMFAVerify = "/mfa/verify"
|
||||
EndpointMFAPrompt = "/mfa/prompt"
|
||||
EndpointMFAInitVerify = "/mfa/init/verify"
|
||||
EndpointMFAInitU2FVerify = "/mfa/init/u2f/verify"
|
||||
EndpointU2FVerification = "/mfa/u2f/verify"
|
||||
EndpointMailVerification = "/mail/verification"
|
||||
EndpointMailVerified = "/mail/verified"
|
||||
EndpointRegisterOption = "/register/option"
|
||||
@@ -46,6 +49,7 @@ func CreateRouter(login *Login, staticDir http.FileSystem, interceptors ...mux.M
|
||||
router.HandleFunc(EndpointLogin, login.handleLogin).Methods(http.MethodGet, http.MethodPost)
|
||||
router.HandleFunc(EndpointExternalLogin, login.handleExternalLogin).Methods(http.MethodGet)
|
||||
router.HandleFunc(EndpointExternalLoginCallback, login.handleExternalLoginCallback).Methods(http.MethodGet)
|
||||
router.HandleFunc(EndpointPasswordlessLogin, login.handlePasswordlessVerification).Methods(http.MethodPost)
|
||||
router.HandleFunc(EndpointLoginName, login.handleLoginName).Methods(http.MethodGet)
|
||||
router.HandleFunc(EndpointLoginName, login.handleLoginNameCheck).Methods(http.MethodPost)
|
||||
router.HandleFunc(EndpointUserSelection, login.handleSelectUser).Methods(http.MethodPost)
|
||||
@@ -56,10 +60,12 @@ func CreateRouter(login *Login, staticDir http.FileSystem, interceptors ...mux.M
|
||||
router.HandleFunc(EndpointPasswordReset, login.handlePasswordReset).Methods(http.MethodGet)
|
||||
router.HandleFunc(EndpointInitUser, login.handleInitUser).Methods(http.MethodGet)
|
||||
router.HandleFunc(EndpointInitUser, login.handleInitUserCheck).Methods(http.MethodPost)
|
||||
router.HandleFunc(EndpointMfaVerify, login.handleMfaVerify).Methods(http.MethodPost)
|
||||
router.HandleFunc(EndpointMfaPrompt, login.handleMfaPromptSelection).Methods(http.MethodGet)
|
||||
router.HandleFunc(EndpointMfaPrompt, login.handleMfaPrompt).Methods(http.MethodPost)
|
||||
router.HandleFunc(EndpointMfaInitVerify, login.handleMfaInitVerify).Methods(http.MethodPost)
|
||||
router.HandleFunc(EndpointMFAVerify, login.handleMFAVerify).Methods(http.MethodPost)
|
||||
router.HandleFunc(EndpointMFAPrompt, login.handleMFAPromptSelection).Methods(http.MethodGet)
|
||||
router.HandleFunc(EndpointMFAPrompt, login.handleMFAPrompt).Methods(http.MethodPost)
|
||||
router.HandleFunc(EndpointMFAInitVerify, login.handleMFAInitVerify).Methods(http.MethodPost)
|
||||
router.HandleFunc(EndpointMFAInitU2FVerify, login.handleRegisterU2F).Methods(http.MethodPost)
|
||||
router.HandleFunc(EndpointU2FVerification, login.handleU2FVerification).Methods(http.MethodPost)
|
||||
router.HandleFunc(EndpointMailVerification, login.handleMailVerification).Methods(http.MethodGet)
|
||||
router.HandleFunc(EndpointMailVerification, login.handleMailVerificationCheck).Methods(http.MethodPost)
|
||||
router.HandleFunc(EndpointChangePassword, login.handleChangePassword).Methods(http.MethodPost)
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package handler
|
||||
|
||||
type webAuthNData struct {
|
||||
userData
|
||||
CredentialCreationData string
|
||||
}
|
||||
|
||||
type webAuthNFormData struct {
|
||||
CredentialData string `schema:"credentialData"`
|
||||
Name string `schema:"name"`
|
||||
Recreate bool `schema:"recreate"`
|
||||
}
|
||||
Reference in New Issue
Block a user