-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Settings v2] Manage MFA (except recovery code)
ref DEV-1964 ref DEV-1967 ref DEV-1971 ref DEV-1961
- Loading branch information
Showing
59 changed files
with
9,449 additions
and
573 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
120 changes: 120 additions & 0 deletions
120
pkg/auth/handler/webapp/authflowv2/settings_mfa_change_password.go
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,120 @@ | ||
package authflowv2 | ||
|
||
import ( | ||
"net/http" | ||
|
||
handlerwebapp "github.com/authgear/authgear-server/pkg/auth/handler/webapp" | ||
"github.com/authgear/authgear-server/pkg/auth/handler/webapp/viewmodels" | ||
"github.com/authgear/authgear-server/pkg/auth/webapp" | ||
"github.com/authgear/authgear-server/pkg/lib/accountmanagement" | ||
"github.com/authgear/authgear-server/pkg/lib/session" | ||
"github.com/authgear/authgear-server/pkg/util/httproute" | ||
pwd "github.com/authgear/authgear-server/pkg/util/password" | ||
"github.com/authgear/authgear-server/pkg/util/template" | ||
"github.com/authgear/authgear-server/pkg/util/validation" | ||
) | ||
|
||
var TemplateWebSettingsV2MFAChangePasswordHTML = template.RegisterHTML( | ||
"web/authflowv2/settings_mfa_change_password.html", | ||
handlerwebapp.SettingsComponents..., | ||
) | ||
|
||
var AuthflowV2SettingsMFAChangePasswordSchema = validation.NewSimpleSchema(` | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"x_old_password": { "type": "string" }, | ||
"x_new_password": { "type": "string" }, | ||
"x_confirm_password": { "type": "string" } | ||
}, | ||
"required": ["x_old_password", "x_new_password", "x_confirm_password"] | ||
} | ||
`) | ||
|
||
type AuthflowV2SettingsMFAChangePasswordHandler struct { | ||
ControllerFactory handlerwebapp.ControllerFactory | ||
BaseViewModel *viewmodels.BaseViewModeler | ||
Renderer handlerwebapp.Renderer | ||
AccountManagementService *accountmanagement.Service | ||
PasswordPolicy handlerwebapp.PasswordPolicy | ||
} | ||
|
||
func ConfigureAuthflowV2SettingsMFAChangePassword(route httproute.Route) httproute.Route { | ||
return route. | ||
WithMethods("OPTIONS", "POST", "GET"). | ||
WithPathPattern(AuthflowV2RouteSettingsMFAChangePassword) | ||
} | ||
|
||
func (h *AuthflowV2SettingsMFAChangePasswordHandler) GetData(r *http.Request, rw http.ResponseWriter) (map[string]interface{}, error) { | ||
data := make(map[string]interface{}) | ||
|
||
// BaseViewModel | ||
baseViewModel := h.BaseViewModel.ViewModel(r, rw) | ||
viewmodels.Embed(data, baseViewModel) | ||
|
||
passwordPolicyViewModel := viewmodels.NewPasswordPolicyViewModel( | ||
h.PasswordPolicy.PasswordPolicy(), | ||
h.PasswordPolicy.PasswordRules(), | ||
baseViewModel.RawError, | ||
viewmodels.GetDefaultPasswordPolicyViewModelOptions(), | ||
) | ||
viewmodels.Embed(data, passwordPolicyViewModel) | ||
|
||
viewmodels.Embed(data, handlerwebapp.ChangePasswordViewModel{ | ||
Force: false, | ||
}) | ||
|
||
return data, nil | ||
} | ||
|
||
func (h *AuthflowV2SettingsMFAChangePasswordHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctrl, err := h.ControllerFactory.New(r, w) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
defer ctrl.ServeWithoutDBTx() | ||
|
||
ctrl.Get(func() error { | ||
data, err := h.GetData(r, w) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
h.Renderer.RenderHTML(w, r, TemplateWebSettingsV2MFAChangePasswordHTML, data) | ||
|
||
return nil | ||
}) | ||
|
||
ctrl.PostAction("", func() error { | ||
err := AuthflowV2SettingsMFAChangePasswordSchema.Validator().ValidateValue(handlerwebapp.FormToJSON(r.Form)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
oldPassword := r.Form.Get("x_old_password") | ||
newPassword := r.Form.Get("x_new_password") | ||
confirmPassword := r.Form.Get("x_confirm_password") | ||
|
||
err = pwd.ConfirmPassword(newPassword, confirmPassword) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s := session.GetSession(r.Context()) | ||
input := &accountmanagement.ChangeSecondaryPasswordInput{ | ||
OldPassword: oldPassword, | ||
NewPassword: newPassword, | ||
} | ||
|
||
_, err = h.AccountManagementService.ChangeSecondaryPassword(s, input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
result := webapp.Result{RedirectURI: AuthflowV2RouteSettingsMFAPassword} | ||
result.WriteResponse(w, r) | ||
return nil | ||
}) | ||
|
||
} |
125 changes: 125 additions & 0 deletions
125
pkg/auth/handler/webapp/authflowv2/settings_mfa_create_oob_otp.go
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,125 @@ | ||
package authflowv2 | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/authgear/authgear-server/pkg/api/model" | ||
handlerwebapp "github.com/authgear/authgear-server/pkg/auth/handler/webapp" | ||
"github.com/authgear/authgear-server/pkg/auth/handler/webapp/viewmodels" | ||
"github.com/authgear/authgear-server/pkg/auth/webapp" | ||
"github.com/authgear/authgear-server/pkg/lib/accountmanagement" | ||
"github.com/authgear/authgear-server/pkg/lib/session" | ||
"github.com/authgear/authgear-server/pkg/util/httproute" | ||
"github.com/authgear/authgear-server/pkg/util/template" | ||
"github.com/authgear/authgear-server/pkg/util/validation" | ||
) | ||
|
||
var TemplateWebSettingsMFACreateOOBOTPHTML = template.RegisterHTML( | ||
"web/authflowv2/settings_mfa_create_oob_otp.html", | ||
handlerwebapp.SettingsComponents..., | ||
) | ||
|
||
var AuthflowV2SettingsMFACreateOOBOTPSchema = validation.NewSimpleSchema(` | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"x_target": { "type": "string" } | ||
}, | ||
"required": ["x_target"] | ||
} | ||
`) | ||
|
||
func ConfigureAuthflowV2SettingsMFACreateOOBOTPRoute(route httproute.Route) httproute.Route { | ||
return route. | ||
WithMethods("OPTIONS", "POST", "GET"). | ||
WithPathPattern(AuthflowV2RouteSettingsMFACreateOOBOTP) | ||
} | ||
|
||
type SettingsMFACreateOOBOTPViewModel struct { | ||
OOBAuthenticatorType model.AuthenticatorType | ||
Channel model.AuthenticatorOOBChannel | ||
} | ||
|
||
type AuthflowV2SettingsMFACreateOOBOTPHandler struct { | ||
ControllerFactory handlerwebapp.ControllerFactory | ||
BaseViewModel *viewmodels.BaseViewModeler | ||
Renderer handlerwebapp.Renderer | ||
|
||
AccountManagementService *accountmanagement.Service | ||
} | ||
|
||
func NewSettingsMFACreateOOBOTPViewModel(channel model.AuthenticatorOOBChannel, authenticatorType model.AuthenticatorType) SettingsMFACreateOOBOTPViewModel { | ||
return SettingsMFACreateOOBOTPViewModel{ | ||
OOBAuthenticatorType: authenticatorType, | ||
Channel: channel, | ||
} | ||
} | ||
|
||
func (h *AuthflowV2SettingsMFACreateOOBOTPHandler) GetData(r *http.Request, w http.ResponseWriter, channel model.AuthenticatorOOBChannel) (map[string]interface{}, error) { | ||
data := make(map[string]interface{}) | ||
|
||
authenticatorType, err := model.GetOOBAuthenticatorType(channel) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
baseViewModel := h.BaseViewModel.ViewModelForAuthFlow(r, w) | ||
viewmodels.Embed(data, baseViewModel) | ||
|
||
settingsViewModel := NewSettingsMFACreateOOBOTPViewModel(channel, authenticatorType) | ||
viewmodels.Embed(data, settingsViewModel) | ||
|
||
return data, nil | ||
} | ||
|
||
func (h *AuthflowV2SettingsMFACreateOOBOTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctrl, err := h.ControllerFactory.New(r, w) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
defer ctrl.ServeWithoutDBTx() | ||
|
||
channel := model.AuthenticatorOOBChannel(httproute.GetParam(r, "channel")) | ||
|
||
ctrl.Get(func() error { | ||
data, err := h.GetData(r, w, channel) | ||
if err != nil { | ||
return err | ||
} | ||
h.Renderer.RenderHTML(w, r, TemplateWebSettingsMFACreateOOBOTPHTML, data) | ||
return nil | ||
}) | ||
|
||
ctrl.PostAction("", func() error { | ||
err := AuthflowV2SettingsMFACreateOOBOTPSchema.Validator().ValidateValue(handlerwebapp.FormToJSON(r.Form)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
target := r.Form.Get("x_target") | ||
|
||
s := session.GetSession(r.Context()) | ||
output, err := h.AccountManagementService.StartAddOOBOTPAuthenticator(s, &accountmanagement.StartAddOOBOTPAuthenticatorInput{ | ||
Channel: channel, | ||
Target: target, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
redirectURI, err := url.Parse(AuthflowV2RouteSettingsMFAEnterOOBOTP) | ||
if err != nil { | ||
return err | ||
} | ||
q := redirectURI.Query() | ||
q.Set("q_token", output.Token) | ||
redirectURI.RawQuery = q.Encode() | ||
|
||
result := webapp.Result{RedirectURI: redirectURI.String()} | ||
result.WriteResponse(w, r) | ||
|
||
return nil | ||
}) | ||
} |
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.