Skip to content

Commit 16ea044

Browse files
committed
MFA
1 parent a4fbf65 commit 16ea044

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

packages-exp/auth-compat-exp/src/auth.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
convertConfirmationResult,
3535
convertCredential
3636
} from './user_credential';
37-
import { unwrap, Wrapper } from './wrap';
37+
import { ReverseWrapper, unwrap, Wrapper } from './wrap';
3838

3939
const _assert: typeof exp._assert = exp._assert;
4040

@@ -45,6 +45,7 @@ export class Auth
4545
constructor(readonly app: FirebaseApp, provider: Provider<'auth-exp'>) {
4646
if (provider.isInitialized()) {
4747
this.auth = provider.getImmediate() as exp.AuthImpl;
48+
this.linkUnderlyingAuth();
4849
return;
4950
}
5051

@@ -89,6 +90,7 @@ export class Auth
8990
}) as exp.AuthImpl;
9091

9192
this.auth._updateErrorMap(exp.debugErrorMap);
93+
this.linkUnderlyingAuth();
9294
}
9395

9496
get emulatorConfig(): compat.EmulatorConfig | null {
@@ -330,6 +332,9 @@ export class Auth
330332
_delete(): Promise<void> {
331333
return this.auth._delete();
332334
}
335+
private linkUnderlyingAuth(): void {
336+
(this.auth as unknown as ReverseWrapper<Auth>).wrapped = () => this;
337+
}
333338
}
334339

335340
function wrapObservers(

packages-exp/auth-compat-exp/src/user_credential.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import * as exp from '@firebase/auth-exp/internal';
1919
import * as compat from '@firebase/auth-types';
2020
import { FirebaseError } from '@firebase/util';
21+
import { Auth } from './auth';
2122
import { User } from './user';
23+
import { unwrap, wrapped } from './wrap';
2224

2325
const enum CredentialFrom {
2426
ERROR = 'credentialFromError',
@@ -39,7 +41,7 @@ function modifyError(
3941
const response = (e.customData as exp.TaggedWithTokenResponse|undefined)?._tokenResponse as unknown as Record<string, string>;
4042
if (e.code === 'auth/multi-factor-auth-required') {
4143
const mfaErr = e as compat.MultiFactorError;
42-
mfaErr.resolver = exp.getMultiFactorResolver(auth, e as any);
44+
mfaErr.resolver = new MultiFactorResolver(auth, exp.getMultiFactorResolver(auth, e as any));
4345
} else if (response) {
4446
const credential = credentialFromObject(e);
4547
const credErr = e as compat.AuthError;
@@ -110,7 +112,7 @@ function credentialFromObject(object: FirebaseError|exp.UserCredential): exp.Aut
110112
// TODO(avolkovi): uncomment this and get it working with SAML & OIDC
111113
if (pendingToken) {
112114
if (providerId.indexOf('saml.') == 0) {
113-
return exp.SAMLAuthProvider.credential(providerId, pendingToken);
115+
return exp.SAMLAuthCredential._create(providerId, pendingToken);
114116
} else {
115117
// OIDC and non-default providers excluding Twitter.
116118
return exp.OAuthCredential._fromParams(
@@ -169,3 +171,22 @@ export async function convertConfirmationResult(
169171
convertCredential(auth, confirmationResultExp.confirm(verificationCode))
170172
};
171173
}
174+
175+
class MultiFactorResolver implements compat.MultiFactorResolver {
176+
readonly auth: Auth;
177+
constructor(auth: exp.Auth, private readonly resolver: exp.MultiFactorResolver) {
178+
this.auth = wrapped(auth);
179+
}
180+
181+
get session(): compat.MultiFactorSession {
182+
return this.resolver.session;
183+
}
184+
185+
get hints(): compat.MultiFactorInfo[] {
186+
return this.resolver.hints;
187+
}
188+
189+
resolveSignIn(assertion: compat.MultiFactorAssertion): Promise<compat.UserCredential> {
190+
return convertCredential(unwrap(this.auth), this.resolver.resolveSignIn(assertion as exp.MultiFactorAssertion));
191+
}
192+
}

packages-exp/auth-compat-exp/src/wrap.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,20 @@
1515
* limitations under the License.
1616
*/
1717

18+
/** Forward direction wrapper from Compat --unwrap-> Exp */
1819
export interface Wrapper<T> {
1920
unwrap(): T;
2021
}
2122

23+
/** Reverse direction wrapper from Exp --wrapped--> Compat */
24+
export interface ReverseWrapper<T> {
25+
wrapped(): T;
26+
}
27+
2228
export function unwrap<T>(object: unknown): T {
2329
return (object as Wrapper<T>).unwrap();
2430
}
31+
32+
export function wrapped<T>(object: unknown): T {
33+
return (object as ReverseWrapper<T>).wrapped();
34+
}

packages-exp/auth-exp/internal/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ export { AuthPopup } from '../src/platform_browser/util/popup';
4444
export { _getRedirectResult } from '../src/platform_browser/strategies/redirect';
4545
export { cordovaPopupRedirectResolver } from '../src/platform_cordova/popup_redirect/popup_redirect';
4646
export { FetchProvider } from '../src/core/util/fetch_provider';
47+
export { SAMLAuthCredential } from '../src/core/credentials/saml';

0 commit comments

Comments
 (0)