Skip to content
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

feat: use cross-fetch instead of XMLHttpRequest to handle redirects #15

Merged
merged 5 commits into from
Oct 1, 2019
Merged
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -4,6 +4,6 @@
"editor.tabSize": 2,
"editor.detectIndentation": false,
"[typescript]": {
"editor.formatOnSave": true
"editor.formatOnSave": false
mi-xu marked this conversation as resolved.
Show resolved Hide resolved
}
}
34 changes: 17 additions & 17 deletions src/__tests__/resolver.test.ts
Original file line number Diff line number Diff line change
@@ -15,31 +15,31 @@ describe('web did resolver', () => {
id: `${did}#owner`,
type: 'Secp256k1VerificationKey2018',
owner: did,
ethereumAddress: identity,
},
ethereumAddress: identity
}
],
authentication: [
{
type: 'Secp256k1SignatureAuthentication2018',
publicKey: `${did}#owner`,
},
],
publicKey: `${did}#owner`
}
]
}
const noContextResponse: object = {
id: validResponse.id,
publicKey: validResponse.publicKey,
authentication: validResponse.authentication,
authentication: validResponse.authentication
}
const wrongIdResponse: object = {
'@context': validResponse['@context'],
id: 'did:web:wrong.com',
publicKey: validResponse.publicKey,
authentication: validResponse.authentication,
authentication: validResponse.authentication
}
const noPublicKeyResponse: object = {
'@context': validResponse['@context'],
id: validResponse.id,
authentication: validResponse.authentication,
authentication: validResponse.authentication
}

let didResolver: Resolver
@@ -52,7 +52,7 @@ describe('web did resolver', () => {

it('resolves document', () => {
mockedFetch.mockResolvedValueOnce({
json: () => validResponse,
json: () => validResponse
})
return expect(didResolver.resolve(did)).resolves.toEqual(validResponse)
})
@@ -66,37 +66,37 @@ describe('web did resolver', () => {
mockedFetch.mockResolvedValueOnce({
json: () => {
throw new Error('unable to parse json')
},
}
})
return expect(didResolver.resolve(did)).rejects.toThrowError(
/unable to parse json/,
/unable to parse json/
)
})

it('fails if the did document is missing a context', () => {
mockedFetch.mockResolvedValueOnce({
json: () => noContextResponse,
json: () => noContextResponse
})
return expect(didResolver.resolve(did)).rejects.toThrowError(
'DID document missing context',
'DID document missing context'
)
})

it('fails if the did document id does not match', () => {
mockedFetch.mockResolvedValueOnce({
json: () => wrongIdResponse,
json: () => wrongIdResponse
})
return expect(didResolver.resolve(did)).rejects.toThrowError(
'DID document id does not match requested did',
'DID document id does not match requested did'
)
})

it('fails if the did document has no public keys', () => {
mockedFetch.mockResolvedValueOnce({
json: () => noPublicKeyResponse,
json: () => noPublicKeyResponse
})
return expect(didResolver.resolve(did)).rejects.toThrowError(
'DID document has no public keys',
'DID document has no public keys'
)
})
})
11 changes: 6 additions & 5 deletions src/resolver.ts
Original file line number Diff line number Diff line change
@@ -6,8 +6,8 @@ const DOC_PATH = '/.well-known/did.json'
async function get(url: string): Promise<any> {
const res = await fetch(url, {
headers: {
'Access-Control-Allow-Origin': '*',
},
'Access-Control-Allow-Origin': '*'
}
})
if (res.status >= 400) {
throw new Error(`Bad response ${res.statusText}`)
@@ -18,7 +18,7 @@ async function get(url: string): Promise<any> {
export default function getResolver() {
async function resolve(
did: string,
parsed: ParsedDID,
parsed: ParsedDID
): Promise<DIDDocument | null> {
const url: string = `https://${parsed.id}${DOC_PATH}`

@@ -29,16 +29,17 @@ export default function getResolver() {
throw new Error(
`DID must resolve to a valid https URL containing a JSON document: ${
error.message
}`,
}`
)
}

const hasContext = data['@context'] === 'https://w3id.org/did/v1'
if (!hasContext) throw new Error('DID document missing context')

const docIdMatchesDid = data.id === did
if (!docIdMatchesDid)
if (!docIdMatchesDid) {
throw new Error('DID document id does not match requested did')
}

const docHasPublicKey =
Array.isArray(data.publicKey) && data.publicKey.length > 0