From 81b83e97685d69666449b17bd97a024021a38bdd Mon Sep 17 00:00:00 2001 From: Chuck Reeves Date: Tue, 10 Dec 2024 13:08:17 -0500 Subject: [PATCH] refactor: update auth type for apis (#971) --- packages/accounts/__tests__/accounts.test.ts | 21 +++++++++++----- packages/accounts/lib/accounts.ts | 2 +- .../__tests__/__dataSets__/buyNumbers.ts | 4 ++-- .../numbers/__tests__/__dataSets__/cancel.ts | 2 +- .../__tests__/__dataSets__/getOwnedNumbers.ts | 4 ++-- .../numbers/__tests__/__dataSets__/search.ts | 22 ++++++++--------- .../numbers/__tests__/__dataSets__/update.ts | 2 +- packages/numbers/__tests__/numbers.test.ts | 7 +++--- packages/numbers/lib/numbers.ts | 6 ++++- packages/pricing/__tests__/pricing.test.ts | 24 +++++++++++++------ packages/pricing/lib/pricing.ts | 5 +++- packages/sms/__tests__/__dataSets__/sms.ts | 13 ---------- packages/sms/__tests__/sms.test.ts | 3 +++ packages/sms/lib/sms.ts | 9 ++++--- 14 files changed, 71 insertions(+), 53 deletions(-) diff --git a/packages/accounts/__tests__/accounts.test.ts b/packages/accounts/__tests__/accounts.test.ts index e37af16a..5f5ce0bb 100644 --- a/packages/accounts/__tests__/accounts.test.ts +++ b/packages/accounts/__tests__/accounts.test.ts @@ -15,10 +15,13 @@ describe('accounts', () => { 'autoReload': false, }; - nock('https://rest.nexmo.com') + nock('https://rest.nexmo.com', { + reqheaders: { + 'authorization': 'Basic YWJjZDoxMjM0', + } + }) .persist() .get('/account/get-balance') - .query({ api_key: 'abcd', api_secret: '1234' }) .reply(200, expectedResponse); const lookup = await client.getBalance(); @@ -32,10 +35,13 @@ describe('accounts', () => { 'error-code-label': 'success', }; - nock('https://rest.nexmo.com') + nock('https://rest.nexmo.com', { + reqheaders: { + 'authorization': 'Basic YWJjZDoxMjM0', + } + }) .persist() .post('/account/top-up', /trx=8ef2447e69604f642ae59363aa5f781b/) - .query({ api_key: 'abcd', api_secret: '1234' }) .reply(200, expectedResponse); const lookup = await client.topUpBalance( @@ -63,10 +69,13 @@ describe('accounts', () => { const queryString = new URLSearchParams(callbacks); const re = new RegExp(queryString.toString(), 'g'); - nock('https://rest.nexmo.com') + nock('https://rest.nexmo.com', { + reqheaders: { + 'authorization': 'Basic YWJjZDoxMjM0', + } + } ) .persist() .post('/account/settings', re) - .query({ api_key: 'abcd', api_secret: '1234' }) .reply(200, expectedResponse); const lookup = await client.updateAccountCallbacks(callbacks); diff --git a/packages/accounts/lib/accounts.ts b/packages/accounts/lib/accounts.ts index a3b5f36d..bc150b76 100644 --- a/packages/accounts/lib/accounts.ts +++ b/packages/accounts/lib/accounts.ts @@ -41,7 +41,7 @@ export class Accounts extends Client { /** * @see {@link Client.authType} */ - protected authType = AuthenticationType.QUERY_KEY_SECRET; + protected authType = AuthenticationType.BASIC; /** * Retrieves the current balance of the Vonage API account. diff --git a/packages/numbers/__tests__/__dataSets__/buyNumbers.ts b/packages/numbers/__tests__/__dataSets__/buyNumbers.ts index 90573566..644ae7cf 100644 --- a/packages/numbers/__tests__/__dataSets__/buyNumbers.ts +++ b/packages/numbers/__tests__/__dataSets__/buyNumbers.ts @@ -8,7 +8,7 @@ export default [ request: { url: BASE_URL, intercept: [ - '/number/buy?api_key=12345&api_secret=ABCDE', + '/number/buy', 'POST', new URLSearchParams([ ['country', 'US'], @@ -43,7 +43,7 @@ export default [ request: { url: BASE_URL, intercept: [ - '/number/buy?api_key=12345&api_secret=ABCDE', + '/number/buy', 'POST', new URLSearchParams([ ['country', 'US'], diff --git a/packages/numbers/__tests__/__dataSets__/cancel.ts b/packages/numbers/__tests__/__dataSets__/cancel.ts index 6927372b..6a2610b5 100644 --- a/packages/numbers/__tests__/__dataSets__/cancel.ts +++ b/packages/numbers/__tests__/__dataSets__/cancel.ts @@ -10,7 +10,7 @@ export default [ request: { url: BASE_URL, intercept: [ - '/number/cancel?api_key=12345&api_secret=ABCDE', + '/number/cancel', 'POST', new URLSearchParams([ ['country', 'US'], diff --git a/packages/numbers/__tests__/__dataSets__/getOwnedNumbers.ts b/packages/numbers/__tests__/__dataSets__/getOwnedNumbers.ts index 65c73003..c5e7ebd7 100644 --- a/packages/numbers/__tests__/__dataSets__/getOwnedNumbers.ts +++ b/packages/numbers/__tests__/__dataSets__/getOwnedNumbers.ts @@ -23,7 +23,7 @@ export default [ clientMethod: CLIENT_METHOD, request: { url: BASE_URL, - intercept: ['/account/numbers?api_key=12345&api_secret=ABCDE', 'GET'], + intercept: ['/account/numbers', 'GET'], reply: [200, validResponse], }, parameters: [{}], @@ -35,7 +35,7 @@ export default [ request: { url: BASE_URL, intercept: [ - '/account/numbers?application_id=foo-bar&has_application=false&search_pattern=searchPattern&country=US&pattern=pattern&size=42&index=1&api_key=12345&api_secret=ABCDE', + '/account/numbers?application_id=foo-bar&has_application=false&search_pattern=searchPattern&country=US&pattern=pattern&size=42&index=1', 'GET', ], reply: [200, validResponse], diff --git a/packages/numbers/__tests__/__dataSets__/search.ts b/packages/numbers/__tests__/__dataSets__/search.ts index a4e67c1f..9e657657 100644 --- a/packages/numbers/__tests__/__dataSets__/search.ts +++ b/packages/numbers/__tests__/__dataSets__/search.ts @@ -20,7 +20,7 @@ export default [ clientMethod: CLIENT_METHOD, request: { url: BASE_URL, - intercept: ['/number/search?api_key=12345&api_secret=ABCDE', 'GET'], + intercept: ['/number/search?', 'GET'], reply: [ 200, validResponse, @@ -36,7 +36,7 @@ export default [ request: { url: BASE_URL, intercept: [ - `/number/search?api_key=12345&api_secret=ABCDE&country=US&features=${Feature.MMS}`, + `/number/search?country=US&features=${Feature.MMS}`, 'GET', ], reply: [200, validResponse], @@ -55,7 +55,7 @@ export default [ request: { url: BASE_URL, intercept: [ - `/number/search?api_key=12345&api_secret=ABCDE&country=US&features=${Feature.VOICE},${Feature.MMS}`, + `/number/search?country=US&features=${Feature.VOICE},${Feature.MMS}`, 'GET', ], reply: [200, validResponse], @@ -74,7 +74,7 @@ export default [ request: { url: BASE_URL, intercept: [ - `/number/search?api_key=12345&api_secret=ABCDE&country=US&features=${Feature.SMS},${Feature.MMS},${Feature.VOICE}`, + `/number/search?country=US&features=${Feature.SMS},${Feature.MMS},${Feature.VOICE}`, 'GET', ], reply: [200, validResponse], @@ -93,7 +93,7 @@ export default [ request: { url: BASE_URL, intercept: [ - '/number/search?api_key=12345&api_secret=ABCDE&pattern=1234&search_pattern=0&country=US', + '/number/search?pattern=1234&search_pattern=0&country=US', 'GET', ], reply: [200, validResponse], @@ -112,7 +112,7 @@ export default [ request: { url: BASE_URL, intercept: [ - '/number/search?api_key=12345&api_secret=ABCDE&pattern=1234&search_pattern=2&country=US', + '/number/search?pattern=1234&search_pattern=2&country=US', 'GET', ], reply: [200, validResponse], @@ -131,7 +131,7 @@ export default [ request: { url: BASE_URL, intercept: [ - '/number/search?api_key=12345&api_secret=ABCDE&pattern=1234&search_pattern=1&country=US', + '/number/search?pattern=1234&search_pattern=1&country=US', 'GET', ], reply: [200, validResponse], @@ -150,7 +150,7 @@ export default [ request: { url: BASE_URL, intercept: [ - '/number/search?api_key=12345&api_secret=ABCDE&pattern=1234&search_pattern=1&country=US', + '/number/search?pattern=1234&search_pattern=1&country=US', 'GET', ], reply: [200, validResponse], @@ -171,7 +171,7 @@ export default [ request: { url: BASE_URL, intercept: [ - '/number/search?api_key=12345&api_secret=ABCDE&pattern=0987&search_pattern=2&country=US', + '/number/search?pattern=0987&search_pattern=2&country=US', 'GET', ], reply: [200, validResponse], @@ -191,7 +191,7 @@ export default [ request: { url: BASE_URL, intercept: [ - '/number/search?api_key=12345&api_secret=ABCDE&pattern=5309&search_pattern=0&country=US', + '/number/search?pattern=5309&search_pattern=0&country=US', 'GET', ], reply: [200, validResponse], @@ -214,7 +214,7 @@ export default [ request: { url: BASE_URL, intercept: [ - '/number/search?api_key=12345&api_secret=ABCDE&pattern=5309&search_pattern=1&country=US', + '/number/search?pattern=5309&search_pattern=1&country=US', 'GET', ], reply: [200, validResponse], diff --git a/packages/numbers/__tests__/__dataSets__/update.ts b/packages/numbers/__tests__/__dataSets__/update.ts index 5abebc68..479f5888 100644 --- a/packages/numbers/__tests__/__dataSets__/update.ts +++ b/packages/numbers/__tests__/__dataSets__/update.ts @@ -12,7 +12,7 @@ export default [ request: { url: BASE_URL, intercept: [ - '/number/update?api_key=12345&api_secret=ABCDE', + '/number/update', 'POST', new URLSearchParams([ ['app_id', '123abc'], diff --git a/packages/numbers/__tests__/numbers.test.ts b/packages/numbers/__tests__/numbers.test.ts index 4838b789..191b52eb 100644 --- a/packages/numbers/__tests__/numbers.test.ts +++ b/packages/numbers/__tests__/numbers.test.ts @@ -8,8 +8,6 @@ import { TestRequest, TestTuple, apiKeyAuth, - apiKey, - apiSecret, } from '../../../testHelpers'; const applicationsTest = dataSet.map((dataSet): TestTuple => { @@ -19,11 +17,12 @@ const applicationsTest = dataSet.map((dataSet): TestTuple => { name: label, tests: tests.map((test): SDKTestCase => { const requestUrl = new URL(`${test.request.url}${test.request.intercept[0]}`); - requestUrl.searchParams.set('api_key', apiKey); - requestUrl.searchParams.set('api_secret', apiSecret); return { label: test.label, baseUrl: 'https://rest.nexmo.com', + reqHeaders: { + 'authorization': 'Basic dGVzdEtleTp0ZXN0U2VjcmV0', + }, requests: [ [ `${requestUrl.pathname}${requestUrl.search.toString()}`, diff --git a/packages/numbers/lib/numbers.ts b/packages/numbers/lib/numbers.ts index bc302544..c9057aa9 100644 --- a/packages/numbers/lib/numbers.ts +++ b/packages/numbers/lib/numbers.ts @@ -121,7 +121,11 @@ const sortFeatures = (features: Feature[]): string => { * ``` */ export class Numbers extends Client { - protected authType = AuthenticationType.QUERY_KEY_SECRET; + /** + * @see {@link Client.authType} + */ + protected authType = AuthenticationType.BASIC; + /** * Buy a phone number. diff --git a/packages/pricing/__tests__/pricing.test.ts b/packages/pricing/__tests__/pricing.test.ts index ffe50572..88d3bb02 100644 --- a/packages/pricing/__tests__/pricing.test.ts +++ b/packages/pricing/__tests__/pricing.test.ts @@ -11,7 +11,6 @@ describe('pricing', () => { client = new Pricing(new Auth({ apiKey: 'abcd', apiSecret: '1234' })); }); - test('do a country lookup', async () => { const expectedResponse = { countryCode: 'CA', @@ -33,10 +32,14 @@ describe('pricing', () => { ], }; - nock(BASE_URL) + nock(BASE_URL, { + reqheaders: { + 'authorization': 'Basic YWJjZDoxMjM0', + } + }) .persist() .get('/account/get-pricing/outbound/sms') - .query({ api_key: 'abcd', api_secret: '1234', country: 'CA' }) + .query({ country: 'CA' }) .reply(200, expectedResponse); const lookup = await client.listCountryPricing(ServiceType.SMS, 'CA'); @@ -76,10 +79,13 @@ describe('pricing', () => { ], }; - nock(BASE_URL) + nock(BASE_URL, { + reqheaders: { + 'authorization': 'Basic YWJjZDoxMjM0', + } + }) .persist() .get('/account/get-full-pricing/outbound/sms') - .query({ api_key: 'abcd', api_secret: '1234' }) .reply(200, expectedResponse); const lookup = await client.listAllCountriesPricing(ServiceType.SMS); @@ -112,10 +118,14 @@ describe('pricing', () => { ], }; - nock(BASE_URL) + nock(BASE_URL, { + reqheaders: { + 'authorization': 'Basic YWJjZDoxMjM0', + } + }) .persist() .get('/account/get-prefix-pricing/outbound/sms') - .query({ api_key: 'abcd', api_secret: '1234', prefix: '1' }) + .query({ prefix: '1' }) .reply(200, expectedResponse); const lookup = await client.listPrefixPricing(ServiceType.SMS, '1'); diff --git a/packages/pricing/lib/pricing.ts b/packages/pricing/lib/pricing.ts index b31efc91..5a2651db 100644 --- a/packages/pricing/lib/pricing.ts +++ b/packages/pricing/lib/pricing.ts @@ -37,7 +37,10 @@ import { * ``` */ export class Pricing extends Client { - protected authType = AuthenticationType.QUERY_KEY_SECRET; + /** + * @see {@link Client.authType} + */ + protected authType = AuthenticationType.BASIC; /** * Retrieves pricing information for a specific country and service type. diff --git a/packages/sms/__tests__/__dataSets__/sms.ts b/packages/sms/__tests__/__dataSets__/sms.ts index 9f86069b..20897442 100644 --- a/packages/sms/__tests__/__dataSets__/sms.ts +++ b/packages/sms/__tests__/__dataSets__/sms.ts @@ -1,4 +1,3 @@ -import { apiKey, apiSecret} from '../../../../testHelpers'; import { MessageClassEnum, SMSStatus, @@ -20,8 +19,6 @@ export default [ '/sms/json', 'POST', { - api_key: apiKey, - api_secret: apiSecret, from: '12126875309', to: '14152739164', text: 'too many secrets', @@ -76,8 +73,6 @@ export default [ '/sms/json', 'POST', { - api_key: apiKey, - api_secret: apiSecret, from: '12126875309', to: '19162255887', text: 'I\'ll always dial the "K" for you', @@ -148,8 +143,6 @@ export default [ '/sms/json', 'POST', { - api_key: apiKey, - api_secret: apiSecret, from: '12126875309', to: '18187779311', text: 'Whats your number? ⏳', @@ -206,8 +199,6 @@ export default [ '/sms/json', 'POST', { - api_key: apiKey, - api_secret: apiSecret, from: '12126875309', to: '18186345789', type: TypeEnum.BINARY, @@ -268,8 +259,6 @@ export default [ '/sms/json', 'POST', { - api_key: apiKey, - api_secret: apiSecret, from: '12126875309', to: '14152739164', text: 'too many secrets', @@ -314,8 +303,6 @@ export default [ '/sms/json', 'POST', { - api_key: apiKey, - api_secret: apiSecret, from: '12126875309', to: '14152739164', text: 'too many secrets', diff --git a/packages/sms/__tests__/sms.test.ts b/packages/sms/__tests__/sms.test.ts index 156f1cb6..cd673bde 100644 --- a/packages/sms/__tests__/sms.test.ts +++ b/packages/sms/__tests__/sms.test.ts @@ -19,6 +19,9 @@ const applicationsTest = testDataSets.map((dataSet): TestTuple => { return { label: test.label, baseUrl: 'https://rest.nexmo.com', + reqHeaders: { + 'authorization': 'Basic dGVzdEtleTp0ZXN0U2VjcmV0', + }, requests: [test.request] as TestRequest[], responses: [test.response] as TestResponse[], client: new SMS(apiKeyAuth), diff --git a/packages/sms/lib/sms.ts b/packages/sms/lib/sms.ts index 1aaef5c3..bb3c6895 100644 --- a/packages/sms/lib/sms.ts +++ b/packages/sms/lib/sms.ts @@ -45,7 +45,10 @@ import { AlgorithmTypes } from '@vonage/auth'; * ``` */ export class SMS extends Client { - protected authType?: AuthenticationType = AuthenticationType.KEY_SECRET; + /** + * @see {@link Client.authType} + */ + protected authType?: AuthenticationType = AuthenticationType.BASIC; /** * Sends an SMS message using the legacy Vonage SMS API. @@ -62,7 +65,7 @@ export class SMS extends Client { * from: FROM_NUMBER, * text: 'Hello from Vonage SMS API', * }); - * console.log(`Number of messages sent: ${response.messageCount}`);); + * console.log(`Number of messages sent: ${response.messageCount}`);); * ``` */ public async send(params?: SMSParams): Promise { @@ -117,7 +120,7 @@ export class SMS extends Client { * sms.verifySignature( * sig, * {}, // request parameters - * VONAGE_API_SIGNATURE_SECRET, + * VONAGE_API_SIGNATURE_SECRET, * AlgorithmTypes.md5hash, * ) === params.sig) { * console.log("Valid signature");