CreateAuthURIRequest.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. /** @var kCreateAuthURIEndpoint
  16. @brief The "createAuthUri" endpoint.
  17. */
  18. private let kCreateAuthURIEndpoint = "createAuthUri"
  19. /** @var kProviderIDKey
  20. @brief The key for the "providerId" value in the request.
  21. */
  22. private let kProviderIDKey = "providerId"
  23. /** @var kIdentifierKey
  24. @brief The key for the "identifier" value in the request.
  25. */
  26. private let kIdentifierKey = "identifier"
  27. /** @var kContinueURIKey
  28. @brief The key for the "continueUri" value in the request.
  29. */
  30. private let kContinueURIKey = "continueUri"
  31. /** @var kOpenIDRealmKey
  32. @brief The key for the "openidRealm" value in the request.
  33. */
  34. private let kOpenIDRealmKey = "openidRealm"
  35. /** @var kClientIDKey
  36. @brief The key for the "clientId" value in the request.
  37. */
  38. private let kClientIDKey = "clientId"
  39. /** @var kContextKey
  40. @brief The key for the "context" value in the request.
  41. */
  42. private let kContextKey = "context"
  43. /** @var kAppIDKey
  44. @brief The key for the "appId" value in the request.
  45. */
  46. private let kAppIDKey = "appId"
  47. /** @var kTenantIDKey
  48. @brief The key for the tenant id value in the request.
  49. */
  50. private let kTenantIDKey = "tenantId"
  51. /** @class FIRCreateAuthURIRequest
  52. @brief Represents the parameters for the createAuthUri endpoint.
  53. @see https://developers.google.com/identity/toolkit/web/reference/relyingparty/createAuthUri
  54. */
  55. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  56. class CreateAuthURIRequest: IdentityToolkitRequest, AuthRPCRequest {
  57. typealias Response = CreateAuthURIResponse
  58. /** @property identifier
  59. @brief The email or federated ID of the user.
  60. */
  61. let identifier: String
  62. /** @property continueURI
  63. @brief The URI to which the IDP redirects the user after the federated login flow.
  64. */
  65. let continueURI: String
  66. /** @property openIDRealm
  67. @brief Optional realm for OpenID protocol. The sub string "scheme://domain:port" of the param
  68. "continueUri" is used if this is not set.
  69. */
  70. var openIDRealm: String?
  71. /** @property providerID
  72. @brief The IdP ID. For white listed IdPs it's a short domain name e.g. google.com, aol.com,
  73. live.net and yahoo.com. For other OpenID IdPs it's the OP identifier.
  74. */
  75. var providerID: String?
  76. /** @property clientID
  77. @brief The relying party OAuth client ID.
  78. */
  79. var clientID: String?
  80. /** @property context
  81. @brief The opaque value used by the client to maintain context info between the authentication
  82. request and the IDP callback.
  83. */
  84. var context: String?
  85. /** @property appID
  86. @brief The iOS client application's bundle identifier.
  87. */
  88. var appID: String?
  89. init(identifier: String, continueURI: String,
  90. requestConfiguration: AuthRequestConfiguration) {
  91. self.identifier = identifier
  92. self.continueURI = continueURI
  93. super.init(endpoint: kCreateAuthURIEndpoint, requestConfiguration: requestConfiguration)
  94. }
  95. func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  96. var postBody: [String: AnyHashable] = [
  97. kIdentifierKey: identifier,
  98. kContinueURIKey: continueURI,
  99. ]
  100. if let providerID = providerID {
  101. postBody[kProviderIDKey] = providerID
  102. }
  103. if let openIDRealm = openIDRealm {
  104. postBody[kOpenIDRealmKey] = openIDRealm
  105. }
  106. if let clientID = clientID {
  107. postBody[kClientIDKey] = clientID
  108. }
  109. if let context = context {
  110. postBody[kContextKey] = context
  111. }
  112. if let appID = appID {
  113. postBody[kAppIDKey] = appID
  114. }
  115. if let tenantID = tenantID {
  116. postBody[kTenantIDKey] = tenantID
  117. }
  118. return postBody
  119. }
  120. }