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