8000 Identity resolver's `resolve()` method returns valud consistent with `resolveIdentity` by matthieusieben · Pull Request #3982 · bluesky-social/atproto · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Identity resolver's resolve() method returns valud consistent with resolveIdentity #3982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dirty-ducks-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@atproto/oauth-client": patch
---

Allow providing custom `identityProvider` implementation as `OAuthClient` constructor option
5 changes: 5 additions & 0 deletions .changeset/few-rivers-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@atproto-labs/identity-resolver": minor
---

Identity resolver's `resolve()` method returns value consistent with `com.atproto.identity.resolveIdentity`
5 changes: 5 additions & 0 deletions .changeset/four-bags-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@atproto-labs/identity-resolver": minor
---

`IdentityResolver` is now an interface. The `IdentityResolverProto` class is the default implementation for the `IdentityResolver` interface.
5 changes: 5 additions & 0 deletions .changeset/neat-rocks-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@atproto/oauth-client": minor
---

`didResolver` and `handleResolver` no longer exposed on `OAuthClient` class
5 changes: 5 additions & 0 deletions .changeset/soft-melons-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@atproto-labs/identity-resolver": patch
---

Export `HANDLE_INVALID` constant
126 changes: 126 additions & 0 deletions packages/internal/identity-resolver/src/atproto-identity-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import {
AtprotoDid,
AtprotoIdentityDidMethods,
DidDocument,
DidResolver,
ResolveDidOptions,
isAtprotoDid,
} from '@atproto-labs/did-resolver'
import {
HandleResolver,
ResolveHandleOptions,
} from '@atproto-labs/handle-resolver'
import { HANDLE_INVALID } from './constants.js'
import { IdentityResolverError } from './identity-resolver-error.js'
import {
IdentityInfo,
IdentityResolver,
ResolveIdentityOptions,
} from './identity-resolver.js'
import { asNormalizedHandle, extractNormalizedHandle } from './util.js'

// @TODO Move this to its own package as soon as we have a distinct
// implementation based on XRPC calls to the
// "com.atproto.identity.resolveIdentity" method.

/**
* Implementation of the official ATPROTO identity resolution strategy.
* This implementation relies on two primitives:
* - DID resolution (using the `DidResolver` interface)
* - Handle resolution (using the `HandleResolver` interface)
*/
export class AtprotoIdentityResolver implements IdentityResolver {
constructor(
protected readonly didResolver: DidResolver<AtprotoIdentityDidMethods>,
protected readonly handleResolver: HandleResolver,
) {}

public async resolve(
input: string,
options?: ResolveIdentityOptions,
): Promise<IdentityInfo> {
return isAtprotoDid(input)
? this.resolveFromDid(input, options)
: this.resolveFromHandle(input, options)
}

public async resolveFromDid(
did: AtprotoDid,
options?: ResolveDidOptions,
): Promise<IdentityInfo> {
const document = await this.getDocumentFromDid(did, options)

options?.signal?.throwIfAborted()

// We will only return the document's handle alias if it resolves to the
// same DID as the input.
const handle = extractNormalizedHandle(document)
const resolvedDid = handle
? await this.handleResolver
.resolve(handle, options)
.catch(() => undefined) // Ignore errors (temporarily unavailable)
: undefined

return {
did: document.id,
didDoc: document,
handle: handle && resolvedDid === did ? handle : HANDLE_INVALID,
}
}

public async resolveFromHandle(
handle: string,
options?: ResolveHandleOptions,
): Promise<IdentityInfo> {
const document = awa 8000 it this.getDocumentFromHandle(handle, options)

// @NOTE bi-directional resolution enforced in getDocumentFromHandle()

return {
did: document.id,
didDoc: document,
handle: extractNormalizedHandle(document) || HANDLE_INVALID,
}
}

public async getDocumentFromDid(
did: AtprotoDid,
options?: ResolveDidOptions,
): Promise<DidDocument<AtprotoIdentityDidMethods>> {
return this.didResolver.resolve(did, options)
}

public async getDocumentFromHandle(
input: string,
options?: ResolveHandleOptions,
): Promise<DidDocument<AtprotoIdentityDidMethods>> {
const handle = asNormalizedHandle(input)
if (!handle) {
throw new IdentityResolverError(`Invalid handle "${input}" provided.`)
}

const did = await this.handleResolver.resolve(handle, options)

if (!did) {
throw new IdentityResolverError(
`Handle "${handle}" does not resolve to a DID`,
)
}

options?.signal?.throwIfAborted()

// Note: Not using "return this.resolveDid(did, options)" to make the extra
// check for the handle in the DID document:

const document = await this.didResolver.resolve(did, options)

// Enforce bi-directional resolution
if (handle !== extractNormalizedHandle(document)) {
throw new IdentityResolverError(
`Did document for "${did}" does not include the handle "${handle}"`,
)
}

return document
}
}
1 change: 1 addition & 0 deletions packages/internal/identity-resolver/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const HANDLE_INVALID = 'handle.invalid'
125 changes: 19 additions & 106 deletions packages/internal/identity-resolver/src/identity-resolver.ts
const resolvedDid = handle
Original file line number Diff line number Diff line change
Expand Up @@ -2,119 +2,32 @@ import {
AtprotoDid,
AtprotoIdentityDidMethods,
DidDocument,
DidResolver,
ResolveDidOptions,
isAtprotoDid,
} from '@atproto-labs/did-resolver'
import {
9E12 HandleResolver,
ResolveHandleOptions,
} from '@atproto-labs/handle-resolver'
import { IdentityResolverError } from './identity-resolver-error'
import { asNormalizedHandle, extractNormalizedHandle } from './util'
import { HANDLE_INVALID } from './constants'

// Consistent with `com.atproto.identity.defs#identityInfo` returned by
// `com.atproto.identity.resolveIdentity` endpoint.
export type IdentityInfo = {
did: AtprotoDid

export type ResolvedIdentity = {
document: DidDocument<AtprotoIdentityDidMethods>
didDoc: DidDocument<AtprotoIdentityDidMethods>

/**
* Will be defined if the document contains a handle alias that resolves
* to the document's DID.
* Will be 'handle.invalid' if the handle does not resolve to the
* same DID as the input, or if the handle is not present in the DID
* document.
*/
handle?: string
handle: typeof HANDLE_INVALID | string
}

export type ResolveIdentityOptions = ResolveDidOptions & ResolveHandleOptions

export class IdentityResolver {
constructor(
readonly didResolver: DidResolver<AtprotoIdentityDidMethods>,
readonly handleResolver: HandleResolver,
) {}
export type ResolveIdentityOptions = {
signal?: AbortSignal
noCache?: boolean
}

public async resolve(
input: string,
export interface IdentityResolver {
resolve(
identifier: string,
options?: ResolveIdentityOptions,
): Promise<ResolvedIdentity> {
return isAtprotoDid(input)
? this.resolveFromDid(input, options)
: this.resolveFromHandle(input, options)
}

public async resolveFromDid(
did: AtprotoDid,
options?: ResolveDidOptions,
): Promise<ResolvedIdentity> {
const document = await this.getDocumentFromDid(did, options)

options?.signal?.throwIfAborted()

// We will only return the document's handle alias if it resolves to the
// same DID as the input.
const handle = extractNormalizedHandle(document)
? await this.handleResolver
.resolve(handle, options)
.catch(() => undefined) // Ignore errors (temporarily unavailable)
: undefined

return {
document,
handle: resolvedDid === did ? handle : undefined,
}
}

public async resolveFromHandle(
handle: string,
options?: ResolveHandleOptions,
): Promise<ResolvedIdentity> {
const document = await this.getDocumentFromHandle(handle, options)

// @NOTE bi-directional resolution enforced in getDocumentFromHandle()

return {
document,
handle: extractNormalizedHandle(document),
}
}

public async getDocumentFromDid(
did: AtprotoDid,
options?: ResolveDidOptions,
): Promise<DidDocument<AtprotoIdentityDidMethods>> {
return this.didResolver.resolve(did, options)
}

public async getDocumentFromHandle(
input: string,
options?: ResolveHandleOptions,
): Promise<DidDocument<AtprotoIdentityDidMethods>> {
const handle = asNormalizedHandle(input)
if (!handle) {
throw new IdentityResolverError(`Invalid handle "${input}" provided.`)
}

const did = await this.handleResolver.resolve(handle, options)

if (!did) {
throw new IdentityResolverError(
`Handle "${handle}" does not resolve to a DID`,
)
}

options?.signal?.throwIfAborted()

// Note: Not using "return this.resolveDid(did, options)" to make the extra
// check for the handle in the DID document:

const document = await this.didResolver.resolve(did, options)

// Enforce bi-directional resolution
if (handle !== extractNormalizedHandle(document)) {
throw new IdentityResolverError(
`Did document for "${did}" does not include the handle "${handle}"`,
)
}

return document
}
): Promise<IdentityInfo>
}
2 changes: 2 additions & 0 deletions packages/internal/identity-resolver/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './atproto-identity-resolver.js'
export * from './constants.js'
export * from './identity-resolver-error.js'
export * from './identity-resolver.js'
export * from './util.js'
Loading
0