Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SeverApp] Restrict certain auth operations #8015

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs-devsite/auth.auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ auth.setPersistence(browserSessionPersistence);

Signs out the current user. This does not automatically revoke the user's ID token.

This method is not supported by [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

```typescript
Expand Down
40 changes: 30 additions & 10 deletions docs-devsite/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ On successful creation of the user account, this user will also be signed in to

User account creation can fail if the account already exists or the password is invalid.

This method is not supported on [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

Note: The email address acts as a unique identifier for the user and enables an email-based password reset. This function will create a new user account and set the initial user password.

<b>Signature:</b>
Expand Down Expand Up @@ -451,7 +453,7 @@ Returns a [UserCredential](./auth.usercredential.md#usercredential_interface) fr

If sign-in succeeded, returns the signed in user. If sign-in was unsuccessful, fails with an error. If no redirect operation was called, returns `null`<!-- -->.

This method does not work in a Node.js environment.
This method does not work in a Node.js environment or with [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

Expand Down Expand Up @@ -728,7 +730,7 @@ Changes the type of persistence on the [Auth](./auth.auth.md#auth_interface) ins

This makes it easy for a user signing in to specify whether their session should be remembered or not. It also makes it easier to never persist the `Auth` state for applications that are shared by other users or have sensitive data.

This method does not work in a Node.js environment.
This method does not work in a Node.js environment or with [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

Expand Down Expand Up @@ -763,6 +765,8 @@ Asynchronously signs in as an anonymous user.

If there is already an anonymous user signed in, that user will be returned; otherwise, a new anonymous user identity will be created and returned.

This method is not supported by [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

```typescript
Expand All @@ -785,6 +789,8 @@ Asynchronously signs in with the given credentials.

An [AuthProvider](./auth.authprovider.md#authprovider_interface) can be used to generate the credential.

This method is not supported by [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

```typescript
Expand All @@ -810,6 +816,8 @@ Custom tokens are used to integrate Firebase Auth with existing auth systems, an

Fails with an error if the token is invalid, expired, or not accepted by the Firebase Auth service.

This method is not supported by [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

```typescript
Expand All @@ -833,6 +841,8 @@ Asynchronously signs in using an email and password.

Fails with an error if the email address and password do not match. When \[Email Enumeration Protection\](https://cloud.google.com/identity-platform/docs/admin/email-enumeration-protection) is enabled, this method fails with "auth/invalid-credential" in case of an invalid email/password.

This method is not supported on [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

Note: The user's password is NOT the password used to access the user's email account. The email address serves as a unique identifier for the user, and the password is used to access the user's account in your Firebase project. See also: [createUserWithEmailAndPassword()](./auth.md#createuserwithemailandpassword_21ad33b)<!-- -->.

<b>Signature:</b>
Expand Down Expand Up @@ -861,6 +871,8 @@ If no link is passed, the link is inferred from the current URL.

Fails with an error if the email address is invalid or OTP in email link expires.

This method is not supported by [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

Note: Confirm the link is a sign-in email link before calling this method firebase.auth.Auth.isSignInWithEmailLink.

<b>Signature:</b>
Expand Down Expand Up @@ -913,7 +925,7 @@ This method sends a code via SMS to the given phone number, and returns a [Confi

For abuse prevention, this method also requires a [ApplicationVerifier](./auth.applicationverifier.md#applicationverifier_interface)<!-- -->. This SDK includes a reCAPTCHA-based implementation, [RecaptchaVerifier](./auth.recaptchaverifier.md#recaptchaverifier_class)<!-- -->. This function can work on other platforms that do not support the [RecaptchaVerifier](./auth.recaptchaverifier.md#recaptchaverifier_class) (like React Native), but you need to use a third-party [ApplicationVerifier](./auth.applicationverifier.md#applicationverifier_interface) implementation.

This method does not work in a Node.js environment.
This method does not work in a Node.js environment or with [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

Expand Down Expand Up @@ -951,7 +963,7 @@ Authenticates a Firebase client using a popup-based OAuth authentication flow.

If succeeds, returns the signed in user along with the provider's credential. If sign in was unsuccessful, returns an error object containing additional information about the error.

This method does not work in a Node.js environment.
This method does not work in a Node.js environment or with [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

Expand Down Expand Up @@ -993,7 +1005,7 @@ Authenticates a Firebase client using a full-page redirect flow.

To handle the results and errors for this operation, refer to [getRedirectResult()](./auth.md#getredirectresult_c35dc1f)<!-- -->. Follow the [best practices](https://firebase.google.com/docs/auth/web/redirect-best-practices) when using [signInWithRedirect()](./auth.md#signinwithredirect_770f816)<!-- -->.

This method does not work in a Node.js environment.
This method does not work in a Node.js environment or with [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

Expand Down Expand Up @@ -1045,6 +1057,8 @@ const operationType = result.operationType;

Signs out the current user.

This method is not supported by [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

```typescript
Expand All @@ -1071,6 +1085,8 @@ This will trigger [onAuthStateChanged()](./auth.md#onauthstatechanged_b0d07ab) a

The operation fails with an error if the user to be updated belongs to a different Firebase project.

This method is not supported by [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

```typescript
Expand Down Expand Up @@ -1347,7 +1363,7 @@ Links the [OAuthProvider](./auth.oauthprovider.md#oauthprovider_class) to the us

To handle the results and errors for this operation, refer to [getRedirectResult()](./auth.md#getredirectresult_c35dc1f)<!-- -->. Follow the [best practices](https://firebase.google.com/docs/auth/web/redirect-best-practices) when using [linkWithRedirect()](./auth.md#linkwithredirect_41c0b31)<!-- -->.

This method does not work in a Node.js environment.
This method does not work in a Node.js environment or with [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

Expand Down Expand Up @@ -1411,6 +1427,8 @@ Re-authenticates a user using a fresh credential.

Use before operations such as [updatePassword()](./auth.md#updatepassword_6df673e) that require tokens from recent sign-in attempts. This method can be used to recover from a `CREDENTIAL_TOO_OLD_LOGIN_AGAIN` error or a `TOKEN_EXPIRED` error.

This method is not supported on any [User](./auth.user.md#user_interface) signed in by [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

```typescript
Expand All @@ -1434,7 +1452,7 @@ Re-authenticates a user using a fresh phone credential.

Use before operations such as [updatePassword()](./auth.md#updatepassword_6df673e) that require tokens from recent sign-in attempts.

This method does not work in a Node.js environment.
This method does not work in a Node.js environment or on any [User](./auth.user.md#user_interface) signed in by [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

Expand All @@ -1460,7 +1478,7 @@ Reauthenticates the current user with the specified [OAuthProvider](./auth.oauth

If the reauthentication is successful, the returned result will contain the user and the provider's credential.

This method does not work in a Node.js environment.
This method does not work in a Node.js environment or on any [User](./auth.user.md#user_interface) signed in by [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

Expand Down Expand Up @@ -1498,7 +1516,7 @@ Reauthenticates the current user with the specified [OAuthProvider](./auth.oauth

To handle the results and errors for this operation, refer to [getRedirectResult()](./auth.md#getredirectresult_c35dc1f)<!-- -->. Follow the [best practices](https://firebase.google.com/docs/auth/web/redirect-best-practices) when using [reauthenticateWithRedirect()](./auth.md#reauthenticatewithredirect_41c0b31)<!-- -->.

This method does not work in a Node.js environment.
This method does not work in a Node.js environment or with [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

Expand Down Expand Up @@ -1630,6 +1648,8 @@ Updates the user's email address.

An email will be sent to the original email address (if it was set) that allows to revoke the email address change, in order to protect them from account hijacking.

This method is not supported on any [User](./auth.user.md#user_interface) signed in by [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

Important: this is a security sensitive operation that requires the user to have recently signed in. If this requirement isn't met, ask the user to authenticate again and then call [reauthenticateWithCredential()](./auth.md#reauthenticatewithcredential_60f8043)<!-- -->.

<b>Signature:</b>
Expand Down Expand Up @@ -1676,7 +1696,7 @@ Promise&lt;void&gt;

Updates the user's phone number.

This method does not work in a Node.js environment.
This method does not work in a Node.js environment or on any [User](./auth.user.md#user_interface) signed in by [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

Expand Down
2 changes: 2 additions & 0 deletions docs-devsite/auth.user.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ Deletes and signs out the user.

Important: this is a security-sensitive operation that requires the user to have recently signed in. If this requirement isn't met, ask the user to authenticate again and then call one of the reauthentication methods like [reauthenticateWithCredential()](./auth.md#reauthenticatewithcredential_60f8043)<!-- -->.

This method is not supported on any [User](./auth.user.md#user_interface) signed in by [Auth](./auth.auth.md#auth_interface) instances created with a [FirebaseServerApp](./app.firebaseserverapp.md#firebaseserverapp_interface)<!-- -->.

<b>Signature:</b>

```typescript
Expand Down
17 changes: 16 additions & 1 deletion packages/auth/src/core/auth/auth_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import {
PersistenceUserManager
} from '../persistence/persistence_user_manager';
import { _reloadWithoutSaving } from '../user/reload';
import { _assert } from '../util/assert';
import { _assert, _createError } from '../util/assert';
import { _getInstance } from '../util/instantiator';
import { _getUserLanguage } from '../util/navigator';
import { _getClientVersion } from '../util/version';
Expand Down Expand Up @@ -354,6 +354,11 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
}

async updateCurrentUser(userExtern: User | null): Promise<void> {
if (_isFirebaseServerApp(this.app)) {
return Promise.reject(
_createError(this, AuthErrorCode.OPERATION_NOT_SUPPORTED)
);
}
// The public updateCurrentUser method needs to make a copy of the user,
// and also check that the project matches
const user = userExtern
Expand Down Expand Up @@ -395,6 +400,11 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
}

async signOut(): Promise<void> {
if (_isFirebaseServerApp(this.app)) {
return Promise.reject(
_createError(this, AuthErrorCode.OPERATION_NOT_SUPPORTED)
);
}
// Run first, to block _setRedirectUser() if any callbacks fail.
await this.beforeStateQueue.runMiddleware(null);
// Clear the redirect user when signOut is called
Expand All @@ -408,6 +418,11 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
}

setPersistence(persistence: Persistence): Promise<void> {
if (_isFirebaseServerApp(this.app)) {
return Promise.reject(
_createError(this, AuthErrorCode.OPERATION_NOT_SUPPORTED)
);
}
return this.queue(async () => {
await this.assertedPersistence.setPersistence(_getInstance(persistence));
});
Expand Down
10 changes: 9 additions & 1 deletion packages/auth/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export {
* remembered or not. It also makes it easier to never persist the `Auth` state for applications
* that are shared by other users or have sensitive data.
*
* This method does not work in a Node.js environment.
* This method does not work in a Node.js environment or with {@link Auth} instances created with a
* {@link @firebase/app#FirebaseServerApp}.
*
* @example
* ```javascript
Expand Down Expand Up @@ -223,6 +224,9 @@ export function useDeviceLanguage(auth: Auth): void {
* The operation fails with an error if the user to be updated belongs to a different Firebase
* project.
*
* This method is not supported by {@link Auth} instances created with a
* {@link @firebase/app#FirebaseServerApp}.
*
* @param auth - The {@link Auth} instance.
* @param user - The new {@link User}.
*
Expand All @@ -237,6 +241,10 @@ export function updateCurrentUser(
/**
* Signs out the current user.
*
* @remarks
* This method is not supported by {@link Auth} instances created with a
* {@link @firebase/app#FirebaseServerApp}.
*
* @param auth - The {@link Auth} instance.
*
* @public
Expand Down
11 changes: 11 additions & 0 deletions packages/auth/src/core/strategies/anonymous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import { UserInternal } from '../../model/user';
import { UserCredentialImpl } from '../user/user_credential_impl';
import { _castAuth } from '../auth/auth_impl';
import { OperationType } from '../../model/enums';
import { _isFirebaseServerApp } from '@firebase/app';
import { _createError } from '../../core/util/assert';
import { AuthErrorCode } from '../../core/errors';

/**
* Asynchronously signs in as an anonymous user.
Expand All @@ -29,11 +32,19 @@ import { OperationType } from '../../model/enums';
* If there is already an anonymous user signed in, that user will be returned; otherwise, a
* new anonymous user identity will be created and returned.
*
* This method is not supported by {@link Auth} instances created with a
* {@link @firebase/app#FirebaseServerApp}.
*
* @param auth - The {@link Auth} instance.
*
* @public
*/
export async function signInAnonymously(auth: Auth): Promise<UserCredential> {
if (_isFirebaseServerApp(auth.app)) {
return Promise.reject(
_createError(auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
);
}
const authInternal = _castAuth(auth);
await authInternal._initializationPromise;
if (authInternal.currentUser?.isAnonymous) {
Expand Down
14 changes: 14 additions & 0 deletions packages/auth/src/core/strategies/credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ import { UserCredentialImpl } from '../user/user_credential_impl';
import { _castAuth } from '../auth/auth_impl';
import { getModularInstance } from '@firebase/util';
import { OperationType } from '../../model/enums';
import { _isFirebaseServerApp } from '@firebase/app';
import { _createError } from '../../core/util/assert';
import { AuthErrorCode } from '../../core/errors';

export async function _signInWithCredential(
auth: AuthInternal,
credential: AuthCredential,
bypassAuthState = false
): Promise<UserCredential> {
if (_isFirebaseServerApp(auth.app)) {
return Promise.reject(
_createError(auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
);
}
const operationType = OperationType.SIGN_IN;
const response = await _processCredentialSavingMfaContextIfNecessary(
auth,
Expand All @@ -57,6 +65,9 @@ export async function _signInWithCredential(
* @remarks
* An {@link AuthProvider} can be used to generate the credential.
*
* This method is not supported by {@link Auth} instances created with a
* {@link @firebase/app#FirebaseServerApp}.
*
* @param auth - The {@link Auth} instance.
* @param credential - The auth credential.
*
Expand Down Expand Up @@ -99,6 +110,9 @@ export async function linkWithCredential(
* attempts. This method can be used to recover from a `CREDENTIAL_TOO_OLD_LOGIN_AGAIN` error
* or a `TOKEN_EXPIRED` error.
*
* This method is not supported on any {@link User} signed in by {@link Auth} instances
* created with a {@link @firebase/app#FirebaseServerApp}.
*
* @param user - The user.
* @param credential - The auth credential.
*
Expand Down
12 changes: 11 additions & 1 deletion packages/auth/src/core/strategies/custom_token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import { IdTokenResponse } from '../../model/id_token';
import { UserCredentialImpl } from '../user/user_credential_impl';
import { _castAuth } from '../auth/auth_impl';
import { OperationType } from '../../model/enums';

import { _isFirebaseServerApp } from '@firebase/app';
import { _createError } from '../../core/util/assert';
import { AuthErrorCode } from '../../core/errors';
/**
* Asynchronously signs in using a custom token.
*
Expand All @@ -34,6 +36,9 @@ import { OperationType } from '../../model/enums';
*
* Fails with an error if the token is invalid, expired, or not accepted by the Firebase Auth service.
*
* This method is not supported by {@link Auth} instances created with a
* {@link @firebase/app#FirebaseServerApp}.
*
* @param auth - The {@link Auth} instance.
* @param customToken - The custom token to sign in with.
*
Expand All @@ -43,6 +48,11 @@ export async function signInWithCustomToken(
auth: Auth,
customToken: string
): Promise<UserCredential> {
if (_isFirebaseServerApp(auth.app)) {
return Promise.reject(
_createError(auth, AuthErrorCode.OPERATION_NOT_SUPPORTED)
);
}
const authInternal = _castAuth(auth);
const response: IdTokenResponse = await getIdTokenResponse(authInternal, {
token: customToken,
Expand Down
Loading
Loading