create account + create session
This commit is contained in:
@@ -196,6 +196,85 @@ func (q *Queries) CreatePrincipal(ctx context.Context, arg CreatePrincipalParams
|
||||
return i, err
|
||||
}
|
||||
|
||||
const createSession = `-- name: CreateSession :one
|
||||
|
||||
INSERT INTO session (
|
||||
id,
|
||||
token_hash,
|
||||
principal_id,
|
||||
kind,
|
||||
label,
|
||||
user_agent,
|
||||
ip_prefix,
|
||||
created_at,
|
||||
last_seen_at,
|
||||
expires_at,
|
||||
idle_expires_at
|
||||
) VALUES (
|
||||
?1,
|
||||
?2,
|
||||
?3,
|
||||
?4,
|
||||
?5,
|
||||
?6,
|
||||
?7,
|
||||
?8,
|
||||
?9,
|
||||
?10,
|
||||
?11
|
||||
)
|
||||
RETURNING id, token_hash, principal_id, kind, label, user_agent, ip_prefix, created_at, last_seen_at, expires_at, idle_expires_at, revoked_at
|
||||
`
|
||||
|
||||
type CreateSessionParams struct {
|
||||
ID ident.UUID
|
||||
TokenHash []byte
|
||||
PrincipalID ident.UUID
|
||||
Kind string
|
||||
Label sql.NullString
|
||||
UserAgent sql.NullString
|
||||
IpPrefix sql.NullString
|
||||
CreatedAt int64
|
||||
LastSeenAt int64
|
||||
ExpiresAt int64
|
||||
IdleExpiresAt int64
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// session
|
||||
// ============================================================================
|
||||
func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) (Session, error) {
|
||||
row := q.db.QueryRowContext(ctx, createSession,
|
||||
arg.ID,
|
||||
arg.TokenHash,
|
||||
arg.PrincipalID,
|
||||
arg.Kind,
|
||||
arg.Label,
|
||||
arg.UserAgent,
|
||||
arg.IpPrefix,
|
||||
arg.CreatedAt,
|
||||
arg.LastSeenAt,
|
||||
arg.ExpiresAt,
|
||||
arg.IdleExpiresAt,
|
||||
)
|
||||
var i Session
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.TokenHash,
|
||||
&i.PrincipalID,
|
||||
&i.Kind,
|
||||
&i.Label,
|
||||
&i.UserAgent,
|
||||
&i.IpPrefix,
|
||||
&i.CreatedAt,
|
||||
&i.LastSeenAt,
|
||||
&i.ExpiresAt,
|
||||
&i.IdleExpiresAt,
|
||||
&i.RevokedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteAccount = `-- name: DeleteAccount :exec
|
||||
DELETE FROM account
|
||||
WHERE principal_id = ?1
|
||||
@@ -206,6 +285,20 @@ func (q *Queries) DeleteAccount(ctx context.Context, principalID ident.UUID) err
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteExpiredSessions = `-- name: DeleteExpiredSessions :execrows
|
||||
DELETE FROM session
|
||||
WHERE expires_at <= ?1
|
||||
OR idle_expires_at <= ?1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteExpiredSessions(ctx context.Context, now int64) (int64, error) {
|
||||
result, err := q.db.ExecContext(ctx, deleteExpiredSessions, now)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected()
|
||||
}
|
||||
|
||||
const deleteMembership = `-- name: DeleteMembership :exec
|
||||
DELETE FROM membership
|
||||
WHERE group_id = ?1
|
||||
@@ -401,6 +494,58 @@ func (q *Queries) GetPrincipalByNameAndKind(ctx context.Context, arg GetPrincipa
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getSession = `-- name: GetSession :one
|
||||
SELECT id, token_hash, principal_id, kind, label, user_agent, ip_prefix, created_at, last_seen_at, expires_at, idle_expires_at, revoked_at
|
||||
FROM session
|
||||
WHERE id = ?1
|
||||
`
|
||||
|
||||
func (q *Queries) GetSession(ctx context.Context, id ident.UUID) (Session, error) {
|
||||
row := q.db.QueryRowContext(ctx, getSession, id)
|
||||
var i Session
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.TokenHash,
|
||||
&i.PrincipalID,
|
||||
&i.Kind,
|
||||
&i.Label,
|
||||
&i.UserAgent,
|
||||
&i.IpPrefix,
|
||||
&i.CreatedAt,
|
||||
&i.LastSeenAt,
|
||||
&i.ExpiresAt,
|
||||
&i.IdleExpiresAt,
|
||||
&i.RevokedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getSessionByTokenHash = `-- name: GetSessionByTokenHash :one
|
||||
SELECT id, token_hash, principal_id, kind, label, user_agent, ip_prefix, created_at, last_seen_at, expires_at, idle_expires_at, revoked_at
|
||||
FROM session
|
||||
WHERE token_hash = ?1
|
||||
`
|
||||
|
||||
func (q *Queries) GetSessionByTokenHash(ctx context.Context, tokenHash []byte) (Session, error) {
|
||||
row := q.db.QueryRowContext(ctx, getSessionByTokenHash, tokenHash)
|
||||
var i Session
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.TokenHash,
|
||||
&i.PrincipalID,
|
||||
&i.Kind,
|
||||
&i.Label,
|
||||
&i.UserAgent,
|
||||
&i.IpPrefix,
|
||||
&i.CreatedAt,
|
||||
&i.LastSeenAt,
|
||||
&i.ExpiresAt,
|
||||
&i.IdleExpiresAt,
|
||||
&i.RevokedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const incrementFailedAttempts = `-- name: IncrementFailedAttempts :one
|
||||
UPDATE account
|
||||
SET failed_attempts = failed_attempts + 1
|
||||
@@ -506,6 +651,49 @@ func (q *Queries) ListGroupMembers(ctx context.Context, groupID ident.UUID) ([]P
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listGroupMembersWithRole = `-- name: ListGroupMembersWithRole :many
|
||||
SELECT p.id, p.name, p.kind, m.role
|
||||
FROM membership m
|
||||
JOIN principal p ON p.id = m.member_id
|
||||
WHERE m.group_id = ?1
|
||||
ORDER BY p.name
|
||||
`
|
||||
|
||||
type ListGroupMembersWithRoleRow struct {
|
||||
ID ident.UUID
|
||||
Name string
|
||||
Kind string
|
||||
Role string
|
||||
}
|
||||
|
||||
func (q *Queries) ListGroupMembersWithRole(ctx context.Context, groupID ident.UUID) ([]ListGroupMembersWithRoleRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listGroupMembersWithRole, groupID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListGroupMembersWithRoleRow
|
||||
for rows.Next() {
|
||||
var i ListGroupMembersWithRoleRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.Kind,
|
||||
&i.Role,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listMemberGroups = `-- name: ListMemberGroups :many
|
||||
SELECT p.id, p.kind, p.name, p.public_key, p.status, p.successor_id, p.created_at, p.disabled_at
|
||||
FROM membership m
|
||||
@@ -546,6 +734,43 @@ func (q *Queries) ListMemberGroups(ctx context.Context, memberID ident.UUID) ([]
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listMemberGroupsWithRole = `-- name: ListMemberGroupsWithRole :many
|
||||
SELECT p.id, p.name, m.role
|
||||
FROM membership m
|
||||
JOIN principal p ON p.id = m.group_id
|
||||
WHERE m.member_id = ?1
|
||||
ORDER BY p.name
|
||||
`
|
||||
|
||||
type ListMemberGroupsWithRoleRow struct {
|
||||
ID ident.UUID
|
||||
Name string
|
||||
Role string
|
||||
}
|
||||
|
||||
func (q *Queries) ListMemberGroupsWithRole(ctx context.Context, memberID ident.UUID) ([]ListMemberGroupsWithRoleRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listMemberGroupsWithRole, memberID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListMemberGroupsWithRoleRow
|
||||
for rows.Next() {
|
||||
var i ListMemberGroupsWithRoleRow
|
||||
if err := rows.Scan(&i.ID, &i.Name, &i.Role); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listMembershipsByGroup = `-- name: ListMembershipsByGroup :many
|
||||
SELECT group_id, member_id, role, created_at
|
||||
FROM membership
|
||||
@@ -693,6 +918,49 @@ func (q *Queries) ListPrincipalsByKind(ctx context.Context, kind string) ([]Prin
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listSessionsByPrincipal = `-- name: ListSessionsByPrincipal :many
|
||||
SELECT id, token_hash, principal_id, kind, label, user_agent, ip_prefix, created_at, last_seen_at, expires_at, idle_expires_at, revoked_at
|
||||
FROM session
|
||||
WHERE principal_id = ?1
|
||||
ORDER BY created_at DESC
|
||||
`
|
||||
|
||||
func (q *Queries) ListSessionsByPrincipal(ctx context.Context, principalID ident.UUID) ([]Session, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listSessionsByPrincipal, principalID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Session
|
||||
for rows.Next() {
|
||||
var i Session
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.TokenHash,
|
||||
&i.PrincipalID,
|
||||
&i.Kind,
|
||||
&i.Label,
|
||||
&i.UserAgent,
|
||||
&i.IpPrefix,
|
||||
&i.CreatedAt,
|
||||
&i.LastSeenAt,
|
||||
&i.ExpiresAt,
|
||||
&i.IdleExpiresAt,
|
||||
&i.RevokedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const lockAccount = `-- name: LockAccount :one
|
||||
UPDATE account
|
||||
SET state = 'locked',
|
||||
@@ -758,6 +1026,46 @@ func (q *Queries) RecordLogin(ctx context.Context, arg RecordLoginParams) (Accou
|
||||
return i, err
|
||||
}
|
||||
|
||||
const registerFailedLogin = `-- name: RegisterFailedLogin :one
|
||||
UPDATE account
|
||||
SET failed_attempts = failed_attempts + 1,
|
||||
state = CASE
|
||||
WHEN failed_attempts + 1 >= ?1 THEN 'locked'
|
||||
ELSE state
|
||||
END,
|
||||
locked_until = CASE
|
||||
WHEN failed_attempts + 1 >= ?1 THEN ?2
|
||||
ELSE locked_until
|
||||
END
|
||||
WHERE principal_id = ?3
|
||||
RETURNING principal_id, login, login_key, password_hash, password_changed_at, must_change_password, state, failed_attempts, locked_until, last_login_at, created_at
|
||||
`
|
||||
|
||||
type RegisterFailedLoginParams struct {
|
||||
MaxAttempts int64
|
||||
LockedUntil sql.NullInt64
|
||||
PrincipalID ident.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) RegisterFailedLogin(ctx context.Context, arg RegisterFailedLoginParams) (Account, error) {
|
||||
row := q.db.QueryRowContext(ctx, registerFailedLogin, arg.MaxAttempts, arg.LockedUntil, arg.PrincipalID)
|
||||
var i Account
|
||||
err := row.Scan(
|
||||
&i.PrincipalID,
|
||||
&i.Login,
|
||||
&i.LoginKey,
|
||||
&i.PasswordHash,
|
||||
&i.PasswordChangedAt,
|
||||
&i.MustChangePassword,
|
||||
&i.State,
|
||||
&i.FailedAttempts,
|
||||
&i.LockedUntil,
|
||||
&i.LastLoginAt,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const resetFailedAttempts = `-- name: ResetFailedAttempts :one
|
||||
UPDATE account
|
||||
SET failed_attempts = 0,
|
||||
@@ -785,6 +1093,104 @@ func (q *Queries) ResetFailedAttempts(ctx context.Context, principalID ident.UUI
|
||||
return i, err
|
||||
}
|
||||
|
||||
const revokeSession = `-- name: RevokeSession :one
|
||||
UPDATE session
|
||||
SET revoked_at = ?1
|
||||
WHERE id = ?2
|
||||
RETURNING id, token_hash, principal_id, kind, label, user_agent, ip_prefix, created_at, last_seen_at, expires_at, idle_expires_at, revoked_at
|
||||
`
|
||||
|
||||
type RevokeSessionParams struct {
|
||||
RevokedAt sql.NullInt64
|
||||
ID ident.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) RevokeSession(ctx context.Context, arg RevokeSessionParams) (Session, error) {
|
||||
row := q.db.QueryRowContext(ctx, revokeSession, arg.RevokedAt, arg.ID)
|
||||
var i Session
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.TokenHash,
|
||||
&i.PrincipalID,
|
||||
&i.Kind,
|
||||
&i.Label,
|
||||
&i.UserAgent,
|
||||
&i.IpPrefix,
|
||||
&i.CreatedAt,
|
||||
&i.LastSeenAt,
|
||||
&i.ExpiresAt,
|
||||
&i.IdleExpiresAt,
|
||||
&i.RevokedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const revokeSessionByTokenHash = `-- name: RevokeSessionByTokenHash :one
|
||||
UPDATE session
|
||||
SET revoked_at = ?1
|
||||
WHERE token_hash = ?2
|
||||
RETURNING id, token_hash, principal_id, kind, label, user_agent, ip_prefix, created_at, last_seen_at, expires_at, idle_expires_at, revoked_at
|
||||
`
|
||||
|
||||
type RevokeSessionByTokenHashParams struct {
|
||||
RevokedAt sql.NullInt64
|
||||
TokenHash []byte
|
||||
}
|
||||
|
||||
func (q *Queries) RevokeSessionByTokenHash(ctx context.Context, arg RevokeSessionByTokenHashParams) (Session, error) {
|
||||
row := q.db.QueryRowContext(ctx, revokeSessionByTokenHash, arg.RevokedAt, arg.TokenHash)
|
||||
var i Session
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.TokenHash,
|
||||
&i.PrincipalID,
|
||||
&i.Kind,
|
||||
&i.Label,
|
||||
&i.UserAgent,
|
||||
&i.IpPrefix,
|
||||
&i.CreatedAt,
|
||||
&i.LastSeenAt,
|
||||
&i.ExpiresAt,
|
||||
&i.IdleExpiresAt,
|
||||
&i.RevokedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const touchSession = `-- name: TouchSession :one
|
||||
UPDATE session
|
||||
SET last_seen_at = ?1,
|
||||
idle_expires_at = ?2
|
||||
WHERE id = ?3
|
||||
RETURNING id, token_hash, principal_id, kind, label, user_agent, ip_prefix, created_at, last_seen_at, expires_at, idle_expires_at, revoked_at
|
||||
`
|
||||
|
||||
type TouchSessionParams struct {
|
||||
LastSeenAt int64
|
||||
IdleExpiresAt int64
|
||||
ID ident.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) TouchSession(ctx context.Context, arg TouchSessionParams) (Session, error) {
|
||||
row := q.db.QueryRowContext(ctx, touchSession, arg.LastSeenAt, arg.IdleExpiresAt, arg.ID)
|
||||
var i Session
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.TokenHash,
|
||||
&i.PrincipalID,
|
||||
&i.Kind,
|
||||
&i.Label,
|
||||
&i.UserAgent,
|
||||
&i.IpPrefix,
|
||||
&i.CreatedAt,
|
||||
&i.LastSeenAt,
|
||||
&i.ExpiresAt,
|
||||
&i.IdleExpiresAt,
|
||||
&i.RevokedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const unlockAccount = `-- name: UnlockAccount :one
|
||||
UPDATE account
|
||||
SET state = 'active',
|
||||
|
||||
@@ -154,6 +154,20 @@ DELETE FROM membership
|
||||
WHERE group_id = sqlc.arg(group_id)
|
||||
AND member_id = sqlc.arg(member_id);
|
||||
|
||||
-- name: ListMemberGroupsWithRole :many
|
||||
SELECT p.id, p.name, m.role
|
||||
FROM membership m
|
||||
JOIN principal p ON p.id = m.group_id
|
||||
WHERE m.member_id = sqlc.arg(member_id)
|
||||
ORDER BY p.name;
|
||||
|
||||
-- name: ListGroupMembersWithRole :many
|
||||
SELECT p.id, p.name, p.kind, m.role
|
||||
FROM membership m
|
||||
JOIN principal p ON p.id = m.member_id
|
||||
WHERE m.group_id = sqlc.arg(group_id)
|
||||
ORDER BY p.name;
|
||||
|
||||
-- ============================================================================
|
||||
-- account
|
||||
-- ============================================================================
|
||||
@@ -251,3 +265,89 @@ RETURNING *;
|
||||
-- name: DeleteAccount :exec
|
||||
DELETE FROM account
|
||||
WHERE principal_id = sqlc.arg(principal_id);
|
||||
|
||||
-- name: RegisterFailedLogin :one
|
||||
UPDATE account
|
||||
SET failed_attempts = failed_attempts + 1,
|
||||
state = CASE
|
||||
WHEN failed_attempts + 1 >= sqlc.arg(max_attempts) THEN 'locked'
|
||||
ELSE state
|
||||
END,
|
||||
locked_until = CASE
|
||||
WHEN failed_attempts + 1 >= sqlc.arg(max_attempts) THEN sqlc.arg(locked_until)
|
||||
ELSE locked_until
|
||||
END
|
||||
WHERE principal_id = sqlc.arg(principal_id)
|
||||
RETURNING *;
|
||||
|
||||
-- ============================================================================
|
||||
-- session
|
||||
-- ============================================================================
|
||||
|
||||
-- name: CreateSession :one
|
||||
INSERT INTO session (
|
||||
id,
|
||||
token_hash,
|
||||
principal_id,
|
||||
kind,
|
||||
label,
|
||||
user_agent,
|
||||
ip_prefix,
|
||||
created_at,
|
||||
last_seen_at,
|
||||
expires_at,
|
||||
idle_expires_at
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(token_hash),
|
||||
sqlc.arg(principal_id),
|
||||
sqlc.arg(kind),
|
||||
sqlc.narg(label),
|
||||
sqlc.narg(user_agent),
|
||||
sqlc.narg(ip_prefix),
|
||||
sqlc.arg(created_at),
|
||||
sqlc.arg(last_seen_at),
|
||||
sqlc.arg(expires_at),
|
||||
sqlc.arg(idle_expires_at)
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetSession :one
|
||||
SELECT *
|
||||
FROM session
|
||||
WHERE id = sqlc.arg(id);
|
||||
|
||||
-- name: GetSessionByTokenHash :one
|
||||
SELECT *
|
||||
FROM session
|
||||
WHERE token_hash = sqlc.arg(token_hash);
|
||||
|
||||
-- name: TouchSession :one
|
||||
UPDATE session
|
||||
SET last_seen_at = sqlc.arg(last_seen_at),
|
||||
idle_expires_at = sqlc.arg(idle_expires_at)
|
||||
WHERE id = sqlc.arg(id)
|
||||
RETURNING *;
|
||||
|
||||
-- name: RevokeSession :one
|
||||
UPDATE session
|
||||
SET revoked_at = sqlc.arg(revoked_at)
|
||||
WHERE id = sqlc.arg(id)
|
||||
RETURNING *;
|
||||
|
||||
-- name: RevokeSessionByTokenHash :one
|
||||
UPDATE session
|
||||
SET revoked_at = sqlc.arg(revoked_at)
|
||||
WHERE token_hash = sqlc.arg(token_hash)
|
||||
RETURNING *;
|
||||
|
||||
-- name: ListSessionsByPrincipal :many
|
||||
SELECT *
|
||||
FROM session
|
||||
WHERE principal_id = sqlc.arg(principal_id)
|
||||
ORDER BY created_at DESC;
|
||||
|
||||
-- name: DeleteExpiredSessions :execrows
|
||||
DELETE FROM session
|
||||
WHERE expires_at <= sqlc.arg(now)
|
||||
OR idle_expires_at <= sqlc.arg(now);
|
||||
|
||||
Reference in New Issue
Block a user