2018-05-17 09:35:00 +05:30
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2018-05-17 09:35:00 +05:30
|
|
|
|
2022-01-02 18:42:35 +05:30
|
|
|
package security
|
2018-05-17 09:35:00 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"html/template"
|
|
|
|
"image/png"
|
2021-04-05 21:00:52 +05:30
|
|
|
"net/http"
|
2018-05-17 09:35:00 +05:30
|
|
|
"strings"
|
|
|
|
|
2022-01-02 18:42:35 +05:30
|
|
|
"code.gitea.io/gitea/models/auth"
|
2020-05-17 18:13:29 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2018-05-17 09:35:00 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-01-26 21:06:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web"
|
2024-02-27 12:42:22 +05:30
|
|
|
"code.gitea.io/gitea/services/context"
|
2021-04-07 01:14:05 +05:30
|
|
|
"code.gitea.io/gitea/services/forms"
|
2018-05-17 09:35:00 +05:30
|
|
|
|
|
|
|
"github.com/pquerna/otp"
|
|
|
|
"github.com/pquerna/otp/totp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RegenerateScratchTwoFactor regenerates the user's 2FA scratch code.
|
|
|
|
func RegenerateScratchTwoFactor(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsSecurity"] = true
|
|
|
|
|
2023-09-15 11:43:19 +05:30
|
|
|
t, err := auth.GetTwoFactorByUID(ctx, ctx.Doer.ID)
|
2018-05-17 09:35:00 +05:30
|
|
|
if err != nil {
|
2022-01-02 18:42:35 +05:30
|
|
|
if auth.IsErrTwoFactorNotEnrolled(err) {
|
2021-09-25 19:57:01 +05:30
|
|
|
ctx.Flash.Error(ctx.Tr("settings.twofa_not_enrolled"))
|
2020-05-17 18:13:29 +05:30
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security")
|
|
|
|
}
|
|
|
|
ctx.ServerError("SettingsTwoFactor: Failed to GetTwoFactorByUID", err)
|
2018-05-17 09:35:00 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-27 18:24:50 +05:30
|
|
|
token, err := t.GenerateScratchToken()
|
|
|
|
if err != nil {
|
2020-05-17 18:13:29 +05:30
|
|
|
ctx.ServerError("SettingsTwoFactor: Failed to GenerateScratchToken", err)
|
2018-05-17 09:35:00 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-15 11:43:19 +05:30
|
|
|
if err = auth.UpdateTwoFactor(ctx, t); err != nil {
|
2020-05-17 18:13:29 +05:30
|
|
|
ctx.ServerError("SettingsTwoFactor: Failed to UpdateTwoFactor", err)
|
2018-05-17 09:35:00 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-27 18:24:50 +05:30
|
|
|
ctx.Flash.Success(ctx.Tr("settings.twofa_scratch_token_regenerated", token))
|
2018-05-17 09:35:00 +05:30
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security")
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisableTwoFactor deletes the user's 2FA settings.
|
|
|
|
func DisableTwoFactor(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsSecurity"] = true
|
|
|
|
|
2023-09-15 11:43:19 +05:30
|
|
|
t, err := auth.GetTwoFactorByUID(ctx, ctx.Doer.ID)
|
2018-05-17 09:35:00 +05:30
|
|
|
if err != nil {
|
2022-01-02 18:42:35 +05:30
|
|
|
if auth.IsErrTwoFactorNotEnrolled(err) {
|
2021-09-25 19:57:01 +05:30
|
|
|
ctx.Flash.Error(ctx.Tr("settings.twofa_not_enrolled"))
|
2020-05-17 18:13:29 +05:30
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security")
|
|
|
|
}
|
|
|
|
ctx.ServerError("SettingsTwoFactor: Failed to GetTwoFactorByUID", err)
|
2018-05-17 09:35:00 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-15 11:43:19 +05:30
|
|
|
if err = auth.DeleteTwoFactorByID(ctx, t.ID, ctx.Doer.ID); err != nil {
|
2022-01-02 18:42:35 +05:30
|
|
|
if auth.IsErrTwoFactorNotEnrolled(err) {
|
2020-05-17 18:13:29 +05:30
|
|
|
// There is a potential DB race here - we must have been disabled by another request in the intervening period
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.twofa_disabled"))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security")
|
|
|
|
}
|
|
|
|
ctx.ServerError("SettingsTwoFactor: Failed to DeleteTwoFactorByID", err)
|
2018-05-17 09:35:00 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.twofa_disabled"))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security")
|
|
|
|
}
|
|
|
|
|
|
|
|
func twofaGenerateSecretAndQr(ctx *context.Context) bool {
|
|
|
|
var otpKey *otp.Key
|
|
|
|
var err error
|
|
|
|
uri := ctx.Session.Get("twofaUri")
|
|
|
|
if uri != nil {
|
|
|
|
otpKey, err = otp.NewKeyFromURL(uri.(string))
|
2019-06-13 01:11:28 +05:30
|
|
|
if err != nil {
|
2020-05-17 18:13:29 +05:30
|
|
|
ctx.ServerError("SettingsTwoFactor: Failed NewKeyFromURL: ", err)
|
2019-06-13 01:11:28 +05:30
|
|
|
return false
|
|
|
|
}
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
2019-05-27 22:30:32 +05:30
|
|
|
// Filter unsafe character ':' in issuer
|
2020-10-12 01:57:20 +05:30
|
|
|
issuer := strings.ReplaceAll(setting.AppName+" ("+setting.Domain+")", ":", "")
|
2018-05-17 09:35:00 +05:30
|
|
|
if otpKey == nil {
|
|
|
|
otpKey, err = totp.Generate(totp.GenerateOpts{
|
2018-07-04 03:40:35 +05:30
|
|
|
SecretSize: 40,
|
2019-05-27 22:30:32 +05:30
|
|
|
Issuer: issuer,
|
2022-03-22 12:33:22 +05:30
|
|
|
AccountName: ctx.Doer.Name,
|
2018-05-17 09:35:00 +05:30
|
|
|
})
|
|
|
|
if err != nil {
|
2020-05-17 18:13:29 +05:30
|
|
|
ctx.ServerError("SettingsTwoFactor: totpGenerate Failed", err)
|
2018-05-17 09:35:00 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["TwofaSecret"] = otpKey.Secret()
|
|
|
|
img, err := otpKey.Image(320, 240)
|
|
|
|
if err != nil {
|
2020-05-17 18:13:29 +05:30
|
|
|
ctx.ServerError("SettingsTwoFactor: otpKey image generation failed", err)
|
2018-05-17 09:35:00 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var imgBytes bytes.Buffer
|
|
|
|
if err = png.Encode(&imgBytes, img); err != nil {
|
2020-05-17 18:13:29 +05:30
|
|
|
ctx.ServerError("SettingsTwoFactor: otpKey png encoding failed", err)
|
2018-05-17 09:35:00 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["QrUri"] = template.URL("data:image/png;base64," + base64.StdEncoding.EncodeToString(imgBytes.Bytes()))
|
2020-05-17 18:13:29 +05:30
|
|
|
|
|
|
|
if err := ctx.Session.Set("twofaSecret", otpKey.Secret()); err != nil {
|
|
|
|
ctx.ServerError("SettingsTwoFactor: Failed to set session for twofaSecret", err)
|
2019-06-13 01:11:28 +05:30
|
|
|
return false
|
|
|
|
}
|
2020-05-17 18:13:29 +05:30
|
|
|
|
|
|
|
if err := ctx.Session.Set("twofaUri", otpKey.String()); err != nil {
|
|
|
|
ctx.ServerError("SettingsTwoFactor: Failed to set session for twofaUri", err)
|
2019-06-13 01:11:28 +05:30
|
|
|
return false
|
|
|
|
}
|
2020-05-17 18:13:29 +05:30
|
|
|
|
|
|
|
// Here we're just going to try to release the session early
|
|
|
|
if err := ctx.Session.Release(); err != nil {
|
|
|
|
// we'll tolerate errors here as they *should* get saved elsewhere
|
|
|
|
log.Error("Unable to save changes to the session: %v", err)
|
|
|
|
}
|
2018-05-17 09:35:00 +05:30
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnrollTwoFactor shows the page where the user can enroll into 2FA.
|
|
|
|
func EnrollTwoFactor(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsSecurity"] = true
|
|
|
|
|
2023-09-15 11:43:19 +05:30
|
|
|
t, err := auth.GetTwoFactorByUID(ctx, ctx.Doer.ID)
|
2018-05-17 09:35:00 +05:30
|
|
|
if t != nil {
|
2020-05-17 18:13:29 +05:30
|
|
|
// already enrolled - we should redirect back!
|
2022-03-22 12:33:22 +05:30
|
|
|
log.Warn("Trying to re-enroll %-v in twofa when already enrolled", ctx.Doer)
|
2021-09-25 19:57:01 +05:30
|
|
|
ctx.Flash.Error(ctx.Tr("settings.twofa_is_enrolled"))
|
2020-05-17 18:13:29 +05:30
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security")
|
2018-05-17 09:35:00 +05:30
|
|
|
return
|
|
|
|
}
|
2022-01-02 18:42:35 +05:30
|
|
|
if err != nil && !auth.IsErrTwoFactorNotEnrolled(err) {
|
2020-05-17 18:13:29 +05:30
|
|
|
ctx.ServerError("SettingsTwoFactor: GetTwoFactorByUID", err)
|
2018-05-17 09:35:00 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !twofaGenerateSecretAndQr(ctx) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplSettingsTwofaEnroll)
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// EnrollTwoFactorPost handles enrolling the user into 2FA.
|
2021-01-26 21:06:53 +05:30
|
|
|
func EnrollTwoFactorPost(ctx *context.Context) {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.TwoFactorAuthForm)
|
2018-05-17 09:35:00 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsSecurity"] = true
|
|
|
|
|
2023-09-15 11:43:19 +05:30
|
|
|
t, err := auth.GetTwoFactorByUID(ctx, ctx.Doer.ID)
|
2018-05-17 09:35:00 +05:30
|
|
|
if t != nil {
|
|
|
|
// already enrolled
|
2021-09-25 19:57:01 +05:30
|
|
|
ctx.Flash.Error(ctx.Tr("settings.twofa_is_enrolled"))
|
2020-05-17 18:13:29 +05:30
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security")
|
2018-05-17 09:35:00 +05:30
|
|
|
return
|
|
|
|
}
|
2022-01-02 18:42:35 +05:30
|
|
|
if err != nil && !auth.IsErrTwoFactorNotEnrolled(err) {
|
2020-05-17 18:13:29 +05:30
|
|
|
ctx.ServerError("SettingsTwoFactor: Failed to check if already enrolled with GetTwoFactorByUID", err)
|
2018-05-17 09:35:00 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
if !twofaGenerateSecretAndQr(ctx) {
|
|
|
|
return
|
|
|
|
}
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplSettingsTwofaEnroll)
|
2018-05-17 09:35:00 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-19 02:08:41 +05:30
|
|
|
secretRaw := ctx.Session.Get("twofaSecret")
|
|
|
|
if secretRaw == nil {
|
|
|
|
ctx.Flash.Error(ctx.Tr("settings.twofa_failed_get_secret"))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security/two_factor/enroll")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
secret := secretRaw.(string)
|
2018-05-17 09:35:00 +05:30
|
|
|
if !totp.Validate(form.Passcode, secret) {
|
|
|
|
if !twofaGenerateSecretAndQr(ctx) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Flash.Error(ctx.Tr("settings.passcode_invalid"))
|
2021-01-18 14:21:32 +05:30
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security/two_factor/enroll")
|
2018-05-17 09:35:00 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-02 18:42:35 +05:30
|
|
|
t = &auth.TwoFactor{
|
2022-03-22 12:33:22 +05:30
|
|
|
UID: ctx.Doer.ID,
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
|
|
|
err = t.SetSecret(secret)
|
|
|
|
if err != nil {
|
2020-05-17 18:13:29 +05:30
|
|
|
ctx.ServerError("SettingsTwoFactor: Failed to set secret", err)
|
2018-05-17 09:35:00 +05:30
|
|
|
return
|
|
|
|
}
|
2018-07-27 18:24:50 +05:30
|
|
|
token, err := t.GenerateScratchToken()
|
2018-05-17 09:35:00 +05:30
|
|
|
if err != nil {
|
2020-05-17 18:13:29 +05:30
|
|
|
ctx.ServerError("SettingsTwoFactor: Failed to generate scratch token", err)
|
2018-05-17 09:35:00 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-17 18:13:29 +05:30
|
|
|
// Now we have to delete the secrets - because if we fail to insert then it's highly likely that they have already been used
|
|
|
|
// If we can detect the unique constraint failure below we can move this to after the NewTwoFactor
|
|
|
|
if err := ctx.Session.Delete("twofaSecret"); err != nil {
|
|
|
|
// tolerate this failure - it's more important to continue
|
|
|
|
log.Error("Unable to delete twofaSecret from the session: Error: %v", err)
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
2020-05-17 18:13:29 +05:30
|
|
|
if err := ctx.Session.Delete("twofaUri"); err != nil {
|
|
|
|
// tolerate this failure - it's more important to continue
|
|
|
|
log.Error("Unable to delete twofaUri from the session: Error: %v", err)
|
2019-06-13 01:11:28 +05:30
|
|
|
}
|
2020-05-17 18:13:29 +05:30
|
|
|
if err := ctx.Session.Release(); err != nil {
|
|
|
|
// tolerate this failure - it's more important to continue
|
|
|
|
log.Error("Unable to save changes to the session: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-09-15 11:43:19 +05:30
|
|
|
if err = auth.NewTwoFactor(ctx, t); err != nil {
|
2020-05-17 18:13:29 +05:30
|
|
|
// FIXME: We need to handle a unique constraint fail here it's entirely possible that another request has beaten us.
|
|
|
|
// If there is a unique constraint fail we should just tolerate the error
|
|
|
|
ctx.ServerError("SettingsTwoFactor: Failed to save two factor", err)
|
2019-06-13 01:11:28 +05:30
|
|
|
return
|
|
|
|
}
|
2020-05-17 18:13:29 +05:30
|
|
|
|
2018-07-27 18:24:50 +05:30
|
|
|
ctx.Flash.Success(ctx.Tr("settings.twofa_enrolled", token))
|
2018-05-17 09:35:00 +05:30
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security")
|
|
|
|
}
|