deno.land / x / oauth4webapi@v1.2.2 / test / par.test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226import anyTest, { type TestFn } from 'ava'import setup, { type Context, teardown, issuer, endpoint, client, getResponse, UA,} from './_setup.js'import * as lib from '../src/index.js'
const test = anyTest as TestFn<Context & { es256: CryptoKeyPair; rs256: CryptoKeyPair }>
test.before(setup)test.after(teardown)
const tClient: lib.Client = { ...client, client_secret: 'foo' }
test('pushedAuthorizationRequest()', async (t) => { await t.throwsAsync(lib.pushedAuthorizationRequest(issuer, tClient, new URLSearchParams()), { message: '"as.pushed_authorization_request_endpoint" must be a string', })
await t.throwsAsync(lib.pushedAuthorizationRequest(issuer, tClient, <any>null), { message: '"parameters" must be an instance of URLSearchParams', })
const tIssuer: lib.AuthorizationServer = { ...issuer, pushed_authorization_request_endpoint: endpoint('par-1'), }
t.context .intercept({ path: '/par-1', method: 'POST', headers: { accept: 'application/json', 'user-agent': UA, }, body(body) { return new URLSearchParams(body).get('client_id') === client.client_id }, }) .reply(200, { request_uri: 'urn:example:uri', expires_in: 60 })
await t.notThrowsAsync(lib.pushedAuthorizationRequest(tIssuer, tClient, new URLSearchParams()))})
test('pushedAuthorizationRequest() w/ Custom Headers', async (t) => { const tIssuer: lib.AuthorizationServer = { ...issuer, pushed_authorization_request_endpoint: endpoint('par-headers'), }
t.context .intercept({ path: '/par-headers', method: 'POST', headers: { accept: 'application/json', 'user-agent': 'foo', foo: 'bar', }, }) .reply(200, { request_uri: 'urn:example:uri', expires_in: 60 })
await t.notThrowsAsync( lib.pushedAuthorizationRequest(tIssuer, tClient, new URLSearchParams(), { headers: new Headers([ ['accept', 'will be overwritten'], ['user-agent', 'foo'], ['foo', 'bar'], ]), }), )})
test('pushedAuthorizationRequest() w/ DPoP', async (t) => { const tIssuer: lib.AuthorizationServer = { ...issuer, pushed_authorization_request_endpoint: endpoint('par-2'), }
t.context .intercept({ path: '/par-2', method: 'POST', headers: { accept: 'application/json', dpop: /.+/, }, body(body) { return new URLSearchParams(body).has('dpop_jkt') }, }) .reply(200, { request_uri: 'urn:example:uri', expires_in: 60 })
const DPoP = await lib.generateKeyPair('ES256') await t.notThrowsAsync( lib.pushedAuthorizationRequest(tIssuer, tClient, new URLSearchParams(), { DPoP }), )})
test('pushedAuthorizationRequest() w/ Request Object', async (t) => { const tIssuer: lib.AuthorizationServer = { ...issuer, pushed_authorization_request_endpoint: endpoint('par-3'), }
t.context .intercept({ path: '/par-3', method: 'POST', headers: { accept: 'application/json', }, body(body) { const params = new URLSearchParams(body) return params.has('client_id') && params.has('request') }, }) .reply(200, { request_uri: 'urn:example:uri', expires_in: 60 })
const sign = await lib.generateKeyPair('ES256') await t.notThrowsAsync( lib.pushedAuthorizationRequest( tIssuer, tClient, new URLSearchParams({ request: await lib.issueRequestObject(tIssuer, tClient, new URLSearchParams(), { key: sign.privateKey, }), }), ), )})
test('processPushedAuthorizationResponse()', async (t) => { await t.throwsAsync(lib.processPushedAuthorizationResponse(issuer, client, <any>null), { message: '"response" must be an instance of Response', }) await t.throwsAsync( lib.processPushedAuthorizationResponse(issuer, client, getResponse('', { status: 404 })), { message: '"response" is not a conform Pushed Authorization Request Endpoint response', }, ) await t.throwsAsync( lib.processPushedAuthorizationResponse(issuer, client, getResponse('{"', { status: 201 })), { message: 'failed to parse "response" body as JSON', }, ) await t.throwsAsync( lib.processPushedAuthorizationResponse(issuer, client, getResponse('null', { status: 201 })), { message: '"response" body must be a top level object', }, ) await t.throwsAsync( lib.processPushedAuthorizationResponse(issuer, client, getResponse('[]', { status: 201 })), { message: '"response" body must be a top level object', }, )
await t.throwsAsync( lib.processPushedAuthorizationResponse( issuer, client, getResponse(JSON.stringify({ request_uri: null, expires_in: 60 }), { status: 201 }), ), { message: '"response" body "request_uri" property must be a non-empty string', }, )
await t.throwsAsync( lib.processPushedAuthorizationResponse( issuer, client, getResponse(JSON.stringify({ request_uri: 'urn:example:uri', expires_in: null }), { status: 201, }), ), { message: '"response" body "expires_in" property must be a positive number', }, )
t.deepEqual( await lib.processPushedAuthorizationResponse( issuer, client, getResponse(JSON.stringify({ request_uri: 'urn:example:uri', expires_in: 60 }), { status: 201, }), ), { request_uri: 'urn:example:uri', expires_in: 60 }, )
t.true( lib.isOAuth2Error( await lib.processPushedAuthorizationResponse( issuer, client, getResponse(JSON.stringify({ error: 'invalid_client' }), { status: 401 }), ), ), )
t.false( lib.isOAuth2Error( await lib.processPushedAuthorizationResponse( issuer, client, getResponse(JSON.stringify({ request_uri: 'urn:example:uri', expires_in: 60 }), { status: 201, }), ), ), )})
Version Info