2017-02-22 00:14:37 -07:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
2021-11-28 07:11:58 -07:00
|
|
|
package user
|
2017-02-22 00:14:37 -07:00
|
|
|
|
2019-10-14 00:10:42 -06:00
|
|
|
import (
|
2021-11-28 07:11:58 -07:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-10-14 00:10:42 -06:00
|
|
|
"time"
|
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-09-24 05:32:56 -06:00
|
|
|
"code.gitea.io/gitea/models/login"
|
2019-10-14 00:10:42 -06:00
|
|
|
|
|
|
|
"github.com/markbates/goth"
|
|
|
|
"xorm.io/builder"
|
|
|
|
)
|
2017-02-22 00:14:37 -07:00
|
|
|
|
2021-11-28 07:11:58 -07:00
|
|
|
// ErrExternalLoginUserAlreadyExist represents a "ExternalLoginUserAlreadyExist" kind of error.
|
|
|
|
type ErrExternalLoginUserAlreadyExist struct {
|
|
|
|
ExternalID string
|
|
|
|
UserID int64
|
|
|
|
LoginSourceID int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrExternalLoginUserAlreadyExist checks if an error is a ExternalLoginUserAlreadyExist.
|
|
|
|
func IsErrExternalLoginUserAlreadyExist(err error) bool {
|
|
|
|
_, ok := err.(ErrExternalLoginUserAlreadyExist)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrExternalLoginUserAlreadyExist) Error() string {
|
|
|
|
return fmt.Sprintf("external login user already exists [externalID: %s, userID: %d, loginSourceID: %d]", err.ExternalID, err.UserID, err.LoginSourceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrExternalLoginUserNotExist represents a "ExternalLoginUserNotExist" kind of error.
|
|
|
|
type ErrExternalLoginUserNotExist struct {
|
|
|
|
UserID int64
|
|
|
|
LoginSourceID int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrExternalLoginUserNotExist checks if an error is a ExternalLoginUserNotExist.
|
|
|
|
func IsErrExternalLoginUserNotExist(err error) bool {
|
|
|
|
_, ok := err.(ErrExternalLoginUserNotExist)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrExternalLoginUserNotExist) Error() string {
|
|
|
|
return fmt.Sprintf("external login user link does not exists [userID: %d, loginSourceID: %d]", err.UserID, err.LoginSourceID)
|
|
|
|
}
|
|
|
|
|
2017-02-22 00:14:37 -07:00
|
|
|
// ExternalLoginUser makes the connecting between some existing user and additional external login sources
|
|
|
|
type ExternalLoginUser struct {
|
2019-10-14 00:10:42 -06:00
|
|
|
ExternalID string `xorm:"pk NOT NULL"`
|
|
|
|
UserID int64 `xorm:"INDEX NOT NULL"`
|
|
|
|
LoginSourceID int64 `xorm:"pk NOT NULL"`
|
|
|
|
RawData map[string]interface{} `xorm:"TEXT JSON"`
|
|
|
|
Provider string `xorm:"index VARCHAR(25)"`
|
|
|
|
Email string
|
|
|
|
Name string
|
|
|
|
FirstName string
|
|
|
|
LastName string
|
|
|
|
NickName string
|
|
|
|
Description string
|
|
|
|
AvatarURL string
|
|
|
|
Location string
|
2019-10-18 00:58:36 -06:00
|
|
|
AccessToken string `xorm:"TEXT"`
|
|
|
|
AccessTokenSecret string `xorm:"TEXT"`
|
|
|
|
RefreshToken string `xorm:"TEXT"`
|
2019-10-14 00:10:42 -06:00
|
|
|
ExpiresAt time.Time
|
2017-02-22 00:14:37 -07:00
|
|
|
}
|
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(ExternalLoginUser))
|
|
|
|
}
|
|
|
|
|
2017-02-22 00:14:37 -07:00
|
|
|
// GetExternalLogin checks if a externalID in loginSourceID scope already exists
|
|
|
|
func GetExternalLogin(externalLoginUser *ExternalLoginUser) (bool, error) {
|
2021-09-23 09:45:36 -06:00
|
|
|
return db.GetEngine(db.DefaultContext).Get(externalLoginUser)
|
2017-02-22 00:14:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListAccountLinks returns a map with the ExternalLoginUser and its LoginSource
|
2021-11-28 07:11:58 -07:00
|
|
|
func ListAccountLinks(user *User) ([]*ExternalLoginUser, error) {
|
2017-02-22 00:14:37 -07:00
|
|
|
externalAccounts := make([]*ExternalLoginUser, 0, 5)
|
2021-09-23 09:45:36 -06:00
|
|
|
err := db.GetEngine(db.DefaultContext).Where("user_id=?", user.ID).
|
2017-02-22 00:14:37 -07:00
|
|
|
Desc("login_source_id").
|
|
|
|
Find(&externalAccounts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return externalAccounts, nil
|
|
|
|
}
|
|
|
|
|
2019-10-14 00:10:42 -06:00
|
|
|
// LinkExternalToUser link the external user to the user
|
2021-11-28 07:11:58 -07:00
|
|
|
func LinkExternalToUser(user *User, externalLoginUser *ExternalLoginUser) error {
|
2021-09-23 09:45:36 -06:00
|
|
|
has, err := db.GetEngine(db.DefaultContext).Where("external_id=? AND login_source_id=?", externalLoginUser.ExternalID, externalLoginUser.LoginSourceID).
|
2019-10-14 00:10:42 -06:00
|
|
|
NoAutoCondition().
|
|
|
|
Exist(externalLoginUser)
|
2017-02-22 00:14:37 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if has {
|
2019-10-14 00:10:42 -06:00
|
|
|
return ErrExternalLoginUserAlreadyExist{externalLoginUser.ExternalID, user.ID, externalLoginUser.LoginSourceID}
|
2017-02-22 00:14:37 -07:00
|
|
|
}
|
|
|
|
|
2021-09-23 09:45:36 -06:00
|
|
|
_, err = db.GetEngine(db.DefaultContext).Insert(externalLoginUser)
|
2017-02-22 00:14:37 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAccountLink will remove all external login sources for the given user
|
2021-11-28 07:11:58 -07:00
|
|
|
func RemoveAccountLink(user *User, loginSourceID int64) (int64, error) {
|
2021-09-23 09:45:36 -06:00
|
|
|
deleted, err := db.GetEngine(db.DefaultContext).Delete(&ExternalLoginUser{UserID: user.ID, LoginSourceID: loginSourceID})
|
2017-02-22 00:14:37 -07:00
|
|
|
if err != nil {
|
|
|
|
return deleted, err
|
|
|
|
}
|
|
|
|
if deleted < 1 {
|
|
|
|
return deleted, ErrExternalLoginUserNotExist{user.ID, loginSourceID}
|
|
|
|
}
|
|
|
|
return deleted, err
|
|
|
|
}
|
|
|
|
|
2021-11-28 07:11:58 -07:00
|
|
|
// RemoveAllAccountLinks will remove all external login sources for the given user
|
|
|
|
func RemoveAllAccountLinks(ctx context.Context, user *User) error {
|
|
|
|
_, err := db.GetEngine(ctx).Delete(&ExternalLoginUser{UserID: user.ID})
|
2017-02-22 00:14:37 -07:00
|
|
|
return err
|
|
|
|
}
|
2019-10-14 00:10:42 -06:00
|
|
|
|
|
|
|
// GetUserIDByExternalUserID get user id according to provider and userID
|
2021-03-14 12:52:12 -06:00
|
|
|
func GetUserIDByExternalUserID(provider, userID string) (int64, error) {
|
2019-10-14 00:10:42 -06:00
|
|
|
var id int64
|
2021-09-23 09:45:36 -06:00
|
|
|
_, err := db.GetEngine(db.DefaultContext).Table("external_login_user").
|
2019-10-14 00:10:42 -06:00
|
|
|
Select("user_id").
|
|
|
|
Where("provider=?", provider).
|
|
|
|
And("external_id=?", userID).
|
|
|
|
Get(&id)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateExternalUser updates external user's information
|
2021-11-28 07:11:58 -07:00
|
|
|
func UpdateExternalUser(user *User, gothUser goth.User) 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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
externalLoginUser := &ExternalLoginUser{
|
|
|
|
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-09-23 09:45:36 -06:00
|
|
|
has, err := db.GetEngine(db.DefaultContext).Where("external_id=? AND login_source_id=?", gothUser.UserID, loginSource.ID).
|
2019-10-14 00:10:42 -06:00
|
|
|
NoAutoCondition().
|
|
|
|
Exist(externalLoginUser)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
|
|
|
return ErrExternalLoginUserNotExist{user.ID, loginSource.ID}
|
|
|
|
}
|
|
|
|
|
2021-09-23 09:45:36 -06:00
|
|
|
_, err = db.GetEngine(db.DefaultContext).Where("external_id=? AND login_source_id=?", gothUser.UserID, loginSource.ID).AllCols().Update(externalLoginUser)
|
2019-10-14 00:10:42 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindExternalUserOptions represents an options to find external users
|
|
|
|
type FindExternalUserOptions struct {
|
|
|
|
Provider string
|
|
|
|
Limit int
|
|
|
|
Start int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts FindExternalUserOptions) toConds() builder.Cond {
|
2021-03-14 12:52:12 -06:00
|
|
|
cond := builder.NewCond()
|
2019-10-14 00:10:42 -06:00
|
|
|
if len(opts.Provider) > 0 {
|
|
|
|
cond = cond.And(builder.Eq{"provider": opts.Provider})
|
|
|
|
}
|
|
|
|
return cond
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindExternalUsersByProvider represents external users via provider
|
|
|
|
func FindExternalUsersByProvider(opts FindExternalUserOptions) ([]ExternalLoginUser, error) {
|
|
|
|
var users []ExternalLoginUser
|
2021-09-23 09:45:36 -06:00
|
|
|
err := db.GetEngine(db.DefaultContext).Where(opts.toConds()).
|
2019-10-14 00:10:42 -06:00
|
|
|
Limit(opts.Limit, opts.Start).
|
2019-10-14 01:22:46 -06:00
|
|
|
OrderBy("login_source_id ASC, external_id ASC").
|
2019-10-14 00:10:42 -06:00
|
|
|
Find(&users)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|