-
Notifications
You must be signed in to change notification settings - Fork 1.6k
implementing regionalized auth and exchangeToken #14865
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 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
860fb46
implementing regionalized auth and exchangeToken
srushtisv 0fac248
addressing review comments
srushtisv 5bf9162
lint fixes
srushtisv 60940b7
error fixing
srushtisv dcdfa66
resolving git checks
srushtisv aed4e2e
resolving git checks
srushtisv d86b0d1
updating changes
srushtisv dc606c6
resolving errors
srushtisv ed886f9
lint changes
srushtisv f2703e6
refactoring identityToolkitRequest with corrected logic for r-gcip
srushtisv 05d25cc
ading more IdentityToolkitRequestTests
srushtisv f068bbb
lint fix
srushtisv 1d26493
updating from comments
srushtisv 776e17c
changing region to location in IdentityToolkitRequestTests
srushtisv 56270fc
Delete ExchangeTokenTests.swift
srushtisv b0ca282
adding check for prod-global in path
srushtisv 4ce3a4d
removing emulator endpoint
srushtisv ea9223a
sample app changes with updated logic (#14960) _final
srushtisv 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,15 +44,24 @@ final class AuthRequestConfiguration { | |
/// If set, the local emulator host and port to point to instead of the remote backend. | ||
var emulatorHostAndPort: String? | ||
|
||
/// R-GCIP region, set once during Auth init. | ||
var location: String? | ||
|
||
/// R-GCIP tenantId, set once during Auth init. | ||
var tenantId: String? | ||
|
||
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. Any reason we are setting this separately? Why can't we have tenantConfig here? |
||
init(apiKey: String, | ||
appID: String, | ||
auth: Auth? = nil, | ||
heartbeatLogger: FIRHeartbeatLoggerProtocol? = nil, | ||
appCheck: AppCheckInterop? = nil) { | ||
appCheck: AppCheckInterop? = nil, | ||
tenantConfig: TenantConfig? = nil) { | ||
self.apiKey = apiKey | ||
self.appID = appID | ||
self.auth = auth | ||
self.heartbeatLogger = heartbeatLogger | ||
self.appCheck = appCheck | ||
location = tenantConfig?.location | ||
tenantId = tenantConfig?.tenantId | ||
} | ||
} |
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
98 changes: 98 additions & 0 deletions
98
FirebaseAuth/Sources/Swift/Backend/RPC/ExchangeTokenRequest.swift
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,98 @@ | ||
// 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 Foundation | ||
|
||
private let kCustomTokenKey = "customToken" | ||
srushtisv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// A request to exchange a third-party OIDC token for a Firebase STS token. | ||
/// | ||
/// This structure encapsulates the parameters required to make an API request | ||
/// to exchange an OIDC token for a Firebase ID token. It conforms to the | ||
/// `AuthRPCRequest` protocol, providing the necessary properties and | ||
/// methods for the authentication backend to perform the request. | ||
@available(iOS 13, *) | ||
struct ExchangeTokenRequest: AuthRPCRequest { | ||
/// The type of the expected response. | ||
typealias Response = ExchangeTokenResponse | ||
|
||
/// The OIDC provider's Authorization code or Id Token to exchange. | ||
let customToken: String | ||
|
||
/// The ExternalUserDirectoryId corresponding to the OIDC custom Token. | ||
let idpConfigID: String | ||
|
||
/// The configuration for the request, holding API key, tenant, etc. | ||
let config: AuthRequestConfiguration | ||
|
||
var path: String { | ||
guard let region = config.location, | ||
let tenant = config.tenantId, | ||
let project = config.auth?.app?.options.projectID | ||
else { | ||
fatalError( | ||
"exchangeOidcToken requires `auth.location` & `auth.tenantID`" | ||
) | ||
} | ||
_ = "\(region)-identityplatform.googleapis.com" | ||
srushtisv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return "/v2alpha/projects/\(project)/locations/\(region)" + | ||
"/tenants/\(tenant)/idpConfigs/\(idpConfigID):exchangeOidcToken" | ||
srushtisv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// Initializes a new `ExchangeTokenRequest` instance. | ||
/// | ||
/// - Parameters: | ||
/// - idpConfigID: The identifier of the OIDC provider configuration. | ||
/// - idToken: The third-party OIDC token to exchange. | ||
/// - config: The configuration for the request. | ||
init(customToken: String, | ||
idpConfigID: String, | ||
config: AuthRequestConfiguration) { | ||
self.idpConfigID = idpConfigID | ||
self.customToken = customToken | ||
self.config = config | ||
} | ||
|
||
/// The unencoded HTTP request body for the API. | ||
var unencodedHTTPRequestBody: [String: AnyHashable]? { | ||
return ["custom_token": customToken] | ||
} | ||
|
||
/// Constructs the URL for the API request. | ||
/// | ||
/// - Returns: The URL for the token exchange endpoint. | ||
/// - FatalError: if location, tenantID, projectID or apiKey are missing. | ||
func requestURL() -> URL { | ||
guard let region = config.location, | ||
let tenant = config.tenantId, | ||
let project = config.auth?.app?.options.projectID | ||
else { | ||
fatalError( | ||
"exchangeOidcToken requires `auth.useIdentityPlatform`, `auth.location`, `auth.tenantID` & `projectID`" | ||
) | ||
} | ||
let host = "\(region)-identityplatform.googleapis.com" | ||
let path = "/v2/projects/$\(project)/locations/$\(region)" + | ||
"/tenants/$\(tenant)/idpConfigs/$\(idpConfigID):exchangeOidcToken" | ||
guard let url = URL(string: "https://\(host)\(path)?key=\(config.apiKey)") else { | ||
fatalError("Failed to create URL for exchangeOidcToken") | ||
} | ||
return url | ||
} | ||
|
||
/// Returns the request configuration. | ||
/// | ||
/// - Returns: The `AuthRequestConfiguration`. | ||
func requestConfiguration() -> AuthRequestConfiguration { config } | ||
} |
44 changes: 44 additions & 0 deletions
44
FirebaseAuth/Sources/Swift/Backend/RPC/ExchangeTokenResponse.swift
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,44 @@ | ||
// 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 Foundation | ||
|
||
import Foundation | ||
|
||
/// Response containing the new Firebase STS token and its expiration time in seconds. | ||
@available(iOS 13, *) | ||
struct ExchangeTokenResponse: AuthRPCResponse { | ||
/// The Firebase ID token. | ||
let firebaseToken: String | ||
|
||
/// The time interval (in *seconds*) until the token expires. | ||
let expiresIn: TimeInterval | ||
|
||
/// The expiration date of the token, calculated from `expiresInSeconds`. | ||
let expirationDate: Date | ||
|
||
/// Initializes a new ExchangeTokenResponse from a dictionary. | ||
/// | ||
/// - Parameter dictionary: The dictionary representing the JSON response from the server. | ||
/// - Throws: `AuthErrorUtils.unexpectedResponse` if the dictionary is missing required fields | ||
/// or contains invalid data. | ||
init(dictionary: [String: AnyHashable]) throws { | ||
guard let token = dictionary["idToken"] as? String else { | ||
throw AuthErrorUtils.unexpectedResponse(deserializedResponse: dictionary) | ||
} | ||
firebaseToken = token | ||
expiresIn = (dictionary["expiresIn"] as? TimeInterval) ?? 3600 | ||
expirationDate = Date().addingTimeInterval(expiresIn) | ||
} | ||
} |
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.