social.go 2.4 KB

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