Skip to content

Commit 09439d7

Browse files
OAuth client improvements (#4216)
* wip * Various OAuth client & API improvements * pnpm lock * Minor typing improvements * ci * fix
1 parent f560cf2 commit 09439d7

37 files changed

+665
-352
lines changed

.changeset/chilly-goats-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@atproto/api": patch
3+
---
4+
5+
Create a dedicated type for the `proxy` property

.changeset/cold-toys-know.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@atproto/oauth-types": patch
3+
---
4+
5+
Expose new utilities to build and work with atproto loopback client metadata

.changeset/old-spiders-hide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@atproto/api": patch
3+
---
4+
5+
Improve validation when setting an `Agent`'s `proxy` property

.changeset/perfect-onions-crash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@atproto/oauth-client": patch
3+
---
4+
5+
Use `AbortSignal.timeout` to generate timeout based signals

.changeset/tidy-doors-punch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@atproto/oauth-types": patch
3+
---
4+
5+
Improve validation of `AtprotoOAuthScope`

.changeset/tough-islands-smell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@atproto/oauth-types": patch
3+
---
4+
5+
Add `AtprotoOAuthTokenResponse` schema

.changeset/wicked-foxes-beg.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@atproto/did': patch
3+
---
4+
5+
Minor typing improvements

packages/api/src/agent.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,18 @@ import * as predicate from './predicate'
3333
import { SessionManager } from './session-manager'
3434
import {
3535
AtpAgentGlobalOpts,
36+
AtprotoProxy,
3637
AtprotoServiceType,
3738
BskyFeedViewPreference,
3839
BskyInterestsPreference,
3940
BskyPreferences,
4041
BskyThreadViewPreference,
42+
asAtprotoProxy,
43+
asDid,
44+
isDid,
4145
} from './types'
4246
import {
43-
Did,
44-
asDid,
4547
getSavedFeedType,
46-
isDid,
4748
sanitizeMutedWordValue,
4849
savedFeedsToUriArrays,
4950
validateNux,
@@ -187,12 +188,11 @@ export class Agent extends XrpcClient {
187188

188189
//#region ATPROTO proxy configuration utilities
189190

190-
proxy?: `${Did}#${AtprotoServiceType}`
191+
proxy?: AtprotoProxy
191192

192-
configureProxy(value: `${Did}#${AtprotoServiceType}` | null) {
193+
configureProxy(value: AtprotoProxy | null) {
193194
if (value === null) this.proxy = undefined
194-
else if (isDid(value)) this.proxy = value
195-
else throw new TypeError('Invalid proxy DID')
195+
else this.proxy = asAtprotoProxy(value)
196196
}
197197

198198
/** @deprecated use {@link configureProxy} instead */

packages/api/src/types.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,53 @@
11
import { AppBskyActorDefs } from './client'
22
import { ModerationPrefs } from './moderation/types'
33

4-
/**
5-
* Supported proxy targets
6-
*/
7-
type UnknownServiceType = string & NonNullable<unknown>
4+
export type UnknownServiceType = string & NonNullable<unknown>
85
export type AtprotoServiceType = 'atproto_labeler' | UnknownServiceType
6+
export function isAtprotoServiceType<T extends string>(
7+
input: T,
8+
): input is T & AtprotoServiceType {
9+
return !input.includes(' ') && !input.includes('#')
10+
}
11+
12+
// @TODO use tools from @atproto/did
13+
export type Did = `did:${string}:${string}`
14+
export function isDid<T extends string>(input: T): input is T & Did {
15+
if (!input.startsWith('did:')) return false
16+
if (input.length < 8) return false
17+
if (input.length > 2048) return false
18+
const msidx = input.indexOf(':', 4)
19+
return msidx > 4 && msidx < input.length - 1
20+
}
21+
22+
export function assertDid(input: string): asserts input is Did {
23+
if (!isDid(input)) throw new TypeError(`Invalid DID: ${input}`)
24+
}
25+
26+
export function asDid<T extends string>(input: T) {
27+
assertDid(input)
28+
return input
29+
}
30+
31+
export type AtprotoProxy = `${Did}#${AtprotoServiceType}`
32+
export function isAtprotoProxy(input: string): input is AtprotoProxy {
33+
const { length, [0]: did, [1]: service } = input.split('#')
34+
return length === 2 && isDid(did) && isAtprotoServiceType(service)
35+
}
36+
37+
export function assertAtprotoProxy(
38+
input: string,
39+
): asserts input is AtprotoProxy {
40+
if (!isAtprotoProxy(input)) {
41+
throw new TypeError(
42+
`Invalid DID reference: ${input} (must be of the form did:example:alice#service)`,
43+
)
44+
}
45+
}
46+
47+
export function asAtprotoProxy<T extends string>(input: T) {
48+
assertAtprotoProxy(input)
49+
return input
50+
}
951

1052
/**
1153
* Used by the PersistSessionHandler to indicate what change occurred

packages/api/src/util.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,6 @@ export function validateSavedFeed(savedFeed: AppBskyActorDefs.SavedFeed) {
8282
}
8383
}
8484

85-
export type Did = `did:${string}`
86-
87-
// @TODO use tools from @atproto/did
88-
export const isDid = (str: unknown): str is Did =>
89-
typeof str === 'string' &&
90-
str.startsWith('did:') &&
91-
str.includes(':', 4) &&
92-
str.length > 8 &&
93-
str.length <= 2048
94-
95-
export const asDid = (value: string): Did => {
96-
if (isDid(value)) return value
97-
throw new TypeError(`Invalid DID: ${value}`)
98-
}
99-
10085
export const nuxSchema = z
10186
.object({
10287
id: z.string().max(64),

0 commit comments

Comments
 (0)