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.

95 lines
2.3 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
  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. // "strings"
  10. "time"
  11. "github.com/macaron-contrib/oauth2"
  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 SocialSignIn(ctx *middleware.Context) {
  19. if setting.OauthService == nil {
  20. ctx.Handle(404, "OAuth2 service not enabled", nil)
  21. return
  22. }
  23. next := setting.AppSubUrl + "/user/login"
  24. info := ctx.Session.Get(oauth2.KEY_TOKEN)
  25. if info == nil {
  26. ctx.Redirect(next)
  27. return
  28. }
  29. name := ctx.Params(":name")
  30. connect, ok := social.SocialMap[name]
  31. if !ok {
  32. ctx.Handle(404, "social login not enabled", errors.New(name))
  33. return
  34. }
  35. tk := new(oauth2.Token)
  36. if err := json.Unmarshal(info.([]byte), tk); err != nil {
  37. ctx.Handle(500, "Unmarshal token", err)
  38. return
  39. }
  40. ui, err := connect.UserInfo(tk, ctx.Req.URL)
  41. if err != nil {
  42. ctx.Handle(500, fmt.Sprintf("UserInfo(%s)", name), err)
  43. return
  44. }
  45. if len(ui.Identity) == 0 {
  46. ctx.Handle(404, "no identity is presented", errors.New(name))
  47. return
  48. }
  49. log.Info("social.SocialSignIn(social login): %s", ui)
  50. oa, err := models.GetOauth2(ui.Identity)
  51. switch err {
  52. case nil:
  53. ctx.Session.Set("uid", oa.User.Id)
  54. ctx.Session.Set("uname", oa.User.Name)
  55. case models.ErrOauth2RecordNotExist:
  56. raw, _ := json.Marshal(tk)
  57. oa = &models.Oauth2{
  58. Uid: -1,
  59. Type: connect.Type(),
  60. Identity: ui.Identity,
  61. Token: string(raw),
  62. }
  63. log.Trace("social.SocialSignIn(oa): %v", oa)
  64. if err = models.AddOauth2(oa); err != nil {
  65. log.Error(4, "social.SocialSignIn(add oauth2): %v", err) // 501
  66. return
  67. }
  68. case models.ErrOauth2NotAssociated:
  69. next = setting.AppSubUrl + "/user/sign_up"
  70. default:
  71. ctx.Handle(500, "social.SocialSignIn(GetOauth2)", err)
  72. return
  73. }
  74. oa.Updated = time.Now()
  75. if err = models.UpdateOauth2(oa); err != nil {
  76. log.Error(4, "UpdateOauth2: %v", err)
  77. }
  78. ctx.Session.Set("socialId", oa.Id)
  79. ctx.Session.Set("socialName", ui.Name)
  80. ctx.Session.Set("socialEmail", ui.Email)
  81. log.Trace("social.SocialSignIn(social ID): %v", oa.Id)
  82. ctx.Redirect(next)
  83. }