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