generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added convenience PermissionRequest class (#767)
Convenience class so that a dev doesn't have to deal with a `RecordsWrite` and understand the schema directly.
- Loading branch information
1 parent
8446cdb
commit 4866891
Showing
7 changed files
with
115 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type { DataEncodedRecordsWriteMessage } from '../types/records-types.js'; | ||
import type { PermissionConditions, PermissionRequestData, PermissionScope } from '../types/permission-types.js'; | ||
|
||
import { Encoder } from '../utils/encoder.js'; | ||
import { Message } from '../core/message.js'; | ||
|
||
|
||
/** | ||
* A class representing a Permission Request for a more convenient abstraction. | ||
*/ | ||
export class PermissionRequest { | ||
|
||
/** | ||
* The ID of the permission request, which is the record ID DWN message. | ||
*/ | ||
public readonly id: string; | ||
|
||
/** | ||
* The requester for of the permission. | ||
*/ | ||
public readonly requester: string; | ||
|
||
/** | ||
* Optional string that communicates what the requested grant would be used for. | ||
*/ | ||
public readonly description?: string; | ||
|
||
/** | ||
* Whether the requested grant is delegated or not. | ||
* If `true`, the `requestor` will be able to act as the grantor of the permission within the scope of the requested grant. | ||
*/ | ||
public readonly delegated?: boolean; | ||
|
||
/** | ||
* The scope of the allowed access. | ||
*/ | ||
public readonly scope: PermissionScope; | ||
|
||
/** | ||
* Optional conditions that must be met when the requested grant is used. | ||
*/ | ||
public readonly conditions?: PermissionConditions; | ||
|
||
public static async parse(message: DataEncodedRecordsWriteMessage): Promise<PermissionRequest> { | ||
const permissionRequest = new PermissionRequest(message); | ||
return permissionRequest; | ||
} | ||
|
||
private constructor(message: DataEncodedRecordsWriteMessage) { | ||
// properties derived from the generic DWN message properties | ||
this.id = message.recordId; | ||
this.requester = Message.getSigner(message)!; | ||
|
||
// properties from the data payload itself. | ||
const permissionRequestEncodedData = message.encodedData; | ||
const permissionRequestData = Encoder.base64UrlToObject(permissionRequestEncodedData) as PermissionRequestData; | ||
this.delegated = permissionRequestData.delegated; | ||
this.description = permissionRequestData.description; | ||
this.scope = permissionRequestData.scope; | ||
this.conditions = permissionRequestData.conditions; | ||
} | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { RecordsPermissionScope } from '../../src/types/permission-types.js'; | ||
|
||
import chaiAsPromised from 'chai-as-promised'; | ||
import sinon from 'sinon'; | ||
import chai, { expect } from 'chai'; | ||
|
||
import { Jws } from '../../src/utils/jws.js'; | ||
import { DwnInterfaceName, DwnMethodName, PermissionRequest, PermissionsProtocol, TestDataGenerator } from '../../src/index.js'; | ||
|
||
chai.use(chaiAsPromised); | ||
|
||
describe('PermissionRequest', () => { | ||
afterEach(() => { | ||
// restores all fakes, stubs, spies etc. not restoring causes a memory leak. | ||
// more info here: https://sinonjs.org/releases/v13/general-setup/ | ||
sinon.restore(); | ||
}); | ||
|
||
it('should parse a permission request message into a PermissionRequest', async () => { | ||
const alice = await TestDataGenerator.generateDidKeyPersona(); | ||
const scope: RecordsPermissionScope = { | ||
interface : DwnInterfaceName.Records, | ||
method : DwnMethodName.Query, | ||
protocol : 'https://example.com/protocol/test' | ||
}; | ||
|
||
const permissionRequest = await PermissionsProtocol.createRequest({ | ||
signer : Jws.createSigner(alice), | ||
delegated : true, | ||
scope | ||
}); | ||
|
||
const parsedPermissionRequest = await PermissionRequest.parse(permissionRequest.dataEncodedMessage); | ||
expect (parsedPermissionRequest.id).to.equal(permissionRequest.dataEncodedMessage.recordId); | ||
expect (parsedPermissionRequest.delegated).to.equal(true); | ||
expect (parsedPermissionRequest.scope).to.deep.equal(scope); | ||
}); | ||
}); |