2022-01-02 18:42:35 +05:30
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2022-01-02 18:42:35 +05:30
|
|
|
|
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/auth"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
2023-07-04 04:09:38 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2022-01-02 18:42:35 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2023-07-04 04:09:38 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2022-01-02 18:42:35 +05:30
|
|
|
"code.gitea.io/gitea/modules/web"
|
|
|
|
auth_service "code.gitea.io/gitea/services/auth"
|
2023-02-08 12:14:42 +05:30
|
|
|
"code.gitea.io/gitea/services/auth/source/oauth2"
|
2024-02-27 12:42:22 +05:30
|
|
|
"code.gitea.io/gitea/services/context"
|
2022-01-02 18:42:35 +05:30
|
|
|
"code.gitea.io/gitea/services/externalaccount"
|
|
|
|
"code.gitea.io/gitea/services/forms"
|
|
|
|
|
|
|
|
"github.com/markbates/goth"
|
|
|
|
)
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
var tplLinkAccount base.TplName = "user/auth/link_account"
|
2022-01-02 18:42:35 +05:30
|
|
|
|
|
|
|
// LinkAccount shows the page where the user can decide to login or create a new account
|
|
|
|
func LinkAccount(ctx *context.Context) {
|
|
|
|
ctx.Data["DisablePassword"] = !setting.Service.RequireExternalRegistrationPassword || setting.Service.AllowOnlyExternalRegistration
|
|
|
|
ctx.Data["Title"] = ctx.Tr("link_account")
|
|
|
|
ctx.Data["LinkAccountMode"] = true
|
|
|
|
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha && setting.Service.RequireExternalRegistrationCaptcha
|
|
|
|
ctx.Data["Captcha"] = context.GetImageCaptcha()
|
|
|
|
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
|
|
|
|
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
|
|
|
|
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
|
|
|
|
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
|
2022-08-10 18:50:10 +05:30
|
|
|
ctx.Data["McaptchaSitekey"] = setting.Service.McaptchaSitekey
|
|
|
|
ctx.Data["McaptchaURL"] = setting.Service.McaptchaURL
|
2022-01-02 18:42:35 +05:30
|
|
|
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
|
|
|
|
ctx.Data["AllowOnlyInternalRegistration"] = setting.Service.AllowOnlyInternalRegistration
|
|
|
|
ctx.Data["ShowRegistrationButton"] = false
|
|
|
|
|
|
|
|
// use this to set the right link into the signIn and signUp templates in the link_account template
|
|
|
|
ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
|
|
|
|
ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
|
|
|
|
|
|
|
|
gothUser := ctx.Session.Get("linkAccountGothUser")
|
|
|
|
if gothUser == nil {
|
|
|
|
ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
gu, _ := gothUser.(goth.User)
|
2024-01-04 06:18:20 +05:30
|
|
|
uname, err := getUserName(&gu)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("UserSignIn", err)
|
|
|
|
return
|
|
|
|
}
|
2022-01-02 18:42:35 +05:30
|
|
|
email := gu.Email
|
|
|
|
ctx.Data["user_name"] = uname
|
|
|
|
ctx.Data["email"] = email
|
|
|
|
|
|
|
|
if len(email) != 0 {
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 19:07:34 +05:30
|
|
|
u, err := user_model.GetUserByEmail(ctx, email)
|
2022-01-02 18:42:35 +05:30
|
|
|
if err != nil && !user_model.IsErrUserNotExist(err) {
|
|
|
|
ctx.ServerError("UserSignIn", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if u != nil {
|
|
|
|
ctx.Data["user_exists"] = true
|
|
|
|
}
|
|
|
|
} else if len(uname) != 0 {
|
2022-05-20 19:38:52 +05:30
|
|
|
u, err := user_model.GetUserByName(ctx, uname)
|
2022-01-02 18:42:35 +05:30
|
|
|
if err != nil && !user_model.IsErrUserNotExist(err) {
|
|
|
|
ctx.ServerError("UserSignIn", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if u != nil {
|
|
|
|
ctx.Data["user_exists"] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.HTML(http.StatusOK, tplLinkAccount)
|
|
|
|
}
|
|
|
|
|
2023-07-04 04:09:38 +05:30
|
|
|
func handleSignInError(ctx *context.Context, userName string, ptrForm any, tmpl base.TplName, invoker string, err error) {
|
|
|
|
if errors.Is(err, util.ErrNotExist) {
|
|
|
|
ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tmpl, ptrForm)
|
|
|
|
} else if errors.Is(err, util.ErrInvalidArgument) {
|
|
|
|
ctx.Data["user_exists"] = true
|
|
|
|
ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tmpl, ptrForm)
|
|
|
|
} else if user_model.IsErrUserProhibitLogin(err) {
|
|
|
|
ctx.Data["user_exists"] = true
|
|
|
|
log.Info("Failed authentication attempt for %s from %s: %v", userName, ctx.RemoteAddr(), err)
|
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
|
|
|
|
ctx.HTML(http.StatusOK, "user/auth/prohibit_login")
|
|
|
|
} else if user_model.IsErrUserInactive(err) {
|
|
|
|
ctx.Data["user_exists"] = true
|
|
|
|
if setting.Service.RegisterEmailConfirm {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.active_your_account")
|
|
|
|
ctx.HTML(http.StatusOK, TplActivate)
|
|
|
|
} else {
|
|
|
|
log.Info("Failed authentication attempt for %s from %s: %v", userName, ctx.RemoteAddr(), err)
|
|
|
|
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
|
|
|
|
ctx.HTML(http.StatusOK, "user/auth/prohibit_login")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.ServerError(invoker, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 18:42:35 +05:30
|
|
|
// LinkAccountPostSignIn handle the coupling of external account with another account using signIn
|
|
|
|
func LinkAccountPostSignIn(ctx *context.Context) {
|
|
|
|
signInForm := web.GetForm(ctx).(*forms.SignInForm)
|
|
|
|
ctx.Data["DisablePassword"] = !setting.Service.RequireExternalRegistrationPassword || setting.Service.AllowOnlyExternalRegistration
|
|
|
|
ctx.Data["Title"] = ctx.Tr("link_account")
|
|
|
|
ctx.Data["LinkAccountMode"] = true
|
|
|
|
ctx.Data["LinkAccountModeSignIn"] = true
|
|
|
|
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha && setting.Service.RequireExternalRegistrationCaptcha
|
|
|
|
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
|
|
|
|
ctx.Data["Captcha"] = context.GetImageCaptcha()
|
|
|
|
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
|
|
|
|
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
|
|
|
|
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
|
2022-08-10 18:50:10 +05:30
|
|
|
ctx.Data["McaptchaSitekey"] = setting.Service.McaptchaSitekey
|
|
|
|
ctx.Data["McaptchaURL"] = setting.Service.McaptchaURL
|
2022-01-02 18:42:35 +05:30
|
|
|
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
|
|
|
|
ctx.Data["ShowRegistrationButton"] = false
|
|
|
|
|
|
|
|
// use this to set the right link into the signIn and signUp templates in the link_account template
|
|
|
|
ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
|
|
|
|
ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
|
|
|
|
|
|
|
|
gothUser := ctx.Session.Get("linkAccountGothUser")
|
|
|
|
if gothUser == nil {
|
|
|
|
ctx.ServerError("UserSignIn", errors.New("not in LinkAccount session"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(http.StatusOK, tplLinkAccount)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-14 22:39:32 +05:30
|
|
|
u, _, err := auth_service.UserSignIn(ctx, signInForm.UserName, signInForm.Password)
|
2022-01-02 18:42:35 +05:30
|
|
|
if err != nil {
|
2023-07-04 04:09:38 +05:30
|
|
|
handleSignInError(ctx, signInForm.UserName, &signInForm, tplLinkAccount, "UserLinkAccount", err)
|
2022-01-02 18:42:35 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
linkAccount(ctx, u, gothUser.(goth.User), signInForm.Remember)
|
|
|
|
}
|
|
|
|
|
|
|
|
func linkAccount(ctx *context.Context, u *user_model.User, gothUser goth.User, remember bool) {
|
2023-10-11 09:54:07 +05:30
|
|
|
updateAvatarIfNeed(ctx, gothUser.AvatarURL, u)
|
2022-01-02 18:42:35 +05:30
|
|
|
|
|
|
|
// If this user is enrolled in 2FA, we can't sign the user in just yet.
|
|
|
|
// Instead, redirect them to the 2FA authentication page.
|
|
|
|
// We deliberately ignore the skip local 2fa setting here because we are linking to a previous user here
|
2023-09-15 11:43:19 +05:30
|
|
|
_, err := auth.GetTwoFactorByUID(ctx, u.ID)
|
2022-01-02 18:42:35 +05:30
|
|
|
if err != nil {
|
|
|
|
if !auth.IsErrTwoFactorNotEnrolled(err) {
|
|
|
|
ctx.ServerError("UserLinkAccount", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-25 18:47:37 +05:30
|
|
|
err = externalaccount.LinkAccountToUser(ctx, u, gothUser)
|
2022-01-02 18:42:35 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("UserLinkAccount", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSignIn(ctx, u, remember)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-05 00:06:08 +05:30
|
|
|
if err := updateSession(ctx, nil, map[string]any{
|
2022-11-10 17:13:06 +05:30
|
|
|
// User needs to use 2FA, save data and redirect to 2FA page.
|
|
|
|
"twofaUid": u.ID,
|
|
|
|
"twofaRemember": remember,
|
|
|
|
"linkAccount": true,
|
|
|
|
}); err != nil {
|
2022-01-02 18:42:35 +05:30
|
|
|
ctx.ServerError("RegenerateSession", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-14 20:33:31 +05:30
|
|
|
// If WebAuthn is enrolled -> Redirect to WebAuthn instead
|
2023-09-16 20:09:12 +05:30
|
|
|
regs, err := auth.GetWebAuthnCredentialsByUID(ctx, u.ID)
|
2022-01-02 18:42:35 +05:30
|
|
|
if err == nil && len(regs) > 0 {
|
2022-01-14 20:33:31 +05:30
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/webauthn")
|
2022-01-02 18:42:35 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/two_factor")
|
|
|
|
}
|
|
|
|
|
|
|
|
// LinkAccountPostRegister handle the creation of a new account for an external account using signUp
|
|
|
|
func LinkAccountPostRegister(ctx *context.Context) {
|
|
|
|
form := web.GetForm(ctx).(*forms.RegisterForm)
|
|
|
|
// TODO Make insecure passwords optional for local accounts also,
|
|
|
|
// once email-based Second-Factor Auth is available
|
|
|
|
ctx.Data["DisablePassword"] = !setting.Service.RequireExternalRegistrationPassword || setting.Service.AllowOnlyExternalRegistration
|
|
|
|
ctx.Data["Title"] = ctx.Tr("link_account")
|
|
|
|
ctx.Data["LinkAccountMode"] = true
|
|
|
|
ctx.Data["LinkAccountModeRegister"] = true
|
|
|
|
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha && setting.Service.RequireExternalRegistrationCaptcha
|
|
|
|
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
|
|
|
|
ctx.Data["Captcha"] = context.GetImageCaptcha()
|
|
|
|
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
|
|
|
|
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
|
|
|
|
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
|
2022-08-10 18:50:10 +05:30
|
|
|
ctx.Data["McaptchaSitekey"] = setting.Service.McaptchaSitekey
|
|
|
|
ctx.Data["McaptchaURL"] = setting.Service.McaptchaURL
|
2022-01-02 18:42:35 +05:30
|
|
|
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
|
|
|
|
ctx.Data["ShowRegistrationButton"] = false
|
|
|
|
|
|
|
|
// use this to set the right link into the signIn and signUp templates in the link_account template
|
|
|
|
ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
|
|
|
|
ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
|
|
|
|
|
|
|
|
gothUserInterface := ctx.Session.Get("linkAccountGothUser")
|
|
|
|
if gothUserInterface == nil {
|
|
|
|
ctx.ServerError("UserSignUp", errors.New("not in LinkAccount session"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
gothUser, ok := gothUserInterface.(goth.User)
|
|
|
|
if !ok {
|
|
|
|
ctx.ServerError("UserSignUp", fmt.Errorf("session linkAccountGothUser type is %t but not goth.User", gothUserInterface))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(http.StatusOK, tplLinkAccount)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if setting.Service.DisableRegistration || setting.Service.AllowOnlyInternalRegistration {
|
|
|
|
ctx.Error(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if setting.Service.EnableCaptcha && setting.Service.RequireExternalRegistrationCaptcha {
|
2022-11-23 02:43:18 +05:30
|
|
|
context.VerifyCaptcha(ctx, tplLinkAccount, form)
|
|
|
|
if ctx.Written() {
|
2022-01-02 18:42:35 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !form.IsEmailDomainAllowed() {
|
|
|
|
ctx.RenderWithErr(ctx.Tr("auth.email_domain_blacklisted"), tplLinkAccount, &form)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if setting.Service.AllowOnlyExternalRegistration || !setting.Service.RequireExternalRegistrationPassword {
|
|
|
|
// In user_model.User an empty password is classed as not set, so we set form.Password to empty.
|
|
|
|
// Eventually the database should be changed to indicate "Second Factor"-enabled accounts
|
|
|
|
// (accounts that do not introduce the security vulnerabilities of a password).
|
|
|
|
// If a user decides to circumvent second-factor security, and purposefully create a password,
|
|
|
|
// they can still do so using the "Recover Account" option.
|
|
|
|
form.Password = ""
|
|
|
|
} else {
|
|
|
|
if (len(strings.TrimSpace(form.Password)) > 0 || len(strings.TrimSpace(form.Retype)) > 0) && form.Password != form.Retype {
|
|
|
|
ctx.Data["Err_Password"] = true
|
|
|
|
ctx.RenderWithErr(ctx.Tr("form.password_not_match"), tplLinkAccount, &form)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(strings.TrimSpace(form.Password)) > 0 && len(form.Password) < setting.MinPasswordLength {
|
|
|
|
ctx.Data["Err_Password"] = true
|
|
|
|
ctx.RenderWithErr(ctx.Tr("auth.password_too_short", setting.MinPasswordLength), tplLinkAccount, &form)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-14 14:07:24 +05:30
|
|
|
authSource, err := auth.GetActiveOAuth2SourceByName(ctx, gothUser.Provider)
|
2022-01-02 18:42:35 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("CreateUser", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
u := &user_model.User{
|
|
|
|
Name: form.UserName,
|
|
|
|
Email: form.Email,
|
|
|
|
Passwd: form.Password,
|
|
|
|
LoginType: auth.OAuth2,
|
|
|
|
LoginSource: authSource.ID,
|
|
|
|
LoginName: gothUser.UserID,
|
|
|
|
}
|
|
|
|
|
2022-04-30 01:08:11 +05:30
|
|
|
if !createAndHandleCreatedUser(ctx, tplLinkAccount, form, u, nil, &gothUser, false) {
|
2022-01-02 18:42:35 +05:30
|
|
|
// error already handled
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-08 12:14:42 +05:30
|
|
|
source := authSource.Cfg.(*oauth2.Source)
|
|
|
|
if err := syncGroupsToTeams(ctx, source, &gothUser, u); err != nil {
|
|
|
|
ctx.ServerError("SyncGroupsToTeams", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-02 18:42:35 +05:30
|
|
|
handleSignIn(ctx, u, false)
|
|
|
|
}
|