2019-10-14 00:10:42 -06: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 05:32:56 -06:00
|
|
|
"code.gitea.io/gitea/models/login"
|
2021-11-24 02:49:20 -07:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-10-14 00:10:42 -06:00
|
|
|
"code.gitea.io/gitea/modules/structs"
|
|
|
|
|
|
|
|
"github.com/markbates/goth"
|
|
|
|
)
|
|
|
|
|
2021-12-14 01:37:11 -07:00
|
|
|
func toExternalLoginUser(user *user_model.User, gothUser goth.User) (*user_model.ExternalLoginUser, error) {
|
2021-09-24 05:32:56 -06:00
|
|
|
loginSource, err := login.GetActiveOAuth2LoginSourceByName(gothUser.Provider)
|
2019-10-14 00:10:42 -06:00
|
|
|
if err != nil {
|
2021-12-14 01:37:11 -07:00
|
|
|
return nil, err
|
2019-10-14 00:10:42 -06:00
|
|
|
}
|
2021-12-14 01:37:11 -07:00
|
|
|
return &user_model.ExternalLoginUser{
|
2019-10-14 00:10:42 -06: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-12-14 01:37:11 -07:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LinkAccountToUser link the gothUser to the user
|
|
|
|
func LinkAccountToUser(user *user_model.User, gothUser goth.User) error {
|
|
|
|
externalLoginUser, err := toExternalLoginUser(user, gothUser)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-14 00:10:42 -06:00
|
|
|
}
|
|
|
|
|
2021-11-28 07:11:58 -07:00
|
|
|
if err := user_model.LinkExternalToUser(user, externalLoginUser); err != nil {
|
2019-10-14 00:10:42 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-16 20:06:28 -06:00
|
|
|
externalID := externalLoginUser.ExternalID
|
2019-10-14 00:10:42 -06: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
|
|
|
|
}
|
2021-12-14 01:37:11 -07:00
|
|
|
|
|
|
|
// UpdateExternalUser updates external user's information
|
|
|
|
func UpdateExternalUser(user *user_model.User, gothUser goth.User) error {
|
|
|
|
externalLoginUser, err := toExternalLoginUser(user, gothUser)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user_model.UpdateExternalUserByExternalID(externalLoginUser)
|
|
|
|
}
|