Skip to content

Commit e32b6ce

Browse files
committed
feat(auth): QR code generation from TOTP secrets
1 parent a074bac commit e32b6ce

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,23 @@ public void finalizeMultiFactorEnrollment(
13051305
});
13061306
}
13071307

1308+
@ReactMethod
1309+
public void generateQrCodeUrl(
1310+
final String appName,
1311+
final String secretKey,
1312+
final String account,
1313+
final String issuer,
1314+
final Promise promise) {
1315+
1316+
TotpSecret secret = mTotpSecrets.get(secretKey);
1317+
if (secret == null) {
1318+
rejectPromiseWithCodeAndMessage(
1319+
promise, "invalid-multi-factor-secret", "can't find secret for provided key");
1320+
return;
1321+
}
1322+
promise.resolve(secret.generateQrCodeUrl(account, issuer));
1323+
}
1324+
13081325
@ReactMethod
13091326
public void finalizeTotpEnrollment(
13101327
final String appName,

packages/auth/ios/RNFBAuth/RNFBAuthModule.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,21 @@ - (void)invalidate {
10261026
}];
10271027
}
10281028

1029+
RCT_EXPORT_METHOD(generateQrCodeUrl
1030+
: (FIRApp *)firebaseApp
1031+
: (NSString *)secretKey
1032+
: (NSString *)accountName
1033+
: (NSString *)issuer
1034+
: (RCTPromiseResolveBlock)resolve
1035+
: (RCTPromiseRejectBlock)reject) {
1036+
DLog(@"generateQrCodeUrl using instance resolve generateQrCodeUrl: %@", firebaseApp.name);
1037+
DLog(@"generateQrCodeUrl using secretKey: %@", secretKey);
1038+
FIRTOTPSecret *totpSecret = cachedTotpSecrets[secretKey];
1039+
NSString *url = [totpSecret generateQRCodeURLWithAccountName:accountName issuer:issuer];
1040+
DLog(@"generateQrCodeUrl got QR Code URL %@", url);
1041+
resolve(url);
1042+
}
1043+
10291044
RCT_EXPORT_METHOD(getSession
10301045
: (FIRApp *)firebaseApp
10311046
: (RCTPromiseResolveBlock)resolve

packages/auth/lib/TotpSecret.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { isString } from '@react-native-firebase/app/lib/common';
18+
1719
export class TotpSecret {
1820
constructor(secretKey, auth) {
1921
// The native TotpSecret has many more properties, but they are
@@ -37,17 +39,14 @@ export class TotpSecret {
3739
*
3840
* @param accountName the name of the account/app along with a user identifier.
3941
* @param issuer issuer of the TOTP (likely the app name).
40-
* @returns A QR code URL string.
42+
* @returns A Promise that resolves to a QR code URL string.
4143
*/
42-
generateQrCodeUrl(_accountName, _issuer) {
43-
throw new Error('`generateQrCodeUrl` is not supported on the native Firebase SDKs.');
44-
// if (!this.hashingAlgorithm || !this.codeLength) {
45-
// return "";
46-
// }
47-
48-
// return (
49-
// `otpauth://totp/${issuer}:${accountName}?secret=${this.secretKey}&issuer=${issuer}` +
50-
// `&algorithm=${this.hashingAlgorithm}&digits=${this.codeLength}`
51-
// );
44+
async generateQrCodeUrl(accountName, issuer) {
45+
// accountName and issure are nullable in the API specification but are
46+
// required by tha native SDK. The JS SDK returns '' if they are missing/empty.
47+
if (!isString(accountName) || !isString(issuer) || accountName === '' || issuer === '') {
48+
return '';
49+
}
50+
return this.auth.native.generateQrCodeUrl(this.secretKey, accountName, issuer);
5251
}
5352
}

0 commit comments

Comments
 (0)