Skip to content

Commit faa49ef

Browse files
authored
fix(amazon-cognito-identity-js): Use JavaScript fallbacks when RNAWSCognito not available. (#14602)
2 parents e38d52c + 10883db commit faa49ef

File tree

3 files changed

+98
-29
lines changed

3 files changed

+98
-29
lines changed

.github/workflows/callable-unit-tests.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ jobs:
2020
path: amplify-js
2121
- name: Setup node and build the repository
2222
uses: ./amplify-js/.github/actions/node-and-build
23+
- name: Clear dependency cache and reinstall
24+
working-directory: ./amplify-js
25+
run: |
26+
rm -rf node_modules
27+
yarn install
28+
shell: bash
2329
- name: Run tests
2430
working-directory: ./amplify-js
2531
env:
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import BigInteger from '../src/BigInteger';
2+
import AuthenticationHelper from '../src/AuthenticationHelper';
3+
4+
// Mock React Native
5+
jest.mock('react-native', () => ({
6+
NativeModules: {
7+
RNAWSCognito: null
8+
}
9+
}));
10+
11+
describe('enhance-rn fallbacks', () => {
12+
beforeAll(() => {
13+
global.RNAWSCognito = null;
14+
require('../enhance-rn');
15+
});
16+
17+
it('should provide JavaScript fallback for modPow when native module is null', (done) => {
18+
const base = new BigInteger();
19+
base.fromInt(2);
20+
const exp = new BigInteger();
21+
exp.fromInt(3);
22+
const mod = new BigInteger();
23+
mod.fromInt(5);
24+
25+
base.modPow(exp, mod, (err, result) => {
26+
expect(err).toBeNull();
27+
expect(result.toString(16)).toBe('3');
28+
done();
29+
});
30+
});
31+
32+
it('should provide JavaScript fallback for calculateS when native module is null', (done) => {
33+
const instance = new AuthenticationHelper('TestPool');
34+
const xValue = new BigInteger('deadbeef', 16);
35+
const serverValue = new BigInteger('abcd1234', 16);
36+
37+
// Set up required instance variables
38+
instance.k = new BigInteger('deadbeef', 16);
39+
instance.UValue = new BigInteger('abc', 16);
40+
41+
instance.calculateS(xValue, serverValue, (err, result) => {
42+
expect(err).toBeNull();
43+
expect(result).toBeInstanceOf(BigInteger);
44+
done();
45+
});
46+
});
47+
});

packages/amazon-cognito-identity-js/enhance-rn.js

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,60 @@ export * from './src';
77

88
const { RNAWSCognito } = NativeModules;
99

10+
// Store the original functions before overriding
11+
const originalModPow = BigInteger.prototype.modPow;
12+
const originalCalculateS = src.AuthenticationHelper.prototype.calculateS;
13+
1014
BigInteger.prototype.modPow = function nativeModPow(e, m, callback) {
11-
RNAWSCognito.computeModPow(
12-
{
13-
target: this.toString(16),
14-
value: e.toString(16),
15-
modifier: m.toString(16),
16-
},
17-
(err, result) => {
18-
if (err) {
19-
return callback(new Error(err), null);
15+
// Check if native module is available
16+
if (RNAWSCognito && RNAWSCognito.computeModPow) {
17+
RNAWSCognito.computeModPow(
18+
{
19+
target: this.toString(16),
20+
value: e.toString(16),
21+
modifier: m.toString(16),
22+
},
23+
(err, result) => {
24+
if (err) {
25+
return callback(new Error(err), null);
26+
}
27+
const bigIntResult = new BigInteger(result, 16);
28+
return callback(null, bigIntResult);
2029
}
21-
const bigIntResult = new BigInteger(result, 16);
22-
return callback(null, bigIntResult);
23-
}
24-
);
30+
);
31+
} else {
32+
// Fall back to original JavaScript implementation
33+
return originalModPow.call(this, e, m, callback);
34+
}
2535
};
2636

2737
src.AuthenticationHelper.prototype.calculateS = function nativeComputeS(
2838
xValue,
2939
serverBValue,
3040
callback
3141
) {
32-
RNAWSCognito.computeS(
33-
{
34-
g: this.g.toString(16),
35-
x: xValue.toString(16),
36-
k: this.k.toString(16),
37-
a: this.smallAValue.toString(16),
38-
b: serverBValue.toString(16),
39-
u: this.UValue.toString(16),
40-
},
41-
(err, result) => {
42-
if (err) {
43-
return callback(new Error(err), null);
42+
// Check if native module is available
43+
if (RNAWSCognito && RNAWSCognito.computeS) {
44+
RNAWSCognito.computeS(
45+
{
46+
g: this.g.toString(16),
47+
x: xValue.toString(16),
48+
k: this.k.toString(16),
49+
a: this.smallAValue.toString(16),
50+
b: serverBValue.toString(16),
51+
u: this.UValue.toString(16),
52+
},
53+
(err, result) => {
54+
if (err) {
55+
return callback(new Error(err), null);
56+
}
57+
const bigIntResult = new BigInteger(result, 16);
58+
return callback(null, bigIntResult);
4459
}
45-
const bigIntResult = new BigInteger(result, 16);
46-
return callback(null, bigIntResult);
47-
}
48-
);
60+
);
61+
} else {
62+
// Fall back to original JavaScript implementation
63+
return originalCalculateS.call(this, xValue, serverBValue, callback);
64+
}
4965
return undefined;
5066
};

0 commit comments

Comments
 (0)