2018-05-16 22:05:00 -06:00
|
|
|
// 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.
|
|
|
|
|
2022-01-02 06:12:35 -07:00
|
|
|
package security
|
2018-05-16 22:05:00 -06:00
|
|
|
|
|
|
|
import (
|
2021-04-05 09:30:52 -06:00
|
|
|
"net/http"
|
|
|
|
|
2018-05-16 22:05:00 -06:00
|
|
|
"code.gitea.io/gitea/models"
|
2022-01-02 06:12:35 -07:00
|
|
|
"code.gitea.io/gitea/models/auth"
|
2021-11-17 02:58:31 -07:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2018-05-16 22:05:00 -06:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-01-02 06:12:35 -07:00
|
|
|
tplSettingsSecurity base.TplName = "user/settings/security/security"
|
|
|
|
tplSettingsTwofaEnroll base.TplName = "user/settings/security/twofa_enroll"
|
2018-05-16 22:05:00 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Security render change user's password page and 2FA
|
|
|
|
func Security(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsSecurity"] = true
|
2021-01-20 14:17:46 -07:00
|
|
|
ctx.Data["RequireU2F"] = true
|
2018-05-16 22:05:00 -06:00
|
|
|
|
2021-08-10 18:31:13 -06:00
|
|
|
if ctx.FormString("openid.return_to") != "" {
|
2018-06-18 12:24:45 -06:00
|
|
|
settingsOpenIDVerify(ctx)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
loadSecurityData(ctx)
|
|
|
|
|
2021-04-05 09:30:52 -06:00
|
|
|
ctx.HTML(http.StatusOK, tplSettingsSecurity)
|
2018-06-18 12:24:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAccountLink delete a single account link
|
|
|
|
func DeleteAccountLink(ctx *context.Context) {
|
2021-07-28 19:42:15 -06:00
|
|
|
id := ctx.FormInt64("id")
|
2019-02-06 23:51:23 -07:00
|
|
|
if id <= 0 {
|
|
|
|
ctx.Flash.Error("Account link id is not given")
|
2018-06-18 12:24:45 -06:00
|
|
|
} else {
|
2022-03-22 01:03:22 -06:00
|
|
|
if _, err := user_model.RemoveAccountLink(ctx.Doer, id); err != nil {
|
2019-02-06 23:51:23 -07:00
|
|
|
ctx.Flash.Error("RemoveAccountLink: " + err.Error())
|
|
|
|
} else {
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.remove_account_link_success"))
|
|
|
|
}
|
2018-06-18 12:24:45 -06:00
|
|
|
}
|
|
|
|
|
2021-04-05 09:30:52 -06:00
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
2018-06-18 12:24:45 -06:00
|
|
|
"redirect": setting.AppSubURL + "/user/settings/security",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadSecurityData(ctx *context.Context) {
|
2022-03-22 01:03:22 -06:00
|
|
|
enrolled, err := auth.HasTwoFactorByUID(ctx.Doer.ID)
|
2018-05-16 22:05:00 -06:00
|
|
|
if err != nil {
|
2021-11-08 15:47:19 -07:00
|
|
|
ctx.ServerError("SettingsTwoFactor", err)
|
|
|
|
return
|
2018-05-16 22:05:00 -06:00
|
|
|
}
|
2021-11-08 15:47:19 -07:00
|
|
|
ctx.Data["TOTPEnrolled"] = enrolled
|
|
|
|
|
2022-03-22 01:03:22 -06:00
|
|
|
credentials, err := auth.GetWebAuthnCredentialsByUID(ctx.Doer.ID)
|
2021-11-08 15:47:19 -07:00
|
|
|
if err != nil {
|
2022-01-14 08:03:31 -07:00
|
|
|
ctx.ServerError("GetWebAuthnCredentialsByUID", err)
|
2021-11-08 15:47:19 -07:00
|
|
|
return
|
2018-05-19 08:12:37 -06:00
|
|
|
}
|
2022-01-14 08:03:31 -07:00
|
|
|
ctx.Data["WebAuthnCredentials"] = credentials
|
2018-05-16 22:05:00 -06:00
|
|
|
|
2022-03-22 01:03:22 -06:00
|
|
|
tokens, err := models.ListAccessTokens(models.ListAccessTokensOptions{UserID: ctx.Doer.ID})
|
2018-05-16 22:05:00 -06:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("ListAccessTokens", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Tokens"] = tokens
|
|
|
|
|
2022-03-22 01:03:22 -06:00
|
|
|
accountLinks, err := user_model.ListAccountLinks(ctx.Doer)
|
2018-05-16 22:05:00 -06:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("ListAccountLinks", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-02 06:12:35 -07:00
|
|
|
// map the provider display name with the AuthSource
|
|
|
|
sources := make(map[*auth.Source]string)
|
2018-05-16 22:05:00 -06:00
|
|
|
for _, externalAccount := range accountLinks {
|
2022-01-02 06:12:35 -07:00
|
|
|
if authSource, err := auth.GetSourceByID(externalAccount.LoginSourceID); err == nil {
|
2018-05-16 22:05:00 -06:00
|
|
|
var providerDisplayName string
|
2021-08-05 19:11:08 -06:00
|
|
|
|
|
|
|
type DisplayNamed interface {
|
|
|
|
DisplayName() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Named interface {
|
|
|
|
Name() string
|
|
|
|
}
|
|
|
|
|
2022-01-02 06:12:35 -07:00
|
|
|
if displayNamed, ok := authSource.Cfg.(DisplayNamed); ok {
|
2021-08-05 19:11:08 -06:00
|
|
|
providerDisplayName = displayNamed.DisplayName()
|
2022-01-02 06:12:35 -07:00
|
|
|
} else if named, ok := authSource.Cfg.(Named); ok {
|
2021-08-05 19:11:08 -06:00
|
|
|
providerDisplayName = named.Name()
|
2018-05-16 22:05:00 -06:00
|
|
|
} else {
|
2022-01-02 06:12:35 -07:00
|
|
|
providerDisplayName = authSource.Name
|
2018-05-16 22:05:00 -06:00
|
|
|
}
|
2022-01-02 06:12:35 -07:00
|
|
|
sources[authSource] = providerDisplayName
|
2018-05-16 22:05:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx.Data["AccountLinks"] = sources
|
|
|
|
|
2022-03-22 01:03:22 -06:00
|
|
|
openid, err := user_model.GetUserOpenIDs(ctx.Doer.ID)
|
2018-05-16 22:05:00 -06:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetUserOpenIDs", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["OpenIDs"] = openid
|
|
|
|
}
|