diff --git a/package.json b/package.json index 6338180..ad8cf65 100644 --- a/package.json +++ b/package.json @@ -56,10 +56,8 @@ "typescript": "~3.8.3" }, "dependencies": { - "@types/atob": "^2.1.2", - "atob": "^2.1.2", "ethereum-cryptography": "^1.0.1", - "node-fetch": "^2.6.0" + "node-fetch": "^2.6.7" }, "husky": { "hooks": { diff --git a/src/core/sdk.ts b/src/core/sdk.ts index 4e51aea..c7a54f4 100644 --- a/src/core/sdk.ts +++ b/src/core/sdk.ts @@ -1,4 +1,3 @@ -import '../utils/shim'; import { TokenModule } from '../modules/token'; import { UsersModule } from '../modules/users'; import { UtilsModule } from '../modules/utils'; diff --git a/src/utils/codec.ts b/src/utils/codec.ts new file mode 100644 index 0000000..396707b --- /dev/null +++ b/src/utils/codec.ts @@ -0,0 +1,25 @@ +/* + * Check if global object window is defined + */ +export function isBrowser() { + return typeof window !== 'undefined'; +} + +/* + * Decode base64 value, returns string + * @Params: string + */ +export function decodeValue(value: string): string { + if (!value) { + return ''; + } + + const valueToString = value.toString(); + + if (isBrowser()) { + return atob(valueToString); + } + + const buff = Buffer.from(valueToString, 'base64'); + return buff.toString('ascii'); +} diff --git a/src/utils/parse-didt.ts b/src/utils/parse-didt.ts index 33b57ea..434e0c0 100644 --- a/src/utils/parse-didt.ts +++ b/src/utils/parse-didt.ts @@ -1,6 +1,7 @@ import { Claim, ParsedDIDToken } from '../types'; import { isDIDTClaim } from './type-guards'; import { createMalformedTokenError } from '../core/sdk-exceptions'; +import { decodeValue } from './codec'; interface ParseDIDTokenResult { raw: [string, string]; @@ -10,9 +11,9 @@ interface ParseDIDTokenResult { /** * Parses a DID Token so that the encoded `claim` is in object form. */ -export function parseDIDToken(DIDToken: string): ParseDIDTokenResult { +export function parseDIDToken(didToken: string): ParseDIDTokenResult { try { - const [proof, claim] = JSON.parse(globalThis.atob(DIDToken)) as [string, string]; + const [proof, claim] = JSON.parse(decodeValue(didToken)) as [string, string]; const parsedClaim = JSON.parse(claim) as Claim; if (isDIDTClaim(parsedClaim)) return { raw: [proof, claim], withParsedClaim: [proof, parsedClaim] }; throw new Error(); diff --git a/src/utils/shim.ts b/src/utils/shim.ts deleted file mode 100644 index 75762d0..0000000 --- a/src/utils/shim.ts +++ /dev/null @@ -1,4 +0,0 @@ -import atob from 'atob'; - -// Shims for atob being undefined in node.js prior version 14 -if (!globalThis.atob) globalThis.atob = atob; diff --git a/test/lib/constants.ts b/test/lib/constants.ts index b55444e..3448908 100644 --- a/test/lib/constants.ts +++ b/test/lib/constants.ts @@ -4,6 +4,7 @@ export const API_KEY = 'sk_test_123'; export const VALID_DIDT = 'WyIweGUwMjQzNTVlNDI5ZGNhZDM1MTdhZDk5ZWEzNDEwYWJmZDQ1YjBiNjM5OGIwNjY1NGRiYTQxNzljODdlMTYyNzgxNTc1YjA5ODFjNjU4ZjcwMjYwZTQ5MjMwZGE5NDg4YTA0ZDk5NzBlYjM4ZTZmZGRlY2Q2NTA5YTAyN2IwOGI5MWIiLCJ7XCJpYXRcIjoxNTg1MDExMjA0LFwiZXh0XCI6MTkwMDQxMTIwNCxcImlzc1wiOlwiZGlkOmV0aHI6MHhCMmVjOWI2MTY5OTc2MjQ5MWI2NTQyMjc4RTlkRkVDOTA1MGY4MDg5XCIsXCJzdWJcIjpcIjZ0RlhUZlJ4eWt3TUtPT2pTTWJkUHJFTXJwVWwzbTNqOERReWNGcU8ydHc9XCIsXCJhdWRcIjpcImRpZDptYWdpYzpmNTQxNjhlOS05Y2U5LTQ3ZjItODFjOC03Y2IyYTk2YjI2YmFcIixcIm5iZlwiOjE1ODUwMTEyMDQsXCJ0aWRcIjpcIjJkZGY1OTgzLTk4M2ItNDg3ZC1iNDY0LWJjNWUyODNhMDNjNVwiLFwiYWRkXCI6XCIweDkxZmJlNzRiZTZjNmJmZDhkZGRkZDkzMDExYjA1OWI5MjUzZjEwNzg1NjQ5NzM4YmEyMTdlNTFlMGUzZGYxMzgxZDIwZjUyMWEzNjQxZjIzZWI5OWNjYjM0ZTNiYzVkOTYzMzJmZGViYzhlZmE1MGNkYjQxNWU0NTUwMDk1MmNkMWNcIn0iXQ=='; +export const DECODED_DIDT = `["0xe024355e429dcad3517ad99ea3410abfd45b0b6398b06654dba4179c87e162781575b0981c658f70260e49230da9488a04d9970eb38e6fddecd6509a027b08b91b","{\\"iat\\":1585011204,\\"ext\\":1900411204,\\"iss\\":\\"did:ethr:0xB2ec9b61699762491b6542278E9dFEC9050f8089\\",\\"sub\\":\\"6tFXTfRxykwMKOOjSMbdPrEMrpUl3m3j8DQycFqO2tw=\\",\\"aud\\":\\"did:magic:f54168e9-9ce9-47f2-81c8-7cb2a96b26ba\\",\\"nbf\\":1585011204,\\"tid\\":\\"2ddf5983-983b-487d-b464-bc5e283a03c5\\",\\"add\\":\\"0x91fbe74be6c6bfd8ddddd93011b059b9253f10785649738ba217e51e0e3df1381d20f521a3641f23eb99ccb34e3bc5d96332fdebc8efa50cdb415e45500952cd1c\\"}"]`; export const VALID_DIDT_WITH_INVALID_RECOVERY_BIT = 'WyIweGUwMjQzNTVlNDI5ZGNhZDM1MTdhZDk5ZWEzNDEwYWJmZDQ1YjBiNjM5OGIwNjY1NGRiYTQxNzljODdlMTYyNzgxNTc1YjA5ODFjNjU4ZjcwMjYwZTQ5MjMwZGE5NDg4YTA0ZDk5NzBlYjM4ZTZmZGRlY2Q2NTA5YTAyN2IwOGI5MjMiLCJ7XCJpYXRcIjoxNTg1MDExMjA0LFwiZXh0XCI6MTkwMDQxMTIwNCxcImlzc1wiOlwiZGlkOmV0aHI6MHhCMmVjOWI2MTY5OTc2MjQ5MWI2NTQyMjc4RTlkRkVDOTA1MGY4MDg5XCIsXCJzdWJcIjpcIjZ0RlhUZlJ4eWt3TUtPT2pTTWJkUHJFTXJwVWwzbTNqOERReWNGcU8ydHc9XCIsXCJhdWRcIjpcImRpZDptYWdpYzpmNTQxNjhlOS05Y2U5LTQ3ZjItODFjOC03Y2IyYTk2YjI2YmFcIixcIm5iZlwiOjE1ODUwMTEyMDQsXCJ0aWRcIjpcIjJkZGY1OTgzLTk4M2ItNDg3ZC1iNDY0LWJjNWUyODNhMDNjNVwiLFwiYWRkXCI6XCIweDkxZmJlNzRiZTZjNmJmZDhkZGRkZDkzMDExYjA1OWI5MjUzZjEwNzg1NjQ5NzM4YmEyMTdlNTFlMGUzZGYxMzgxZDIwZjUyMWEzNjQxZjIzZWI5OWNjYjM0ZTNiYzVkOTYzMzJmZGViYzhlZmE1MGNkYjQxNWU0NTUwMDk1MmNkMWNcIn0iXQ=='; diff --git a/test/spec/utils/codec.spec.ts b/test/spec/utils/codec.spec.ts new file mode 100644 index 0000000..8717d62 --- /dev/null +++ b/test/spec/utils/codec.spec.ts @@ -0,0 +1,26 @@ +import { decodeValue } from '../../../src/utils/codec'; +import { DECODED_DIDT, VALID_DIDT } from '../../lib/constants'; + +test('Decoding', async () => { + const decodedString = decodeValue(VALID_DIDT); + + expect(decodedString).toBe(DECODED_DIDT); +}); + +test('Decoding malformed string', async () => { + const decodedString = decodeValue(`tRollGoat${VALID_DIDT}`); + + expect(decodedString).not.toBe(DECODED_DIDT); +}); + +test('Decoding null value', async () => { + const decodedString = decodeValue(null); + + expect(decodedString).toBe(''); +}); + +test('Decoding undefined', async () => { + const decodedString = decodeValue(undefined); + + expect(decodedString).toBe(''); +}); diff --git a/test/spec/utils/parse-didt.spec.ts b/test/spec/utils/parse-didt.spec.ts index 095af25..be57e4a 100644 --- a/test/spec/utils/parse-didt.spec.ts +++ b/test/spec/utils/parse-didt.spec.ts @@ -1,4 +1,3 @@ -import '../../../src/utils/shim'; import { VALID_DIDT, VALID_DIDT_DECODED, INVALID_DIDT_MALFORMED_CLAIM } from '../../lib/constants'; import { createMalformedTokenError } from '../../../src/core/sdk-exceptions'; import { parseDIDToken } from '../../../src/utils/parse-didt'; diff --git a/test/spec/utils/shim.spec.ts b/test/spec/utils/shim.spec.ts deleted file mode 100644 index 08a8f19..0000000 --- a/test/spec/utils/shim.spec.ts +++ /dev/null @@ -1,24 +0,0 @@ -import atob from 'atob'; - -test('Shim overwrites undefined global atob', async () => { - globalThis.atob = undefined; - - expect(globalThis.atob).toBeUndefined(); - - // eslint-disable-next-line global-require - require('../../../src/utils/shim'); - - expect(globalThis.atob).toBe(atob); -}); - -test('Shim does not overwrite exisiting atob', async () => { - const dummyFunc = () => ''; - globalThis.atob = dummyFunc; - - expect(globalThis.atob).toBe(dummyFunc); - - // eslint-disable-next-line global-require - require('../../../src/utils/shim'); - - expect(globalThis.atob).toBe(dummyFunc); -}); diff --git a/yarn.lock b/yarn.lock index d65c937..15e8037 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3913,11 +3913,18 @@ nice-try@^1.0.4: resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366" integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ== -node-fetch@2.6.1, node-fetch@^2.6.0, node-fetch@^2.6.1: +node-fetch@2.6.1, node-fetch@^2.6.1: version "2.6.1" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052" integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw== +node-fetch@^2.6.7: + version "2.7.0" + resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d" + integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A== + dependencies: + whatwg-url "^5.0.0" + node-int64@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b" @@ -5191,6 +5198,11 @@ tr46@^2.1.0: dependencies: punycode "^2.1.1" +tr46@~0.0.3: + version "0.0.3" + resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a" + integrity sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw== + trim-newlines@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-1.0.0.tgz#5887966bb582a4503a41eb524f7d35011815a613" @@ -5402,6 +5414,11 @@ walker@^1.0.7: dependencies: makeerror "1.0.12" +webidl-conversions@^3.0.0: + version "3.0.1" + resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871" + integrity sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ== + webidl-conversions@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-5.0.0.tgz#ae59c8a00b121543a2acc65c0434f57b0fc11aff" @@ -5424,6 +5441,14 @@ whatwg-mimetype@^2.3.0: resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz#3d4b1e0312d2079879f826aff18dbeeca5960fbf" integrity sha512-M4yMwr6mAnQz76TbJm914+gPpB/nCwvZbJU28cUD6dR004SAxDLOOSUaB1JDRqLtaOV/vi0IC5lEAGFgrjGv/g== +whatwg-url@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d" + integrity sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw== + dependencies: + tr46 "~0.0.3" + webidl-conversions "^3.0.0" + whatwg-url@^8.0.0, whatwg-url@^8.5.0: version "8.7.0" resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-8.7.0.tgz#656a78e510ff8f3937bc0bcbe9f5c0ac35941b77"