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.

99 lines
2.4 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
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. "github.com/go-martini/martini"
  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, params martini.Params) {
  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 := 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. code := ctx.Query("code")
  38. if code == "" {
  39. // redirect to social login page
  40. connect.SetRedirectUrl(strings.TrimSuffix(setting.AppUrl, "/") + ctx.Req.URL.Path)
  41. ctx.Redirect(connect.AuthCodeURL(next))
  42. return
  43. }
  44. // handle call back
  45. tk, err := connect.Exchange(code)
  46. if err != nil {
  47. ctx.Handle(500, "social.SocialSignIn(Exchange)", err)
  48. return
  49. }
  50. next = extractPath(ctx.Query("state"))
  51. log.Trace("social.SocialSignIn(Got token)")
  52. ui, err := connect.UserInfo(tk, ctx.Req.URL)
  53. if err != nil {
  54. ctx.Handle(500, fmt.Sprintf("social.SocialSignIn(get info from %s)", name), err)
  55. return
  56. }
  57. log.Info("social.SocialSignIn(social login): %s", ui)
  58. oa, err := models.GetOauth2(ui.Identity)
  59. switch err {
  60. case nil:
  61. ctx.Session.Set("userId", oa.User.Id)
  62. ctx.Session.Set("userName", oa.User.Name)
  63. case models.ErrOauth2RecordNotExist:
  64. raw, _ := json.Marshal(tk)
  65. oa = &models.Oauth2{
  66. Uid: -1,
  67. Type: connect.Type(),
  68. Identity: ui.Identity,
  69. Token: string(raw),
  70. }
  71. log.Trace("social.SocialSignIn(oa): %v", oa)
  72. if err = models.AddOauth2(oa); err != nil {
  73. log.Error("social.SocialSignIn(add oauth2): %v", err) // 501
  74. return
  75. }
  76. case models.ErrOauth2NotAssociated:
  77. next = "/user/sign_up"
  78. default:
  79. ctx.Handle(500, "social.SocialSignIn(GetOauth2)", err)
  80. return
  81. }
  82. ctx.Session.Set("socialId", oa.Id)
  83. ctx.Session.Set("socialName", ui.Name)
  84. ctx.Session.Set("socialEmail", ui.Email)
  85. log.Trace("social.SocialSignIn(social ID): %v", oa.Id)
  86. ctx.Redirect(next)
  87. }