-
Notifications
You must be signed in to change notification settings - Fork 943
Implement exchangeToken public api #9039
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
54db430
Throw Operation Not Allowed for Invalid Auth Endpoint (#9013) (#9019)
mansisampat 251353f
Throw Operation Not Allowed for Invalid Auth Endpoint (#9013) (#9019)
mansisampat 8690a01
Implement exchangeToken public api
mansisampat 8965130
Using expiry_in returned by backend
mansisampat 978f78f
Addressing review comments and adding some Unit test
mansisampat d9ef125
Docgen
mansisampat bf98477
Addressing review comments and adding some Unit test
mansisampat 395494a
Typo
mansisampat c20bb3f
yarn docgen
mansisampat b8c9e6d
Fixing typo
mansisampat 14bb1cd
yarn run format
mansisampat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
packages/auth/src/api/authentication/exchange_token.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* @license | ||
* Copyright 2025 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. | ||
*/ | ||
|
||
import { expect, use } from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
|
||
import { | ||
regionalTestAuth, | ||
testAuth, | ||
TestAuth | ||
} from '../../../test/helpers/mock_auth'; | ||
import * as mockFetch from '../../../test/helpers/mock_fetch'; | ||
import { mockRegionalEndpointWithParent } from '../../../test/helpers/api/helper'; | ||
import { exchangeToken } from './exchange_token'; | ||
import { HttpHeader, RegionalEndpoint } from '..'; | ||
import { FirebaseError } from '@firebase/util'; | ||
import { ServerError } from '../errors'; | ||
|
||
use(chaiAsPromised); | ||
|
||
describe('api/authentication/exchange_token', () => { | ||
let auth: TestAuth; | ||
let regionalAuth: TestAuth; | ||
const request = { | ||
parent: 'test-parent', | ||
token: 'custom-token' | ||
}; | ||
|
||
beforeEach(async () => { | ||
auth = await testAuth(); | ||
regionalAuth = await regionalTestAuth(); | ||
mockFetch.setUp(); | ||
}); | ||
|
||
afterEach(mockFetch.tearDown); | ||
|
||
it('returns accesss token for Regional Auth', async () => { | ||
const mock = mockRegionalEndpointWithParent( | ||
RegionalEndpoint.EXCHANGE_TOKEN, | ||
'test-parent', | ||
{ accessToken: 'outbound-token', expiresIn: '1000' } | ||
); | ||
|
||
const response = await exchangeToken(regionalAuth, request); | ||
expect(response.accessToken).equal('outbound-token'); | ||
expect(response.expiresIn).equal('1000'); | ||
expect(mock.calls[0].request).to.eql({ | ||
parent: 'test-parent', | ||
token: 'custom-token' | ||
}); | ||
expect(mock.calls[0].method).to.eq('POST'); | ||
expect(mock.calls[0].headers!.get(HttpHeader.CONTENT_TYPE)).to.eq( | ||
'application/json' | ||
); | ||
}); | ||
|
||
it('throws exception for default Auth', async () => { | ||
await expect(exchangeToken(auth, request)).to.be.rejectedWith( | ||
FirebaseError, | ||
'Firebase: Operations not allowed for the auth object initialized. (auth/operation-not-allowed).' | ||
); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
const mock = mockRegionalEndpointWithParent( | ||
RegionalEndpoint.EXCHANGE_TOKEN, | ||
'test-parent', | ||
{ | ||
error: { | ||
code: 400, | ||
message: ServerError.INVALID_CUSTOM_TOKEN, | ||
errors: [ | ||
{ | ||
message: ServerError.INVALID_CUSTOM_TOKEN | ||
} | ||
] | ||
} | ||
}, | ||
400 | ||
); | ||
|
||
await expect(exchangeToken(regionalAuth, request)).to.be.rejectedWith( | ||
FirebaseError, | ||
'(auth/invalid-custom-token).' | ||
); | ||
expect(mock.calls[0].request).to.eql(request); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* @license | ||
* Copyright 2025 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. | ||
*/ | ||
import { | ||
RegionalEndpoint, | ||
HttpMethod, | ||
_performRegionalApiRequest | ||
} from '../index'; | ||
import { Auth } from '../../model/public_types'; | ||
|
||
export interface ExchangeTokenRequest { | ||
parent: string; | ||
token: string; | ||
} | ||
|
||
export interface ExchangeTokenResponse { | ||
accessToken: string; | ||
expiresIn?: string; | ||
} | ||
|
||
export async function exchangeToken( | ||
auth: Auth, | ||
request: ExchangeTokenRequest | ||
): Promise<ExchangeTokenResponse> { | ||
return _performRegionalApiRequest< | ||
ExchangeTokenRequest, | ||
ExchangeTokenResponse | ||
>( | ||
auth, | ||
HttpMethod.POST, | ||
RegionalEndpoint.EXCHANGE_TOKEN, | ||
request, | ||
{}, | ||
request.parent | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.