2017-03-17 23:16:08 +09:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-28 03:20:29 +09:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-03-17 23:16:08 +09:00
|
|
|
|
2021-11-17 18:58:31 +09:00
|
|
|
package user
|
2017-03-17 23:16:08 +09:00
|
|
|
|
|
|
|
import (
|
2022-05-20 23:08:52 +09:00
|
|
|
"context"
|
2021-11-17 18:58:31 +09:00
|
|
|
"fmt"
|
2017-03-17 23:16:08 +09:00
|
|
|
|
2021-09-19 20:49:59 +09:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-10-18 14:50:37 +09:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2017-03-17 23:16:08 +09:00
|
|
|
)
|
|
|
|
|
2021-03-15 03:52:12 +09:00
|
|
|
// ErrOpenIDNotExist openid is not known
|
2022-12-31 20:49:37 +09:00
|
|
|
var ErrOpenIDNotExist = util.NewNotExistErrorf("OpenID is unknown")
|
2017-03-17 23:16:08 +09:00
|
|
|
|
|
|
|
// UserOpenID is the list of all OpenID identities of a user.
|
2021-11-17 18:58:31 +09:00
|
|
|
// Since this is a middle table, name it OpenID is not suitable, so we ignore the lint here
|
|
|
|
type UserOpenID struct { //revive:disable-line:exported
|
2017-03-21 09:55:00 +09: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 23:16:08 +09:00
|
|
|
}
|
|
|
|
|
2021-09-19 20:49:59 +09:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(UserOpenID))
|
|
|
|
}
|
|
|
|
|
2017-03-17 23:16:08 +09:00
|
|
|
// GetUserOpenIDs returns all openid addresses that belongs to given user.
|
2023-09-15 15:13:19 +09:00
|
|
|
func GetUserOpenIDs(ctx context.Context, uid int64) ([]*UserOpenID, error) {
|
2017-03-17 23:16:08 +09:00
|
|
|
openids := make([]*UserOpenID, 0, 5)
|
2023-09-15 15:13:19 +09:00
|
|
|
if err := db.GetEngine(ctx).
|
2017-03-17 23:16:08 +09:00
|
|
|
Where("uid=?", uid).
|
2017-03-20 17:31:08 +09:00
|
|
|
Asc("id").
|
2017-03-17 23:16:08 +09:00
|
|
|
Find(&openids); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return openids, nil
|
|
|
|
}
|
|
|
|
|
2021-04-09 16:40:34 +09:00
|
|
|
// isOpenIDUsed returns true if the openid has been used.
|
2022-05-20 23:08:52 +09:00
|
|
|
func isOpenIDUsed(ctx context.Context, uri string) (bool, error) {
|
2017-03-17 23:16:08 +09:00
|
|
|
if len(uri) == 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:08:52 +09:00
|
|
|
return db.GetEngine(ctx).Get(&UserOpenID{URI: uri})
|
2017-03-17 23:16:08 +09:00
|
|
|
}
|
|
|
|
|
2021-11-17 18:58:31 +09:00
|
|
|
// ErrOpenIDAlreadyUsed represents a "OpenIDAlreadyUsed" kind of error.
|
|
|
|
type ErrOpenIDAlreadyUsed struct {
|
|
|
|
OpenID string
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrOpenIDAlreadyUsed checks if an error is a ErrOpenIDAlreadyUsed.
|
|
|
|
func IsErrOpenIDAlreadyUsed(err error) bool {
|
|
|
|
_, ok := err.(ErrOpenIDAlreadyUsed)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrOpenIDAlreadyUsed) Error() string {
|
|
|
|
return fmt.Sprintf("OpenID already in use [oid: %s]", err.OpenID)
|
|
|
|
}
|
|
|
|
|
2022-10-18 14:50:37 +09:00
|
|
|
func (err ErrOpenIDAlreadyUsed) Unwrap() error {
|
|
|
|
return util.ErrAlreadyExist
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:08:52 +09:00
|
|
|
// AddUserOpenID adds an pre-verified/normalized OpenID URI to given user.
|
2017-03-17 23:16:08 +09:00
|
|
|
// NOTE: make sure openid.URI is normalized already
|
2022-05-20 23:08:52 +09:00
|
|
|
func AddUserOpenID(ctx context.Context, openid *UserOpenID) error {
|
|
|
|
used, err := isOpenIDUsed(ctx, openid.URI)
|
2017-03-17 23:16:08 +09:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if used {
|
|
|
|
return ErrOpenIDAlreadyUsed{openid.URI}
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:08:52 +09:00
|
|
|
return db.Insert(ctx, openid)
|
2017-03-17 23:16:08 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteUserOpenID deletes an openid address of given user.
|
2023-09-15 15:13:19 +09:00
|
|
|
func DeleteUserOpenID(ctx context.Context, openid *UserOpenID) (err error) {
|
2017-03-17 23:16:08 +09:00
|
|
|
var deleted int64
|
|
|
|
// ask to check UID
|
2021-03-15 03:52:12 +09:00
|
|
|
address := UserOpenID{
|
2017-03-17 23:16:08 +09:00
|
|
|
UID: openid.UID,
|
|
|
|
}
|
|
|
|
if openid.ID > 0 {
|
2023-09-15 15:13:19 +09:00
|
|
|
deleted, err = db.GetEngine(ctx).ID(openid.ID).Delete(&address)
|
2017-03-17 23:16:08 +09:00
|
|
|
} else {
|
2023-09-15 15:13:19 +09:00
|
|
|
deleted, err = db.GetEngine(ctx).
|
2017-03-17 23:16:08 +09:00
|
|
|
Where("openid=?", openid.URI).
|
|
|
|
Delete(&address)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if deleted != 1 {
|
|
|
|
return ErrOpenIDNotExist
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-20 17:31:08 +09:00
|
|
|
// ToggleUserOpenIDVisibility toggles visibility of an openid address of given user.
|
2023-09-15 15:13:19 +09:00
|
|
|
func ToggleUserOpenIDVisibility(ctx context.Context, id int64) (err error) {
|
|
|
|
_, err = db.GetEngine(ctx).Exec("update `user_open_id` set `show` = not `show` where `id` = ?", id)
|
2017-03-20 17:31:08 +09:00
|
|
|
return err
|
|
|
|
}
|