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.

107 lines
2.6 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs 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. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "net/url"
  10. "strings"
  11. "time"
  12. "github.com/gogits/gogs/models"
  13. "github.com/gogits/gogs/modules/log"
  14. "github.com/gogits/gogs/modules/middleware"
  15. "github.com/gogits/gogs/modules/setting"
  16. "github.com/gogits/gogs/modules/social"
  17. )
  18. func extractPath(next string) string {
  19. n, err := url.Parse(next)
  20. if err != nil {
  21. return "/"
  22. }
  23. return n.Path
  24. }
  25. func SocialSignIn(ctx *middleware.Context) {
  26. if setting.OauthService == nil {
  27. ctx.Handle(404, "social.SocialSignIn(oauth service not enabled)", nil)
  28. return
  29. }
  30. next := extractPath(ctx.Query("next"))
  31. name := ctx.Params(":name")
  32. connect, ok := social.SocialMap[name]
  33. if !ok {
  34. ctx.Handle(404, "social.SocialSignIn(social login not enabled)", errors.New(name))
  35. return
  36. }
  37. appUrl := strings.TrimSuffix(setting.AppUrl, "/")
  38. if name == "weibo" {
  39. appUrl = strings.Replace(appUrl, "localhost", "127.0.0.1", 1)
  40. }
  41. code := ctx.Query("code")
  42. if code == "" {
  43. // redirect to social login page
  44. connect.SetRedirectUrl(appUrl + ctx.Req.URL.Path)
  45. ctx.Redirect(connect.AuthCodeURL(next))
  46. return
  47. }
  48. // handle call back
  49. tk, err := connect.Exchange(code)
  50. if err != nil {
  51. ctx.Handle(500, "social.SocialSignIn(Exchange)", err)
  52. return
  53. }
  54. next = extractPath(ctx.Query("state"))
  55. log.Trace("social.SocialSignIn(Got token)")
  56. ui, err := connect.UserInfo(tk, ctx.Req.URL)
  57. if err != nil {
  58. ctx.Handle(500, fmt.Sprintf("social.SocialSignIn(get info from %s)", name), err)
  59. return
  60. }
  61. log.Info("social.SocialSignIn(social login): %s", ui)
  62. oa, err := models.GetOauth2(ui.Identity)
  63. switch err {
  64. case nil:
  65. ctx.Session.Set("uid", oa.User.Id)
  66. ctx.Session.Set("uname", oa.User.Name)
  67. case models.ErrOauth2RecordNotExist:
  68. raw, _ := json.Marshal(tk)
  69. oa = &models.Oauth2{
  70. Uid: -1,
  71. Type: connect.Type(),
  72. Identity: ui.Identity,
  73. Token: string(raw),
  74. }
  75. log.Trace("social.SocialSignIn(oa): %v", oa)
  76. if err = models.AddOauth2(oa); err != nil {
  77. log.Error(4, "social.SocialSignIn(add oauth2): %v", err) // 501
  78. return
  79. }
  80. case models.ErrOauth2NotAssociated:
  81. next = "/user/sign_up"
  82. default:
  83. ctx.Handle(500, "social.SocialSignIn(GetOauth2)", err)
  84. return
  85. }
  86. oa.Updated = time.Now()
  87. if err = models.UpdateOauth2(oa); err != nil {
  88. log.Error(4, "UpdateOauth2: %v", err)
  89. }
  90. ctx.Session.Set("socialId", oa.Id)
  91. ctx.Session.Set("socialName", ui.Name)
  92. ctx.Session.Set("socialEmail", ui.Email)
  93. log.Trace("social.SocialSignIn(social ID): %v", oa.Id)
  94. ctx.Redirect(next)
  95. }