CreateAuthURIRequest.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. typealias Response = CreateAuthURIResponse
  38. /// The email or federated ID of the user.
  39. let identifier: String
  40. /// The URI to which the IDP redirects the user after the federated login flow.
  41. let continueURI: String
  42. /// Optional realm for OpenID protocol. The sub string "scheme://domain:port" of the param
  43. /// "continueUri" is used if this is not set.
  44. var openIDRealm: String?
  45. /// The IdP ID. For white listed IdPs it's a short domain name e.g. google.com, aol.com,
  46. /// live.net and yahoo.com. For other OpenID IdPs it's the OP identifier.
  47. var providerID: String?
  48. /// The relying party OAuth client ID.
  49. var clientID: String?
  50. /// The opaque value used by the client to maintain context info between the authentication
  51. /// request and the IDP callback.
  52. var context: String?
  53. /// The iOS client application's bundle identifier.
  54. var appID: String?
  55. init(identifier: String, continueURI: String,
  56. requestConfiguration: AuthRequestConfiguration) {
  57. self.identifier = identifier
  58. self.continueURI = continueURI
  59. super.init(endpoint: kCreateAuthURIEndpoint, requestConfiguration: requestConfiguration)
  60. }
  61. func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  62. var postBody: [String: AnyHashable] = [
  63. kIdentifierKey: identifier,
  64. kContinueURIKey: continueURI,
  65. ]
  66. if let providerID = providerID {
  67. postBody[kProviderIDKey] = providerID
  68. }
  69. if let openIDRealm = openIDRealm {
  70. postBody[kOpenIDRealmKey] = openIDRealm
  71. }
  72. if let clientID = clientID {
  73. postBody[kClientIDKey] = clientID
  74. }
  75. if let context = context {
  76. postBody[kContextKey] = context
  77. }
  78. if let appID = appID {
  79. postBody[kAppIDKey] = appID
  80. }
  81. if let tenantID = tenantID {
  82. postBody[kTenantIDKey] = tenantID
  83. }
  84. return postBody
  85. }
  86. }