2017-03-17 22:16:08 +08: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.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2017-03-17 22:16:08 +08:00
|
|
|
"code.gitea.io/gitea/modules/auth/openid"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
)
|
|
|
|
|
2021-03-15 02:52:12 +08:00
|
|
|
// ErrOpenIDNotExist openid is not known
|
|
|
|
var ErrOpenIDNotExist = errors.New("OpenID is unknown")
|
2017-03-17 22:16:08 +08:00
|
|
|
|
|
|
|
// UserOpenID is the list of all OpenID identities of a user.
|
|
|
|
type UserOpenID struct {
|
2017-03-21 08:55:00 +08:00
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
UID int64 `xorm:"INDEX NOT NULL"`
|
|
|
|
URI string `xorm:"UNIQUE NOT NULL"`
|
|
|
|
Show bool `xorm:"DEFAULT false"`
|
2017-03-17 22:16:08 +08:00
|
|
|
}
|
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(UserOpenID))
|
|
|
|
}
|
|
|
|
|
2017-03-17 22:16:08 +08:00
|
|
|
// GetUserOpenIDs returns all openid addresses that belongs to given user.
|
|
|
|
func GetUserOpenIDs(uid int64) ([]*UserOpenID, error) {
|
|
|
|
openids := make([]*UserOpenID, 0, 5)
|
2021-09-23 23:45:36 +08:00
|
|
|
if err := db.GetEngine(db.DefaultContext).
|
2017-03-17 22:16:08 +08:00
|
|
|
Where("uid=?", uid).
|
2017-03-20 16:31:08 +08:00
|
|
|
Asc("id").
|
2017-03-17 22:16:08 +08:00
|
|
|
Find(&openids); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return openids, nil
|
|
|
|
}
|
|
|
|
|
2021-04-09 15:40:34 +08:00
|
|
|
// isOpenIDUsed returns true if the openid has been used.
|
2021-09-19 19:49:59 +08:00
|
|
|
func isOpenIDUsed(e db.Engine, uri string) (bool, error) {
|
2017-03-17 22:16:08 +08:00
|
|
|
if len(uri) == 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.Get(&UserOpenID{URI: uri})
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: make sure openid.URI is normalized already
|
2021-09-19 19:49:59 +08:00
|
|
|
func addUserOpenID(e db.Engine, openid *UserOpenID) error {
|
2017-03-17 22:16:08 +08:00
|
|
|
used, err := isOpenIDUsed(e, openid.URI)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if used {
|
|
|
|
return ErrOpenIDAlreadyUsed{openid.URI}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = e.Insert(openid)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddUserOpenID adds an pre-verified/normalized OpenID URI to given user.
|
|
|
|
func AddUserOpenID(openid *UserOpenID) error {
|
2021-09-23 23:45:36 +08:00
|
|
|
return addUserOpenID(db.GetEngine(db.DefaultContext), openid)
|
2017-03-17 22:16:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteUserOpenID deletes an openid address of given user.
|
|
|
|
func DeleteUserOpenID(openid *UserOpenID) (err error) {
|
|
|
|
var deleted int64
|
|
|
|
// ask to check UID
|
2021-03-15 02:52:12 +08:00
|
|
|
address := UserOpenID{
|
2017-03-17 22:16:08 +08:00
|
|
|
UID: openid.UID,
|
|
|
|
}
|
|
|
|
if openid.ID > 0 {
|
2021-09-23 23:45:36 +08:00
|
|
|
deleted, err = db.GetEngine(db.DefaultContext).ID(openid.ID).Delete(&address)
|
2017-03-17 22:16:08 +08:00
|
|
|
} else {
|
2021-09-23 23:45:36 +08:00
|
|
|
deleted, err = db.GetEngine(db.DefaultContext).
|
2017-03-17 22:16:08 +08:00
|
|
|
Where("openid=?", openid.URI).
|
|
|
|
Delete(&address)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if deleted != 1 {
|
|
|
|
return ErrOpenIDNotExist
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-20 16:31:08 +08:00
|
|
|
// ToggleUserOpenIDVisibility toggles visibility of an openid address of given user.
|
|
|
|
func ToggleUserOpenIDVisibility(id int64) (err error) {
|
2021-09-23 23:45:36 +08:00
|
|
|
_, err = db.GetEngine(db.DefaultContext).Exec("update `user_open_id` set `show` = not `show` where `id` = ?", id)
|
2017-03-20 16:31:08 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-17 22:16:08 +08:00
|
|
|
// GetUserByOpenID returns the user object by given OpenID if exists.
|
|
|
|
func GetUserByOpenID(uri string) (*User, error) {
|
|
|
|
if len(uri) == 0 {
|
|
|
|
return nil, ErrUserNotExist{0, uri, 0}
|
|
|
|
}
|
|
|
|
|
|
|
|
uri, err := openid.Normalize(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("Normalized OpenID URI: " + uri)
|
|
|
|
|
|
|
|
// Otherwise, check in openid table
|
2020-06-18 01:50:11 +08:00
|
|
|
oid := &UserOpenID{}
|
2021-09-23 23:45:36 +08:00
|
|
|
has, err := db.GetEngine(db.DefaultContext).Where("uri=?", uri).Get(oid)
|
2017-03-17 22:16:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if has {
|
|
|
|
return GetUserByID(oid.UID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrUserNotExist{0, uri, 0}
|
|
|
|
}
|