2019-10-14 01:10:42 -05:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package externalaccount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-09-24 06:32:56 -05:00
|
|
|
"code.gitea.io/gitea/models/login"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-10-14 01:10:42 -05:00
|
|
|
"code.gitea.io/gitea/modules/structs"
|
|
|
|
|
|
|
|
"github.com/markbates/goth"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LinkAccountToUser link the gothUser to the user
|
2021-11-24 04:49:20 -05:00
|
|
|
func LinkAccountToUser(user *user_model.User, gothUser goth.User) error {
|
2021-09-24 06:32:56 -05:00
|
|
|
loginSource, err := login.GetActiveOAuth2LoginSourceByName(gothUser.Provider)
|
2019-10-14 01:10:42 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-28 09:11:58 -05:00
|
|
|
externalLoginUser := &user_model.ExternalLoginUser{
|
2019-10-14 01:10:42 -05:00
|
|
|
ExternalID: gothUser.UserID,
|
|
|
|
UserID: user.ID,
|
|
|
|
LoginSourceID: loginSource.ID,
|
|
|
|
RawData: gothUser.RawData,
|
|
|
|
Provider: gothUser.Provider,
|
|
|
|
Email: gothUser.Email,
|
|
|
|
Name: gothUser.Name,
|
|
|
|
FirstName: gothUser.FirstName,
|
|
|
|
LastName: gothUser.LastName,
|
|
|
|
NickName: gothUser.NickName,
|
|
|
|
Description: gothUser.Description,
|
|
|
|
AvatarURL: gothUser.AvatarURL,
|
|
|
|
Location: gothUser.Location,
|
|
|
|
AccessToken: gothUser.AccessToken,
|
|
|
|
AccessTokenSecret: gothUser.AccessTokenSecret,
|
|
|
|
RefreshToken: gothUser.RefreshToken,
|
|
|
|
ExpiresAt: gothUser.ExpiresAt,
|
|
|
|
}
|
|
|
|
|
2021-11-28 09:11:58 -05:00
|
|
|
if err := user_model.LinkExternalToUser(user, externalLoginUser); err != nil {
|
2019-10-14 01:10:42 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-16 21:06:28 -05:00
|
|
|
externalID := externalLoginUser.ExternalID
|
2019-10-14 01:10:42 -05:00
|
|
|
|
|
|
|
var tp structs.GitServiceType
|
|
|
|
for _, s := range structs.SupportedFullGitService {
|
|
|
|
if strings.EqualFold(s.Name(), gothUser.Provider) {
|
|
|
|
tp = s
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tp.Name() != "" {
|
|
|
|
return models.UpdateMigrationsByType(tp, externalID, user.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|