-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2b6a93
commit a9ee31b
Showing
34 changed files
with
2,414 additions
and
472 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
packages/pds/src/account-manager/db/migrations/003-oauth.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Kysely, sql } from 'kysely' | ||
|
||
export async function up(db: Kysely<unknown>): Promise<void> { | ||
await db.schema | ||
.createTable('authorization_request') | ||
.addColumn('id', 'varchar', (col) => col.primaryKey()) | ||
.addColumn('did', 'varchar') | ||
.addColumn('deviceId', 'varchar') | ||
.addColumn('clientId', 'varchar', (col) => col.notNull()) | ||
.addColumn('clientAuth', 'varchar', (col) => col.notNull()) | ||
.addColumn('parameters', 'varchar', (col) => col.notNull()) | ||
.addColumn('expiresAt', 'varchar', (col) => col.notNull()) | ||
.addColumn('code', 'varchar') | ||
.execute() | ||
|
||
await db.schema | ||
.createIndex('authorization_request_code_idx') | ||
.unique() | ||
.on('authorization_request') | ||
// https://github.com/kysely-org/kysely/issues/302 | ||
.expression(sql`code DESC) WHERE (code IS NOT NULL`) | ||
.execute() | ||
|
||
await db.schema | ||
.createIndex('authorization_request_expires_at_idx') | ||
.on('authorization_request') | ||
.column('expiresAt') | ||
.execute() | ||
|
||
await db.schema | ||
.createTable('device') | ||
.addColumn('id', 'varchar', (col) => col.primaryKey()) | ||
.addColumn('sessionId', 'varchar', (col) => col.notNull()) | ||
.addColumn('userAgent', 'varchar') | ||
.addColumn('ipAddress', 'varchar', (col) => col.notNull()) | ||
.addColumn('lastSeenAt', 'varchar', (col) => col.notNull()) | ||
.addUniqueConstraint('device_session_id_idx', ['sessionId']) | ||
.execute() | ||
|
||
await db.schema | ||
.createTable('device_account') | ||
.addColumn('did', 'varchar', (col) => col.notNull()) | ||
.addColumn('deviceId', 'varchar', (col) => col.notNull()) | ||
.addColumn('authenticatedAt', 'varchar', (col) => col.notNull()) | ||
.addColumn('remember', 'boolean', (col) => col.notNull()) | ||
.addColumn('authorizedClients', 'varchar', (col) => col.notNull()) | ||
.addUniqueConstraint('device_account_did_device_id_idx', [ | ||
'deviceId', // first because this table will be joined from the "device" table | ||
'did', | ||
]) | ||
.execute() | ||
|
||
await db.schema | ||
.createTable('token') | ||
.addColumn('id', 'integer', (col) => col.primaryKey().autoIncrement()) | ||
.addColumn('did', 'varchar', (col) => col.notNull()) | ||
.addColumn('tokenId', 'varchar', (col) => col.notNull()) | ||
.addColumn('createdAt', 'varchar', (col) => col.notNull()) | ||
.addColumn('updatedAt', 'varchar', (col) => col.notNull()) | ||
.addColumn('expiresAt', 'varchar', (col) => col.notNull()) | ||
.addColumn('clientId', 'varchar', (col) => col.notNull()) | ||
.addColumn('clientAuth', 'varchar', (col) => col.notNull()) | ||
.addColumn('deviceId', 'varchar') | ||
.addColumn('parameters', 'varchar', (col) => col.notNull()) | ||
.addColumn('details', 'varchar') | ||
.addColumn('code', 'varchar') | ||
.addColumn('currentRefreshToken', 'varchar') | ||
.addUniqueConstraint('token_current_refresh_token_unique_idx', [ | ||
'currentRefreshToken', | ||
]) | ||
.addUniqueConstraint('token_id_unique_idx', ['tokenId']) | ||
.execute() | ||
|
||
await db.schema | ||
.createIndex('token_code_idx') | ||
.unique() | ||
.on('token') | ||
// https://github.com/kysely-org/kysely/issues/302 | ||
.expression(sql`code DESC) WHERE (code IS NOT NULL`) | ||
.execute() | ||
|
||
await db.schema | ||
.createTable('used_refresh_token') | ||
.addColumn('id', 'integer', (col) => col.notNull()) | ||
.addColumn('usedRefreshToken', 'varchar', (col) => col.notNull()) | ||
.addUniqueConstraint('used_refresh_token_used_refresh_token_idx', [ | ||
'usedRefreshToken', | ||
'id', | ||
]) | ||
.execute() | ||
} | ||
|
||
export async function down(db: Kysely<unknown>): Promise<void> { | ||
await db.schema.dropTable('used_refresh_token').execute() | ||
await db.schema.dropTable('token').execute() | ||
await db.schema.dropTable('device_account').execute() | ||
await db.schema.dropTable('device').execute() | ||
await db.schema.dropTable('authorization_request').execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import * as mig001 from './001-init' | ||
import * as mig002 from './002-account-deactivation' | ||
import * as mig003 from './003-oauth' | ||
|
||
export default { | ||
'001': mig001, | ||
'002': mig002, | ||
'003': mig003, | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/pds/src/account-manager/db/schema/authorization-request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { | ||
Code, | ||
DeviceId, | ||
OAuthClientId, | ||
RequestId, | ||
} from '@atproto/oauth-provider' | ||
import { Selectable } from 'kysely' | ||
import { DateISO, JsonObject } from '../../../db' | ||
|
||
export interface AuthorizationRequest { | ||
id: RequestId | ||
did: string | null | ||
deviceId: DeviceId | null | ||
|
||
clientId: OAuthClientId | ||
clientAuth: JsonObject | ||
parameters: JsonObject | ||
expiresAt: DateISO // TODO: Index this | ||
code: Code | null | ||
} | ||
|
||
export type AuthorizationRequestEntry = Selectable<AuthorizationRequest> | ||
|
||
export const tableName = 'authorization_request' | ||
|
||
export type PartialDB = { [tableName]: AuthorizationRequest } |
15 changes: 15 additions & 0 deletions
15
packages/pds/src/account-manager/db/schema/device-account.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { DeviceId } from '@atproto/oauth-provider' | ||
import { DateISO, JsonArray } from '../../../db' | ||
|
||
export interface DeviceAccount { | ||
did: string | ||
deviceId: DeviceId | ||
|
||
authenticatedAt: DateISO | ||
authorizedClients: JsonArray | ||
remember: 0 | 1 | ||
} | ||
|
||
export const tableName = 'device_account' | ||
|
||
export type PartialDB = { [tableName]: DeviceAccount } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { DeviceId, SessionId } from '@atproto/oauth-provider' | ||
import { Selectable } from 'kysely' | ||
import { DateISO } from '../../../db' | ||
|
||
export interface Device { | ||
id: DeviceId | ||
sessionId: SessionId | ||
|
||
userAgent: string | null | ||
ipAddress: string | ||
lastSeenAt: DateISO | ||
} | ||
|
||
export type DeviceEntry = Selectable<Device> | ||
|
||
export const tableName = 'device' | ||
|
||
export type PartialDB = { [tableName]: Device } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { | ||
Code, | ||
DeviceId, | ||
OAuthClientId, | ||
RefreshToken, | ||
Sub, | ||
TokenId, | ||
} from '@atproto/oauth-provider' | ||
import { Generated, Selectable } from 'kysely' | ||
|
||
import { DateISO, JsonArray, JsonObject } from '../../../db/cast.js' | ||
|
||
export interface Token { | ||
id: Generated<number> | ||
did: Sub | ||
|
||
tokenId: TokenId | ||
createdAt: DateISO | ||
updatedAt: DateISO | ||
expiresAt: DateISO | ||
clientId: OAuthClientId | ||
clientAuth: JsonObject | ||
deviceId: DeviceId | null | ||
parameters: JsonObject | ||
details: JsonArray | null | ||
code: Code | null | ||
currentRefreshToken: RefreshToken | null // TODO: Index this | ||
} | ||
|
||
export type TokenEntry = Selectable<Token> | ||
|
||
export const tableName = 'token' | ||
|
||
export type PartialDB = { [tableName]: Token } |
13 changes: 13 additions & 0 deletions
13
packages/pds/src/account-manager/db/schema/used-refresh-token.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { RefreshToken } from '@atproto/oauth-provider' | ||
import { Selectable } from 'kysely' | ||
|
||
export interface UsedRefreshToken { | ||
id: number // TODO: Index this (foreign key to token) | ||
usedRefreshToken: RefreshToken | ||
} | ||
|
||
export type UsedRefreshTokenEntry = Selectable<UsedRefreshToken> | ||
|
||
export const tableName = 'used_refresh_token' | ||
|
||
export type PartialDB = { [tableName]: UsedRefreshToken } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.