0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-26 17:34:11 -05:00
forgejo/models/user/openid.go

109 lines
2.8 KiB
Go
Raw Normal View History

2017-03-17 09:16:08 -05: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 user
2017-03-17 09:16:08 -05:00
import (
"context"
2017-03-17 09:16:08 -05:00
"errors"
"fmt"
2017-03-17 09:16:08 -05:00
"code.gitea.io/gitea/models/db"
2017-03-17 09:16:08 -05:00
)
// ErrOpenIDNotExist openid is not known
var ErrOpenIDNotExist = errors.New("OpenID is unknown")
2017-03-17 09:16:08 -05:00
// UserOpenID is the list of all OpenID identities of a user.
// 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-20 19:55:00 -05: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 09:16:08 -05:00
}
func init() {
db.RegisterModel(new(UserOpenID))
}
2017-03-17 09:16:08 -05:00
// GetUserOpenIDs returns all openid addresses that belongs to given user.
func GetUserOpenIDs(uid int64) ([]*UserOpenID, error) {
openids := make([]*UserOpenID, 0, 5)
if err := db.GetEngine(db.DefaultContext).
2017-03-17 09:16:08 -05:00
Where("uid=?", uid).
Asc("id").
2017-03-17 09:16:08 -05:00
Find(&openids); err != nil {
return nil, err
}
return openids, nil
}
// isOpenIDUsed returns true if the openid has been used.
func isOpenIDUsed(ctx context.Context, uri string) (bool, error) {
2017-03-17 09:16:08 -05:00
if len(uri) == 0 {
return true, nil
}
return db.GetEngine(ctx).Get(&UserOpenID{URI: uri})
2017-03-17 09:16:08 -05: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)
}
// AddUserOpenID adds an pre-verified/normalized OpenID URI to given user.
2017-03-17 09:16:08 -05:00
// NOTE: make sure openid.URI is normalized already
func AddUserOpenID(ctx context.Context, openid *UserOpenID) error {
used, err := isOpenIDUsed(ctx, openid.URI)
2017-03-17 09:16:08 -05:00
if err != nil {
return err
} else if used {
return ErrOpenIDAlreadyUsed{openid.URI}
}
return db.Insert(ctx, openid)
2017-03-17 09:16:08 -05:00
}
// DeleteUserOpenID deletes an openid address of given user.
func DeleteUserOpenID(openid *UserOpenID) (err error) {
var deleted int64
// ask to check UID
address := UserOpenID{
2017-03-17 09:16:08 -05:00
UID: openid.UID,
}
if openid.ID > 0 {
deleted, err = db.GetEngine(db.DefaultContext).ID(openid.ID).Delete(&address)
2017-03-17 09:16:08 -05:00
} else {
deleted, err = db.GetEngine(db.DefaultContext).
2017-03-17 09:16:08 -05:00
Where("openid=?", openid.URI).
Delete(&address)
}
if err != nil {
return err
} else if deleted != 1 {
return ErrOpenIDNotExist
}
return nil
}
// ToggleUserOpenIDVisibility toggles visibility of an openid address of given user.
func ToggleUserOpenIDVisibility(id int64) (err error) {
_, err = db.GetEngine(db.DefaultContext).Exec("update `user_open_id` set `show` = not `show` where `id` = ?", id)
return err
}