Compare commits
3 Commits
491c9a824d
...
4df7561dd3
Author | SHA1 | Date | |
---|---|---|---|
4df7561dd3 | |||
8f753b2561 | |||
11748bb68e |
186
internal/repository/api_services.sql.go
Normal file
186
internal/repository/api_services.sql.go
Normal file
@ -0,0 +1,186 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.29.0
|
||||||
|
// source: api_services.sql
|
||||||
|
|
||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
const createApiService = `-- name: CreateApiService :one
|
||||||
|
INSERT INTO api_services (
|
||||||
|
client_id, client_secret, name, redirect_uris, scopes, grant_types
|
||||||
|
) VALUES (
|
||||||
|
$1, $2, $3, $4, $5, $6
|
||||||
|
) RETURNING id, client_id, client_secret, name, redirect_uris, scopes, grant_types, created_at, updated_at, is_active
|
||||||
|
`
|
||||||
|
|
||||||
|
type CreateApiServiceParams struct {
|
||||||
|
ClientID string `json:"client_id"`
|
||||||
|
ClientSecret string `json:"client_secret"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
RedirectUris []string `json:"redirect_uris"`
|
||||||
|
Scopes []string `json:"scopes"`
|
||||||
|
GrantTypes []string `json:"grant_types"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) CreateApiService(ctx context.Context, arg CreateApiServiceParams) (ApiService, error) {
|
||||||
|
row := q.db.QueryRow(ctx, createApiService,
|
||||||
|
arg.ClientID,
|
||||||
|
arg.ClientSecret,
|
||||||
|
arg.Name,
|
||||||
|
arg.RedirectUris,
|
||||||
|
arg.Scopes,
|
||||||
|
arg.GrantTypes,
|
||||||
|
)
|
||||||
|
var i ApiService
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.ClientID,
|
||||||
|
&i.ClientSecret,
|
||||||
|
&i.Name,
|
||||||
|
&i.RedirectUris,
|
||||||
|
&i.Scopes,
|
||||||
|
&i.GrantTypes,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
&i.IsActive,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const deactivateApiService = `-- name: DeactivateApiService :exec
|
||||||
|
UPDATE api_services
|
||||||
|
SET is_active = false,
|
||||||
|
updated_at = NOW()
|
||||||
|
WHERE client_id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) DeactivateApiService(ctx context.Context, clientID string) error {
|
||||||
|
_, err := q.db.Exec(ctx, deactivateApiService, clientID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const getApiServiceCID = `-- name: GetApiServiceCID :one
|
||||||
|
SELECT id, client_id, client_secret, name, redirect_uris, scopes, grant_types, created_at, updated_at, is_active FROM api_services
|
||||||
|
WHERE client_id = $1
|
||||||
|
AND is_active = true
|
||||||
|
LIMIT 1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetApiServiceCID(ctx context.Context, clientID string) (ApiService, error) {
|
||||||
|
row := q.db.QueryRow(ctx, getApiServiceCID, clientID)
|
||||||
|
var i ApiService
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.ClientID,
|
||||||
|
&i.ClientSecret,
|
||||||
|
&i.Name,
|
||||||
|
&i.RedirectUris,
|
||||||
|
&i.Scopes,
|
||||||
|
&i.GrantTypes,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
&i.IsActive,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const listApiServices = `-- name: ListApiServices :many
|
||||||
|
SELECT id, client_id, client_secret, name, redirect_uris, scopes, grant_types, created_at, updated_at, is_active FROM api_services
|
||||||
|
ORDER BY created_at DESC
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListApiServices(ctx context.Context) ([]ApiService, error) {
|
||||||
|
rows, err := q.db.Query(ctx, listApiServices)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var items []ApiService
|
||||||
|
for rows.Next() {
|
||||||
|
var i ApiService
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.ClientID,
|
||||||
|
&i.ClientSecret,
|
||||||
|
&i.Name,
|
||||||
|
&i.RedirectUris,
|
||||||
|
&i.Scopes,
|
||||||
|
&i.GrantTypes,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
&i.IsActive,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateApiService = `-- name: UpdateApiService :one
|
||||||
|
UPDATE api_services
|
||||||
|
SET
|
||||||
|
name = $2,
|
||||||
|
redirect_uris = $3,
|
||||||
|
scopes = $4,
|
||||||
|
grant_types = $5,
|
||||||
|
updated_at = NOW()
|
||||||
|
WHERE client_id = $1
|
||||||
|
RETURNING id, client_id, client_secret, name, redirect_uris, scopes, grant_types, created_at, updated_at, is_active
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdateApiServiceParams struct {
|
||||||
|
ClientID string `json:"client_id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
RedirectUris []string `json:"redirect_uris"`
|
||||||
|
Scopes []string `json:"scopes"`
|
||||||
|
GrantTypes []string `json:"grant_types"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) UpdateApiService(ctx context.Context, arg UpdateApiServiceParams) (ApiService, error) {
|
||||||
|
row := q.db.QueryRow(ctx, updateApiService,
|
||||||
|
arg.ClientID,
|
||||||
|
arg.Name,
|
||||||
|
arg.RedirectUris,
|
||||||
|
arg.Scopes,
|
||||||
|
arg.GrantTypes,
|
||||||
|
)
|
||||||
|
var i ApiService
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.ClientID,
|
||||||
|
&i.ClientSecret,
|
||||||
|
&i.Name,
|
||||||
|
&i.RedirectUris,
|
||||||
|
&i.Scopes,
|
||||||
|
&i.GrantTypes,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
&i.IsActive,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateClientSecret = `-- name: UpdateClientSecret :exec
|
||||||
|
UPDATE api_services
|
||||||
|
SET client_secret = $2,
|
||||||
|
updated_at = NOW()
|
||||||
|
WHERE client_id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdateClientSecretParams struct {
|
||||||
|
ClientID string `json:"client_id"`
|
||||||
|
ClientSecret string `json:"client_secret"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) UpdateClientSecret(ctx context.Context, arg UpdateClientSecretParams) error {
|
||||||
|
_, err := q.db.Exec(ctx, updateClientSecret, arg.ClientID, arg.ClientSecret)
|
||||||
|
return err
|
||||||
|
}
|
@ -5,10 +5,25 @@
|
|||||||
package repository
|
package repository
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ApiService struct {
|
||||||
|
ID uuid.UUID `json:"id"`
|
||||||
|
ClientID string `json:"client_id"`
|
||||||
|
ClientSecret string `json:"client_secret"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
RedirectUris []string `json:"redirect_uris"`
|
||||||
|
Scopes []string `json:"scopes"`
|
||||||
|
GrantTypes []string `json:"grant_types"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
IsActive bool `json:"is_active"`
|
||||||
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID uuid.UUID `json:"id"`
|
ID uuid.UUID `json:"id"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
|
27
migrations/00004_create_services_table.sql
Normal file
27
migrations/00004_create_services_table.sql
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
-- +goose Up
|
||||||
|
-- +goose StatementBegin
|
||||||
|
CREATE TABLE api_services (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), -- Unique identifier
|
||||||
|
|
||||||
|
-- OIDC-required fields
|
||||||
|
client_id TEXT UNIQUE NOT NULL,
|
||||||
|
client_secret TEXT NOT NULL, -- Store as hashed value
|
||||||
|
|
||||||
|
-- Metadata
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
|
||||||
|
redirect_uris TEXT[] DEFAULT '{}',
|
||||||
|
scopes TEXT[] DEFAULT '{openid}',
|
||||||
|
grant_types TEXT[] DEFAULT '{authorization_code}',
|
||||||
|
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
|
||||||
|
is_active BOOLEAN NOT NULL DEFAULT true
|
||||||
|
);
|
||||||
|
-- +goose StatementEnd
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
-- +goose StatementBegin
|
||||||
|
DROP TABLE api_services;
|
||||||
|
-- +goose StatementEnd
|
39
queries/api_services.sql
Normal file
39
queries/api_services.sql
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
-- name: CreateApiService :one
|
||||||
|
INSERT INTO api_services (
|
||||||
|
client_id, client_secret, name, redirect_uris, scopes, grant_types
|
||||||
|
) VALUES (
|
||||||
|
$1, $2, $3, $4, $5, $6
|
||||||
|
) RETURNING *;
|
||||||
|
|
||||||
|
-- name: GetApiServiceCID :one
|
||||||
|
SELECT * FROM api_services
|
||||||
|
WHERE client_id = $1
|
||||||
|
AND is_active = true
|
||||||
|
LIMIT 1;
|
||||||
|
|
||||||
|
-- name: ListApiServices :many
|
||||||
|
SELECT * FROM api_services
|
||||||
|
ORDER BY created_at DESC;
|
||||||
|
|
||||||
|
-- name: UpdateApiService :one
|
||||||
|
UPDATE api_services
|
||||||
|
SET
|
||||||
|
name = $2,
|
||||||
|
redirect_uris = $3,
|
||||||
|
scopes = $4,
|
||||||
|
grant_types = $5,
|
||||||
|
updated_at = NOW()
|
||||||
|
WHERE client_id = $1
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: DeactivateApiService :exec
|
||||||
|
UPDATE api_services
|
||||||
|
SET is_active = false,
|
||||||
|
updated_at = NOW()
|
||||||
|
WHERE client_id = $1;
|
||||||
|
|
||||||
|
-- name: UpdateClientSecret :exec
|
||||||
|
UPDATE api_services
|
||||||
|
SET client_secret = $2,
|
||||||
|
updated_at = NOW()
|
||||||
|
WHERE client_id = $1;
|
Reference in New Issue
Block a user