From e486f80a4e7a1f65498335e7408f3301c9e7cb44 Mon Sep 17 00:00:00 2001 From: Justin Wind Date: Thu, 1 Jun 2023 11:58:46 -0700 Subject: [PATCH] rename redeemProfileCode to redeemCode, with deprecated alias --- README.md | 4 ++-- lib/communication.js | 39 +++++++++++++++++++++++++++------------ test/lib/communication.js | 20 ++++++++++++++++---- 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index fe3e362..47c8cc6 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,8 @@ Notable methods on the Communication class: - `async fetchJSON(urlObject)` Retrieve json from an endpoint. -- `async redeemProfileCode(urlObj, code, codeVerifier, clientId, redirectURI)` - Submit a code to get a profile response. +- `async redeemCode(urlObj, code, codeVerifier, clientId, redirectURI)` + Submit a code to get a profile or ticket response. - `async introspectToken(introspectionUrlObj, authenticationHeader, token)` Submit a token for introspection. diff --git a/lib/communication.js b/lib/communication.js index 82400c3..2a69e34 100644 --- a/lib/communication.js +++ b/lib/communication.js @@ -749,8 +749,8 @@ class Communication { /** - * POST to the auth endpoint, to redeem a code for a profile object. - * FIXME: [name] this isn't specific to profile redemption, it works for tokens too + * POST to the auth endpoint, to redeem a code for a profile or token. + * N.B. this absorbs any errors! * @param {URL} urlObj * @param {String} code * @param {String} codeVerifier @@ -758,8 +758,8 @@ class Communication { * @param {String} redirectURI * @returns {Object} */ - async redeemProfileCode(urlObj, code, codeVerifier, clientId, redirectURI) { - const _scope = _fileScope('redeemProfileCode'); + async redeemCode(urlObj, code, codeVerifier, clientId, redirectURI) { + const _scope = _fileScope('redeemCode'); const formData = common.formData({ 'grant_type': 'authorization_code', @@ -769,13 +769,13 @@ class Communication { 'code_verifier': codeVerifier, }); - const postRedeemProfileCodeConfig = Communication._axiosConfig('POST', urlObj, formData, {}, { + const postRedeemCodeConfig = Communication._axiosConfig('POST', urlObj, formData, {}, { [Enum.Header.ContentType]: Enum.ContentType.ApplicationForm, [Enum.Header.Accept]: `${Enum.ContentType.ApplicationJson}, ${Enum.ContentType.Any};q=0.1`, }); try { - const response = await this.axios(postRedeemProfileCodeConfig); + const response = await this.axios(postRedeemCodeConfig); try { return JSON.parse(response.data); } catch (e) { @@ -783,14 +783,29 @@ class Communication { throw e; } } catch (e) { - this.logger.error(_scope, 'redeem profile code request failed', { error: e, url: urlObj.href }); + this.logger.error(_scope, 'redeem code request failed', { error: e, url: urlObj.href }); return; } } /** - * Verify a token with an IdP endpoint, using the Authentication header supplied. + * Deprecated method name. + * @see redeemCode + * @param {URL} urlObj + * @param {String} code + * @param {Strin} codeVerifier + * @param {String} clientId + * @param {String} redirectURI + * @returns {Object} + */ + async redeemProfileCode(urlObj, code, codeVerifier, clientId, redirectURI) { + return await this.redeemCode(urlObj, code, codeVerifier, clientId, redirectURI); + } + + + /** + * Verify a token with an IdP endpoint, using the Authorization header supplied. * @param {URL} introspectionUrlObj * @param {String} authorizationHeader * @param {String} token @@ -844,10 +859,10 @@ class Communication { /** * Attempt to deliver a ticket to an endpoint. * N.B. does not absorb errors - * @param {*} ticketEndpointUrlObj - * @param {*} resourceUrlObj - * @param {*} subjectUrlObj - * @param {*} ticket + * @param {URL} ticketEndpointUrlObj + * @param {URL} resourceUrlObj + * @param {URL} subjectUrlObj + * @param {String} ticket * @returns {Promise} */ async deliverTicket(ticketEndpointUrlObj, resourceUrlObj, subjectUrlObj, ticket) { diff --git a/test/lib/communication.js b/test/lib/communication.js index c79d261..a94bf90 100644 --- a/test/lib/communication.js +++ b/test/lib/communication.js @@ -870,7 +870,7 @@ describe('Communication', function () { }); }); // fetchProfile - describe('redeemProfileCode', function () { + describe('redeemCode', function () { let expected, urlObj, code, codeVerifier, clientId, redirectURI; beforeEach(function () { urlObj = new URL('https://example.com/auth'); @@ -887,6 +887,18 @@ describe('Communication', function () { me: 'https://profile.example.com/', }; + const result = await communication.redeemCode(urlObj, code, codeVerifier, clientId, redirectURI); + + assert.deepStrictEqual(result, expected); + }); + it('covers deprecated method name', async function () { + communication.axios.resolves({ + data: '{"me":"https://profile.example.com/"}', + }); + expected = { + me: 'https://profile.example.com/', + }; + const result = await communication.redeemProfileCode(urlObj, code, codeVerifier, clientId, redirectURI); assert.deepStrictEqual(result, expected); @@ -894,11 +906,11 @@ describe('Communication', function () { it('covers failure', async function () { communication.axios.resolves('Not a JSON payload.'); - const result = await communication.redeemProfileCode(urlObj, code, codeVerifier, clientId, redirectURI); + const result = await communication.redeemCode(urlObj, code, codeVerifier, clientId, redirectURI); assert.strictEqual(result, undefined); }); - }); // redeemProfileCode + }); // redeemCode describe('introspectToken', function () { let introspectionUrlObj, authenticationHeader, token; @@ -958,4 +970,4 @@ describe('Communication', function () { }); }); // deliverTicket -}); // Communication \ No newline at end of file +}); // Communication -- 2.43.2