social.go 2.4 KB

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