You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.6 KiB
Go
131 lines
3.6 KiB
Go
6 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.
|
||
|
|
||
6 years ago
|
package setting
|
||
8 years ago
|
|
||
|
import (
|
||
|
"code.gitea.io/gitea/models"
|
||
|
"code.gitea.io/gitea/modules/auth"
|
||
|
"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"
|
||
|
)
|
||
|
|
||
6 years ago
|
// OpenIDPost response for change user's openid
|
||
|
func OpenIDPost(ctx *context.Context, form auth.AddOpenIDForm) {
|
||
8 years ago
|
ctx.Data["Title"] = ctx.Tr("settings")
|
||
6 years ago
|
ctx.Data["PageIsSettingsSecurity"] = true
|
||
8 years ago
|
|
||
|
if ctx.HasError() {
|
||
8 years ago
|
openid, err := models.GetUserOpenIDs(ctx.User.ID)
|
||
|
if err != nil {
|
||
7 years ago
|
ctx.ServerError("GetUserOpenIDs", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
ctx.Data["OpenIDs"] = openid
|
||
6 years ago
|
ctx.HTML(200, 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 {
|
||
6 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 {
|
||
6 years ago
|
ctx.RenderWithErr(ctx.Tr("form.openid_been_used", id), tplSettingsSecurity, &form)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
6 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 {
|
||
6 years ago
|
ctx.RenderWithErr(err.Error(), tplSettingsSecurity, &form)
|
||
8 years ago
|
return
|
||
|
}
|
||
8 years ago
|
ctx.Redirect(url)
|
||
|
}
|
||
|
|
||
|
func settingsOpenIDVerify(ctx *context.Context) {
|
||
8 years ago
|
log.Trace("Incoming call to: " + ctx.Req.Request.URL.String())
|
||
8 years ago
|
|
||
8 years ago
|
fullURL := setting.AppURL + ctx.Req.Request.URL.String()[1:]
|
||
|
log.Trace("Full URL: " + fullURL)
|
||
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
|
||
|
|
||
|
id, err := openid.Verify(fullURL)
|
||
|
if err != nil {
|
||
6 years ago
|
ctx.RenderWithErr(err.Error(), tplSettingsSecurity, &auth.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) {
|
||
6 years ago
|
ctx.RenderWithErr(ctx.Tr("form.openid_been_used", id), tplSettingsSecurity, &auth.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"))
|
||
|
|
||
6 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"))
|
||
|
ctx.JSON(200, map[string]interface{}{
|
||
6 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
|
||
|
}
|
||
|
|
||
6 years ago
|
ctx.Redirect(setting.AppSubURL + "/user/settings/security")
|
||
8 years ago
|
}
|