You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
251 lines
7.8 KiB
251 lines
7.8 KiB
7 years ago
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
3 years ago
|
package security
|
||
7 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/base64"
|
||
|
"html/template"
|
||
|
"image/png"
|
||
4 years ago
|
"net/http"
|
||
7 years ago
|
"strings"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/auth"
|
||
7 years ago
|
"code.gitea.io/gitea/modules/context"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/log"
|
||
7 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/web"
|
||
4 years ago
|
"code.gitea.io/gitea/services/forms"
|
||
7 years ago
|
|
||
|
"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
|
||
|
|
||
3 years ago
|
t, err := auth.GetTwoFactorByUID(ctx.User.ID)
|
||
7 years ago
|
if err != nil {
|
||
3 years ago
|
if auth.IsErrTwoFactorNotEnrolled(err) {
|
||
3 years ago
|
ctx.Flash.Error(ctx.Tr("settings.twofa_not_enrolled"))
|
||
5 years ago
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security")
|
||
|
}
|
||
|
ctx.ServerError("SettingsTwoFactor: Failed to GetTwoFactorByUID", err)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
6 years ago
|
token, err := t.GenerateScratchToken()
|
||
|
if err != nil {
|
||
5 years ago
|
ctx.ServerError("SettingsTwoFactor: Failed to GenerateScratchToken", err)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
3 years ago
|
if err = auth.UpdateTwoFactor(t); err != nil {
|
||
5 years ago
|
ctx.ServerError("SettingsTwoFactor: Failed to UpdateTwoFactor", err)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
6 years ago
|
ctx.Flash.Success(ctx.Tr("settings.twofa_scratch_token_regenerated", token))
|
||
7 years ago
|
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
|
||
|
|
||
3 years ago
|
t, err := auth.GetTwoFactorByUID(ctx.User.ID)
|
||
7 years ago
|
if err != nil {
|
||
3 years ago
|
if auth.IsErrTwoFactorNotEnrolled(err) {
|
||
3 years ago
|
ctx.Flash.Error(ctx.Tr("settings.twofa_not_enrolled"))
|
||
5 years ago
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security")
|
||
|
}
|
||
|
ctx.ServerError("SettingsTwoFactor: Failed to GetTwoFactorByUID", err)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
3 years ago
|
if err = auth.DeleteTwoFactorByID(t.ID, ctx.User.ID); err != nil {
|
||
|
if auth.IsErrTwoFactorNotEnrolled(err) {
|
||
5 years ago
|
// 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)
|
||
7 years ago
|
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))
|
||
5 years ago
|
if err != nil {
|
||
5 years ago
|
ctx.ServerError("SettingsTwoFactor: Failed NewKeyFromURL: ", err)
|
||
5 years ago
|
return false
|
||
|
}
|
||
7 years ago
|
}
|
||
6 years ago
|
// Filter unsafe character ':' in issuer
|
||
4 years ago
|
issuer := strings.ReplaceAll(setting.AppName+" ("+setting.Domain+")", ":", "")
|
||
7 years ago
|
if otpKey == nil {
|
||
|
otpKey, err = totp.Generate(totp.GenerateOpts{
|
||
6 years ago
|
SecretSize: 40,
|
||
6 years ago
|
Issuer: issuer,
|
||
7 years ago
|
AccountName: ctx.User.Name,
|
||
|
})
|
||
|
if err != nil {
|
||
5 years ago
|
ctx.ServerError("SettingsTwoFactor: totpGenerate Failed", err)
|
||
7 years ago
|
return false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ctx.Data["TwofaSecret"] = otpKey.Secret()
|
||
|
img, err := otpKey.Image(320, 240)
|
||
|
if err != nil {
|
||
5 years ago
|
ctx.ServerError("SettingsTwoFactor: otpKey image generation failed", err)
|
||
7 years ago
|
return false
|
||
|
}
|
||
|
|
||
|
var imgBytes bytes.Buffer
|
||
|
if err = png.Encode(&imgBytes, img); err != nil {
|
||
5 years ago
|
ctx.ServerError("SettingsTwoFactor: otpKey png encoding failed", err)
|
||
7 years ago
|
return false
|
||
|
}
|
||
|
|
||
|
ctx.Data["QrUri"] = template.URL("data:image/png;base64," + base64.StdEncoding.EncodeToString(imgBytes.Bytes()))
|
||
5 years ago
|
|
||
|
if err := ctx.Session.Set("twofaSecret", otpKey.Secret()); err != nil {
|
||
|
ctx.ServerError("SettingsTwoFactor: Failed to set session for twofaSecret", err)
|
||
5 years ago
|
return false
|
||
|
}
|
||
5 years ago
|
|
||
|
if err := ctx.Session.Set("twofaUri", otpKey.String()); err != nil {
|
||
|
ctx.ServerError("SettingsTwoFactor: Failed to set session for twofaUri", err)
|
||
5 years ago
|
return false
|
||
|
}
|
||
5 years ago
|
|
||
|
// 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)
|
||
|
}
|
||
7 years ago
|
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
|
||
|
|
||
3 years ago
|
t, err := auth.GetTwoFactorByUID(ctx.User.ID)
|
||
7 years ago
|
if t != nil {
|
||
5 years ago
|
// already enrolled - we should redirect back!
|
||
|
log.Warn("Trying to re-enroll %-v in twofa when already enrolled", ctx.User)
|
||
3 years ago
|
ctx.Flash.Error(ctx.Tr("settings.twofa_is_enrolled"))
|
||
5 years ago
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security")
|
||
7 years ago
|
return
|
||
|
}
|
||
3 years ago
|
if err != nil && !auth.IsErrTwoFactorNotEnrolled(err) {
|
||
5 years ago
|
ctx.ServerError("SettingsTwoFactor: GetTwoFactorByUID", err)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if !twofaGenerateSecretAndQr(ctx) {
|
||
|
return
|
||
|
}
|
||
|
|
||
4 years ago
|
ctx.HTML(http.StatusOK, tplSettingsTwofaEnroll)
|
||
7 years ago
|
}
|
||
|
|
||
|
// EnrollTwoFactorPost handles enrolling the user into 2FA.
|
||
4 years ago
|
func EnrollTwoFactorPost(ctx *context.Context) {
|
||
4 years ago
|
form := web.GetForm(ctx).(*forms.TwoFactorAuthForm)
|
||
7 years ago
|
ctx.Data["Title"] = ctx.Tr("settings")
|
||
|
ctx.Data["PageIsSettingsSecurity"] = true
|
||
|
|
||
3 years ago
|
t, err := auth.GetTwoFactorByUID(ctx.User.ID)
|
||
7 years ago
|
if t != nil {
|
||
|
// already enrolled
|
||
3 years ago
|
ctx.Flash.Error(ctx.Tr("settings.twofa_is_enrolled"))
|
||
5 years ago
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security")
|
||
7 years ago
|
return
|
||
|
}
|
||
3 years ago
|
if err != nil && !auth.IsErrTwoFactorNotEnrolled(err) {
|
||
5 years ago
|
ctx.ServerError("SettingsTwoFactor: Failed to check if already enrolled with GetTwoFactorByUID", err)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if ctx.HasError() {
|
||
|
if !twofaGenerateSecretAndQr(ctx) {
|
||
|
return
|
||
|
}
|
||
4 years ago
|
ctx.HTML(http.StatusOK, tplSettingsTwofaEnroll)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
4 years ago
|
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)
|
||
7 years ago
|
if !totp.Validate(form.Passcode, secret) {
|
||
|
if !twofaGenerateSecretAndQr(ctx) {
|
||
|
return
|
||
|
}
|
||
|
ctx.Flash.Error(ctx.Tr("settings.passcode_invalid"))
|
||
4 years ago
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security/two_factor/enroll")
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
3 years ago
|
t = &auth.TwoFactor{
|
||
7 years ago
|
UID: ctx.User.ID,
|
||
|
}
|
||
|
err = t.SetSecret(secret)
|
||
|
if err != nil {
|
||
5 years ago
|
ctx.ServerError("SettingsTwoFactor: Failed to set secret", err)
|
||
7 years ago
|
return
|
||
|
}
|
||
6 years ago
|
token, err := t.GenerateScratchToken()
|
||
7 years ago
|
if err != nil {
|
||
5 years ago
|
ctx.ServerError("SettingsTwoFactor: Failed to generate scratch token", err)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
5 years ago
|
// 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)
|
||
7 years ago
|
}
|
||
5 years ago
|
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)
|
||
5 years ago
|
}
|
||
5 years ago
|
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)
|
||
|
}
|
||
|
|
||
3 years ago
|
if err = auth.NewTwoFactor(t); err != nil {
|
||
5 years ago
|
// 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)
|
||
5 years ago
|
return
|
||
|
}
|
||
5 years ago
|
|
||
6 years ago
|
ctx.Flash.Success(ctx.Tr("settings.twofa_enrolled", token))
|
||
7 years ago
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security")
|
||
|
}
|