add sessions
This commit is contained in:
@@ -57,6 +57,13 @@ type Customer struct {
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
ID string
|
||||
UserID string
|
||||
CreatedAt sql.NullTime
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
type SvgIcon struct {
|
||||
ID int64
|
||||
Svg string
|
||||
|
||||
@@ -176,6 +176,22 @@ func (q *Queries) CreateRedirectURI(ctx context.Context, arg CreateRedirectURIPa
|
||||
return err
|
||||
}
|
||||
|
||||
const createSession = `-- name: CreateSession :exec
|
||||
INSERT INTO sessions (id, user_id, expires_at)
|
||||
VALUES (?, ?, ?)
|
||||
`
|
||||
|
||||
type CreateSessionParams struct {
|
||||
ID string
|
||||
UserID string
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) error {
|
||||
_, err := q.db.ExecContext(ctx, createSession, arg.ID, arg.UserID, arg.ExpiresAt)
|
||||
return err
|
||||
}
|
||||
|
||||
const createToken = `-- name: CreateToken :exec
|
||||
INSERT INTO tokens (token_type, token_value, user_id, client_id, scope, expires_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
@@ -291,6 +307,16 @@ func (q *Queries) DeleteOldAuthCodes(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteOldSessions = `-- name: DeleteOldSessions :exec
|
||||
DELETE FROM sessions
|
||||
WHERE expires_at < CURRENT_TIMESTAMP
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteOldSessions(ctx context.Context) error {
|
||||
_, err := q.db.ExecContext(ctx, deleteOldSessions)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteOldTokens = `-- name: DeleteOldTokens :exec
|
||||
DELETE FROM tokens
|
||||
WHERE expires_at < CURRENT_TIMESTAMP
|
||||
@@ -434,6 +460,24 @@ func (q *Queries) GetOIDCClientByID(ctx context.Context, id string) (Client, err
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getSessionByID = `-- name: GetSessionByID :one
|
||||
SELECT id, user_id, created_at, expires_at
|
||||
FROM sessions
|
||||
WHERE id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) GetSessionByID(ctx context.Context, id string) (Session, error) {
|
||||
row := q.db.QueryRowContext(ctx, getSessionByID, id)
|
||||
var i Session
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.UserID,
|
||||
&i.CreatedAt,
|
||||
&i.ExpiresAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getSvgCount = `-- name: GetSvgCount :one
|
||||
SELECT COUNT(*) as count FROM svg_icon
|
||||
`
|
||||
@@ -554,7 +598,7 @@ FROM clients
|
||||
WHERE owner = ?
|
||||
`
|
||||
|
||||
// -------- go-oidc
|
||||
// -------- go-oidc ----------
|
||||
func (q *Queries) GetUserClients(ctx context.Context, owner string) ([]Client, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getUserClients, owner)
|
||||
if err != nil {
|
||||
|
||||
@@ -145,7 +145,7 @@ SELECT permission FROM user_permission WHERE user_id = ?;
|
||||
INSERT INTO user_permission (user_id, permission) VALUES (?, ?);
|
||||
|
||||
|
||||
---------- go-oidc
|
||||
---------- go-oidc ----------
|
||||
|
||||
-- name: GetUserClients :many
|
||||
SELECT *
|
||||
@@ -191,6 +191,10 @@ WHERE expires_at < CURRENT_TIMESTAMP;
|
||||
DELETE FROM tokens
|
||||
WHERE expires_at < CURRENT_TIMESTAMP;
|
||||
|
||||
-- name: DeleteOldSessions :exec
|
||||
DELETE FROM sessions
|
||||
WHERE expires_at < CURRENT_TIMESTAMP;
|
||||
|
||||
-- name: GetTokenByValue :one
|
||||
SELECT *
|
||||
FROM tokens
|
||||
@@ -212,3 +216,13 @@ WHERE user_id = ? AND client_id = ?;
|
||||
-- name: DeleteAuthCode :exec
|
||||
DELETE FROM authorization_codes
|
||||
WHERE code = ?;
|
||||
|
||||
-- name: GetSessionByID :one
|
||||
SELECT *
|
||||
FROM sessions
|
||||
WHERE id = ?;
|
||||
|
||||
-- name: CreateSession :exec
|
||||
INSERT INTO sessions (id, user_id, expires_at)
|
||||
VALUES (?, ?, ?);
|
||||
|
||||
|
||||
@@ -123,3 +123,11 @@ CREATE TABLE IF NOT EXISTS client_approvals (
|
||||
,FOREIGN KEY (user_id) REFERENCES users (id)
|
||||
,FOREIGN KEY (client_id) REFERENCES clients (id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
id TEXT PRIMARY KEY
|
||||
,user_id TEXT NOT NULL
|
||||
,created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
,expires_at DATETIME NOT NULL
|
||||
,FOREIGN KEY (user_id) REFERENCES user (id)
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user