diff --git a/package.json b/package.json index 4a94c80..cd25015 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "@types/node-fetch": "^2.5.4", "@types/qs": "^6.9.5", "@types/url-safe-base64": "^1.1.0", + "@types/uuid": "^8.3.0", "@typescript-eslint/eslint-plugin": "^4.5.0", "@typescript-eslint/parser": "^4.5.0", "conventional-changelog-cli": "^2.1.0", diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 8950c26..170bd66 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -28,7 +28,6 @@ describe('index', () => { const options = instance.storedOptions; const storedOptions = { clientId: options.clientId, - codeVerifier: options.codeVerifier, mode: options.mode, redirectUri: options.redirectUri, state: options.state, @@ -81,12 +80,10 @@ describe('index', () => { mtLinkSdk.init('clientId', { mode: 'local', state: 'state', - codeVerifier: 'codeVerifier', }); expect(mtLinkSdk.storedOptions.mode).toBe('local'); expect(mtLinkSdk.storedOptions.state).toBe('state'); - expect(mtLinkSdk.storedOptions.codeVerifier).toBe('codeVerifier'); }); test('invalid mode default to production', () => { diff --git a/src/api/__tests__/authorize.test.ts b/src/api/__tests__/authorize.test.ts index 44c2a39..39d3867 100644 --- a/src/api/__tests__/authorize.test.ts +++ b/src/api/__tests__/authorize.test.ts @@ -39,7 +39,6 @@ describe('api', () => { mockedStorage.set.mockClear(); open.mockClear(); - const codeVerifier = 'codeVerifier'; const country = 'JP'; const scopes = 'points_read'; const cobrandClientId = 'cobrandClientId'; @@ -48,7 +47,6 @@ describe('api', () => { const mtLinkSdk = new MtLinkSdk(); mtLinkSdk.init(clientId, { redirectUri, - codeVerifier, country, scopes, locale, @@ -65,24 +63,18 @@ describe('api', () => { response_type: 'code', scope: scopes, redirect_uri: redirectUri, - code_challenge: 'N1E4yRMD7xixn_oFyO_W3htYN3rY7-HMDKJe6z6r928', - code_challenge_method: 'S256', country, locale, configs: generateConfigs(), }); const url = `${MY_ACCOUNT_DOMAINS.production}/oauth/authorize?${query}`; expect(open).toBeCalledWith(url, '_self'); - - expect(mockedStorage.set).toBeCalledTimes(1); - expect(mockedStorage.set).toBeCalledWith('codeVerifier', codeVerifier); }); test('with options', () => { mockedStorage.set.mockClear(); open.mockClear(); - const codeVerifier = 'codeVerifier'; const state = 'state'; const country = 'JP'; const scopes = 'points_read'; @@ -92,7 +84,6 @@ describe('api', () => { authorize(mtLinkSdk.storedOptions, { state, - codeVerifier, redirectUri, country, scopes, @@ -105,17 +96,12 @@ describe('api', () => { response_type: 'code', scope: scopes, redirect_uri: redirectUri, - code_challenge: 'N1E4yRMD7xixn_oFyO_W3htYN3rY7-HMDKJe6z6r928', - code_challenge_method: 'S256', state, country, configs: generateConfigs(), }); const url = `${MY_ACCOUNT_DOMAINS.production}/oauth/authorize?${query}`; expect(open).toBeCalledWith(url, '_self'); - - expect(mockedStorage.set).toBeCalledTimes(2); - expect(mockedStorage.set).toBeCalledWith('codeVerifier', codeVerifier); }); test('without window', () => { diff --git a/src/api/__tests__/onboard.test.ts b/src/api/__tests__/onboard.test.ts index e46efe9..24e471b 100644 --- a/src/api/__tests__/onboard.test.ts +++ b/src/api/__tests__/onboard.test.ts @@ -67,7 +67,6 @@ describe('api', () => { mockedStorage.set.mockClear(); open.mockClear(); - const codeVerifier = 'codeVerifier'; const country = 'JP'; const scopes = 'points_read'; const cobrandClientId = 'cobrandClientId'; @@ -76,7 +75,6 @@ describe('api', () => { const mtLinkSdk = new MtLinkSdk(); mtLinkSdk.init(clientId, { redirectUri, - codeVerifier, country, scopes, email, @@ -94,24 +92,18 @@ describe('api', () => { response_type: 'code', scope: scopes, redirect_uri: redirectUri, - code_challenge: 'N1E4yRMD7xixn_oFyO_W3htYN3rY7-HMDKJe6z6r928', - code_challenge_method: 'S256', country, locale, configs: generateConfigs({ email }), }); const url = `${MY_ACCOUNT_DOMAINS.production}/onboard?${query}`; expect(open).toBeCalledWith(url, '_self'); - - expect(mockedStorage.set).toBeCalledTimes(1); - expect(mockedStorage.set).toBeCalledWith('codeVerifier', codeVerifier); }); test('with options', () => { mockedStorage.set.mockClear(); open.mockClear(); - const codeVerifier = 'codeVerifier'; const state = 'state'; const country = 'JP'; const scopes = 'points_read'; @@ -121,7 +113,6 @@ describe('api', () => { onboard(mtLinkSdk.storedOptions, { state, - codeVerifier, redirectUri, country, scopes, @@ -135,17 +126,12 @@ describe('api', () => { response_type: 'code', scope: scopes, redirect_uri: redirectUri, - code_challenge: 'N1E4yRMD7xixn_oFyO_W3htYN3rY7-HMDKJe6z6r928', - code_challenge_method: 'S256', state, country, configs: generateConfigs({ email }), }); const url = `${MY_ACCOUNT_DOMAINS.production}/onboard?${query}`; expect(open).toBeCalledWith(url, '_self'); - - expect(mockedStorage.set).toBeCalledTimes(2); - expect(mockedStorage.set).toBeCalledWith('codeVerifier', codeVerifier); }); test('without window', () => { diff --git a/src/api/__tests__/token-info.test.ts b/src/api/__tests__/token-info.test.ts index becd165..35b53a8 100644 --- a/src/api/__tests__/token-info.test.ts +++ b/src/api/__tests__/token-info.test.ts @@ -1,7 +1,9 @@ import fetch from 'jest-fetch-mock'; +import qs from 'qs'; import { MY_ACCOUNT_DOMAINS } from '../../server-paths'; import { MtLinkSdk } from '../..'; +import { generateConfigs } from '../../helper'; import tokenInfo from '../token-info'; describe('api', () => { @@ -39,15 +41,19 @@ describe('api', () => { fetch.mockResponseOnce(JSON.stringify(response)); await tokenInfo(mtLinkSdk.storedOptions, token); + const query = qs.stringify({ + client_id: clientId, + configs: generateConfigs(), + }); - const url = `${MY_ACCOUNT_DOMAINS.production}/oauth/token/info.json`; + const url = `${MY_ACCOUNT_DOMAINS.production}/oauth/token/info.json?${query}`; expect(fetch).toBeCalledTimes(1); expect(fetch).toBeCalledWith(url, { method: 'GET', headers: { Authorization: `Bearer ${token}`, - 'x-api-key': clientId, + 'API-Version': '1604911588', }, }); }); diff --git a/src/api/authorize.ts b/src/api/authorize.ts index 6589d45..361506c 100644 --- a/src/api/authorize.ts +++ b/src/api/authorize.ts @@ -11,7 +11,10 @@ import { MY_ACCOUNT_DOMAINS } from '../server-paths'; import { StoredOptions, AuthorizeOptions } from '../typings'; import storage from '../storage'; -export default function authorize(storedOptions: StoredOptions, options: AuthorizeOptions = {}): void { +export default function authorize( + storedOptions: StoredOptions, + options: AuthorizeOptions = {} +): void { if (!window) { throw new Error('[mt-link-sdk] `authorize` only works in the browser.'); } diff --git a/src/api/exchange-token.ts b/src/api/exchange-token.ts index fd19730..218defa 100644 --- a/src/api/exchange-token.ts +++ b/src/api/exchange-token.ts @@ -21,21 +21,13 @@ export default async function exchangeToken( storedOptions: StoredOptions, options: ExchangeTokenOptions = {} ): Promise { - const { - clientId, - redirectUri: defaultRedirectUri, - mode - } = storedOptions; + const { clientId, redirectUri: defaultRedirectUri, mode } = storedOptions; if (!clientId) { throw new Error('[mt-link-sdk] Make sure to call `init` before calling `exchangeToken`.'); } - const { - redirectUri = defaultRedirectUri, - code = getCode(), - codeVerifier, - } = options; + const { redirectUri = defaultRedirectUri, code = getCode(), codeVerifier } = options; if (!code) { throw new Error( diff --git a/src/api/token-info.ts b/src/api/token-info.ts index 0410fb6..f0a1675 100644 --- a/src/api/token-info.ts +++ b/src/api/token-info.ts @@ -15,17 +15,20 @@ export default async function tokenInfo( const queryString = stringify({ client_id: clientId, - configs: generateConfigs() - }) + configs: generateConfigs(), + }); try { - const response = await fetch(`${MY_ACCOUNT_DOMAINS[mode]}/oauth/token/info.json?${queryString}`, { - method: 'GET', - headers: { - Authorization: `Bearer ${token}`, - 'API-Version': '1604911588', - }, - }); + const response = await fetch( + `${MY_ACCOUNT_DOMAINS[mode]}/oauth/token/info.json?${queryString}`, + { + method: 'GET', + headers: { + Authorization: `Bearer ${token}`, + 'API-Version': '1604911588', + }, + } + ); const result = await response.json(); diff --git a/yarn.lock b/yarn.lock index 349d315..1e096bc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -858,6 +858,11 @@ resolved "https://registry.yarnpkg.com/@types/url-safe-base64/-/url-safe-base64-1.1.0.tgz#90d35b9432164896a9d5b3d43bb88fb2ef86f2e2" integrity sha512-cVZIEorjpbV8NgdodMXyLItd9q0wT4k075a2F5Ew5L/zwB80rLIGhb7Owo17le0hhnZcJXizxc7hHdTs1or51A== +"@types/uuid@^8.3.0": + version "8.3.0" + resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.0.tgz#215c231dff736d5ba92410e6d602050cce7e273f" + integrity sha512-eQ9qFW/fhfGJF8WKHGEHZEyVWfZxrT+6CLIJGBcZPfxUh/+BnEj+UCGYMlr9qZuX/2AltsvwrGqp0LhEW8D0zQ== + "@types/yargs-parser@*": version "15.0.0" resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-15.0.0.tgz#cb3f9f741869e20cce330ffbeb9271590483882d"