2017-03-17 19:46:08 +05:30
|
|
|
// Copyright 2017 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.
|
|
|
|
|
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-05 21:00:52 +05:30
|
|
|
"net/http"
|
2017-03-17 19:46:08 +05:30
|
|
|
"net/url"
|
|
|
|
|
2021-11-17 15:28:31 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2017-03-17 19:46:08 +05:30
|
|
|
"code.gitea.io/gitea/modules/auth/openid"
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2020-10-03 09:07:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/hcaptcha"
|
2017-03-17 19:46:08 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2018-07-05 09:43:05 +05:30
|
|
|
"code.gitea.io/gitea/modules/recaptcha"
|
2021-12-20 19:42:26 +05:30
|
|
|
"code.gitea.io/gitea/modules/session"
|
2017-03-17 19:46:08 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-05-10 12:15:17 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 21:06:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-03-07 13:42:43 +05:30
|
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
2021-07-24 15:46:34 +05:30
|
|
|
"code.gitea.io/gitea/services/auth"
|
2021-04-07 01:14:05 +05:30
|
|
|
"code.gitea.io/gitea/services/forms"
|
2017-03-17 19:46:08 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
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 06:01:13 +05:30
|
|
|
if ctx.FormString("openid.return_to") != "" {
|
2017-03-17 19:46:08 +05:30
|
|
|
signInOpenIDVerify(ctx)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check auto-login.
|
|
|
|
isSucceed, err := AutoSignIn(ctx)
|
|
|
|
if err != nil {
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.ServerError("AutoSignIn", err)
|
2017-03-17 19:46:08 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:01:13 +05:30
|
|
|
redirectTo := ctx.FormString("redirect_to")
|
2017-03-17 19:46:08 +05:30
|
|
|
if len(redirectTo) > 0 {
|
2021-03-07 13:42:43 +05:30
|
|
|
middleware.SetRedirectToCookie(ctx.Resp, redirectTo)
|
2017-03-17 19:46:08 +05:30
|
|
|
} else {
|
2019-03-21 07:36:16 +05:30
|
|
|
redirectTo = ctx.GetCookie("redirect_to")
|
2017-03-17 19:46:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if isSucceed {
|
2021-03-07 13:42:43 +05:30
|
|
|
middleware.DeleteRedirectToCookie(ctx.Resp)
|
2018-03-16 02:43:34 +05:30
|
|
|
ctx.RedirectToFirst(redirectTo)
|
2017-03-17 19:46:08 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["PageIsSignIn"] = true
|
|
|
|
ctx.Data["PageIsLoginOpenID"] = true
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplSignInOpenID)
|
2017-03-17 19:46:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// 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 16:27:43 +05:30
|
|
|
if len(setting.Service.OpenIDWhitelist) != 0 {
|
|
|
|
for _, pat := range setting.Service.OpenIDWhitelist {
|
2017-03-17 19:46:08 +05:30
|
|
|
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 16:27:43 +05:30
|
|
|
for _, pat := range setting.Service.OpenIDBlacklist {
|
2017-03-17 19:46:08 +05:30
|
|
|
if pat.MatchString(uri) {
|
|
|
|
return fmt.Errorf("URI forbidden by blacklist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignInOpenIDPost response for openid sign in request
|
2021-01-26 21:06:53 +05:30
|
|
|
func SignInOpenIDPost(ctx *context.Context) {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.SignInOpenIDForm)
|
2017-03-17 19:46:08 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("sign_in")
|
|
|
|
ctx.Data["PageIsSignIn"] = true
|
|
|
|
ctx.Data["PageIsLoginOpenID"] = true
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplSignInOpenID)
|
2017-03-17 19:46:08 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := openid.Normalize(form.Openid)
|
|
|
|
if err != nil {
|
|
|
|
ctx.RenderWithErr(err.Error(), tplSignInOpenID, &form)
|
2017-03-21 06:25:00 +05:30
|
|
|
return
|
2017-03-17 19:46:08 +05:30
|
|
|
}
|
|
|
|
form.Openid = id
|
|
|
|
|
|
|
|
log.Trace("OpenID uri: " + id)
|
|
|
|
|
2017-03-21 06:25:00 +05:30
|
|
|
err = allowedOpenIDURI(id)
|
|
|
|
if err != nil {
|
2017-03-17 19:46:08 +05:30
|
|
|
ctx.RenderWithErr(err.Error(), tplSignInOpenID, &form)
|
2017-03-21 06:25:00 +05:30
|
|
|
return
|
2017-03-17 19:46:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
redirectTo := setting.AppURL + "user/login/openid"
|
|
|
|
url, err := openid.RedirectURL(id, redirectTo, setting.AppURL)
|
2017-03-21 06:25:00 +05:30
|
|
|
if err != nil {
|
2019-04-02 13:18:31 +05:30
|
|
|
log.Error("Error in OpenID redirect URL: %s, %v", redirectTo, err.Error())
|
2019-01-13 00:54:47 +05:30
|
|
|
ctx.RenderWithErr(fmt.Sprintf("Unable to find OpenID provider in %s", redirectTo), tplSignInOpenID, &form)
|
2017-03-21 06:25:00 +05:30
|
|
|
return
|
|
|
|
}
|
2017-03-17 19:46:08 +05:30
|
|
|
|
|
|
|
// 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 13:18:31 +05:30
|
|
|
log.Trace("Form-passed openid-remember: %t", form.Remember)
|
2020-05-17 18:13:29 +05:30
|
|
|
|
|
|
|
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-13 01:11:28 +05:30
|
|
|
}
|
2017-03-17 19:46:08 +05:30
|
|
|
|
|
|
|
ctx.Redirect(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
// signInOpenIDVerify handles response from OpenID provider
|
|
|
|
func signInOpenIDVerify(ctx *context.Context) {
|
|
|
|
|
2021-12-14 14:07:11 +05:30
|
|
|
log.Trace("Incoming call to: %s", ctx.Req.URL.String())
|
2017-03-17 19:46:08 +05:30
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
fullURL := setting.AppURL + ctx.Req.URL.String()[1:]
|
2021-12-14 14:07:11 +05:30
|
|
|
log.Trace("Full URL: %s", fullURL)
|
2017-03-17 19:46:08 +05:30
|
|
|
|
|
|
|
var id, err = openid.Verify(fullURL)
|
|
|
|
if err != nil {
|
2021-04-07 01:14:05 +05:30
|
|
|
ctx.RenderWithErr(err.Error(), tplSignInOpenID, &forms.SignInOpenIDForm{
|
2017-03-17 19:46:08 +05:30
|
|
|
Openid: id,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-14 14:07:11 +05:30
|
|
|
log.Trace("Verified ID: %s", id)
|
2017-03-17 19:46:08 +05:30
|
|
|
|
|
|
|
/* Now we should seek for the user and log him in, or prompt
|
|
|
|
* to register if not found */
|
|
|
|
|
2021-11-24 15:19:20 +05:30
|
|
|
u, err := user_model.GetUserByOpenID(id)
|
2017-03-17 19:46:08 +05:30
|
|
|
if err != nil {
|
2021-11-24 15:19:20 +05:30
|
|
|
if !user_model.IsErrUserNotExist(err) {
|
2021-04-07 01:14:05 +05:30
|
|
|
ctx.RenderWithErr(err.Error(), tplSignInOpenID, &forms.SignInOpenIDForm{
|
2017-03-17 19:46:08 +05:30
|
|
|
Openid: id,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2019-06-13 01:11:28 +05:30
|
|
|
log.Error("signInOpenIDVerify: %v", err)
|
2017-03-17 19:46:08 +05:30
|
|
|
}
|
|
|
|
if u != nil {
|
|
|
|
log.Trace("User exists, logging in")
|
|
|
|
remember, _ := ctx.Session.Get("openid_signin_remember").(bool)
|
2019-04-02 13:18:31 +05:30
|
|
|
log.Trace("Session stored openid-remember: %t", remember)
|
2017-03-17 19:46:08 +05:30
|
|
|
handleSignIn(ctx, u, remember)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-14 14:07:11 +05:30
|
|
|
log.Trace("User with openid: %s does not exist, should connect or register", id)
|
2017-03-17 19:46:08 +05:30
|
|
|
|
|
|
|
parsedURL, err := url.Parse(fullURL)
|
|
|
|
if err != nil {
|
2021-04-07 01:14:05 +05:30
|
|
|
ctx.RenderWithErr(err.Error(), tplSignInOpenID, &forms.SignInOpenIDForm{
|
2017-03-17 19:46:08 +05:30
|
|
|
Openid: id,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
values, err := url.ParseQuery(parsedURL.RawQuery)
|
|
|
|
if err != nil {
|
2021-04-07 01:14:05 +05:30
|
|
|
ctx.RenderWithErr(err.Error(), tplSignInOpenID, &forms.SignInOpenIDForm{
|
2017-03-17 19:46:08 +05:30
|
|
|
Openid: id,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
email := values.Get("openid.sreg.email")
|
|
|
|
nickname := values.Get("openid.sreg.nickname")
|
|
|
|
|
2021-12-14 14:07:11 +05:30
|
|
|
log.Trace("User has email=%s and nickname=%s", email, nickname)
|
2017-03-17 19:46:08 +05:30
|
|
|
|
|
|
|
if email != "" {
|
2021-11-24 15:19:20 +05:30
|
|
|
u, err = user_model.GetUserByEmail(email)
|
2017-03-17 19:46:08 +05:30
|
|
|
if err != nil {
|
2021-11-24 15:19:20 +05:30
|
|
|
if !user_model.IsErrUserNotExist(err) {
|
2021-04-07 01:14:05 +05:30
|
|
|
ctx.RenderWithErr(err.Error(), tplSignInOpenID, &forms.SignInOpenIDForm{
|
2017-03-17 19:46:08 +05:30
|
|
|
Openid: id,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2019-06-13 01:11:28 +05:30
|
|
|
log.Error("signInOpenIDVerify: %v", err)
|
2017-03-17 19:46:08 +05:30
|
|
|
}
|
|
|
|
if u != nil {
|
2021-12-14 14:07:11 +05:30
|
|
|
log.Trace("Local user %s has OpenID provided email %s", u.LowerName, email)
|
2017-03-17 19:46:08 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if u == nil && nickname != "" {
|
2021-11-24 15:19:20 +05:30
|
|
|
u, _ = user_model.GetUserByName(nickname)
|
2017-03-17 19:46:08 +05:30
|
|
|
if err != nil {
|
2021-11-24 15:19:20 +05:30
|
|
|
if !user_model.IsErrUserNotExist(err) {
|
2021-04-07 01:14:05 +05:30
|
|
|
ctx.RenderWithErr(err.Error(), tplSignInOpenID, &forms.SignInOpenIDForm{
|
2017-03-17 19:46:08 +05:30
|
|
|
Openid: id,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if u != nil {
|
2021-12-14 14:07:11 +05:30
|
|
|
log.Trace("Local user %s has OpenID provided nickname %s", u.LowerName, nickname)
|
2017-03-17 19:46:08 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 19:42:26 +05:30
|
|
|
if _, err := session.RegenerateSession(ctx.Resp, ctx.Req); err != nil {
|
|
|
|
ctx.ServerError("RegenerateSession", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-17 18:13:29 +05:30
|
|
|
if err := ctx.Session.Set("openid_verified_uri", id); err != nil {
|
|
|
|
log.Error("signInOpenIDVerify: Could not set openid_verified_uri in session: %v", err)
|
2019-06-13 01:11:28 +05:30
|
|
|
}
|
2020-05-17 18:13:29 +05:30
|
|
|
if err := ctx.Session.Set("openid_determined_email", email); err != nil {
|
|
|
|
log.Error("signInOpenIDVerify: Could not set openid_determined_email in session: %v", err)
|
2019-06-13 01:11:28 +05:30
|
|
|
}
|
2017-03-17 19:46:08 +05:30
|
|
|
|
|
|
|
if u != nil {
|
|
|
|
nickname = u.LowerName
|
|
|
|
}
|
|
|
|
|
2020-05-17 18:13:29 +05:30
|
|
|
if err := ctx.Session.Set("openid_determined_username", nickname); err != nil {
|
|
|
|
log.Error("signInOpenIDVerify: Could not set openid_determined_username in session: %v", err)
|
|
|
|
}
|
|
|
|
if err := ctx.Session.Release(); err != nil {
|
|
|
|
log.Error("signInOpenIDVerify: Unable to save changes to the session: %v", err)
|
2019-06-13 01:11:28 +05:30
|
|
|
}
|
2017-03-17 19:46:08 +05:30
|
|
|
|
2021-05-09 19:43:35 +05:30
|
|
|
if u != nil || !setting.Service.EnableOpenIDSignUp || setting.Service.AllowOnlyInternalRegistration {
|
2017-03-17 19:46:08 +05:30
|
|
|
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 16:27:43 +05:30
|
|
|
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
|
2021-05-09 19:43:35 +05:30
|
|
|
ctx.Data["AllowOnlyInternalRegistration"] = setting.Service.AllowOnlyInternalRegistration
|
2017-03-17 19:46:08 +05:30
|
|
|
ctx.Data["OpenID"] = oid
|
|
|
|
userName, _ := ctx.Session.Get("openid_determined_username").(string)
|
|
|
|
if userName != "" {
|
|
|
|
ctx.Data["user_name"] = userName
|
|
|
|
}
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplConnectOID)
|
2017-03-17 19:46:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectOpenIDPost handles submission of a form to connect an OpenID URI to an existing account
|
2021-01-26 21:06:53 +05:30
|
|
|
func ConnectOpenIDPost(ctx *context.Context) {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.ConnectOpenIDForm)
|
2017-03-17 19:46:08 +05:30
|
|
|
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 16:27:43 +05:30
|
|
|
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
|
2017-03-17 19:46:08 +05:30
|
|
|
ctx.Data["OpenID"] = oid
|
|
|
|
|
2021-09-17 17:13:47 +05:30
|
|
|
u, _, err := auth.UserSignIn(form.UserName, form.Password)
|
2017-03-17 19:46:08 +05:30
|
|
|
if err != nil {
|
2021-11-24 15:19:20 +05:30
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2017-03-17 19:46:08 +05:30
|
|
|
ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), tplConnectOID, &form)
|
|
|
|
} else {
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.ServerError("ConnectOpenIDPost", err)
|
2017-03-17 19:46:08 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add OpenID for the user
|
2021-11-17 15:28:31 +05:30
|
|
|
userOID := &user_model.UserOpenID{UID: u.ID, URI: oid}
|
|
|
|
if err = user_model.AddUserOpenID(userOID); err != nil {
|
|
|
|
if user_model.IsErrOpenIDAlreadyUsed(err) {
|
2017-03-17 19:46:08 +05:30
|
|
|
ctx.RenderWithErr(ctx.Tr("form.openid_been_used", oid), tplConnectOID, &form)
|
|
|
|
return
|
|
|
|
}
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.ServerError("AddUserOpenID", err)
|
2017-03-17 19:46:08 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.add_openid_success"))
|
|
|
|
|
|
|
|
remember, _ := ctx.Session.Get("openid_signin_remember").(bool)
|
2019-04-02 13:18:31 +05:30
|
|
|
log.Trace("Session stored openid-remember: %t", remember)
|
2017-03-17 19:46:08 +05:30
|
|
|
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 16:27:43 +05:30
|
|
|
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
|
2021-05-09 19:43:35 +05:30
|
|
|
ctx.Data["AllowOnlyInternalRegistration"] = setting.Service.AllowOnlyInternalRegistration
|
2017-03-17 22:10:39 +05:30
|
|
|
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
|
2021-01-27 20:26:54 +05:30
|
|
|
ctx.Data["Captcha"] = context.GetImageCaptcha()
|
2018-07-05 09:43:05 +05:30
|
|
|
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
|
|
|
|
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
|
2020-10-03 09:07:53 +05:30
|
|
|
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
|
2019-05-02 18:39:39 +05:30
|
|
|
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
|
2017-03-17 19:46:08 +05:30
|
|
|
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 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplSignUpOID)
|
2017-03-17 19:46:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterOpenIDPost handles submission of a form to create a new user authenticated via an OpenID URI
|
2021-01-26 21:06:53 +05:30
|
|
|
func RegisterOpenIDPost(ctx *context.Context) {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.SignUpOpenIDForm)
|
2017-03-17 19:46:08 +05:30
|
|
|
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 16:27:43 +05:30
|
|
|
ctx.Data["EnableOpenIDSignUp"] = setting.Service.EnableOpenIDSignUp
|
2017-03-17 22:10:39 +05:30
|
|
|
ctx.Data["EnableCaptcha"] = setting.Service.EnableCaptcha
|
2019-05-02 18:39:39 +05:30
|
|
|
ctx.Data["RecaptchaURL"] = setting.Service.RecaptchaURL
|
2021-01-27 20:26:54 +05:30
|
|
|
ctx.Data["Captcha"] = context.GetImageCaptcha()
|
2018-07-05 09:43:05 +05:30
|
|
|
ctx.Data["CaptchaType"] = setting.Service.CaptchaType
|
|
|
|
ctx.Data["RecaptchaSitekey"] = setting.Service.RecaptchaSitekey
|
2020-10-03 09:07:53 +05:30
|
|
|
ctx.Data["HcaptchaSitekey"] = setting.Service.HcaptchaSitekey
|
2017-03-17 19:46:08 +05:30
|
|
|
ctx.Data["OpenID"] = oid
|
|
|
|
|
2021-05-09 19:43:35 +05:30
|
|
|
if setting.Service.AllowOnlyInternalRegistration {
|
|
|
|
ctx.Error(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-07 01:18:02 +05:30
|
|
|
if setting.Service.EnableCaptcha {
|
|
|
|
var valid bool
|
2020-10-03 09:07:53 +05:30
|
|
|
var err error
|
2019-07-07 01:18:02 +05:30
|
|
|
switch setting.Service.CaptchaType {
|
|
|
|
case setting.ImageCaptcha:
|
2021-01-26 21:06:53 +05:30
|
|
|
valid = context.GetImageCaptcha().VerifyReq(ctx.Req)
|
2019-07-07 01:18:02 +05:30
|
|
|
case setting.ReCaptcha:
|
2020-10-03 09:07:53 +05:30
|
|
|
if err := ctx.Req.ParseForm(); err != nil {
|
2019-07-07 01:18:02 +05:30
|
|
|
ctx.ServerError("", err)
|
|
|
|
return
|
|
|
|
}
|
2021-05-31 11:48:11 +05:30
|
|
|
valid, err = recaptcha.Verify(ctx, form.GRecaptchaResponse)
|
2020-10-03 09:07:53 +05:30
|
|
|
case setting.HCaptcha:
|
|
|
|
if err := ctx.Req.ParseForm(); err != nil {
|
|
|
|
ctx.ServerError("", err)
|
|
|
|
return
|
|
|
|
}
|
2021-05-31 11:48:11 +05:30
|
|
|
valid, err = hcaptcha.Verify(ctx, form.HcaptchaResponse)
|
2019-07-07 01:18:02 +05:30
|
|
|
default:
|
|
|
|
ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
|
2019-06-13 01:11:28 +05:30
|
|
|
return
|
|
|
|
}
|
2020-10-03 09:07:53 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Debug("%s", err.Error())
|
|
|
|
}
|
2019-07-07 01:18:02 +05:30
|
|
|
|
2018-07-05 09:43:05 +05:30
|
|
|
if !valid {
|
|
|
|
ctx.Data["Err_Captcha"] = true
|
|
|
|
ctx.RenderWithErr(ctx.Tr("form.captcha_incorrect"), tplSignUpOID, &form)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 21:15:54 +05:30
|
|
|
length := setting.MinPasswordLength
|
|
|
|
if length < 256 {
|
|
|
|
length = 256
|
2017-03-21 06:25:00 +05:30
|
|
|
}
|
2021-05-10 12:15:17 +05:30
|
|
|
password, err := util.RandomString(int64(length))
|
2017-03-17 19:46:08 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.RenderWithErr(err.Error(), tplSignUpOID, form)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-24 15:19:20 +05:30
|
|
|
u := &user_model.User{
|
2017-03-17 19:46:08 +05:30
|
|
|
Name: form.UserName,
|
|
|
|
Email: form.Email,
|
|
|
|
Passwd: password,
|
2021-02-05 08:33:20 +05:30
|
|
|
IsActive: !(setting.Service.RegisterEmailConfirm || setting.Service.RegisterManualConfirm),
|
2017-03-17 19:46:08 +05:30
|
|
|
}
|
2021-04-14 17:32:12 +05:30
|
|
|
if !createUserInContext(ctx, tplSignUpOID, form, u, nil, false) {
|
|
|
|
// error already handled
|
2017-03-17 19:46:08 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add OpenID for the user
|
2021-11-17 15:28:31 +05:30
|
|
|
userOID := &user_model.UserOpenID{UID: u.ID, URI: oid}
|
|
|
|
if err = user_model.AddUserOpenID(userOID); err != nil {
|
|
|
|
if user_model.IsErrOpenIDAlreadyUsed(err) {
|
2017-03-17 19:46:08 +05:30
|
|
|
ctx.RenderWithErr(ctx.Tr("form.openid_been_used", oid), tplSignUpOID, &form)
|
|
|
|
return
|
|
|
|
}
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.ServerError("AddUserOpenID", err)
|
2017-03-17 19:46:08 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-14 17:32:12 +05:30
|
|
|
if !handleUserCreated(ctx, u, nil) {
|
|
|
|
// error already handled
|
2017-03-17 19:46:08 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
remember, _ := ctx.Session.Get("openid_signin_remember").(bool)
|
2019-04-02 13:18:31 +05:30
|
|
|
log.Trace("Session stored openid-remember: %t", remember)
|
2017-03-17 19:46:08 +05:30
|
|
|
handleSignIn(ctx, u, remember)
|
|
|
|
}
|