mirror of
https://github.com/usememos/memos.git
synced 2026-12-03 09:19:13 +01:00
e52a507924
A configuration file may now declare which identity-provider subject signs
in as a declared user, as a fourth resource kind:
"linkedIdentities": [
{ "name": "users/admin/linkedIdentities/sso", "externalUid": "subject" }
]
An administrator on an SSO-only instance otherwise has no way in before the
first sign-in, which provisions a new account because the sign-in path
matches a provider and subject and nothing else. Matching an email or
username claim was rejected: neither proves ownership on this instance. A
declared subject trusts no claim, and the sign-in path is unchanged.
Rules: the user must be declared in the same configuration, a subject is
declared once, the provider must exist after apply, and a subject already
linked to a different user fails the plan rather than being taken over. A
link made by signing in with the declared subject is adopted; a different
stored subject is replaced. Links are written after users in the apply
transaction. A declared user with a declared link may be created without a
password; it gets an unusable random one, as SSO provisioning does.
The API refuses to create or delete a managed link, and to delete a
provider a declared link names. The account page hides Unlink for a
declared link. LinkedIdentity.external_uid is no longer output only.
180 lines
5.2 KiB
Go
180 lines
5.2 KiB
Go
package sqlite
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/usememos/memos/store"
|
|
)
|
|
|
|
// rowQuerier is satisfied by both *sql.DB and *sql.Tx so the insert statements
|
|
// can be shared between the standalone and transactional creation paths.
|
|
type rowQuerier interface {
|
|
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
|
|
}
|
|
|
|
func insertUser(ctx context.Context, q rowQuerier, create *store.User) error {
|
|
stmt := "INSERT INTO user (`username`, `role`, `email`, `nickname`, `password_hash`, `avatar_url`) VALUES (?, ?, ?, ?, ?, ?) RETURNING id, description, created_ts, updated_ts, row_status"
|
|
return q.QueryRowContext(
|
|
ctx,
|
|
stmt,
|
|
create.Username,
|
|
create.Role,
|
|
nullableEmail(create.Email),
|
|
create.Nickname,
|
|
create.PasswordHash,
|
|
create.AvatarURL,
|
|
).Scan(
|
|
&create.ID,
|
|
&create.Description,
|
|
&create.CreatedTs,
|
|
&create.UpdatedTs,
|
|
&create.RowStatus,
|
|
)
|
|
}
|
|
|
|
func insertUserIdentity(ctx context.Context, q rowQuerier, create *store.UserIdentity) error {
|
|
stmt := "INSERT INTO `user_identity` (`user_id`, `provider`, `extern_uid`) VALUES (?, ?, ?) RETURNING `id`, `created_ts`, `updated_ts`"
|
|
return q.QueryRowContext(ctx, stmt, create.UserID, create.Provider, create.ExternUID).Scan(
|
|
&create.ID,
|
|
&create.CreatedTs,
|
|
&create.UpdatedTs,
|
|
)
|
|
}
|
|
|
|
func (d *DB) CreateUserIdentity(ctx context.Context, create *store.UserIdentity) (*store.UserIdentity, error) {
|
|
return createUserIdentity(ctx, d.db, create)
|
|
}
|
|
|
|
// CreateUserIdentityTx is CreateUserIdentity within tx.
|
|
func (*DB) CreateUserIdentityTx(ctx context.Context, tx *sql.Tx, create *store.UserIdentity) (*store.UserIdentity, error) {
|
|
return createUserIdentity(ctx, tx, create)
|
|
}
|
|
|
|
func createUserIdentity(ctx context.Context, q dbExecutor, create *store.UserIdentity) (*store.UserIdentity, error) {
|
|
if err := insertUserIdentity(ctx, q, create); err != nil {
|
|
return nil, errors.Wrap(err, "failed to create user identity")
|
|
}
|
|
return create, nil
|
|
}
|
|
|
|
func (d *DB) CreateUserWithIdentity(ctx context.Context, createUser *store.User, createIdentity *store.UserIdentity) (*store.User, error) {
|
|
tx, err := d.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to begin user identity transaction")
|
|
}
|
|
defer func() {
|
|
_ = tx.Rollback()
|
|
}()
|
|
|
|
if err := insertUser(ctx, tx, createUser); err != nil {
|
|
return nil, errors.Wrap(err, "failed to create user")
|
|
}
|
|
|
|
createIdentity.UserID = createUser.ID
|
|
if err := insertUserIdentity(ctx, tx, createIdentity); err != nil {
|
|
return nil, errors.Wrap(err, "failed to create user identity")
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
return nil, errors.Wrap(err, "failed to commit user identity transaction")
|
|
}
|
|
return createUser, nil
|
|
}
|
|
|
|
func (d *DB) ListUserIdentities(ctx context.Context, find *store.FindUserIdentity) ([]*store.UserIdentity, error) {
|
|
return listUserIdentities(ctx, d.db, find)
|
|
}
|
|
|
|
// ListUserIdentitiesTx is ListUserIdentities within tx.
|
|
func (*DB) ListUserIdentitiesTx(ctx context.Context, tx *sql.Tx, find *store.FindUserIdentity) ([]*store.UserIdentity, error) {
|
|
return listUserIdentities(ctx, tx, find)
|
|
}
|
|
|
|
func listUserIdentities(ctx context.Context, q dbExecutor, find *store.FindUserIdentity) ([]*store.UserIdentity, error) {
|
|
where, args := []string{"1 = 1"}, []any{}
|
|
|
|
if find.ID != nil {
|
|
where, args = append(where, "`id` = ?"), append(args, *find.ID)
|
|
}
|
|
if find.UserID != nil {
|
|
where, args = append(where, "`user_id` = ?"), append(args, *find.UserID)
|
|
}
|
|
if find.Provider != nil {
|
|
where, args = append(where, "`provider` = ?"), append(args, *find.Provider)
|
|
}
|
|
if find.ExternUID != nil {
|
|
where, args = append(where, "`extern_uid` = ?"), append(args, *find.ExternUID)
|
|
}
|
|
|
|
rows, err := q.QueryContext(ctx, `
|
|
SELECT
|
|
id,
|
|
user_id,
|
|
provider,
|
|
extern_uid,
|
|
created_ts,
|
|
updated_ts
|
|
FROM user_identity
|
|
WHERE `+strings.Join(where, " AND ")+`
|
|
ORDER BY id ASC`,
|
|
args...,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
list := []*store.UserIdentity{}
|
|
for rows.Next() {
|
|
ui := &store.UserIdentity{}
|
|
if err := rows.Scan(
|
|
&ui.ID,
|
|
&ui.UserID,
|
|
&ui.Provider,
|
|
&ui.ExternUID,
|
|
&ui.CreatedTs,
|
|
&ui.UpdatedTs,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
list = append(list, ui)
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
func (d *DB) DeleteUserIdentities(ctx context.Context, delete *store.DeleteUserIdentity) error {
|
|
return deleteUserIdentities(ctx, d.db, delete)
|
|
}
|
|
|
|
// DeleteUserIdentitiesTx is DeleteUserIdentities within tx.
|
|
func (*DB) DeleteUserIdentitiesTx(ctx context.Context, tx *sql.Tx, delete *store.DeleteUserIdentity) error {
|
|
return deleteUserIdentities(ctx, tx, delete)
|
|
}
|
|
|
|
func deleteUserIdentities(ctx context.Context, q dbExecutor, delete *store.DeleteUserIdentity) error {
|
|
where, args := []string{"1 = 1"}, []any{}
|
|
|
|
if delete.ID != nil {
|
|
where, args = append(where, "`id` = ?"), append(args, *delete.ID)
|
|
}
|
|
if delete.UserID != nil {
|
|
where, args = append(where, "`user_id` = ?"), append(args, *delete.UserID)
|
|
}
|
|
if delete.Provider != nil {
|
|
where, args = append(where, "`provider` = ?"), append(args, *delete.Provider)
|
|
}
|
|
|
|
if _, err := q.ExecContext(ctx, "DELETE FROM `user_identity` WHERE "+strings.Join(where, " AND "), args...); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|