2017-03-17 14:16:08 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-03-17 14:16:08 +00:00
|
|
|
|
2022-01-02 13:12:35 +00:00
|
|
|
package auth
|
2017-03-17 14:16:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-05 15:30:52 +00:00
|
|
|
"net/http"
|
2017-03-17 14:16:08 +00:00
|
|
|
"net/url"
|
|
|
|
|
2021-11-17 09:58:31 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2017-03-17 14:16:08 +00:00
|
|
|
"code.gitea.io/gitea/modules/auth/openid"
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-05-10 06:45:17 +00:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 15:36:53 +00:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-03-07 08:12:43 +00:00
|
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
2021-07-24 10:16:34 +00:00
|
|
|
"code.gitea.io/gitea/services/auth"
|
2021-04-06 19:44:05 +00:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2017-03-17 14:16:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tplSignInOpenID base.TplName = "user/auth/signin_openid"
|
|
|
|
tplConnectOID base.TplName = "user/auth/signup_openid_connect"
|
|
|
|
tplSignUpOID base.TplName = "user/auth/signup_openid_register"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SignInOpenID render sign in page
|
|
|
|
func SignInOpenID(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("sign_in")
|
|
|
|
|
2021-08-11 00:31:13 +00:00
|
|
|
if ctx.FormString("openid.return_to") != "" {
|
2017-03-17 14:16:08 +00:00
|
|
|
signInOpenIDVerify(ctx)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check auto-login.
|
|
|
|
isSucceed, err := AutoSignIn(ctx)
|
|
|
|
if err != nil {
|
2018-01-10 21:34:17 +00:00
|
|
|
ctx.ServerError("AutoSignIn", err)
|
2017-03-17 14:16:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-11 00:31:13 +00:00
|
|
|
redirectTo := ctx.FormString("redirect_to")
|
2017-03-17 14:16:08 +00:00
|
|
|
if len(redirectTo) > 0 {
|
2021-03-07 08:12:43 +00:00
|
|
|
middleware.SetRedirectToCookie(ctx.Resp, redirectTo)
|
2017-03-17 14:16:08 +00:00
|
|
|
} else {
|
2023-04-13 19:45:33 +00:00
|
|
|
redirectTo = ctx.GetSiteCookie("redirect_to")
|
2017-03-17 14:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if isSucceed {
|
2021-03-07 08:12:43 +00:00
|
|
|
middleware.DeleteRedirectToCookie(ctx.Resp)
|
2018-03-15 21:13:34 +00:00
|
|
|
ctx.RedirectToFirst(redirectTo)
|
2017-03-17 14:16:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["PageIsSignIn"] = true
|
|
|
|
ctx.Data["PageIsLoginOpenID"] = true
|
2021-04-05 15:30:52 +00:00
|
|
|
ctx.HTML(http.StatusOK, tplSignInOpenID)
|
2017-03-17 14:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the given OpenID URI is allowed by blacklist/whitelist
|
|
|
|
func allowedOpenIDURI(uri string) (err error) {
|
|
|
|
// In case a Whitelist is present, URI must be in it
|
|
|
|
// in order to be accepted
|
2017-03-29 10:57:43 +00:00
|
|
|
if len(setting.Service.OpenIDWhitelist) != 0 {
|
|
|
|
for _, pat := range setting.Service.OpenIDWhitelist {
|
2017-03-17 14:16:08 +00:00
|
|
|
if pat.MatchString(uri) {
|
|
|
|
return nil // pass
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// must match one of this or be refused
|
|
|
|
return fmt.Errorf("URI not allowed by whitelist")
|
|
|
|
}
|
|
|
|
|
|
|
|
// A blacklist match expliclty forbids
|
2017-03-29 10:57:43 +00:00
|
|
|
for _, pat := range setting.Service.OpenIDBlacklist {
|
2017-03-17 14:16:08 +00:00
|
|
|
if pat.MatchString(uri) {
|
|
|
|
return fmt.Errorf("URI forbidden by blacklist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignInOpenIDPost response for openid sign in request
|
2021-01-26 15:36:53 +00:00
|
|
|
func SignInOpenIDPost(ctx *context.Context) {
|
2021-04-06 19:44:05 +00:00
|
|
|
form := web.GetForm(ctx).(*forms.SignInOpenIDForm)
|
2017-03-17 14:16:08 +00:00
|
|
|
ctx.Data["Title"] = ctx.Tr("sign_in")
|
|
|
|
ctx.Data["PageIsSignIn"] = true
|
|
|
|
ctx.Data["PageIsLoginOpenID"] = true
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2021-04-05 15:30:52 +00:00
|
|
|
ctx.HTML(http.StatusOK, tplSignInOpenID)
|
2017-03-17 14:16:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := openid.Normalize(form.Openid)
|
|
|
|
if err != nil {
|
|
|
|
ctx.RenderWithErr(err.Error(), tplSignInOpenID, &form)
|
2017-03-21 00:55:00 +00:00
|
|
|
return
|
2017-03-17 14:16:08 +00:00
|
|
|
}
|
|
|
|
form.Openid = id
|
|
|
|
|
|
|
|
log.Trace("OpenID uri: " + id)
|
|
|
|
|
2017-03-21 00:55:00 +00:00
|
|
|
err = allowedOpenIDURI(id)
|
|
|
|
if err != nil {
|
2017-03-17 14:16:08 +00:00
|
|
|
ctx.RenderWithErr(err.Error(), tplSignInOpenID, &form)
|
2017-03-21 00:55:00 +00:00
|
|
|
return
|
2017-03-17 14:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
redirectTo := setting.AppURL + "user/login/openid"
|
|
|
|
url, err := openid.RedirectURL(id, redirectTo, setting.AppURL)
|
2017-03-21 00:55:00 +00:00
|
|
|
if err != nil {
|
2019-04-02 07:48:31 +00:00
|
|
|
log.Error("Error in OpenID redirect URL: %s, %v", redirectTo, err.Error())
|
2019-01-12 19:24:47 +00:00
|
|
|
ctx.RenderWithErr(fmt.Sprintf("Unable to find OpenID provider in %s", redirectTo), tplSignInOpenID, &form)
|
2017-03-21 00:55:00 +00:00
|
|
|
return
|
|
|
|
}
|
2017-03-17 14:16:08 +00:00
|
|
|
|
|
|
|
// Request optional nickname and email info
|
|
|
|
// NOTE: change to `openid.sreg.required` to require it
|
|
|
|
url += "&openid.ns.sreg=http%3A%2F%2Fopenid.net%2Fextensions%2Fsreg%2F1.1"
|
|
|
|
url += "&openid.sreg.optional=nickname%2Cemail"
|
|
|
|
|
2019-04-02 07:48:31 +00:00
|
|
|
log.Trace("Form-passed openid-remember: %t", form.Remember)
|
2020-05-17 12:43:29 +00:00
|
|
|
|
|
|
|
if err := ctx.Session.Set("openid_signin_remember", form.Remember); err != nil {
|
|
|
|
log.Error("SignInOpenIDPost: Could not set openid_signin_remember in session: %v", err)
|
|
|
|
}
|
|
|
|
if err := ctx.Session.Release(); err != nil {
|
|
|
|
log.Error("SignInOpenIDPost: Unable to save changes to the session: %v", err)
|
2019-06-12 19:41:28 +00:00
|
|
|
}
|
2017-03-17 14:16:08 +00:00
|
|
|
|
|
|
|
ctx.Redirect(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
// signInOpenIDVerify handles response from OpenID provider
|
|
|
|
func signInOpenIDVerify(ctx *context.Context) {
|
2021-12-14 08:37:11 +00:00
|
|
|
log.Trace("Incoming call to: %s", ctx.Req.URL.String())
|
2017-03-17 14:16:08 +00:00
|
|
|
|
2021-01-26 15:36:53 +00:00
|
|
|
fullURL := setting.AppURL + ctx.Req.URL.String()[1:]
|
2021-12-14 08:37:11 +00:00
|
|
|
log.Trace("Full URL: %s", fullURL)
|
2017-03-17 14:16:08 +00:00
|
|
|
|
2022-01-20 17:46:10 +00:00
|
|
|
id, err := openid.Verify(fullURL)
|
2017-03-17 14:16:08 +00:00
|
|
|
if err != nil {
|
2021-04-06 19:44:05 +00:00
|
|
|
ctx.RenderWithErr(err.Error(), tplSignInOpenID, &forms.SignInOpenIDForm{
|
2017-03-17 14:16:08 +00:00
|
|
|
Openid: id,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-14 08:37:11 +00:00
|
|
|
log.Trace("Verified ID: %s", id)
|
2017-03-17 14:16:08 +00:00
|
|
|
|
|
|
|
/* Now we should seek for the user and log him in, or prompt
|
|
|
|
* to register if not found */
|
|
|
|
|
2023-09-14 17:09:32 +00:00
|
|
|
u, err := user_model.GetUserByOpenID(ctx, id)
|
2017-03-17 14:16:08 +00:00
|
|
|
if err != nil {
|
2021-11-24 09:49:20 +00:00
|
|
|
if !user_model.IsErrUserNotExist(err) {
|
2021-04-06 19:44:05 +00:00
|
|
|
ctx.RenderWithErr(err.Error(), tplSignInOpenID, &forms.SignInOpenIDForm{
|
2017-03-17 14:16:08 +00:00
|
|
|
Openid: id,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2019-06-12 19:41:28 +00:00
|
|
|
log.Error("signInOpenIDVerify: %v", err)
|
2017-03-17 14:16:08 +00:00
|
|
|
}
|
|
|
|
if u != nil {
|
|
|
|
log.Trace("User exists, logging in")
|
|
|
|
remember, _ := ctx.Session.Get("openid_signin_remember").(bool)
|
2019-04-02 07:48:31 +00:00
|
|
|
log.Trace("Session stored openid-remember: %t", remember)
|
2017-03-17 14:16:08 +00:00
|
|
|
handleSignIn(ctx, u, remember)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-14 08:37:11 +00:00
|
|
|
log.Trace("User with openid: %s does not exist, should connect or register", id)
|
2017-03-17 14:16:08 +00:00
|
|
|
|
|
|
|
parsedURL, err := url.Parse(fullURL)
|
|
|
|
if err != nil {
|
2021-04-06 19:44:05 +00:00
|
|
|
ctx.RenderWithErr(err.Error(), tplSignInOpenID, &forms.SignInOpenIDForm{
|
2017-03-17 14:16:08 +00:00
|
|
|
Openid: id,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
values, err := url.ParseQuery(parsedURL.RawQuery)
|
|
|
|
if err != nil {
|
2021-04-06 19:44:05 +00:00
|
|
|
ctx.RenderWithErr(err.Error(), tplSignInOpenID, &forms.SignInOpenIDForm{
|
2017-03-17 14:16:08 +00:00
|
|
|
Openid: id,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
email := values.Get("openid.sreg.email")
|
|
|
|
nickname := values.Get("openid.sreg.nickname")
|
|
|
|
|
2021-12-14 08:37:11 +00:00
|
|
|
log.Trace("User has email=%s and nickname=%s", email, nickname)
|
2017-03-17 14:16:08 +00:00
|
|
|
|
|
|
|
if email != "" {
|
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 13:37:34 +00:00
|
|
|
u, err = user_model.GetUserByEmail(ctx, email)
|
2017-03-17 14:16:08 +00:00
|
|
|
if err != nil {
|
2021-11-24 09:49:20 +00:00
|
|
|
if !user_model.IsErrUserNotExist(err) {
|
2021-04-06 19:44:05 +00:00
|
|
|
ctx.RenderWithErr(err.Error(), tplSignInOpenID, &forms.SignInOpenIDForm{
|
2017-03-17 14:16:08 +00:00
|
|
|
Openid: id,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2019-06-12 19:41:28 +00:00
|
|
|
log.Error("signInOpenIDVerify: %v", err)
|
2017-03-17 14:16:08 +00:00
|
|
|
}
|
|
|
|
if u != nil {
|
2021-12-14 08:37:11 +00:00
|
|
|
log.Trace("Local user %s has OpenID provided email %s", u.LowerName, email)
|
2017-03-17 14:16:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if u == nil && nickname != "" {
|
2022-05-20 14:08:52 +00:00
|
|
|
u, _ = user_model.GetUserByName(ctx, nickname)
|
2017-03-17 14:16:08 +00:00
|
|
|
if err != nil {
|
2021-11-24 09:49:20 +00:00
|
|
|
if !user_model.IsErrUserNotExist(err) {
|
2021-04-06 19:44:05 +00:00
|
|
|
ctx.RenderWithErr(err.Error(), tplSignInOpenID, &forms.SignInOpenIDForm{
|
2017-03-17 14:16:08 +00:00
|
|
|
Openid: id,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if u != nil {
|
2021-12-14 08:37:11 +00:00
|
|
|
log.Trace("Local user %s has OpenID provided nickname %s", u.LowerName, nickname)
|
2017-03-17 14:16:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if u != nil {
|
|
|
|
nickname = u.LowerName
|
|
|
|
}
|
2023-07-04 18:36:08 +00:00
|
|
|
if err := updateSession(ctx, nil, map[string]any{
|
2022-11-10 11:43:06 +00:00
|
|
|
"openid_verified_uri": id,
|
|
|
|
"openid_determined_email": email,
|
|
|
|
"openid_determined_username": nickname,
|
|
|
|
}); err != nil {
|
|
|
|
ctx.ServerError("updateSession", err)
|
|
|
|
return
|
2019-06-12 19:41:28 +00:00
|
|
|
}
|
2017-03-17 14:16:08 +00:00
|
|
|
|
2021-05-09 14:13:35 +00:00
|
|
|
if u != nil || !setting.Service.EnableOpenIDSignUp || setting.Service.AllowOnlyInternalRegistration {
|
2017-03-17 14:16:08 +00:00
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/openid/connect")
|
|
|
|
} else {
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/openid/register")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectOpenID shows a form to connect an OpenID URI to an existing account
|
|
|
|
func ConnectOpenID(ctx *context.Context) {
|
|
|
|
oid, _ := ctx.Session.Get("openid_verified_uri").(string)
|
|
|
|
if oid == "" {
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/login/openid")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Title"] = "OpenID connect"
|
|
|
|
ctx.Data["PageIsSignIn"] = true
|
|
|
|
ctx.Data["PageIsOpenIDConnect"] = true
|
2017-03-29 10:57:43 +00:00
|
|
|
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
|
2021-05-09 14:13:35 +00:00
|
|
|
ctx.Data["AllowOnlyInternalRegistration"] = setting.Service.AllowOnlyInternalRegistration
|
2017-03-17 14:16:08 +00:00
|
|
|
ctx.Data["OpenID"] = oid
|
|
|
|
userName, _ := ctx.Session.Get("openid_determined_username").(string)
|
|
|
|
if userName != "" {
|
|
|
|
ctx.Data["user_name"] = userName
|
|
|
|
}
|
2021-04-05 15:30:52 +00:00
|
|
|
ctx.HTML(http.StatusOK, tplConnectOID)
|
2017-03-17 14:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectOpenIDPost handles submission of a form to connect an OpenID URI to an existing account
|
2021-01-26 15:36:53 +00:00
|
|
|
func ConnectOpenIDPost(ctx *context.Context) {
|
2021-04-06 19:44:05 +00:00
|
|
|
form := web.GetForm(ctx).(*forms.ConnectOpenIDForm)
|
2017-03-17 14:16:08 +00:00
|
|
|
oid, _ := ctx.Session.Get("openid_verified_uri").(string)
|
|
|
|
if oid == "" {
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/login/openid")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Title"] = "OpenID connect"
|
|
|
|
ctx.Data["PageIsSignIn"] = true
|
|
|
|
ctx.Data["PageIsOpenIDConnect"] = true
|
2017-03-29 10:57:43 +00:00
|
|
|
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
|
2017-03-17 14:16:08 +00:00
|
|
|
ctx.Data["OpenID"] = oid
|
|
|
|
|
2023-09-14 17:09:32 +00:00
|
|
|
u, _, err := auth.UserSignIn(ctx, form.UserName, form.Password)
|
2017-03-17 14:16:08 +00:00
|
|
|
if err != nil {
|
2023-07-03 22:39:38 +00:00
|
|
|
handleSignInError(ctx, form.UserName, &form, tplConnectOID, "ConnectOpenIDPost", err)
|
2017-03-17 14:16:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add OpenID for the user
|
2021-11-17 09:58:31 +00:00
|
|
|
userOID := &user_model.UserOpenID{UID: u.ID, URI: oid}
|
2022-05-20 14:08:52 +00:00
|
|
|
if err = user_model.AddUserOpenID(ctx, userOID); err != nil {
|
2021-11-17 09:58:31 +00:00
|
|
|
if user_model.IsErrOpenIDAlreadyUsed(err) {
|
2017-03-17 14:16:08 +00:00
|
|
|
ctx.RenderWithErr(ctx.Tr("form.openid_been_used", oid), tplConnectOID, &form)
|
|
|
|
return
|
|
|
|
}
|
2018-01-10 21:34:17 +00:00
|
|
|
ctx.ServerError("AddUserOpenID", err)
|
2017-03-17 14:16:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.add_openid_success"))
|
|
|
|
|
|
|
|
remember, _ := ctx.Session.Get("openid_signin_remember").(bool)
|
2019-04-02 07:48:31 +00:00
|
|
|
log.Trace("Session stored openid-remember: %t", remember)
|
2017-03-17 14:16:08 +00:00
|
|
|
handleSignIn(ctx, u, remember)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterOpenID shows a form to create a new user authenticated via an OpenID URI
|
|
|
|
func RegisterOpenID(ctx *context.Context) {
|
|
|
|
oid, _ := ctx.Session.Get("openid_verified_uri").(string)
|
|
|
|
if oid == "" {
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/login/openid")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Title"] = "OpenID signup"
|
|
|
|
ctx.Data["PageIsSignIn"] = true
|
|
|
|
ctx.Data["PageIsOpenIDRegister"] = true
|
2017-03-29 10:57:43 +00:00
|
|
|
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
|
2021-05-09 14:13:35 +00:00
|
|
|
ctx.Data["AllowOnlyInternalRegistration"] = setting.Service.AllowOnlyInternalRegistration
|
2017-03-17 16:40:39 +00:00
|
|
|
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
|
2021-01-27 14:56:54 +00:00
|
|
|
ctx.Data["Captcha"] = context.GetImageCaptcha()
|
2018-07-05 04:13:05 +00:00
|
|
|
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
|
|
|
|
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
|
2020-10-03 03:37:53 +00:00
|
|
|
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
|
2019-05-02 13:09:39 +00:00
|
|
|
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
|
2022-08-10 13:20:10 +00:00
|
|
|
ctx.Data["McaptchaSitekey"] = setting.Service.McaptchaSitekey
|
|
|
|
ctx.Data["McaptchaURL"] = setting.Service.McaptchaURL
|
2017-03-17 14:16:08 +00:00
|
|
|
ctx.Data["OpenID"] = oid
|
|
|
|
userName, _ := ctx.Session.Get("openid_determined_username").(string)
|
|
|
|
if userName != "" {
|
|
|
|
ctx.Data["user_name"] = userName
|
|
|
|
}
|
|
|
|
email, _ := ctx.Session.Get("openid_determined_email").(string)
|
|
|
|
if email != "" {
|
|
|
|
ctx.Data["email"] = email
|
|
|
|
}
|
2021-04-05 15:30:52 +00:00
|
|
|
ctx.HTML(http.StatusOK, tplSignUpOID)
|
2017-03-17 14:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterOpenIDPost handles submission of a form to create a new user authenticated via an OpenID URI
|
2021-01-26 15:36:53 +00:00
|
|
|
func RegisterOpenIDPost(ctx *context.Context) {
|
2021-04-06 19:44:05 +00:00
|
|
|
form := web.GetForm(ctx).(*forms.SignUpOpenIDForm)
|
2017-03-17 14:16:08 +00:00
|
|
|
oid, _ := ctx.Session.Get("openid_verified_uri").(string)
|
|
|
|
if oid == "" {
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/login/openid")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["Title"] = "OpenID signup"
|
|
|
|
ctx.Data["PageIsSignIn"] = true
|
|
|
|
ctx.Data["PageIsOpenIDRegister"] = true
|
2017-03-29 10:57:43 +00:00
|
|
|
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
|
2022-11-22 21:13:18 +00:00
|
|
|
context.SetCaptchaData(ctx)
|
2017-03-17 14:16:08 +00:00
|
|
|
ctx.Data["OpenID"] = oid
|
|
|
|
|
2021-05-09 14:13:35 +00:00
|
|
|
if setting.Service.AllowOnlyInternalRegistration {
|
|
|
|
ctx.Error(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-06 19:48:02 +00:00
|
|
|
if setting.Service.EnableCaptcha {
|
2022-11-22 21:13:18 +00:00
|
|
|
if err := ctx.Req.ParseForm(); err != nil {
|
|
|
|
ctx.ServerError("", err)
|
2018-07-05 04:13:05 +00:00
|
|
|
return
|
|
|
|
}
|
2022-11-22 21:13:18 +00:00
|
|
|
context.VerifyCaptcha(ctx, tplSignUpOID, form)
|
2018-07-05 04:13:05 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 15:45:54 +00:00
|
|
|
length := setting.MinPasswordLength
|
|
|
|
if length < 256 {
|
|
|
|
length = 256
|
2017-03-21 00:55:00 +00:00
|
|
|
}
|
2022-01-26 04:10:10 +00:00
|
|
|
password, err := util.CryptoRandomString(int64(length))
|
2017-03-17 14:16:08 +00:00
|
|
|
if err != nil {
|
|
|
|
ctx.RenderWithErr(err.Error(), tplSignUpOID, form)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-24 09:49:20 +00:00
|
|
|
u := &user_model.User{
|
2022-04-29 19:38:11 +00:00
|
|
|
Name: form.UserName,
|
|
|
|
Email: form.Email,
|
|
|
|
Passwd: password,
|
2017-03-17 14:16:08 +00:00
|
|
|
}
|
2022-04-29 19:38:11 +00:00
|
|
|
if !createUserInContext(ctx, tplSignUpOID, form, u, nil, nil, false) {
|
2021-04-14 12:02:12 +00:00
|
|
|
// error already handled
|
2017-03-17 14:16:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add OpenID for the user
|
2021-11-17 09:58:31 +00:00
|
|
|
userOID := &user_model.UserOpenID{UID: u.ID, URI: oid}
|
2022-05-20 14:08:52 +00:00
|
|
|
if err = user_model.AddUserOpenID(ctx, userOID); err != nil {
|
2021-11-17 09:58:31 +00:00
|
|
|
if user_model.IsErrOpenIDAlreadyUsed(err) {
|
2017-03-17 14:16:08 +00:00
|
|
|
ctx.RenderWithErr(ctx.Tr("form.openid_been_used", oid), tplSignUpOID, &form)
|
|
|
|
return
|
|
|
|
}
|
2018-01-10 21:34:17 +00:00
|
|
|
ctx.ServerError("AddUserOpenID", err)
|
2017-03-17 14:16:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:02:12 +00:00
|
|
|
if !handleUserCreated(ctx, u, nil) {
|
|
|
|
// error already handled
|
2017-03-17 14:16:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
remember, _ := ctx.Session.Get("openid_signin_remember").(bool)
|
2019-04-02 07:48:31 +00:00
|
|
|
log.Trace("Session stored openid-remember: %t", remember)
|
2017-03-17 14:16:08 +00:00
|
|
|
handleSignIn(ctx, u, remember)
|
|
|
|
}
|