@atproto/pds 0.4.33 → 0.4.35

Sign up to get free protection for your applications and to get access to all the features.
Files changed (227) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/account-manager/db/migrations/004-oauth.d.ts +4 -0
  3. package/dist/account-manager/db/migrations/004-oauth.d.ts.map +1 -0
  4. package/dist/account-manager/db/migrations/004-oauth.js +106 -0
  5. package/dist/account-manager/db/migrations/004-oauth.js.map +1 -0
  6. package/dist/account-manager/db/migrations/index.d.ts +2 -0
  7. package/dist/account-manager/db/migrations/index.d.ts.map +1 -1
  8. package/dist/account-manager/db/migrations/index.js +2 -0
  9. package/dist/account-manager/db/migrations/index.js.map +1 -1
  10. package/dist/account-manager/db/schema/authorization-request.d.ts +19 -0
  11. package/dist/account-manager/db/schema/authorization-request.d.ts.map +1 -0
  12. package/dist/account-manager/db/schema/authorization-request.js +5 -0
  13. package/dist/account-manager/db/schema/authorization-request.js.map +1 -0
  14. package/dist/account-manager/db/schema/device-account.d.ts +14 -0
  15. package/dist/account-manager/db/schema/device-account.d.ts.map +1 -0
  16. package/dist/account-manager/db/schema/device-account.js +5 -0
  17. package/dist/account-manager/db/schema/device-account.js.map +1 -0
  18. package/dist/account-manager/db/schema/device.d.ts +16 -0
  19. package/dist/account-manager/db/schema/device.d.ts.map +1 -0
  20. package/dist/account-manager/db/schema/device.js +5 -0
  21. package/dist/account-manager/db/schema/device.js.map +1 -0
  22. package/dist/account-manager/db/schema/index.d.ts +11 -1
  23. package/dist/account-manager/db/schema/index.d.ts.map +1 -1
  24. package/dist/account-manager/db/schema/token.d.ts +24 -0
  25. package/dist/account-manager/db/schema/token.d.ts.map +1 -0
  26. package/dist/account-manager/db/schema/token.js +5 -0
  27. package/dist/account-manager/db/schema/token.js.map +1 -0
  28. package/dist/account-manager/db/schema/used-refresh-token.d.ts +12 -0
  29. package/dist/account-manager/db/schema/used-refresh-token.d.ts.map +1 -0
  30. package/dist/account-manager/db/schema/used-refresh-token.js +5 -0
  31. package/dist/account-manager/db/schema/used-refresh-token.js.map +1 -0
  32. package/dist/account-manager/helpers/account.d.ts +27 -5
  33. package/dist/account-manager/helpers/account.d.ts.map +1 -1
  34. package/dist/account-manager/helpers/account.js +15 -14
  35. package/dist/account-manager/helpers/account.js.map +1 -1
  36. package/dist/account-manager/helpers/authorization-request.d.ts +12 -0
  37. package/dist/account-manager/helpers/authorization-request.d.ts.map +1 -0
  38. package/dist/account-manager/helpers/authorization-request.js +59 -0
  39. package/dist/account-manager/helpers/authorization-request.js.map +1 -0
  40. package/dist/account-manager/helpers/device-account.d.ts +108 -0
  41. package/dist/account-manager/helpers/device-account.d.ts.map +1 -0
  42. package/dist/account-manager/helpers/device-account.js +82 -0
  43. package/dist/account-manager/helpers/device-account.js.map +1 -0
  44. package/dist/account-manager/helpers/device.d.ts +9 -0
  45. package/dist/account-manager/helpers/device.d.ts.map +1 -0
  46. package/dist/account-manager/helpers/device.js +32 -0
  47. package/dist/account-manager/helpers/device.js.map +1 -0
  48. package/dist/account-manager/helpers/token.d.ts +485 -0
  49. package/dist/account-manager/helpers/token.d.ts.map +1 -0
  50. package/dist/account-manager/helpers/token.js +123 -0
  51. package/dist/account-manager/helpers/token.js.map +1 -0
  52. package/dist/account-manager/helpers/used-refresh-token.d.ts +10 -0
  53. package/dist/account-manager/helpers/used-refresh-token.d.ts.map +1 -0
  54. package/dist/account-manager/helpers/used-refresh-token.js +25 -0
  55. package/dist/account-manager/helpers/used-refresh-token.js.map +1 -0
  56. package/dist/account-manager/index.d.ts +36 -6
  57. package/dist/account-manager/index.d.ts.map +1 -1
  58. package/dist/account-manager/index.js +223 -22
  59. package/dist/account-manager/index.js.map +1 -1
  60. package/dist/actor-store/preference/reader.d.ts +2 -1
  61. package/dist/actor-store/preference/reader.d.ts.map +1 -1
  62. package/dist/actor-store/preference/reader.js +3 -1
  63. package/dist/actor-store/preference/reader.js.map +1 -1
  64. package/dist/actor-store/preference/transactor.d.ts +2 -1
  65. package/dist/actor-store/preference/transactor.d.ts.map +1 -1
  66. package/dist/actor-store/preference/transactor.js +7 -1
  67. package/dist/actor-store/preference/transactor.js.map +1 -1
  68. package/dist/actor-store/preference/util.d.ts +3 -0
  69. package/dist/actor-store/preference/util.d.ts.map +1 -0
  70. package/dist/actor-store/preference/util.js +12 -0
  71. package/dist/actor-store/preference/util.js.map +1 -0
  72. package/dist/actor-store/record/reader.d.ts +1 -1
  73. package/dist/api/app/bsky/actor/getPreferences.d.ts.map +1 -1
  74. package/dist/api/app/bsky/actor/getPreferences.js +1 -6
  75. package/dist/api/app/bsky/actor/getPreferences.js.map +1 -1
  76. package/dist/api/app/bsky/actor/putPreferences.d.ts.map +1 -1
  77. package/dist/api/app/bsky/actor/putPreferences.js +1 -1
  78. package/dist/api/app/bsky/actor/putPreferences.js.map +1 -1
  79. package/dist/api/app/bsky/util/resolver.d.ts +1 -1
  80. package/dist/api/com/atproto/server/createSession.d.ts.map +1 -1
  81. package/dist/api/com/atproto/server/createSession.js +7 -31
  82. package/dist/api/com/atproto/server/createSession.js.map +1 -1
  83. package/dist/api/com/atproto/server/deleteSession.d.ts.map +1 -1
  84. package/dist/api/com/atproto/server/deleteSession.js +14 -13
  85. package/dist/api/com/atproto/server/deleteSession.js.map +1 -1
  86. package/dist/api/com/atproto/server/getSession.d.ts.map +1 -1
  87. package/dist/api/com/atproto/server/getSession.js +4 -2
  88. package/dist/api/com/atproto/server/getSession.js.map +1 -1
  89. package/dist/api/com/atproto/server/refreshSession.d.ts.map +1 -1
  90. package/dist/api/com/atproto/server/refreshSession.js +4 -2
  91. package/dist/api/com/atproto/server/refreshSession.js.map +1 -1
  92. package/dist/api/com/atproto/sync/getRepoStatus.d.ts.map +1 -1
  93. package/dist/api/com/atproto/sync/getRepoStatus.js +2 -1
  94. package/dist/api/com/atproto/sync/getRepoStatus.js.map +1 -1
  95. package/dist/api/com/atproto/sync/listRepos.js +2 -2
  96. package/dist/api/com/atproto/sync/listRepos.js.map +1 -1
  97. package/dist/api/proxy.d.ts.map +1 -1
  98. package/dist/api/proxy.js +15 -2
  99. package/dist/api/proxy.js.map +1 -1
  100. package/dist/auth-routes.d.ts +4 -0
  101. package/dist/auth-routes.d.ts.map +1 -0
  102. package/dist/auth-routes.js +24 -0
  103. package/dist/auth-routes.js.map +1 -0
  104. package/dist/auth-verifier.d.ts +32 -11
  105. package/dist/auth-verifier.d.ts.map +1 -1
  106. package/dist/auth-verifier.js +238 -79
  107. package/dist/auth-verifier.js.map +1 -1
  108. package/dist/config/config.d.ts +12 -0
  109. package/dist/config/config.d.ts.map +1 -1
  110. package/dist/config/config.js +45 -0
  111. package/dist/config/config.js.map +1 -1
  112. package/dist/config/env.d.ts +8 -0
  113. package/dist/config/env.d.ts.map +1 -1
  114. package/dist/config/env.js +10 -0
  115. package/dist/config/env.js.map +1 -1
  116. package/dist/config/secrets.d.ts +1 -0
  117. package/dist/config/secrets.d.ts.map +1 -1
  118. package/dist/config/secrets.js +1 -0
  119. package/dist/config/secrets.js.map +1 -1
  120. package/dist/context.d.ts +6 -0
  121. package/dist/context.d.ts.map +1 -1
  122. package/dist/context.js +71 -13
  123. package/dist/context.js.map +1 -1
  124. package/dist/db/cast.d.ts +15 -0
  125. package/dist/db/cast.d.ts.map +1 -0
  126. package/dist/db/cast.js +66 -0
  127. package/dist/db/cast.js.map +1 -0
  128. package/dist/db/db.d.ts +2 -2
  129. package/dist/db/db.d.ts.map +1 -1
  130. package/dist/db/db.js +9 -7
  131. package/dist/db/db.js.map +1 -1
  132. package/dist/db/index.d.ts +1 -0
  133. package/dist/db/index.d.ts.map +1 -1
  134. package/dist/db/index.js +1 -0
  135. package/dist/db/index.js.map +1 -1
  136. package/dist/error.d.ts.map +1 -1
  137. package/dist/error.js +5 -0
  138. package/dist/error.js.map +1 -1
  139. package/dist/index.d.ts.map +1 -1
  140. package/dist/index.js +2 -0
  141. package/dist/index.js.map +1 -1
  142. package/dist/lexicon/index.d.ts +4 -0
  143. package/dist/lexicon/index.d.ts.map +1 -1
  144. package/dist/lexicon/index.js +8 -0
  145. package/dist/lexicon/index.js.map +1 -1
  146. package/dist/lexicon/lexicons.d.ts +51 -0
  147. package/dist/lexicon/lexicons.d.ts.map +1 -1
  148. package/dist/lexicon/lexicons.js +51 -0
  149. package/dist/lexicon/lexicons.js.map +1 -1
  150. package/dist/lexicon/types/app/bsky/feed/defs.d.ts +1 -0
  151. package/dist/lexicon/types/app/bsky/feed/defs.d.ts.map +1 -1
  152. package/dist/lexicon/types/app/bsky/feed/defs.js.map +1 -1
  153. package/dist/lexicon/types/app/bsky/graph/muteThread.d.ts +29 -0
  154. package/dist/lexicon/types/app/bsky/graph/muteThread.d.ts.map +1 -0
  155. package/dist/lexicon/types/app/bsky/graph/muteThread.js +3 -0
  156. package/dist/lexicon/types/app/bsky/graph/muteThread.js.map +1 -0
  157. package/dist/lexicon/types/app/bsky/graph/unmuteThread.d.ts +29 -0
  158. package/dist/lexicon/types/app/bsky/graph/unmuteThread.d.ts.map +1 -0
  159. package/dist/lexicon/types/app/bsky/graph/unmuteThread.js +3 -0
  160. package/dist/lexicon/types/app/bsky/graph/unmuteThread.js.map +1 -0
  161. package/dist/logger.d.ts +13 -11
  162. package/dist/logger.d.ts.map +1 -1
  163. package/dist/logger.js +80 -64
  164. package/dist/logger.js.map +1 -1
  165. package/dist/oauth/detailed-account-store.d.ts +27 -0
  166. package/dist/oauth/detailed-account-store.d.ts.map +1 -0
  167. package/dist/oauth/detailed-account-store.js +76 -0
  168. package/dist/oauth/detailed-account-store.js.map +1 -0
  169. package/dist/oauth/provider.d.ts +16 -0
  170. package/dist/oauth/provider.d.ts.map +1 -0
  171. package/dist/oauth/provider.js +45 -0
  172. package/dist/oauth/provider.js.map +1 -0
  173. package/dist/pipethrough.d.ts.map +1 -1
  174. package/dist/pipethrough.js.map +1 -1
  175. package/dist/sequencer/events.d.ts +2 -2
  176. package/example.env +21 -3
  177. package/package.json +9 -7
  178. package/src/account-manager/db/migrations/004-oauth.ts +122 -0
  179. package/src/account-manager/db/migrations/index.ts +2 -0
  180. package/src/account-manager/db/schema/authorization-request.ts +26 -0
  181. package/src/account-manager/db/schema/device-account.ts +15 -0
  182. package/src/account-manager/db/schema/device.ts +18 -0
  183. package/src/account-manager/db/schema/index.ts +15 -0
  184. package/src/account-manager/db/schema/token.ts +34 -0
  185. package/src/account-manager/db/schema/used-refresh-token.ts +13 -0
  186. package/src/account-manager/helpers/account.ts +16 -21
  187. package/src/account-manager/helpers/authorization-request.ts +82 -0
  188. package/src/account-manager/helpers/device-account.ts +135 -0
  189. package/src/account-manager/helpers/device.ts +45 -0
  190. package/src/account-manager/helpers/token.ts +185 -0
  191. package/src/account-manager/helpers/used-refresh-token.ts +30 -0
  192. package/src/account-manager/index.ts +325 -20
  193. package/src/actor-store/preference/reader.ts +8 -2
  194. package/src/actor-store/preference/transactor.ts +10 -0
  195. package/src/actor-store/preference/util.ts +8 -0
  196. package/src/api/app/bsky/actor/getPreferences.ts +2 -9
  197. package/src/api/app/bsky/actor/putPreferences.ts +5 -1
  198. package/src/api/com/atproto/server/createSession.ts +8 -44
  199. package/src/api/com/atproto/server/deleteSession.ts +14 -20
  200. package/src/api/com/atproto/server/getSession.ts +7 -2
  201. package/src/api/com/atproto/server/refreshSession.ts +6 -2
  202. package/src/api/com/atproto/sync/getRepoStatus.ts +3 -1
  203. package/src/api/com/atproto/sync/listRepos.ts +1 -1
  204. package/src/api/proxy.ts +18 -2
  205. package/src/auth-routes.ts +27 -0
  206. package/src/auth-verifier.ts +312 -92
  207. package/src/config/config.ts +66 -0
  208. package/src/config/env.ts +24 -0
  209. package/src/config/secrets.ts +2 -0
  210. package/src/context.ts +80 -14
  211. package/src/db/cast.ts +59 -0
  212. package/src/db/db.ts +15 -12
  213. package/src/db/index.ts +1 -0
  214. package/src/error.ts +7 -0
  215. package/src/index.ts +2 -0
  216. package/src/lexicon/index.ts +24 -0
  217. package/src/lexicon/lexicons.ts +52 -0
  218. package/src/lexicon/types/app/bsky/feed/defs.ts +1 -0
  219. package/src/lexicon/types/app/bsky/graph/muteThread.ts +38 -0
  220. package/src/lexicon/types/app/bsky/graph/unmuteThread.ts +38 -0
  221. package/src/logger.ts +83 -38
  222. package/src/oauth/detailed-account-store.ts +96 -0
  223. package/src/oauth/provider.ts +77 -0
  224. package/src/pipethrough.ts +3 -2
  225. package/tests/preferences.test.ts +67 -1
  226. package/tests/proxied/__snapshots__/feedgen.test.ts.snap +4 -1
  227. package/tests/proxied/__snapshots__/views.test.ts.snap +116 -38
@@ -0,0 +1,135 @@
1
+ import {
2
+ Account,
3
+ DeviceAccountInfo,
4
+ DeviceId,
5
+ OAuthClientId,
6
+ } from '@atproto/oauth-provider'
7
+ import { Insertable, Selectable } from 'kysely'
8
+
9
+ import { fromDateISO, fromJsonArray, toDateISO, toJsonArray } from '../../db'
10
+ import { AccountDb } from '../db'
11
+ import { DeviceAccount } from '../db/schema/device-account'
12
+ import { ActorAccount, selectAccountQB } from './account'
13
+
14
+ export type SelectableDeviceAccount = Pick<
15
+ Selectable<DeviceAccount>,
16
+ 'authenticatedAt' | 'authorizedClients' | 'remember'
17
+ >
18
+
19
+ const selectAccountInfoQB = (db: AccountDb, deviceId: DeviceId) =>
20
+ selectAccountQB(db, { includeDeactivated: true })
21
+ // note: query planner should use "device_account_pk" index
22
+ .innerJoin('device_account', 'device_account.did', 'actor.did')
23
+ .innerJoin('device', 'device.id', 'device_account.deviceId')
24
+ .where('device.id', '=', deviceId)
25
+ .select([
26
+ 'device_account.authenticatedAt',
27
+ 'device_account.remember',
28
+ 'device_account.authorizedClients',
29
+ ])
30
+
31
+ export type InsertableField = {
32
+ authenticatedAt: Date
33
+ authorizedClients: OAuthClientId[]
34
+ remember: boolean
35
+ }
36
+
37
+ function toInsertable<V extends Partial<InsertableField>>(
38
+ values: V,
39
+ ): Pick<Insertable<DeviceAccount>, keyof V & keyof Insertable<DeviceAccount>>
40
+ function toInsertable(
41
+ values: Partial<InsertableField>,
42
+ ): Partial<Insertable<DeviceAccount>> {
43
+ const row: Partial<Insertable<DeviceAccount>> = {}
44
+ if (values.authenticatedAt) {
45
+ row.authenticatedAt = toDateISO(values.authenticatedAt)
46
+ }
47
+ if (values.remember !== undefined) {
48
+ row.remember = values.remember === true ? 1 : 0
49
+ }
50
+ if (values.authorizedClients) {
51
+ row.authorizedClients = toJsonArray(values.authorizedClients)
52
+ }
53
+ return row
54
+ }
55
+
56
+ export function toDeviceAccountInfo(
57
+ row: SelectableDeviceAccount,
58
+ ): DeviceAccountInfo {
59
+ return {
60
+ remembered: row.remember === 1,
61
+ authenticatedAt: fromDateISO(row.authenticatedAt),
62
+ authorizedClients: fromJsonArray<OAuthClientId>(row.authorizedClients),
63
+ }
64
+ }
65
+
66
+ export function toAccount(
67
+ row: Selectable<ActorAccount>,
68
+ audience: string,
69
+ ): Account {
70
+ return {
71
+ sub: row.did,
72
+ aud: audience,
73
+ email: row.email || undefined,
74
+ email_verified: row.email ? row.emailConfirmedAt != null : undefined,
75
+ preferred_username: row.handle || undefined,
76
+ }
77
+ }
78
+
79
+ export const readQB = (db: AccountDb, deviceId: DeviceId, did: string) =>
80
+ db.db
81
+ .selectFrom('device_account')
82
+ .where('did', '=', did)
83
+ .where('deviceId', '=', deviceId)
84
+ .select(['remember', 'authorizedClients', 'authenticatedAt'])
85
+
86
+ export const updateQB = (
87
+ db: AccountDb,
88
+ deviceId: DeviceId,
89
+ did: string,
90
+ entry: {
91
+ authenticatedAt?: Date
92
+ authorizedClients?: OAuthClientId[]
93
+ remember?: boolean
94
+ },
95
+ ) =>
96
+ db.db
97
+ .updateTable('device_account')
98
+ .set(toInsertable(entry))
99
+ .where('did', '=', did)
100
+ .where('deviceId', '=', deviceId)
101
+
102
+ export const createOrUpdateQB = (
103
+ db: AccountDb,
104
+ deviceId: DeviceId,
105
+ did: string,
106
+ remember: boolean,
107
+ ) => {
108
+ const { authorizedClients, ...values } = toInsertable({
109
+ remember,
110
+ authenticatedAt: new Date(),
111
+ authorizedClients: [],
112
+ })
113
+
114
+ return db.db
115
+ .insertInto('device_account')
116
+ .values({ did, deviceId, authorizedClients, ...values })
117
+ .onConflict((oc) => oc.columns(['deviceId', 'did']).doUpdateSet(values))
118
+ }
119
+
120
+ export const getAccountInfoQB = (
121
+ db: AccountDb,
122
+ deviceId: DeviceId,
123
+ did: string,
124
+ ) => {
125
+ return selectAccountInfoQB(db, deviceId).where('actor.did', '=', did)
126
+ }
127
+
128
+ export const listRememberedQB = (db: AccountDb, deviceId: DeviceId) =>
129
+ selectAccountInfoQB(db, deviceId).where('device_account.remember', '=', 1)
130
+
131
+ export const removeQB = (db: AccountDb, deviceId: DeviceId, did: string) =>
132
+ db.db
133
+ .deleteFrom('device_account')
134
+ .where('deviceId', '=', deviceId)
135
+ .where('did', '=', did)
@@ -0,0 +1,45 @@
1
+ import { DeviceId, DeviceData } from '@atproto/oauth-provider'
2
+ import { AccountDb, Device } from '../db'
3
+ import { fromDateISO, toDateISO } from '../../db'
4
+ import { Selectable } from 'kysely'
5
+
6
+ export const rowToDeviceData = (row: Selectable<Device>): DeviceData => ({
7
+ sessionId: row.sessionId,
8
+ userAgent: row.userAgent,
9
+ ipAddress: row.ipAddress,
10
+ lastSeenAt: fromDateISO(row.lastSeenAt),
11
+ })
12
+
13
+ export const createQB = (
14
+ db: AccountDb,
15
+ deviceId: DeviceId,
16
+ { sessionId, userAgent, ipAddress, lastSeenAt }: DeviceData,
17
+ ) =>
18
+ db.db.insertInto('device').values({
19
+ id: deviceId,
20
+ sessionId,
21
+ userAgent,
22
+ ipAddress,
23
+ lastSeenAt: toDateISO(lastSeenAt),
24
+ })
25
+
26
+ export const readQB = (db: AccountDb, deviceId: DeviceId) =>
27
+ db.db.selectFrom('device').where('id', '=', deviceId).selectAll()
28
+
29
+ export const updateQB = (
30
+ db: AccountDb,
31
+ deviceId: DeviceId,
32
+ { sessionId, userAgent, ipAddress, lastSeenAt }: Partial<DeviceData>,
33
+ ) =>
34
+ db.db
35
+ .updateTable('device')
36
+ .if(sessionId != null, (qb) => qb.set({ sessionId }))
37
+ .if(userAgent != null, (qb) => qb.set({ userAgent }))
38
+ .if(ipAddress != null, (qb) => qb.set({ ipAddress }))
39
+ .if(lastSeenAt != null, (qb) =>
40
+ qb.set({ lastSeenAt: toDateISO(lastSeenAt!) }),
41
+ )
42
+ .where('id', '=', deviceId)
43
+
44
+ export const removeQB = (db: AccountDb, deviceId: DeviceId) =>
45
+ db.db.deleteFrom('device').where('id', '=', deviceId)
@@ -0,0 +1,185 @@
1
+ import {
2
+ Code,
3
+ NewTokenData,
4
+ OAuthAuthorizationDetail,
5
+ RefreshToken,
6
+ TokenData,
7
+ TokenId,
8
+ TokenInfo,
9
+ } from '@atproto/oauth-provider'
10
+ import { Selectable } from 'kysely'
11
+ import {
12
+ fromDateISO,
13
+ fromJsonArray,
14
+ fromJsonObject,
15
+ toDateISO,
16
+ toJsonArray,
17
+ toJsonObject,
18
+ } from '../../db'
19
+ import { AccountDb, Token } from '../db'
20
+ import { ActorAccount, selectAccountQB } from './account'
21
+ import {
22
+ SelectableDeviceAccount,
23
+ toAccount,
24
+ toDeviceAccountInfo,
25
+ } from './device-account'
26
+
27
+ type LeftJoined<T> = { [K in keyof T]: null | T[K] }
28
+
29
+ export type ActorAccountToken = Selectable<ActorAccount> &
30
+ Selectable<Omit<Token, 'id' | 'did'>> &
31
+ LeftJoined<SelectableDeviceAccount>
32
+
33
+ export const toTokenInfo = (
34
+ row: ActorAccountToken,
35
+ audience: string,
36
+ ): TokenInfo => ({
37
+ id: row.tokenId,
38
+ data: {
39
+ createdAt: fromDateISO(row.createdAt),
40
+ expiresAt: fromDateISO(row.expiresAt),
41
+ updatedAt: fromDateISO(row.updatedAt),
42
+ clientId: row.clientId,
43
+ clientAuth: fromJsonObject(row.clientAuth),
44
+ deviceId: row.deviceId,
45
+ sub: row.did,
46
+ parameters: fromJsonObject(row.parameters),
47
+ details: row.details
48
+ ? fromJsonArray<OAuthAuthorizationDetail>(row.details)
49
+ : null,
50
+ code: row.code,
51
+ },
52
+ account: toAccount(row, audience),
53
+ info:
54
+ row.authenticatedAt != null &&
55
+ row.authorizedClients != null &&
56
+ row.remember != null
57
+ ? toDeviceAccountInfo(row as SelectableDeviceAccount)
58
+ : undefined,
59
+ currentRefreshToken: row.currentRefreshToken,
60
+ })
61
+
62
+ const selectTokenInfoQB = (db: AccountDb) =>
63
+ selectAccountQB(db, { includeDeactivated: true })
64
+ // uses "token_did_idx" index (though unlikely in practice)
65
+ .innerJoin('token', 'token.did', 'actor.did')
66
+ .leftJoin('device_account', (join) =>
67
+ join
68
+ // uses "device_account_pk" index
69
+ .on('device_account.did', '=', 'token.did')
70
+ // @ts-expect-error "deviceId" is nullable in token
71
+ .on('device_account.deviceId', '=', 'token.deviceId'),
72
+ )
73
+ .select([
74
+ 'token.tokenId',
75
+ 'token.createdAt',
76
+ 'token.updatedAt',
77
+ 'token.expiresAt',
78
+ 'token.clientId',
79
+ 'token.clientAuth',
80
+ 'token.deviceId',
81
+ 'token.did',
82
+ 'token.parameters',
83
+ 'token.details',
84
+ 'token.code',
85
+ 'token.currentRefreshToken',
86
+ 'device_account.authenticatedAt',
87
+ 'device_account.authorizedClients',
88
+ 'device_account.remember',
89
+ ])
90
+
91
+ export const createQB = (
92
+ db: AccountDb,
93
+ tokenId: TokenId,
94
+ data: TokenData,
95
+ refreshToken?: RefreshToken,
96
+ ) =>
97
+ db.db.insertInto('token').values({
98
+ tokenId,
99
+ createdAt: toDateISO(data.createdAt),
100
+ expiresAt: toDateISO(data.expiresAt),
101
+ updatedAt: toDateISO(data.updatedAt),
102
+ clientId: data.clientId,
103
+ clientAuth: toJsonObject(data.clientAuth),
104
+ deviceId: data.deviceId,
105
+ did: data.sub,
106
+ parameters: toJsonObject(data.parameters),
107
+ details: data.details ? toJsonArray(data.details) : null,
108
+ code: data.code,
109
+ currentRefreshToken: refreshToken || null,
110
+ })
111
+
112
+ export const forRotateQB = (db: AccountDb, id: TokenId) =>
113
+ db.db
114
+ .selectFrom('token')
115
+ .where('tokenId', '=', id)
116
+ .where('currentRefreshToken', 'is not', null)
117
+ .select(['id', 'currentRefreshToken'])
118
+
119
+ export const findByQB = (
120
+ db: AccountDb,
121
+ search: {
122
+ id?: number
123
+ code?: Code
124
+ tokenId?: TokenId
125
+ currentRefreshToken?: RefreshToken
126
+ },
127
+ ) => {
128
+ if (
129
+ search.id === undefined &&
130
+ search.code === undefined &&
131
+ search.tokenId === undefined &&
132
+ search.currentRefreshToken === undefined
133
+ ) {
134
+ // Prevent accidental scan
135
+ throw new TypeError('At least one search parameter is required')
136
+ }
137
+
138
+ return selectTokenInfoQB(db)
139
+ .if(search.id !== undefined, (qb) =>
140
+ // uses primary key index
141
+ qb.where('token.id', '=', search.id!),
142
+ )
143
+ .if(search.code !== undefined, (qb) =>
144
+ // uses "token_code_idx" partial index (hence the null check)
145
+ qb
146
+ .where('token.code', '=', search.code!)
147
+ .where('token.code', 'is not', null),
148
+ )
149
+ .if(search.tokenId !== undefined, (qb) =>
150
+ // uses "token_token_id_idx"
151
+ qb.where('token.tokenId', '=', search.tokenId!),
152
+ )
153
+ .if(search.currentRefreshToken !== undefined, (qb) =>
154
+ // uses "token_refresh_token_unique_idx"
155
+ qb.where('token.currentRefreshToken', '=', search.currentRefreshToken!),
156
+ )
157
+ }
158
+
159
+ export const removeByDidQB = (db: AccountDb, did: string) =>
160
+ // uses "token_did_idx" index
161
+ db.db.deleteFrom('token').where('did', '=', did)
162
+
163
+ export const rotateQB = (
164
+ db: AccountDb,
165
+ id: number,
166
+ newTokenId: TokenId,
167
+ newRefreshToken: RefreshToken,
168
+ newData: NewTokenData,
169
+ ) =>
170
+ db.db
171
+ .updateTable('token')
172
+ .set({
173
+ tokenId: newTokenId,
174
+ currentRefreshToken: newRefreshToken,
175
+
176
+ expiresAt: toDateISO(newData.expiresAt),
177
+ updatedAt: toDateISO(newData.updatedAt),
178
+ clientAuth: toJsonObject(newData.clientAuth),
179
+ })
180
+ // uses primary key index
181
+ .where('id', '=', id)
182
+
183
+ export const removeQB = (db: AccountDb, tokenId: TokenId) =>
184
+ // uses "used_refresh_token_fk" to cascade delete
185
+ db.db.deleteFrom('token').where('tokenId', '=', tokenId)
@@ -0,0 +1,30 @@
1
+ import { RefreshToken } from '@atproto/oauth-provider'
2
+ import { AccountDb } from '../db'
3
+
4
+ /**
5
+ * Note that the used refresh tokens will be removed once the token is revoked.
6
+ * This is done through the foreign key constraint in the database.
7
+ */
8
+ export const insertQB = (
9
+ db: AccountDb,
10
+ tokenId: number,
11
+ refreshToken: RefreshToken,
12
+ ) =>
13
+ db.db
14
+ .insertInto('used_refresh_token')
15
+ .values({ tokenId, refreshToken })
16
+ .onConflict((oc) => oc.doNothing())
17
+
18
+ export const findByTokenQB = (db: AccountDb, refreshToken: RefreshToken) =>
19
+ db.db
20
+ .selectFrom('used_refresh_token')
21
+ // uses primary key index
22
+ .where('refreshToken', '=', refreshToken)
23
+ .select('tokenId')
24
+
25
+ export const countQB = (db: AccountDb, refreshToken: RefreshToken) =>
26
+ db.db
27
+ .selectFrom('used_refresh_token')
28
+ // uses primary key index
29
+ .where('refreshToken', '=', refreshToken)
30
+ .select((qb) => qb.fn.count<number>('refreshToken').as('count'))