@aws-amplify/auth 4.6.1-unstable.4 → 4.6.2-size-improvement.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,6 +14,7 @@
14
14
  import { parse } from 'url'; // Used for OAuth parsing of Cognito Hosted UI
15
15
  import { launchUri } from './urlOpener';
16
16
  import * as oAuthStorage from './oauthStorage';
17
+ import { Buffer } from 'buffer';
17
18
 
18
19
  import {
19
20
  OAuthOpts,
@@ -23,13 +24,12 @@ import {
23
24
 
24
25
  import { ConsoleLogger as Logger, Hub, urlSafeEncode } from '@aws-amplify/core';
25
26
 
26
- import sha256 from 'crypto-js/sha256';
27
- import Base64 from 'crypto-js/enc-base64';
28
-
29
- const AMPLIFY_SYMBOL = (typeof Symbol !== 'undefined' &&
30
- typeof Symbol.for === 'function'
31
- ? Symbol.for('amplify_default')
32
- : '@@amplify_default') as Symbol;
27
+ import { Sha256 as jsSha256 } from '@aws-crypto/sha256-js';
28
+ const AMPLIFY_SYMBOL = (
29
+ typeof Symbol !== 'undefined' && typeof Symbol.for === 'function'
30
+ ? Symbol.for('amplify_default')
31
+ : '@@amplify_default'
32
+ ) as Symbol;
33
33
 
34
34
  const dispatchAuthEvent = (event: string, data: any, message: string) => {
35
35
  Hub.dispatch('auth', { event, data, message }, 'Auth', AMPLIFY_SYMBOL);
@@ -169,18 +169,15 @@ export default class OAuth {
169
169
  .map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
170
170
  .join('&');
171
171
 
172
- const {
173
- access_token,
174
- refresh_token,
175
- id_token,
176
- error,
177
- } = await ((await fetch(oAuthTokenEndpoint, {
178
- method: 'POST',
179
- headers: {
180
- 'Content-Type': 'application/x-www-form-urlencoded',
181
- },
182
- body,
183
- })) as any).json();
172
+ const { access_token, refresh_token, id_token, error } = await (
173
+ (await fetch(oAuthTokenEndpoint, {
174
+ method: 'POST',
175
+ headers: {
176
+ 'Content-Type': 'application/x-www-form-urlencoded',
177
+ },
178
+ body,
179
+ })) as any
180
+ ).json();
184
181
 
185
182
  if (error) {
186
183
  throw new Error(error);
@@ -305,15 +302,18 @@ export default class OAuth {
305
302
  }
306
303
 
307
304
  private _generateChallenge(code: string) {
308
- return this._base64URL(sha256(code));
305
+ const awsCryptoHash = new jsSha256();
306
+ awsCryptoHash.update(code);
307
+ const resultFromAWSCrypto = awsCryptoHash.digestSync();
308
+ const b64 = Buffer.from(resultFromAWSCrypto).toString('base64');
309
+
310
+ const base64URLFromAWSCrypto = this._base64URL(b64);
311
+
312
+ return base64URLFromAWSCrypto;
309
313
  }
310
314
 
311
315
  private _base64URL(string) {
312
- return string
313
- .toString(Base64)
314
- .replace(/=/g, '')
315
- .replace(/\+/g, '-')
316
- .replace(/\//g, '_');
316
+ return string.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
317
317
  }
318
318
 
319
319
  private _generateRandom(size: number) {