Skip to content

Commit

Permalink
Correct HTTP status based of DID resolution error code.
Browse files Browse the repository at this point in the history
Check for invalid date string of `versionTime`.
  • Loading branch information
jasny committed Jul 22, 2023
1 parent c964c7a commit e1883a2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/did/did.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ export class DidController {
private async resolveResolution(did: string, versionTime: Date | undefined, res: Response): Promise<Response> {
try {
const resolution = await this.service.resolve(did, versionTime);
const error = resolution.didResolutionMetadata.error;

return res
.status(resolution.didResolutionMetadata.error ? 404 : 200)
.status(!error ? 200 : error === 'notFound' ? 404 : 400)
.header('Content-Type', 'application/ld+json;profile="https://w3id.org/did-resolution";charset=utf-8')
.json(resolution);
} catch (e) {
Expand All @@ -146,7 +147,7 @@ export class DidController {
'@context': 'https://w3id.org/did-resolution/v1',
didDocument: {},
didDocumentMetadata: {},
didResolutionMetadata: { error: 'failed to get DID document', reason: `${e}` },
didResolutionMetadata: { error: 'internalError', reason: `${e}` },
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/did/did.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { KeyType } from './verification-method/model/verification-method.types';
import { base58 } from '@scure/base';
import * as ed2curve from 'ed2curve';
import { isValidAddress, networkId } from '../utils/crypto';
import { isoDate } from '../utils/date';
import { isInvalidDate, isoDate } from '../utils/date';

type DIDDocumentVerificationMethods = Pick<
DIDDocument,
Expand All @@ -35,14 +35,14 @@ export class DIDService {

const created = await this.storage.getAccountCreated(address);

if (!created) {
if (!created || isInvalidDate(versionTime)) {
return {
'@context': 'https://w3id.org/did-resolution/v1',
didDocument: {},
didDocumentMetadata: {},
didResolutionMetadata: {
contentType: 'application/did+ld+json',
error: isValidAddress(address) ? 'notFound' : 'invalidDid',
error: isInvalidDate(versionTime) ? 'invalidDidUrl' : !isValidAddress(address) ? 'invalidDid' : 'notFound',
},
};
}
Expand Down
4 changes: 4 additions & 0 deletions src/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ export function isoDate(date: Date | number): string {
if (typeof date === 'number') date = new Date(date);
return date.toISOString().replace(/\.\d{3}Z$/, 'Z');
}

export function isInvalidDate(date: Date): boolean {
return isNaN(date.getTime());
}

0 comments on commit e1883a2

Please sign in to comment.