-
Notifications
You must be signed in to change notification settings - Fork 946
Add extension package that strips external JS loading #7766
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
Changes from all commits
914216c
c82c6f3
1d9f9e2
ee0167b
f8ee64b
baf695b
12ffe84
26634a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Core functionality shared by all clients | ||
export * from './src'; | ||
|
||
import { ClientPlatform } from './src/core/util/version'; | ||
|
||
import { indexedDBLocalPersistence } from './src/platform_browser/persistence/indexed_db'; | ||
|
||
import { | ||
TotpMultiFactorGenerator, | ||
TotpSecret | ||
} from './src/mfa/assertions/totp'; | ||
import { FirebaseApp, getApp, _getProvider } from '@firebase/app'; | ||
import { Auth, connectAuthEmulator, initializeAuth } from './index.shared'; | ||
import { getDefaultEmulatorHost } from '@firebase/util'; | ||
import { registerAuth } from './src/core/auth/register'; | ||
|
||
/** | ||
* Returns the Auth instance associated with the provided {@link @firebase/app#FirebaseApp}. | ||
* If no instance exists, initializes an Auth instance with platform-specific default dependencies. | ||
* | ||
* @param app - The Firebase App. | ||
* | ||
* @public | ||
*/ | ||
function getAuth(app: FirebaseApp = getApp()): Auth { | ||
const provider = _getProvider(app, 'auth'); | ||
|
||
if (provider.isInitialized()) { | ||
return provider.getImmediate(); | ||
} | ||
|
||
const auth = initializeAuth(app, { | ||
persistence: [indexedDBLocalPersistence] | ||
}); | ||
|
||
const authEmulatorHost = getDefaultEmulatorHost('auth'); | ||
if (authEmulatorHost) { | ||
connectAuthEmulator(auth, `http://${authEmulatorHost}`); | ||
} | ||
|
||
return auth; | ||
} | ||
|
||
registerAuth(ClientPlatform.WEB_EXTENSION); | ||
|
||
export { | ||
indexedDBLocalPersistence, | ||
TotpMultiFactorGenerator, | ||
TotpSecret, | ||
getAuth | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,9 @@ import { indexedDBLocalPersistence } from './persistence/indexed_db'; | |
import { browserPopupRedirectResolver } from './popup_redirect'; | ||
import { Auth, User } from '../model/public_types'; | ||
import { getDefaultEmulatorHost, getExperimentalSetting } from '@firebase/util'; | ||
import { _setExternalJSProvider } from './load_js'; | ||
import { _createError } from '../core/util/assert'; | ||
import { AuthErrorCode } from '../core/errors'; | ||
|
||
const DEFAULT_ID_TOKEN_MAX_AGE = 5 * 60; | ||
const authIdTokenMaxAge = | ||
|
@@ -103,4 +106,32 @@ export function getAuth(app: FirebaseApp = getApp()): Auth { | |
return auth; | ||
} | ||
|
||
function getScriptParentElement(): HTMLDocument | HTMLHeadElement { | ||
return document.getElementsByTagName('head')?.[0] ?? document; | ||
} | ||
|
||
_setExternalJSProvider({ | ||
loadJS(url: string): Promise<Event> { | ||
// TODO: consider adding timeout support & cancellation | ||
return new Promise((resolve, reject) => { | ||
const el = document.createElement('script'); | ||
el.setAttribute('src', url); | ||
el.onload = resolve; | ||
el.onerror = e => { | ||
const error = _createError(AuthErrorCode.INTERNAL_ERROR); | ||
error.customData = e as unknown as Record<string, unknown>; | ||
reject(error); | ||
}; | ||
el.type = 'text/javascript'; | ||
el.charset = 'UTF-8'; | ||
getScriptParentElement().appendChild(el); | ||
}); | ||
}, | ||
|
||
gapiScript: 'https://apis.google.com/js/api.js', | ||
recaptchaV2Script: 'https://www.google.com/recaptcha/api.js', | ||
recaptchaEnterpriseScript: | ||
'https://www.google.com/recaptcha/enterprise.js?render=' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I copied the exact url from https://github.com/firebase/firebase-js-sdk/blob/master/packages/auth/src/platform_browser/recaptcha/recaptcha_enterprise_verifier.ts#L35. The full url we need to pass in _loadJS is 'https://www.google.com/recaptcha/enterprise.js?render=SITE-KEY'. So if we remove '?render=' from here, we will have to update this line to be
|
||
}); | ||
|
||
registerAuth(ClientPlatform.BROWSER); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@firebase/auth/web-extension", | ||
"description": "A Chrome-Manifest-v3-specific build of the Firebase Auth JS SDK", | ||
"main": "../dist/web-extension-cjs/index.js", | ||
"browser": "../dist/web-extension-esm2017/index.js", | ||
"module": "../dist/web-extension-esm2017/index.js", | ||
"typings": "../dist/web-extension-esm2017/index.web-extension.d.ts" | ||
} |
Uh oh!
There was an error while loading. Please reload this page.