CreateAuthURIRequest.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. /// The "createAuthUri" endpoint.
  16. private let kCreateAuthURIEndpoint = "createAuthUri"
  17. /// The key for the "providerId" value in the request.
  18. private let kProviderIDKey = "providerId"
  19. /// The key for the "identifier" value in the request.
  20. private let kIdentifierKey = "identifier"
  21. /// The key for the "continueUri" value in the request.
  22. private let kContinueURIKey = "continueUri"
  23. /// The key for the "openidRealm" value in the request.
  24. private let kOpenIDRealmKey = "openidRealm"
  25. /// The key for the "clientId" value in the request.
  26. private let kClientIDKey = "clientId"
  27. /// The key for the "context" value in the request.
  28. private let kContextKey = "context"
  29. /// The key for the "appId" value in the request.
  30. private let kAppIDKey = "appId"
  31. /// The key for the tenant id value in the request.
  32. private let kTenantIDKey = "tenantId"
  33. /// Represents the parameters for the createAuthUri endpoint.
  34. /// See https://developers.google.com/identity/toolkit/web/reference/relyingparty/createAuthUri
  35. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  36. class CreateAuthURIRequest: IdentityToolkitRequest, AuthRPCRequest,
  37. @unchecked Sendable /* TODO: sendable */ {
  38. typealias Response = CreateAuthURIResponse
  39. /// The email or federated ID of the user.
  40. let identifier: String
  41. /// The URI to which the IDP redirects the user after the federated login flow.
  42. private let continueURI: String
  43. /// Optional realm for OpenID protocol. The sub string "scheme://domain:port" of the param
  44. /// "continueUri" is used if this is not set.
  45. private let openIDRealm: String? = nil
  46. /// The IdP ID. For white listed IdPs it's a short domain name e.g. google.com, aol.com,
  47. /// live.net and yahoo.com. For other OpenID IdPs it's the OP identifier.
  48. private let providerID: String? = nil
  49. /// The relying party OAuth client ID.
  50. private let clientID: String? = nil
  51. /// The opaque value used by the client to maintain context info between the authentication
  52. /// request and the IDP callback.
  53. private let context: String? = nil
  54. /// The iOS client application's bundle identifier.
  55. private let appID: String? = nil
  56. init(identifier: String, continueURI: String,
  57. requestConfiguration: AuthRequestConfiguration) {
  58. self.identifier = identifier
  59. self.continueURI = continueURI
  60. super.init(endpoint: kCreateAuthURIEndpoint, requestConfiguration: requestConfiguration)
  61. }
  62. var unencodedHTTPRequestBody: [String: AnyHashable]? {
  63. var postBody: [String: AnyHashable] = [
  64. kIdentifierKey: identifier,
  65. kContinueURIKey: continueURI,
  66. ]
  67. if let providerID = providerID {
  68. postBody[kProviderIDKey] = providerID
  69. }
  70. if let openIDRealm = openIDRealm {
  71. postBody[kOpenIDRealmKey] = openIDRealm
  72. }
  73. if let clientID = clientID {
  74. postBody[kClientIDKey] = clientID
  75. }
  76. if let context = context {
  77. postBody[kContextKey] = context
  78. }
  79. if let appID = appID {
  80. postBody[kAppIDKey] = appID
  81. }
  82. if let tenantID = tenantID {
  83. postBody[kTenantIDKey] = tenantID
  84. }
  85. return postBody
  86. }
  87. }