mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.5 KiB
130 lines
3.5 KiB
7 years ago
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
||
8 years ago
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
7 years ago
|
package setting
|
||
8 years ago
|
|
||
|
import (
|
||
4 years ago
|
"net/http"
|
||
|
|
||
8 years ago
|
"code.gitea.io/gitea/models"
|
||
|
"code.gitea.io/gitea/modules/auth/openid"
|
||
|
"code.gitea.io/gitea/modules/context"
|
||
|
"code.gitea.io/gitea/modules/log"
|
||
|
"code.gitea.io/gitea/modules/setting"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/web"
|
||
4 years ago
|
"code.gitea.io/gitea/services/forms"
|
||
8 years ago
|
)
|
||
|
|
||
7 years ago
|
// OpenIDPost response for change user's openid
|
||
4 years ago
|
func OpenIDPost(ctx *context.Context) {
|
||
4 years ago
|
form := web.GetForm(ctx).(*forms.AddOpenIDForm)
|
||
8 years ago
|
ctx.Data["Title"] = ctx.Tr("settings")
|
||
7 years ago
|
ctx.Data["PageIsSettingsSecurity"] = true
|
||
8 years ago
|
|
||
|
if ctx.HasError() {
|
||
7 years ago
|
loadSecurityData(ctx)
|
||
|
|
||
4 years ago
|
ctx.HTML(http.StatusOK, tplSettingsSecurity)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
// WARNING: specifying a wrong OpenID here could lock
|
||
|
// a user out of her account, would be better to
|
||
|
// verify/confirm the new OpenID before storing it
|
||
|
|
||
|
// Also, consider allowing for multiple OpenID URIs
|
||
|
|
||
|
id, err := openid.Normalize(form.Openid)
|
||
|
if err != nil {
|
||
7 years ago
|
loadSecurityData(ctx)
|
||
|
|
||
7 years ago
|
ctx.RenderWithErr(err.Error(), tplSettingsSecurity, &form)
|
||
8 years ago
|
return
|
||
8 years ago
|
}
|
||
|
form.Openid = id
|
||
8 years ago
|
log.Trace("Normalized id: " + id)
|
||
8 years ago
|
|
||
|
oids, err := models.GetUserOpenIDs(ctx.User.ID)
|
||
|
if err != nil {
|
||
7 years ago
|
ctx.ServerError("GetUserOpenIDs", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
ctx.Data["OpenIDs"] = oids
|
||
|
|
||
|
// Check that the OpenID is not already used
|
||
|
for _, obj := range oids {
|
||
|
if obj.URI == id {
|
||
7 years ago
|
loadSecurityData(ctx)
|
||
|
|
||
7 years ago
|
ctx.RenderWithErr(ctx.Tr("form.openid_been_used", id), tplSettingsSecurity, &form)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
redirectTo := setting.AppURL + "user/settings/security"
|
||
8 years ago
|
url, err := openid.RedirectURL(id, redirectTo, setting.AppURL)
|
||
8 years ago
|
if err != nil {
|
||
7 years ago
|
loadSecurityData(ctx)
|
||
|
|
||
7 years ago
|
ctx.RenderWithErr(err.Error(), tplSettingsSecurity, &form)
|
||
8 years ago
|
return
|
||
|
}
|
||
8 years ago
|
ctx.Redirect(url)
|
||
|
}
|
||
|
|
||
|
func settingsOpenIDVerify(ctx *context.Context) {
|
||
4 years ago
|
log.Trace("Incoming call to: " + ctx.Req.URL.String())
|
||
8 years ago
|
|
||
4 years ago
|
fullURL := setting.AppURL + ctx.Req.URL.String()[1:]
|
||
8 years ago
|
log.Trace("Full URL: " + fullURL)
|
||
8 years ago
|
|
||
|
id, err := openid.Verify(fullURL)
|
||
|
if err != nil {
|
||
4 years ago
|
ctx.RenderWithErr(err.Error(), tplSettingsSecurity, &forms.AddOpenIDForm{
|
||
8 years ago
|
Openid: id,
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
log.Trace("Verified ID: " + id)
|
||
|
|
||
8 years ago
|
oid := &models.UserOpenID{UID: ctx.User.ID, URI: id}
|
||
8 years ago
|
if err = models.AddUserOpenID(oid); err != nil {
|
||
|
if models.IsErrOpenIDAlreadyUsed(err) {
|
||
4 years ago
|
ctx.RenderWithErr(ctx.Tr("form.openid_been_used", id), tplSettingsSecurity, &forms.AddOpenIDForm{Openid: id})
|
||
8 years ago
|
return
|
||
|
}
|
||
7 years ago
|
ctx.ServerError("AddUserOpenID", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
log.Trace("Associated OpenID %s to user %s", id, ctx.User.Name)
|
||
|
ctx.Flash.Success(ctx.Tr("settings.add_openid_success"))
|
||
|
|
||
7 years ago
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security")
|
||
8 years ago
|
}
|
||
|
|
||
|
// DeleteOpenID response for delete user's openid
|
||
|
func DeleteOpenID(ctx *context.Context) {
|
||
|
if err := models.DeleteUserOpenID(&models.UserOpenID{ID: ctx.QueryInt64("id"), UID: ctx.User.ID}); err != nil {
|
||
7 years ago
|
ctx.ServerError("DeleteUserOpenID", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
log.Trace("OpenID address deleted: %s", ctx.User.Name)
|
||
|
|
||
|
ctx.Flash.Success(ctx.Tr("settings.openid_deletion_success"))
|
||
4 years ago
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
||
7 years ago
|
"redirect": setting.AppSubURL + "/user/settings/security",
|
||
8 years ago
|
})
|
||
|
}
|
||
|
|
||
8 years ago
|
// ToggleOpenIDVisibility response for toggle visibility of user's openid
|
||
|
func ToggleOpenIDVisibility(ctx *context.Context) {
|
||
|
if err := models.ToggleUserOpenIDVisibility(ctx.QueryInt64("id")); err != nil {
|
||
7 years ago
|
ctx.ServerError("ToggleUserOpenIDVisibility", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
7 years ago
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security")
|
||
8 years ago
|
}
|