-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add simple permissions related handlers
- Loading branch information
Showing
7 changed files
with
133 additions
and
1 deletion.
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
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,16 @@ | ||
import { Credentials } from './Credentials'; | ||
import { CredentialsExtractor } from './CredentialsExtractor'; | ||
import { HttpRequest } from '../server/HttpRequest'; | ||
|
||
export class SimpleCredentialsExtractor extends CredentialsExtractor { | ||
public async canHandle(): Promise<void> { | ||
return undefined; | ||
} | ||
|
||
public async handle(input: HttpRequest): Promise<Credentials> { | ||
if (input.headers.authorization) { | ||
return { webID: input.headers.authorization }; | ||
} | ||
return undefined; | ||
} | ||
} |
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,14 @@ | ||
import { UnsupportedHttpError } from '../util/errors/UnsupportedHttpError'; | ||
import { Authorizer, AuthorizerArgs } from './Authorizer'; | ||
|
||
export class SimpleAuthorizer extends Authorizer { | ||
public async canHandle(input: AuthorizerArgs): Promise<void> { | ||
if (!input.identifier || !input.permissions) { | ||
throw new UnsupportedHttpError('Authorizer requires an identifier and permissions.'); | ||
} | ||
} | ||
|
||
public async handle(): Promise<void> { | ||
return undefined; | ||
} | ||
} |
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,18 @@ | ||
import { Operation } from '../operations/Operation'; | ||
import { PermissionSet } from './PermissionSet'; | ||
import { PermissionsExtractor } from './PermissionsExtractor'; | ||
|
||
export class SimplePermissionsExtractor extends PermissionsExtractor { | ||
public async canHandle(): Promise<void> { | ||
return undefined; | ||
} | ||
|
||
public async handle(input: Operation): Promise<PermissionSet> { | ||
return { | ||
read: input.method === 'GET', | ||
append: false, | ||
write: input.method === 'POST' || input.method === 'PUT', | ||
delete: input.method === 'DELETE', | ||
}; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
test/unit/authentication/SimpleCredentialsExtractor.test.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,19 @@ | ||
import { HttpRequest } from '../../../src/server/HttpRequest'; | ||
import { SimpleCredentialsExtractor } from '../../../src/authentication/SimpleCredentialsExtractor'; | ||
|
||
describe('A SimpleCredentialsExtractor', (): void => { | ||
const extractor = new SimpleCredentialsExtractor(); | ||
|
||
it('can handle all input.', async(): Promise<void> => { | ||
await expect(extractor.canHandle()).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('returns undefined if there is no input.', async(): Promise<void> => { | ||
await expect(extractor.handle({ headers: {}} as HttpRequest)).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('returns the authorization header as webID if there is one.', async(): Promise<void> => { | ||
await expect(extractor.handle({ headers: { authorization: 'test' }} as HttpRequest)) | ||
.resolves.toEqual({ webID: '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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { AuthorizerArgs } from '../../../src/authorization/Authorizer'; | ||
import { SimpleAuthorizer } from '../../../src/authorization/SimpleAuthorizer'; | ||
import { UnsupportedHttpError } from '../../../src/util/errors/UnsupportedHttpError'; | ||
|
||
describe('A SimpleAuthorizer', (): void => { | ||
const authorizer = new SimpleAuthorizer(); | ||
|
||
it('requires input to have an identifier and permissions.', async(): Promise<void> => { | ||
await expect(authorizer.canHandle({ identifier: {}, permissions: {}} as AuthorizerArgs)).resolves.toBeUndefined(); | ||
await expect(authorizer.canHandle({ identifier: {}} as AuthorizerArgs)).rejects.toThrow(UnsupportedHttpError); | ||
await expect(authorizer.canHandle({ permissions: {}} as AuthorizerArgs)).rejects.toThrow(UnsupportedHttpError); | ||
}); | ||
|
||
it('always returns undefined.', async(): Promise<void> => { | ||
await expect(authorizer.handle()).resolves.toBeUndefined(); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
test/unit/ldp/permissions/SimplePermissionsExtractor.test.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 { Operation } from '../../../../src/ldp/operations/Operation'; | ||
import { SimplePermissionsExtractor } from '../../../../src/ldp/permissions/SimplePermissionsExtractor'; | ||
|
||
describe('A SimplePermissionsExtractor', (): void => { | ||
const extractor = new SimplePermissionsExtractor(); | ||
|
||
it('can handle all input.', async(): Promise<void> => { | ||
await expect(extractor.canHandle()).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('requires read for GET operations.', async(): Promise<void> => { | ||
await expect(extractor.handle({ method: 'GET' } as Operation)).resolves.toEqual({ | ||
read: true, | ||
append: false, | ||
write: false, | ||
delete: false, | ||
}); | ||
}); | ||
|
||
it('requires write for POST operations.', async(): Promise<void> => { | ||
await expect(extractor.handle({ method: 'POST' } as Operation)).resolves.toEqual({ | ||
read: false, | ||
append: false, | ||
write: true, | ||
delete: false, | ||
}); | ||
}); | ||
|
||
it('requires write for PUT operations.', async(): Promise<void> => { | ||
await expect(extractor.handle({ method: 'PUT' } as Operation)).resolves.toEqual({ | ||
read: false, | ||
append: false, | ||
write: true, | ||
delete: false, | ||
}); | ||
}); | ||
|
||
it('requires delete for DELETE operations.', async(): Promise<void> => { | ||
await expect(extractor.handle({ method: 'DELETE' } as Operation)).resolves.toEqual({ | ||
read: false, | ||
append: false, | ||
write: false, | ||
delete: true, | ||
}); | ||
}); | ||
}); |