Skip to content

Commit 7a0a879

Browse files
authored
Pass API Key as request param for Regional Auth requests & Demo App changes (#9102)
* Pass API Key as request param for Regional Auth requests * Format * Update chrome version * Update chrome version * Update chrome version * Update chrome version * Update chrome version * Update chrome version * BYO-CIAM Demo App Changes (#9106) * BYO-CIAM Demo App Changes
1 parent 1aa8468 commit 7a0a879

File tree

12 files changed

+90
-24
lines changed

12 files changed

+90
-24
lines changed

.github/workflows/test-all.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ env:
2323
# the behavior to use the new URLs.
2424
CHROMEDRIVER_CDNURL: https://googlechromelabs.github.io/
2525
CHROMEDRIVER_CDNBINARIESURL: https://storage.googleapis.com/chrome-for-testing-public
26-
CHROME_VALIDATED_VERSION: linux-132.0.6834.110
26+
CHROME_VALIDATED_VERSION: linux-137.0.0.0
2727
CHROME_VERSION_MISMATCH_MESSAGE: "The Chrome version doesn't match the previously validated version. Consider updating CHROME_VALIDATED_VERSION in the GitHub workflow if tests pass, or rollback the installed Chrome version if tests fail."
2828
artifactRetentionDays: 14
2929
# Bump Node memory limit

.github/workflows/test-changed-auth.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ env:
2323
# the behavior to use the new URLs.
2424
CHROMEDRIVER_CDNURL: https://googlechromelabs.github.io/
2525
CHROMEDRIVER_CDNBINARIESURL: https://storage.googleapis.com/chrome-for-testing-public
26-
CHROME_VALIDATED_VERSION: linux-120.0.6099.71
26+
CHROME_VALIDATED_VERSION: linux-137.0.7151.119
2727
# Bump Node memory limit
2828
NODE_OPTIONS: "--max_old_space_size=4096"
2929

@@ -119,4 +119,4 @@ jobs:
119119
- name: Run tests on changed packages
120120
run: yarn test:changed auth
121121
env:
122-
BROWSERS: 'WebkitHeadless'
122+
BROWSERS: 'WebkitHeadless'

packages/auth/demo/public/index.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@
170170
Action Code Settings
171171
</a>
172172
</li>
173+
<li role="presentation">
174+
<a href="#tab-byo-ciam-content"
175+
aria-controls="tab-byo-ciam-content"
176+
data-toggle="tab" role="tab">
177+
BYO-CIAM methods
178+
</a>
179+
</li>
173180
<li role="presentation" class="visible-xs">
174181
<a href="#logs-section"
175182
aria-controls="logs-section"
@@ -844,6 +851,16 @@
844851
id="action-code-settings-reset">Reset</button>
845852
</form>
846853
</div>
854+
<div class="tab-pane" id="tab-byo-ciam-content">
855+
<h2>Sign in with your CIAM token</h2>
856+
<input type="text" id="byo-ciam-token"
857+
class="form-control" placeholder="Enter CIAM token" />
858+
<button class="btn btn-block btn-primary"
859+
id="exchange-token">
860+
Exchange Token
861+
</button>
862+
<pre id="byo-ciam-result"></pre>
863+
</div>
847864
<div class="tab-pane" id="logs-section">
848865
<pre class="well logs"></pre>
849866
<button class="btn btn-xs btn-default pull-right clear-logs">

packages/auth/demo/src/index.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ import {
7474
connectAuthEmulator,
7575
initializeRecaptchaConfig,
7676
validatePassword,
77-
revokeAccessToken
77+
revokeAccessToken,
78+
exchangeToken
7879
} from '@firebase/auth';
7980

8081
import { config } from './config';
@@ -95,6 +96,7 @@ const AUTH_EMULATOR_URL = 'http://localhost:9099';
9596

9697
let app = null;
9798
let auth = null;
99+
let regionalAuth = null;
98100
let currentTab = null;
99101
let lastUser = null;
100102
let applicationVerifier = null;
@@ -1506,6 +1508,32 @@ function onFinalizeSignInWithTotpMultiFactor(event) {
15061508
}, onAuthError);
15071509
}
15081510

1511+
async function exchangeCIAMToken(token) {
1512+
const firebaseToken = await exchangeToken(
1513+
regaionalAuth,
1514+
(idpConfigId = 'Bar-e2e-idpconfig-002'),
1515+
token
1516+
);
1517+
return firebaseToken;
1518+
}
1519+
1520+
function onExchangeToken(event) {
1521+
event.preventDefault();
1522+
const byoCiamInput = document.getElementById('byo-ciam-token');
1523+
const byoCiamResult = document.getElementById('byo-ciam-result');
1524+
1525+
byoCiamResult.textContent = 'Exchanging token...';
1526+
1527+
exchangeCIAMToken(byoCiamInput.value)
1528+
.then(response => {
1529+
byoCiamResult.textContent = response.accessToken;
1530+
console.log('Token:', response);
1531+
})
1532+
.catch(error => {
1533+
console.error('Error exchanging token:', error);
1534+
});
1535+
}
1536+
15091537
/**
15101538
* Adds a new row to insert an OAuth custom parameter key/value pair.
15111539
* @param {!jQuery.Event} _event The jQuery event object.
@@ -2051,6 +2079,18 @@ function initApp() {
20512079
connectAuthEmulator(auth, AUTH_EMULATOR_URL);
20522080
}
20532081

2082+
let tenantConfig = {
2083+
'location': 'global',
2084+
'tenantId': 'Foo-e2e-tenant-001'
2085+
};
2086+
const regionalApp = initializeApp(config, `${auth.name}-rgcip`);
2087+
2088+
regionalAuth = initializeAuth(regionalApp, {
2089+
persistence: inMemoryPersistence,
2090+
popupRedirectResolver: browserPopupRedirectResolver,
2091+
tenantConfig: tenantConfig
2092+
});
2093+
20542094
tempApp = initializeApp(
20552095
{
20562096
apiKey: config.apiKey,
@@ -2391,6 +2431,9 @@ function initApp() {
23912431
$('#enroll-mfa-totp-finalize').click(onFinalizeEnrollWithTotpMultiFactor);
23922432
// Sets tenant for the current auth instance
23932433
$('#set-tenant-btn').click(onSetTenantIdClick);
2434+
2435+
// Performs Exchange Token
2436+
$('#exchange-token').click(onExchangeToken);
23942437
}
23952438

23962439
$(initApp);

packages/auth/src/api/authentication/exchange_token.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ describe('api/authentication/exchange_token', () => {
3737
let regionalAuth: TestAuth;
3838
const request = {
3939
parent: 'test-parent',
40-
token: 'custom-token'
40+
// eslint-disable-next-line camelcase
41+
id_token: 'custom-token'
4142
};
4243

4344
beforeEach(async () => {
@@ -52,6 +53,7 @@ describe('api/authentication/exchange_token', () => {
5253
const mock = mockRegionalEndpointWithParent(
5354
RegionalEndpoint.EXCHANGE_TOKEN,
5455
'test-parent',
56+
'test-api-key',
5557
{ accessToken: 'outbound-token', expiresIn: '1000' }
5658
);
5759

@@ -60,7 +62,8 @@ describe('api/authentication/exchange_token', () => {
6062
expect(response.expiresIn).equal('1000');
6163
expect(mock.calls[0].request).to.eql({
6264
parent: 'test-parent',
63-
token: 'custom-token'
65+
// eslint-disable-next-line camelcase
66+
id_token: 'custom-token'
6467
});
6568
expect(mock.calls[0].method).to.eq('POST');
6669
expect(mock.calls[0].headers!.get(HttpHeader.CONTENT_TYPE)).to.eq(
@@ -79,6 +82,7 @@ describe('api/authentication/exchange_token', () => {
7982
const mock = mockRegionalEndpointWithParent(
8083
RegionalEndpoint.EXCHANGE_TOKEN,
8184
'test-parent',
85+
'test-api-key',
8286
{
8387
error: {
8488
code: 400,

packages/auth/src/api/authentication/exchange_token.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { Auth } from '../../model/public_types';
2323

2424
export interface ExchangeTokenRequest {
2525
parent: string;
26-
token: string;
26+
id_token: string;
2727
}
2828

2929
export interface ExchangeTokenResponse {

packages/auth/src/api/index.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@ describe('api/_performRegionalApiRequest', () => {
652652
const mock = mockRegionalEndpointWithParent(
653653
RegionalEndpoint.EXCHANGE_TOKEN,
654654
'test-parent',
655+
'test-api-key',
655656
serverResponse
656657
);
657658
const response = await _performRegionalApiRequest<
@@ -689,6 +690,7 @@ describe('api/_performRegionalApiRequest', () => {
689690
const mock = mockRegionalEndpointWithParent(
690691
RegionalEndpoint.EXCHANGE_TOKEN,
691692
'test-parent',
693+
'test-api-key',
692694
serverResponse
693695
);
694696
await _performRegionalApiRequest<typeof request, typeof serverResponse>(

packages/auth/src/api/index.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,10 @@ async function performApiRequest<T, V>(
168168
}
169169
}
170170

171-
let queryParamString: string;
172-
if (isRegionalAuthInitialized(auth)) {
173-
queryParamString = querystring({
174-
...params
175-
}).slice(1);
176-
} else {
177-
queryParamString = querystring({
178-
key: auth.config.apiKey,
179-
...params
180-
}).slice(1);
181-
}
171+
const queryParamString = querystring({
172+
key: auth.config.apiKey,
173+
...params
174+
}).slice(1);
182175

183176
const headers = await (auth as AuthInternal)._getAdditionalHeaders();
184177
headers[HttpHeader.CONTENT_TYPE] = 'application/json';

packages/auth/src/core/auth/auth_impl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const enum DefaultConfig {
9595
API_HOST = 'identitytoolkit.googleapis.com',
9696
API_SCHEME = 'https',
9797
// TODO(sammansi): Update the endpoint before BYO-CIAM Private Preview Release.
98-
REGIONAL_API_HOST = 'identityplatform.googleapis.com/v2alpha/'
98+
REGIONAL_API_HOST = 'autopush-identityplatform.sandbox.googleapis.com/v2alpha/'
9999
}
100100

101101
export class AuthImpl implements AuthInternal, _FirebaseService {

packages/auth/src/core/strategies/exchange_token.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ describe('core/strategies/exchangeToken', () => {
5252
const mock = mockRegionalEndpointWithParent(
5353
RegionalEndpoint.EXCHANGE_TOKEN,
5454
'projects/test-project-id/locations/us/tenants/tenant-1/idpConfigs/idp-config',
55+
'test-api-key',
5556
{ accessToken: 'outbound-token', expiresIn: 10 }
5657
);
5758

@@ -64,7 +65,8 @@ describe('core/strategies/exchangeToken', () => {
6465
expect(mock.calls[0].request).to.eql({
6566
parent:
6667
'projects/test-project-id/locations/us/tenants/tenant-1/idpConfigs/idp-config',
67-
token: 'custom-token'
68+
// eslint-disable-next-line camelcase
69+
id_token: 'custom-token'
6870
});
6971
expect(mock.calls[0].method).to.eq('POST');
7072
expect(mock.calls[0].headers!.get(HttpHeader.CONTENT_TYPE)).to.eq(
@@ -87,6 +89,7 @@ describe('core/strategies/exchangeToken', () => {
8789
const mock = mockRegionalEndpointWithParent(
8890
RegionalEndpoint.EXCHANGE_TOKEN,
8991
'projects/test-project-id/locations/us/tenants/tenant-1/idpConfigs/idp-config',
92+
'test-api-key',
9093
{
9194
error: {
9295
code: 400,
@@ -107,7 +110,8 @@ describe('core/strategies/exchangeToken', () => {
107110
expect(mock.calls[0].request).to.eql({
108111
parent:
109112
'projects/test-project-id/locations/us/tenants/tenant-1/idpConfigs/idp-config',
110-
token: 'custom-token'
113+
// eslint-disable-next-line camelcase
114+
id_token: 'custom-token'
111115
});
112116
expect(mock.calls[0].method).to.eq('POST');
113117
expect(mock.calls[0].headers!.get(HttpHeader.CONTENT_TYPE)).to.eq(

packages/auth/src/core/strategies/exhange_token.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export async function exchangeToken(
5252
const authInternal = _castAuth(auth);
5353
const token = await getToken(authInternal, {
5454
parent: buildParent(auth, idpConfigId),
55-
token: customToken
55+
// eslint-disable-next-line camelcase
56+
id_token: customToken
5657
});
5758
if (token) {
5859
await authInternal._updateFirebaseToken({

packages/auth/test/helpers/api/helper.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ export function mockEndpointWithParams(
5959
export function mockRegionalEndpointWithParent(
6060
endpoint: RegionalEndpoint,
6161
parent: string,
62+
key: string,
6263
response: object,
6364
status = 200
6465
): Route {
65-
const url = `${TEST_SCHEME}://${TEST_HOST}${parent}${endpoint}`;
66-
console.log('here ', url);
66+
let url = `${TEST_SCHEME}://${TEST_HOST}${parent}${endpoint}`;
67+
url += '?key=';
68+
url += key;
6769
return mock(url, response, status);
6870
}

0 commit comments

Comments
 (0)