-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from Sphereon-Opensource/feature/VDX-177/autho…
…rization_details feature/VDX-177/authorization_details
- Loading branch information
Showing
5 changed files
with
247 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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,45 @@ | ||
import { AuthorizationDetails } from '@sphereon/openid4vci-common'; | ||
import { CredentialFormat } from '@sphereon/ssi-types'; | ||
|
||
export class AuthorizationDetailsBuilder { | ||
private authorizationDetails: Partial<AuthorizationDetails>; | ||
|
||
constructor() { | ||
this.authorizationDetails = {}; | ||
} | ||
|
||
withType(type: string): AuthorizationDetailsBuilder { | ||
this.authorizationDetails.type = type; | ||
return this; | ||
} | ||
|
||
withFormats(format: CredentialFormat): AuthorizationDetailsBuilder { | ||
this.authorizationDetails.format = format; | ||
return this; | ||
} | ||
|
||
withLocations(locations: string[]): AuthorizationDetailsBuilder { | ||
if (this.authorizationDetails.locations) { | ||
this.authorizationDetails.locations.push(...locations); | ||
} else { | ||
this.authorizationDetails.locations = locations; | ||
} | ||
return this; | ||
} | ||
|
||
addLocation(location: string): AuthorizationDetailsBuilder { | ||
if (this.authorizationDetails.locations) { | ||
this.authorizationDetails.locations.push(location); | ||
} else { | ||
this.authorizationDetails.locations = [location]; | ||
} | ||
return this; | ||
} | ||
|
||
build(): AuthorizationDetails { | ||
if (this.authorizationDetails.format && this.authorizationDetails.type) { | ||
return this.authorizationDetails as AuthorizationDetails; | ||
} | ||
throw new Error('Type and format are required properties'); | ||
} | ||
} |
This file contains 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
46 changes: 46 additions & 0 deletions
46
packages/client/lib/__tests__/AuthorizationDetailsBuilder.spec.ts
This file contains 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,46 @@ | ||
import { AuthorizationDetailsBuilder } from '../AuthorizationDetailsBuilder'; | ||
|
||
describe('AuthorizationDetailsBuilder test', () => { | ||
it('should create AuthorizationDetails object from arrays', () => { | ||
const actual = new AuthorizationDetailsBuilder().withFormats('jwt_vc').withLocations(['test1', 'test2']).withType('openid_credential').build(); | ||
expect(actual).toEqual({ | ||
type: 'openid_credential', | ||
format: 'jwt_vc', | ||
locations: ['test1', 'test2'], | ||
}); | ||
}); | ||
it('should create AuthorizationDetails object from single objects', () => { | ||
const actual = new AuthorizationDetailsBuilder().withFormats('jwt_vc').withLocations(['test1']).withType('openid_credential').build(); | ||
expect(actual).toEqual({ | ||
type: 'openid_credential', | ||
format: 'jwt_vc', | ||
locations: ['test1'], | ||
}); | ||
}); | ||
it('should create AuthorizationDetails object if locations is missing', () => { | ||
const actual = new AuthorizationDetailsBuilder().withFormats('jwt_vc').withType('openid_credential').build(); | ||
expect(actual).toEqual({ | ||
type: 'openid_credential', | ||
format: 'jwt_vc', | ||
}); | ||
}); | ||
it('should fail if type is missing', () => { | ||
expect(() => { | ||
new AuthorizationDetailsBuilder().withFormats('jwt_vc').withLocations(['test1']).build(); | ||
}).toThrow(Error('Type and format are required properties')); | ||
}); | ||
it('should fail if format is missing', () => { | ||
expect(() => { | ||
new AuthorizationDetailsBuilder().withType('openid_credential').withLocations(['test1']).build(); | ||
}).toThrow(Error('Type and format are required properties')); | ||
}); | ||
it('should be able to add random field to the object', () => { | ||
const actual = new AuthorizationDetailsBuilder().withFormats('jwt_vc').withType('openid_credential').build(); | ||
actual['random'] = 'test'; | ||
expect(actual).toEqual({ | ||
type: 'openid_credential', | ||
format: 'jwt_vc', | ||
random: 'test', | ||
}); | ||
}); | ||
}); |
This file contains 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 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