gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package security
  4. import (
  5. "net/http"
  6. user_model "code.gitea.io/gitea/models/user"
  7. "code.gitea.io/gitea/modules/auth/openid"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/modules/web"
  11. "code.gitea.io/gitea/services/context"
  12. "code.gitea.io/gitea/services/forms"
  13. )
  14. // OpenIDPost response for change user's openid
  15. func OpenIDPost(ctx *context.Context) {
  16. if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureManageCredentials) {
  17. ctx.HTTPError(http.StatusNotFound)
  18. return
  19. }
  20. form := web.GetForm(ctx).(*forms.AddOpenIDForm)
  21. ctx.Data["Title"] = ctx.Tr("settings")
  22. ctx.Data["PageIsSettingsSecurity"] = true
  23. if ctx.HasError() {
  24. loadSecurityData(ctx)
  25. ctx.HTML(http.StatusOK, tplSettingsSecurity)
  26. return
  27. }
  28. // WARNING: specifying a wrong OpenID here could lock
  29. // a user out of her account, would be better to
  30. // verify/confirm the new OpenID before storing it
  31. // Also, consider allowing for multiple OpenID URIs
  32. id, err := openid.Normalize(form.Openid)
  33. if err != nil {
  34. loadSecurityData(ctx)
  35. ctx.RenderWithErr(err.Error(), tplSettingsSecurity, &form)
  36. return
  37. }
  38. form.Openid = id
  39. log.Trace("Normalized id: " + id)
  40. oids, err := user_model.GetUserOpenIDs(ctx, ctx.Doer.ID)
  41. if err != nil {
  42. ctx.ServerError("GetUserOpenIDs", err)
  43. return
  44. }
  45. ctx.Data["OpenIDs"] = oids
  46. // Check that the OpenID is not already used
  47. for _, obj := range oids {
  48. if obj.URI == id {
  49. loadSecurityData(ctx)
  50. ctx.RenderWithErr(ctx.Tr("form.openid_been_used", id), tplSettingsSecurity, &form)
  51. return
  52. }
  53. }
  54. redirectTo := setting.AppURL + "user/settings/security"
  55. url, err := openid.RedirectURL(id, redirectTo, setting.AppURL)
  56. if err != nil {
  57. loadSecurityData(ctx)
  58. ctx.RenderWithErr(err.Error(), tplSettingsSecurity, &form)
  59. return
  60. }
  61. ctx.Redirect(url)
  62. }
  63. func settingsOpenIDVerify(ctx *context.Context) {
  64. log.Trace("Incoming call to: " + ctx.Req.URL.String())
  65. fullURL := setting.AppURL + ctx.Req.URL.String()[1:]
  66. log.Trace("Full URL: " + fullURL)
  67. id, err := openid.Verify(fullURL)
  68. if err != nil {
  69. ctx.RenderWithErr(err.Error(), tplSettingsSecurity, &forms.AddOpenIDForm{
  70. Openid: id,
  71. })
  72. return
  73. }
  74. log.Trace("Verified ID: " + id)
  75. oid := &user_model.UserOpenID{UID: ctx.Doer.ID, URI: id}
  76. if err = user_model.AddUserOpenID(ctx, oid); err != nil {
  77. if user_model.IsErrOpenIDAlreadyUsed(err) {
  78. ctx.RenderWithErr(ctx.Tr("form.openid_been_used", id), tplSettingsSecurity, &forms.AddOpenIDForm{Openid: id})
  79. return
  80. }
  81. ctx.ServerError("AddUserOpenID", err)
  82. return
  83. }
  84. log.Trace("Associated OpenID %s to user %s", id, ctx.Doer.Name)
  85. ctx.Flash.Success(ctx.Tr("settings.add_openid_success"))
  86. ctx.Redirect(setting.AppSubURL + "/user/settings/security")
  87. }
  88. // DeleteOpenID response for delete user's openid
  89. func DeleteOpenID(ctx *context.Context) {
  90. if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureManageCredentials) {
  91. ctx.HTTPError(http.StatusNotFound)
  92. return
  93. }
  94. if err := user_model.DeleteUserOpenID(ctx, &user_model.UserOpenID{ID: ctx.FormInt64("id"), UID: ctx.Doer.ID}); err != nil {
  95. ctx.ServerError("DeleteUserOpenID", err)
  96. return
  97. }
  98. log.Trace("OpenID address deleted: %s", ctx.Doer.Name)
  99. ctx.Flash.Success(ctx.Tr("settings.openid_deletion_success"))
  100. ctx.JSONRedirect(setting.AppSubURL + "/user/settings/security")
  101. }
  102. // ToggleOpenIDVisibility response for toggle visibility of user's openid
  103. func ToggleOpenIDVisibility(ctx *context.Context) {
  104. if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureManageCredentials) {
  105. ctx.HTTPError(http.StatusNotFound)
  106. return
  107. }
  108. if err := user_model.ToggleUserOpenIDVisibility(ctx, ctx.FormInt64("id")); err != nil {
  109. ctx.ServerError("ToggleUserOpenIDVisibility", err)
  110. return
  111. }
  112. ctx.Redirect(setting.AppSubURL + "/user/settings/security")
  113. }