-
Notifications
You must be signed in to change notification settings - Fork 723
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
--- | ||
"@atproto/oauth-client": patch | ||
--- | ||
|
||
Allow providing custom `identityProvider` implementation as `OAuthClient` constructor option |
This file contains hidden or 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,5 @@ | ||
--- | ||
"@atproto-labs/identity-resolver": minor | ||
--- | ||
|
||
Identity resolver's `resolve()` method returns value consistent with `com.atproto.identity.resolveIdentity` |
This file contains hidden or 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,5 @@ | ||
--- | ||
"@atproto-labs/identity-resolver": minor | ||
--- | ||
|
||
`IdentityResolver` is now an interface. The `IdentityResolverProto` class is the default implementation for the `IdentityResolver` interface. |
This file contains hidden or 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,5 @@ | ||
--- | ||
"@atproto/oauth-client": minor | ||
--- | ||
|
||
`didResolver` and `handleResolver` no longer exposed on `OAuthClient` class |
This file contains hidden or 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,5 @@ | ||
--- | ||
"@atproto-labs/identity-resolver": patch | ||
--- | ||
|
||
Export `HANDLE_INVALID` constant |
126 changes: 126 additions & 0 deletions
126
packages/internal/identity-resolver/src/atproto-identity-resolver.ts
This file contains hidden or 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,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 | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
export const HANDLE_INVALID = 'handle.invalid' |
This file contains hidden or 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 hidden or 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,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' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.