SignInWithGameCenterRequest.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. private let kSignInWithGameCenterEndPoint = "signInWithGameCenter"
  16. /** @class FIRSignInWithGameCenterRequest
  17. @brief The request to sign in with Game Center account
  18. */
  19. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  20. @objc(FIRSignInWithGameCenterRequest)
  21. public class SignInWithGameCenterRequest: IdentityToolkitRequest,
  22. AuthRPCRequest {
  23. /** @property teamPlayerID
  24. @brief The team player ID of the Game Center local player.
  25. */
  26. @objc public var teamPlayerID: String?
  27. /** @property gamePlayerID
  28. @brief The game player ID of the Game Center local player.
  29. */
  30. @objc public var gamePlayerID: String?
  31. /** @property publicKeyURL
  32. @brief The URL for the public encryption key.
  33. */
  34. @objc public var publicKeyURL: URL
  35. /** @property signature
  36. @brief The verification signature data generated by Game Center.
  37. */
  38. @objc public var signature: Data
  39. /** @property salt
  40. @brief A random strong used to compute the hash and keep it randomized.
  41. */
  42. @objc public var salt: Data
  43. /** @property timestamp
  44. @brief The date and time that the signature was created.
  45. */
  46. @objc public var timestamp: UInt64
  47. /** @property accessToken
  48. @brief The STS Access Token for the authenticated user, only needed for linking the user.
  49. */
  50. @objc public var accessToken: String?
  51. /** @property displayName
  52. @brief The display name of the local Game Center player.
  53. */
  54. @objc public var displayName: String?
  55. /** @var response
  56. @brief The corresponding response for this request
  57. */
  58. @objc public var response: AuthRPCResponse = SignInWithGameCenterResponse()
  59. /** @fn initWithPlayerID:publicKeyURL:signature:salt:timestamp:displayName:requestConfiguration:
  60. @brief Designated initializer.
  61. @param playerID The ID of the Game Center player.
  62. @param teamPlayerID The teamPlayerID of the Game Center local player.
  63. @param gamePlayerID The gamePlayerID of the Game Center local player.
  64. @param publicKeyURL The URL for the public encryption key.
  65. @param signature The verification signature generated.
  66. @param salt A random string used to compute the hash and keep it randomized.
  67. @param timestamp The date and time that the signature was created.
  68. @param displayName The display name of the Game Center player.
  69. */
  70. @objc public init(teamPlayerID: String?, gamePlayerID: String?,
  71. publicKeyURL: URL,
  72. signature: Data, salt: Data,
  73. timestamp: UInt64, displayName: String?,
  74. requestConfiguration: AuthRequestConfiguration) {
  75. self.teamPlayerID = teamPlayerID
  76. self.gamePlayerID = gamePlayerID
  77. self.publicKeyURL = publicKeyURL
  78. self.signature = signature
  79. self.salt = salt
  80. self.timestamp = timestamp
  81. self.displayName = displayName
  82. super.init(
  83. endpoint: kSignInWithGameCenterEndPoint,
  84. requestConfiguration: requestConfiguration
  85. )
  86. }
  87. public func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  88. var postBody: [String: AnyHashable] = [
  89. "playerId": "", // teamPlayerID or gamePlayerID should be used instead.
  90. "publicKeyUrl": publicKeyURL.absoluteString,
  91. "signature": signature.base64URLEncodedString(),
  92. "salt": salt.base64URLEncodedString(),
  93. ]
  94. if timestamp != 0 {
  95. postBody["timestamp"] = timestamp
  96. }
  97. if let teamPlayerID {
  98. postBody["teamPlayerId"] = teamPlayerID
  99. }
  100. if let gamePlayerID {
  101. postBody["gamePlayerId"] = gamePlayerID
  102. }
  103. if let accessToken {
  104. postBody["idToken"] = accessToken
  105. }
  106. if let displayName {
  107. postBody["displayName"] = displayName
  108. }
  109. return postBody
  110. }
  111. }