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.

66 lines
1.7 KiB

  1. // Copyright 2019 The Gitea 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 externalaccount
  5. import (
  6. "strconv"
  7. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/structs"
  10. "github.com/markbates/goth"
  11. )
  12. // LinkAccountToUser link the gothUser to the user
  13. func LinkAccountToUser(user *models.User, gothUser goth.User) error {
  14. loginSource, err := models.GetActiveOAuth2LoginSourceByName(gothUser.Provider)
  15. if err != nil {
  16. return err
  17. }
  18. externalLoginUser := &models.ExternalLoginUser{
  19. ExternalID: gothUser.UserID,
  20. UserID: user.ID,
  21. LoginSourceID: loginSource.ID,
  22. RawData: gothUser.RawData,
  23. Provider: gothUser.Provider,
  24. Email: gothUser.Email,
  25. Name: gothUser.Name,
  26. FirstName: gothUser.FirstName,
  27. LastName: gothUser.LastName,
  28. NickName: gothUser.NickName,
  29. Description: gothUser.Description,
  30. AvatarURL: gothUser.AvatarURL,
  31. Location: gothUser.Location,
  32. AccessToken: gothUser.AccessToken,
  33. AccessTokenSecret: gothUser.AccessTokenSecret,
  34. RefreshToken: gothUser.RefreshToken,
  35. ExpiresAt: gothUser.ExpiresAt,
  36. }
  37. if err := models.LinkExternalToUser(user, externalLoginUser); err != nil {
  38. return err
  39. }
  40. externalID, err := strconv.ParseInt(externalLoginUser.ExternalID, 10, 64)
  41. if err != nil {
  42. return err
  43. }
  44. var tp structs.GitServiceType
  45. for _, s := range structs.SupportedFullGitService {
  46. if strings.EqualFold(s.Name(), gothUser.Provider) {
  47. tp = s
  48. break
  49. }
  50. }
  51. if tp.Name() != "" {
  52. return models.UpdateMigrationsByType(tp, externalID, user.ID)
  53. }
  54. return nil
  55. }