feat(identity): name, lifetime and last-used on personal API tokens
This commit is contained in:
23 files changed
+449
-99
No files matched your search
@@ -310,6 +310,16 @@ func (s *Store) ClaimSubject(ctx context.Context, placeholder, subject identity.
|
||||
// BindSubject binds an identity to the provider subject that asserts it,
|
||||
// moving any prior binding for that subject.
|
||||
func (s *Store) BindSubject(ctx context.Context, id identity.IdentityID, subject identity.Subject, audit identity.Audit) error {
|
||||
return s.bindSubject(ctx, id, subject, "", nil, audit)
|
||||
}
|
||||
|
||||
// BindPersonalToken binds a personal token's subject with the name and expiry
|
||||
// its owner chose; a nil expiry never lapses.
|
||||
func (s *Store) BindPersonalToken(ctx context.Context, id identity.IdentityID, subject identity.Subject, name string, expiresAt *time.Time, audit identity.Audit) error {
|
||||
return s.bindSubject(ctx, id, subject, name, expiresAt, audit)
|
||||
}
|
||||
|
||||
func (s *Store) bindSubject(ctx context.Context, id identity.IdentityID, subject identity.Subject, name string, expiresAt *time.Time, audit identity.Audit) error {
|
||||
if err := subject.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -339,6 +349,7 @@ func (s *Store) BindSubject(ctx context.Context, id identity.IdentityID, subject
|
||||
|
||||
if err := q.UpsertIdentitySubject(ctx, postgresdb.UpsertIdentitySubjectParams{
|
||||
Issuer: subject.Issuer, Subject: subject.Subject, IdentityID: string(id), BoundAt: now,
|
||||
Name: name, ExpiresAt: timestamptzOf(expiresAt),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -360,11 +371,34 @@ func (s *Store) SubjectsOf(ctx context.Context, id identity.IdentityID, issuer s
|
||||
}
|
||||
out := make([]identity.PersonalToken, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
out = append(out, identity.PersonalToken{ID: row.Subject, CreatedAt: row.BoundAt})
|
||||
out = append(out, identity.PersonalToken{
|
||||
ID: row.Subject, Name: row.Name, CreatedAt: row.BoundAt, ExpiresAt: timePtr(row.ExpiresAt), LastUsedAt: timePtr(row.LastUsedAt),
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// PersonalTokenOf returns one token's lifetime and last use, whoever owns it.
|
||||
func (s *Store) PersonalTokenOf(ctx context.Context, subject identity.Subject) (identity.PersonalToken, error) {
|
||||
row, err := postgresdb.New(s.pool).GetIdentitySubjectUse(ctx, postgresdb.GetIdentitySubjectUseParams{Issuer: subject.Issuer, Subject: subject.Subject})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return identity.PersonalToken{}, identity.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return identity.PersonalToken{}, err
|
||||
}
|
||||
return identity.PersonalToken{
|
||||
ID: row.Subject, Name: row.Name, CreatedAt: row.BoundAt, ExpiresAt: timePtr(row.ExpiresAt), LastUsedAt: timePtr(row.LastUsedAt),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// TouchPersonalToken records when a token last authenticated.
|
||||
func (s *Store) TouchPersonalToken(ctx context.Context, subject identity.Subject, at time.Time) error {
|
||||
return postgresdb.New(s.pool).TouchIdentitySubject(ctx, postgresdb.TouchIdentitySubjectParams{
|
||||
Issuer: subject.Issuer, Subject: subject.Subject, LastUsedAt: timestamptzOf(&at),
|
||||
})
|
||||
}
|
||||
|
||||
// UnbindSubject removes one of an identity's bindings; a binding the
|
||||
// identity does not hold is ErrNotFound, so no caller can remove another's.
|
||||
func (s *Store) UnbindSubject(ctx context.Context, id identity.IdentityID, subject identity.Subject, audit identity.Audit) error {
|
||||
@@ -435,3 +469,18 @@ func (s *Store) BootstrapIdentities(ctx context.Context, legacyNames []string) e
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func timestamptzOf(t *time.Time) pgtype.Timestamptz {
|
||||
if t == nil {
|
||||
return pgtype.Timestamptz{}
|
||||
}
|
||||
return pgtype.Timestamptz{Time: *t, Valid: true}
|
||||
}
|
||||
|
||||
func timePtr(t pgtype.Timestamptz) *time.Time {
|
||||
if !t.Valid {
|
||||
return nil
|
||||
}
|
||||
v := t.Time
|
||||
return &v
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
-- +goose Up
|
||||
ALTER TABLE identity_subjects
|
||||
ADD COLUMN name text NOT NULL DEFAULT '',
|
||||
ADD COLUMN expires_at timestamptz,
|
||||
ADD COLUMN last_used_at timestamptz;
|
||||
|
||||
-- +goose Down
|
||||
ALTER TABLE identity_subjects
|
||||
DROP COLUMN last_used_at,
|
||||
DROP COLUMN expires_at,
|
||||
DROP COLUMN name;
|
||||
@@ -77,6 +77,37 @@ func (q *Queries) GetIdentity(ctx context.Context, id string) (Identity, error)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getIdentitySubjectUse = `-- name: GetIdentitySubjectUse :one
|
||||
SELECT subject, bound_at, name, expires_at, last_used_at FROM identity_subjects
|
||||
WHERE issuer = $1 AND subject = $2
|
||||
`
|
||||
|
||||
type GetIdentitySubjectUseParams struct {
|
||||
Issuer string
|
||||
Subject string
|
||||
}
|
||||
|
||||
type GetIdentitySubjectUseRow struct {
|
||||
Subject string
|
||||
BoundAt time.Time
|
||||
Name string
|
||||
ExpiresAt pgtype.Timestamptz
|
||||
LastUsedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
func (q *Queries) GetIdentitySubjectUse(ctx context.Context, arg GetIdentitySubjectUseParams) (GetIdentitySubjectUseRow, error) {
|
||||
row := q.db.QueryRow(ctx, getIdentitySubjectUse, arg.Issuer, arg.Subject)
|
||||
var i GetIdentitySubjectUseRow
|
||||
err := row.Scan(
|
||||
&i.Subject,
|
||||
&i.BoundAt,
|
||||
&i.Name,
|
||||
&i.ExpiresAt,
|
||||
&i.LastUsedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getVisibleIdentity = `-- name: GetVisibleIdentity :one
|
||||
SELECT id, kind, display_name, lifecycle, version, created_at, updated_at
|
||||
FROM identities WHERE id = $1 AND ($2::text IS NULL
|
||||
@@ -259,7 +290,7 @@ func (q *Queries) ListIdentities(ctx context.Context, scopeOrg pgtype.Text) ([]I
|
||||
}
|
||||
|
||||
const listIdentitySubjects = `-- name: ListIdentitySubjects :many
|
||||
SELECT subject, bound_at FROM identity_subjects
|
||||
SELECT subject, bound_at, name, expires_at, last_used_at FROM identity_subjects
|
||||
WHERE identity_id = $1 AND issuer = $2
|
||||
ORDER BY bound_at DESC
|
||||
`
|
||||
@@ -270,8 +301,11 @@ type ListIdentitySubjectsParams struct {
|
||||
}
|
||||
|
||||
type ListIdentitySubjectsRow struct {
|
||||
Subject string
|
||||
BoundAt time.Time
|
||||
Subject string
|
||||
BoundAt time.Time
|
||||
Name string
|
||||
ExpiresAt pgtype.Timestamptz
|
||||
LastUsedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
func (q *Queries) ListIdentitySubjects(ctx context.Context, arg ListIdentitySubjectsParams) ([]ListIdentitySubjectsRow, error) {
|
||||
@@ -283,7 +317,13 @@ func (q *Queries) ListIdentitySubjects(ctx context.Context, arg ListIdentitySubj
|
||||
var items []ListIdentitySubjectsRow
|
||||
for rows.Next() {
|
||||
var i ListIdentitySubjectsRow
|
||||
if err := rows.Scan(&i.Subject, &i.BoundAt); err != nil {
|
||||
if err := rows.Scan(
|
||||
&i.Subject,
|
||||
&i.BoundAt,
|
||||
&i.Name,
|
||||
&i.ExpiresAt,
|
||||
&i.LastUsedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
@@ -348,6 +388,21 @@ func (q *Queries) ResolveIdentitySubject(ctx context.Context, arg ResolveIdentit
|
||||
return i, err
|
||||
}
|
||||
|
||||
const touchIdentitySubject = `-- name: TouchIdentitySubject :exec
|
||||
UPDATE identity_subjects SET last_used_at = $3 WHERE issuer = $1 AND subject = $2
|
||||
`
|
||||
|
||||
type TouchIdentitySubjectParams struct {
|
||||
Issuer string
|
||||
Subject string
|
||||
LastUsedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
func (q *Queries) TouchIdentitySubject(ctx context.Context, arg TouchIdentitySubjectParams) error {
|
||||
_, err := q.db.Exec(ctx, touchIdentitySubject, arg.Issuer, arg.Subject, arg.LastUsedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateIdentity = `-- name: UpdateIdentity :execrows
|
||||
UPDATE identities SET display_name = $2, lifecycle = $3, version = $4, updated_at = $5
|
||||
WHERE id = $1 AND version = $6
|
||||
@@ -392,9 +447,10 @@ func (q *Queries) UpdateTaskIdentity(ctx context.Context, arg UpdateTaskIdentity
|
||||
}
|
||||
|
||||
const upsertIdentitySubject = `-- name: UpsertIdentitySubject :exec
|
||||
INSERT INTO identity_subjects (issuer, subject, identity_id, bound_at)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (issuer, subject) DO UPDATE SET identity_id = excluded.identity_id, bound_at = excluded.bound_at
|
||||
INSERT INTO identity_subjects (issuer, subject, identity_id, bound_at, name, expires_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
ON CONFLICT (issuer, subject) DO UPDATE SET identity_id = excluded.identity_id, bound_at = excluded.bound_at,
|
||||
name = excluded.name, expires_at = excluded.expires_at
|
||||
`
|
||||
|
||||
type UpsertIdentitySubjectParams struct {
|
||||
@@ -402,6 +458,8 @@ type UpsertIdentitySubjectParams struct {
|
||||
Subject string
|
||||
IdentityID string
|
||||
BoundAt time.Time
|
||||
Name string
|
||||
ExpiresAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
func (q *Queries) UpsertIdentitySubject(ctx context.Context, arg UpsertIdentitySubjectParams) error {
|
||||
@@ -410,6 +468,8 @@ func (q *Queries) UpsertIdentitySubject(ctx context.Context, arg UpsertIdentityS
|
||||
arg.Subject,
|
||||
arg.IdentityID,
|
||||
arg.BoundAt,
|
||||
arg.Name,
|
||||
arg.ExpiresAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
@@ -266,6 +266,9 @@ type IdentitySubject struct {
|
||||
Subject string
|
||||
IdentityID string
|
||||
BoundAt time.Time
|
||||
Name string
|
||||
ExpiresAt pgtype.Timestamptz
|
||||
LastUsedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
type InstalledPackage struct {
|
||||
|
||||
@@ -73,17 +73,25 @@ WHERE issuer = @issuer AND subject = @placeholder
|
||||
RETURNING identity_id;
|
||||
|
||||
-- name: UpsertIdentitySubject :exec
|
||||
INSERT INTO identity_subjects (issuer, subject, identity_id, bound_at)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (issuer, subject) DO UPDATE SET identity_id = excluded.identity_id, bound_at = excluded.bound_at;
|
||||
INSERT INTO identity_subjects (issuer, subject, identity_id, bound_at, name, expires_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
ON CONFLICT (issuer, subject) DO UPDATE SET identity_id = excluded.identity_id, bound_at = excluded.bound_at,
|
||||
name = excluded.name, expires_at = excluded.expires_at;
|
||||
|
||||
-- name: UpdateTaskIdentity :exec
|
||||
UPDATE tasks SET identity = $1 WHERE identity = $2;
|
||||
|
||||
-- name: ListIdentitySubjects :many
|
||||
SELECT subject, bound_at FROM identity_subjects
|
||||
SELECT subject, bound_at, name, expires_at, last_used_at FROM identity_subjects
|
||||
WHERE identity_id = $1 AND issuer = $2
|
||||
ORDER BY bound_at DESC;
|
||||
|
||||
-- name: GetIdentitySubjectUse :one
|
||||
SELECT subject, bound_at, name, expires_at, last_used_at FROM identity_subjects
|
||||
WHERE issuer = $1 AND subject = $2;
|
||||
|
||||
-- name: TouchIdentitySubject :exec
|
||||
UPDATE identity_subjects SET last_used_at = $3 WHERE issuer = $1 AND subject = $2;
|
||||
|
||||
-- name: DeleteIdentitySubject :execrows
|
||||
DELETE FROM identity_subjects WHERE identity_id = $1 AND issuer = $2 AND subject = $3;
|
||||
Reference in new issue
Block a user