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.

155 lines
4.2 KiB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package user
  5. import (
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/auth"
  8. "code.gitea.io/gitea/modules/auth/openid"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. const (
  15. tplSettingsOpenID base.TplName = "user/settings/openid"
  16. )
  17. // SettingsOpenID renders change user's openid page
  18. func SettingsOpenID(ctx *context.Context) {
  19. ctx.Data["Title"] = ctx.Tr("settings")
  20. ctx.Data["PageIsSettingsOpenID"] = true
  21. if ctx.Query("openid.return_to") != "" {
  22. settingsOpenIDVerify(ctx)
  23. return
  24. }
  25. openid, err := models.GetUserOpenIDs(ctx.User.ID)
  26. if err != nil {
  27. ctx.ServerError("GetUserOpenIDs", err)
  28. return
  29. }
  30. ctx.Data["OpenIDs"] = openid
  31. ctx.HTML(200, tplSettingsOpenID)
  32. }
  33. // SettingsOpenIDPost response for change user's openid
  34. func SettingsOpenIDPost(ctx *context.Context, form auth.AddOpenIDForm) {
  35. ctx.Data["Title"] = ctx.Tr("settings")
  36. ctx.Data["PageIsSettingsOpenID"] = true
  37. if ctx.HasError() {
  38. openid, err := models.GetUserOpenIDs(ctx.User.ID)
  39. if err != nil {
  40. ctx.ServerError("GetUserOpenIDs", err)
  41. return
  42. }
  43. ctx.Data["OpenIDs"] = openid
  44. ctx.HTML(200, tplSettingsOpenID)
  45. return
  46. }
  47. // WARNING: specifying a wrong OpenID here could lock
  48. // a user out of her account, would be better to
  49. // verify/confirm the new OpenID before storing it
  50. // Also, consider allowing for multiple OpenID URIs
  51. id, err := openid.Normalize(form.Openid)
  52. if err != nil {
  53. ctx.RenderWithErr(err.Error(), tplSettingsOpenID, &form)
  54. return
  55. }
  56. form.Openid = id
  57. log.Trace("Normalized id: " + id)
  58. oids, err := models.GetUserOpenIDs(ctx.User.ID)
  59. if err != nil {
  60. ctx.ServerError("GetUserOpenIDs", err)
  61. return
  62. }
  63. ctx.Data["OpenIDs"] = oids
  64. // Check that the OpenID is not already used
  65. for _, obj := range oids {
  66. if obj.URI == id {
  67. ctx.RenderWithErr(ctx.Tr("form.openid_been_used", id), tplSettingsOpenID, &form)
  68. return
  69. }
  70. }
  71. redirectTo := setting.AppURL + "user/settings/openid"
  72. url, err := openid.RedirectURL(id, redirectTo, setting.AppURL)
  73. if err != nil {
  74. ctx.RenderWithErr(err.Error(), tplSettingsOpenID, &form)
  75. return
  76. }
  77. ctx.Redirect(url)
  78. }
  79. func settingsOpenIDVerify(ctx *context.Context) {
  80. log.Trace("Incoming call to: " + ctx.Req.Request.URL.String())
  81. fullURL := setting.AppURL + ctx.Req.Request.URL.String()[1:]
  82. log.Trace("Full URL: " + fullURL)
  83. oids, err := models.GetUserOpenIDs(ctx.User.ID)
  84. if err != nil {
  85. ctx.ServerError("GetUserOpenIDs", err)
  86. return
  87. }
  88. ctx.Data["OpenIDs"] = oids
  89. id, err := openid.Verify(fullURL)
  90. if err != nil {
  91. ctx.RenderWithErr(err.Error(), tplSettingsOpenID, &auth.AddOpenIDForm{
  92. Openid: id,
  93. })
  94. return
  95. }
  96. log.Trace("Verified ID: " + id)
  97. oid := &models.UserOpenID{UID: ctx.User.ID, URI: id}
  98. if err = models.AddUserOpenID(oid); err != nil {
  99. if models.IsErrOpenIDAlreadyUsed(err) {
  100. ctx.RenderWithErr(ctx.Tr("form.openid_been_used", id), tplSettingsOpenID, &auth.AddOpenIDForm{Openid: id})
  101. return
  102. }
  103. ctx.ServerError("AddUserOpenID", err)
  104. return
  105. }
  106. log.Trace("Associated OpenID %s to user %s", id, ctx.User.Name)
  107. ctx.Flash.Success(ctx.Tr("settings.add_openid_success"))
  108. ctx.Redirect(setting.AppSubURL + "/user/settings/openid")
  109. }
  110. // DeleteOpenID response for delete user's openid
  111. func DeleteOpenID(ctx *context.Context) {
  112. if err := models.DeleteUserOpenID(&models.UserOpenID{ID: ctx.QueryInt64("id"), UID: ctx.User.ID}); err != nil {
  113. ctx.ServerError("DeleteUserOpenID", err)
  114. return
  115. }
  116. log.Trace("OpenID address deleted: %s", ctx.User.Name)
  117. ctx.Flash.Success(ctx.Tr("settings.openid_deletion_success"))
  118. ctx.JSON(200, map[string]interface{}{
  119. "redirect": setting.AppSubURL + "/user/settings/openid",
  120. })
  121. }
  122. // ToggleOpenIDVisibility response for toggle visibility of user's openid
  123. func ToggleOpenIDVisibility(ctx *context.Context) {
  124. if err := models.ToggleUserOpenIDVisibility(ctx.QueryInt64("id")); err != nil {
  125. ctx.ServerError("ToggleUserOpenIDVisibility", err)
  126. return
  127. }
  128. ctx.Redirect(setting.AppSubURL + "/user/settings/openid")
  129. }