deno.land / x / oauth4webapi@v1.2.2 / test / client_credentials.test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214import 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>
test.before(setup)test.after(teardown)
const tClient: lib.Client = { ...client, client_secret: 'foo' }
test('clientCredentialsGrantRequest()', async (t) => { await t.throwsAsync(lib.clientCredentialsGrantRequest(issuer, tClient, new URLSearchParams()), { message: '"as.token_endpoint" must be a string', })
const tIssuer: lib.AuthorizationServer = { ...issuer, token_endpoint: endpoint('token-1'), }
t.context .intercept({ path: '/token-1', method: 'POST', headers: { accept: 'application/json', 'user-agent': UA, }, body(body) { const params = new URLSearchParams(body) return params.get('grant_type') === 'client_credentials' }, }) .reply(200, { access_token: 'token', token_type: 'Bearer' })
await t.notThrowsAsync(lib.clientCredentialsGrantRequest(tIssuer, tClient, new URLSearchParams()))})
test('clientCredentialsGrantRequest() w/ Extra Parameters', async (t) => { const tIssuer: lib.AuthorizationServer = { ...issuer, token_endpoint: endpoint('token-2'), }
t.context .intercept({ path: '/token-2', method: 'POST', body(body) { const params = new URLSearchParams(body) return params.get('resource') === 'urn:example:resource' }, }) .reply(200, { access_token: 'token', token_type: 'Bearer' })
await t.notThrowsAsync( lib.clientCredentialsGrantRequest( tIssuer, tClient, new URLSearchParams('resource=urn:example:resource'), ), )})
test('clientCredentialsGrantRequest() w/ Custom Headers', async (t) => { const tIssuer: lib.AuthorizationServer = { ...issuer, token_endpoint: endpoint('token-headers'), }
t.context .intercept({ path: '/token-headers', method: 'POST', headers: { accept: 'application/json', 'user-agent': 'foo', foo: 'bar', }, }) .reply(200, { access_token: 'token', token_type: 'Bearer' })
await t.notThrowsAsync( lib.clientCredentialsGrantRequest(tIssuer, tClient, new URLSearchParams(), { headers: new Headers([ ['accept', 'will be overwritten'], ['user-agent', 'foo'], ['foo', 'bar'], ]), }), )})
test('clientCredentialsGrantRequest() w/ DPoP', async (t) => { const tIssuer: lib.AuthorizationServer = { ...issuer, token_endpoint: endpoint('token-3'), }
t.context .intercept({ path: '/token-3', method: 'POST', headers: { dpop: /.+/, }, }) .reply(200, { access_token: 'token', token_type: 'DPoP' })
const DPoP = await lib.generateKeyPair('ES256') await t.notThrowsAsync( lib.clientCredentialsGrantRequest(tIssuer, tClient, new URLSearchParams(), { DPoP }), )})
test('processClientCredentialsResponse()', async (t) => { await t.throwsAsync(lib.processClientCredentialsResponse(issuer, client, <any>null), { message: '"response" must be an instance of Response', }) await t.throwsAsync( lib.processClientCredentialsResponse(issuer, client, getResponse('', { status: 404 })), { message: '"response" is not a conform Token Endpoint response', }, ) await t.throwsAsync(lib.processClientCredentialsResponse(issuer, client, getResponse('{"')), { message: 'failed to parse "response" body as JSON', }) await t.throwsAsync(lib.processClientCredentialsResponse(issuer, client, getResponse('null')), { message: '"response" body must be a top level object', }) await t.throwsAsync(lib.processClientCredentialsResponse(issuer, client, getResponse('[]')), { message: '"response" body must be a top level object', }) await t.throwsAsync( lib.processClientCredentialsResponse( issuer, client, getResponse(JSON.stringify({ token_type: 'Bearer' })), ), { message: '"response" body "access_token" property must be a non-empty string', }, ) await t.throwsAsync( lib.processClientCredentialsResponse( issuer, client, getResponse(JSON.stringify({ access_token: 'token' })), ), { message: '"response" body "token_type" property must be a non-empty string', }, ) await t.throwsAsync( lib.processClientCredentialsResponse( issuer, client, getResponse( JSON.stringify({ access_token: 'token', token_type: 'Bearer', expires_in: new Date().toUTCString(), }), ), ), { message: '"response" body "expires_in" property must be a positive number', }, )
t.deepEqual( await lib.processClientCredentialsResponse( issuer, client, getResponse(JSON.stringify({ access_token: 'token', token_type: 'Bearer', expires_in: 60 })), ), { access_token: 'token', token_type: 'bearer', expires_in: 60, }, )
t.true( lib.isOAuth2Error( await lib.processClientCredentialsResponse( issuer, client, getResponse(JSON.stringify({ error: 'invalid_grant' }), { status: 400 }), ), ), )
t.false( lib.isOAuth2Error( await lib.processClientCredentialsResponse( issuer, client, getResponse(JSON.stringify({ access_token: 'token', token_type: 'Bearer' })), ), ), )})
Version Info