Skip to content

Commit 2c32119

Browse files
bhparijatprameshj
authored andcommitted
adding support TOTP MFA (#6547)
Co-authored-by: Parijat Bhatt <[email protected]>
1 parent 5b696de commit 2c32119

File tree

5 files changed

+146
-2
lines changed

5 files changed

+146
-2
lines changed

common/api-review/auth.api.md

+5
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ export class FacebookAuthProvider extends BaseOAuthProvider {
361361
// @public
362362
export const FactorId: {
363363
readonly PHONE: "phone";
364+
readonly TOTP: "totp";
364365
};
365366

366367
// @public
@@ -745,6 +746,10 @@ export function signInWithRedirect(auth: Auth, provider: AuthProvider, resolver?
745746
// @public
746747
export function signOut(auth: Auth): Promise<void>;
747748

749+
// @public
750+
export interface TotpMultiFactorAssertion extends MultiFactorAssertion {
751+
}
752+
748753
// @public
749754
export class TwitterAuthProvider extends BaseOAuthProvider {
750755
constructor();
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* @license
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
import { TotpSecret } from '../../platform_browser/mfa/assertions/totp';
18+
import {
19+
TotpMultiFactorAssertion,
20+
MultiFactorSession,
21+
FactorId
22+
} from '../../model/public_types';
23+
/**
24+
* Provider for generating a {@link TotpMultiFactorAssertion}.
25+
*
26+
* @public
27+
*/
28+
export class TotpMultiFactorGenerator {
29+
/**
30+
* Provides a {@link TotpMultiFactorAssertion} to confirm ownership of
31+
* the totp(Time-based One Time Password) second factor.
32+
* This assertion is used to complete enrollment in TOTP second factor.
33+
*
34+
* @param secret {@link TotpSecret}.
35+
* @param oneTimePassword One-time password from TOTP App.
36+
* @returns A {@link TotpMultiFactorAssertion} which can be used with
37+
* {@link MultiFactorUser.enroll}.
38+
*/
39+
static assertionForEnrollment(
40+
_secret: TotpSecret,
41+
_oneTimePassword: string
42+
): TotpMultiFactorAssertion {
43+
throw new Error('Unimplemented');
44+
}
45+
/**
46+
* Provides a {@link TotpMultiFactorAssertion} to confirm ownership of the totp second factor.
47+
* This assertion is used to complete signIn with TOTP as the second factor.
48+
*
49+
* @param enrollmentId identifies the enrolled TOTP second factor.
50+
* @param otp One-time password from TOTP App.
51+
* @returns A {@link TotpMultiFactorAssertion} which can be used with
52+
* {@link MultiFactorResolver.resolveSignIn}.
53+
*/
54+
static assertionForSignIn(
55+
_enrollmentId: string,
56+
_otp: string
57+
): TotpMultiFactorAssertion {
58+
throw new Error('Unimplemented');
59+
}
60+
/**
61+
* Returns a promise to {@link TOTPSecret} which contains the TOTP shared secret key and other parameters.
62+
* Creates a TOTP secret as part of enrolling a TOTP second factor.
63+
* Used for generating a QRCode URL or inputting into a TOTP App.
64+
* This method uses the auth instance corresponding to the user in the multiFactorSession.
65+
*
66+
* @param session A link to {@MultiFactorSession}.
67+
* @returns A promise to {@link TotpSecret}.
68+
*/
69+
static async generateSecret(
70+
_session: MultiFactorSession
71+
): Promise<TotpSecret> {
72+
throw new Error('Unimplemented');
73+
}
74+
/**
75+
* The identifier of the TOTP second factor: `totp`.
76+
*/
77+
static FACTOR_ID = FactorId.TOTP;
78+
}

packages/auth/src/model/enum_maps.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
*/
2323
export const FactorId = {
2424
/** Phone as second factor */
25-
PHONE: 'phone'
25+
PHONE: 'phone',
26+
TOTP: 'totp'
2627
} as const;
2728

2829
/**

packages/auth/src/model/public_types.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,8 @@ export interface AuthProvider {
545545
*/
546546
export const enum FactorId {
547547
/** Phone as second factor */
548-
PHONE = 'phone'
548+
PHONE = 'phone',
549+
TOTP = 'totp'
549550
}
550551

551552
/**
@@ -1229,3 +1230,12 @@ export interface Dependencies {
12291230
*/
12301231
errorMap?: AuthErrorMap;
12311232
}
1233+
1234+
/**
1235+
* The class for asserting ownership of a totp second factor. Provided by
1236+
* {@link TotpMultiFactorGenerator.assertion}.
1237+
*
1238+
* @public
1239+
*/
1240+
1241+
export interface TotpMultiFactorAssertion extends MultiFactorAssertion {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @license
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* Stores the shared secret key and other parameters to generate time-based OTPs.
20+
* Implements methods to retrieve the shared secret key, generate a QRCode URL.
21+
* @public
22+
*/
23+
export class TotpSecret {
24+
/**
25+
* Constructor for TotpSecret.
26+
* @param secretKey - Shared secret key/seed used for enrolling in TOTP MFA and generating otps.
27+
* @param hashingAlgorithm - Hashing algorithm used.
28+
* @param codeLength - Length of the one-time passwords to be generated.
29+
* @param codeIntervalSeconds - The interval (in seconds) when the OTP codes should change.
30+
*/
31+
constructor(
32+
readonly secretKey: string,
33+
readonly hashingAlgorithm: string,
34+
readonly codeLength: number,
35+
readonly codeIntervalSeconds: number
36+
) {}
37+
/**
38+
* Returns a QRCode URL as described in
39+
* https://github.com/google/google-authenticator/wiki/Key-Uri-Format
40+
* This can be displayed to the user as a QRCode to be scanned into a TOTP App like Google Authenticator.
41+
* If the optional parameters are unspecified, an accountName of "<firebaseAppName>:<userEmail> and issuer of <firebaseAppName> are used.
42+
*
43+
* @param accountName the name of the account/app along with a user identifier.
44+
* @param issuer issuer of the TOTP(likely the app name).
45+
* @returns A QRCode URL string.
46+
*/
47+
generateQrCodeUrl(_accountName?: string, _issuer?: string): string {
48+
throw new Error('Unimplemented');
49+
}
50+
}

0 commit comments

Comments
 (0)