2022-02-14 17:22:30 +01:00
|
|
|
package login
|
2020-12-02 17:00:04 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"net/http"
|
|
|
|
|
2022-02-14 17:22:30 +01:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2020-12-02 17:00:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tmplPasswordlessVerification = "passwordlessverification"
|
|
|
|
)
|
|
|
|
|
2020-12-18 13:42:21 +01:00
|
|
|
type passwordlessData struct {
|
|
|
|
webAuthNData
|
|
|
|
PasswordLogin bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type passwordlessFormData struct {
|
|
|
|
webAuthNFormData
|
|
|
|
PasswordLogin bool `schema:"passwordlogin"`
|
|
|
|
}
|
|
|
|
|
2021-08-02 15:24:58 +02:00
|
|
|
func (l *Login) renderPasswordlessVerification(w http.ResponseWriter, r *http.Request, authReq *domain.AuthRequest, passwordSet bool, err error) {
|
2021-06-18 10:31:53 +02:00
|
|
|
var errID, errMessage, credentialData string
|
2021-02-08 11:30:30 +01:00
|
|
|
var webAuthNLogin *domain.WebAuthNLogin
|
2020-12-02 17:00:04 +01:00
|
|
|
if err == nil {
|
2022-04-05 07:58:09 +02:00
|
|
|
webAuthNLogin, err = l.authRepo.BeginPasswordlessLogin(setContext(r.Context(), authReq.UserOrgID), authReq.UserID, authReq.UserOrgID, authReq.ID, authReq.AgentID)
|
2020-12-02 17:00:04 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
2021-06-18 10:31:53 +02:00
|
|
|
errID, errMessage = l.getErrorMessage(r, err)
|
2020-12-02 17:00:04 +01:00
|
|
|
}
|
|
|
|
if webAuthNLogin != nil {
|
|
|
|
credentialData = base64.RawURLEncoding.EncodeToString(webAuthNLogin.CredentialAssertionData)
|
|
|
|
}
|
2021-08-02 15:24:58 +02:00
|
|
|
if passwordSet && authReq.LoginPolicy != nil {
|
|
|
|
passwordSet = authReq.LoginPolicy.AllowUsernamePassword
|
2020-12-18 13:42:21 +01:00
|
|
|
}
|
|
|
|
data := &passwordlessData{
|
|
|
|
webAuthNData{
|
2021-06-18 10:31:53 +02:00
|
|
|
userData: l.getUserData(r, authReq, "Login Passwordless", errID, errMessage),
|
2020-12-18 13:42:21 +01:00
|
|
|
CredentialCreationData: credentialData,
|
|
|
|
},
|
2021-08-02 15:24:58 +02:00
|
|
|
passwordSet,
|
2020-12-02 17:00:04 +01:00
|
|
|
}
|
2022-04-25 11:16:36 +02:00
|
|
|
l.renderer.RenderTemplate(w, r, l.getTranslator(r.Context(), authReq), l.renderer.Templates[tmplPasswordlessVerification], data, nil)
|
2020-12-02 17:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Login) handlePasswordlessVerification(w http.ResponseWriter, r *http.Request) {
|
2020-12-18 13:42:21 +01:00
|
|
|
formData := new(passwordlessFormData)
|
2020-12-02 17:00:04 +01:00
|
|
|
authReq, err := l.getAuthRequestAndParseData(r, formData)
|
|
|
|
if err != nil {
|
|
|
|
l.renderError(w, r, authReq, err)
|
|
|
|
return
|
|
|
|
}
|
2020-12-18 13:42:21 +01:00
|
|
|
if formData.PasswordLogin {
|
|
|
|
l.renderPassword(w, r, authReq, nil)
|
|
|
|
return
|
|
|
|
}
|
2020-12-02 17:00:04 +01:00
|
|
|
credData, err := base64.URLEncoding.DecodeString(formData.CredentialData)
|
|
|
|
if err != nil {
|
2021-08-02 15:24:58 +02:00
|
|
|
l.renderPasswordlessVerification(w, r, authReq, formData.PasswordLogin, err)
|
2020-12-02 17:00:04 +01:00
|
|
|
return
|
|
|
|
}
|
2022-04-05 07:58:09 +02:00
|
|
|
err = l.authRepo.VerifyPasswordless(setContext(r.Context(), authReq.UserOrgID), authReq.UserID, authReq.UserOrgID, authReq.ID, authReq.AgentID, credData, domain.BrowserInfoFromRequest(r))
|
2020-12-02 17:00:04 +01:00
|
|
|
if err != nil {
|
2021-08-02 15:24:58 +02:00
|
|
|
l.renderPasswordlessVerification(w, r, authReq, formData.PasswordLogin, err)
|
2020-12-02 17:00:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
l.renderNextStep(w, r, authReq)
|
|
|
|
}
|