SignInWithGameCenterRequest.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. class SignInWithGameCenterRequest: IdentityToolkitRequest, AuthRPCRequest {
  21. typealias Response = SignInWithGameCenterResponse
  22. /** @property playerID
  23. @brief The playerID to verify.
  24. */
  25. var playerID: String
  26. /** @property teamPlayerID
  27. @brief The team player ID of the Game Center local player.
  28. */
  29. var teamPlayerID: String?
  30. /** @property gamePlayerID
  31. @brief The game player ID of the Game Center local player.
  32. */
  33. var gamePlayerID: String?
  34. /** @property publicKeyURL
  35. @brief The URL for the encryption key.
  36. */
  37. var publicKeyURL: URL
  38. /** @property signature
  39. @brief The verification signature data generated by Game Center.
  40. */
  41. var signature: Data
  42. /** @property salt
  43. @brief A random strong used to compute the hash and keep it randomized.
  44. */
  45. var salt: Data
  46. /** @property timestamp
  47. @brief The date and time that the signature was created.
  48. */
  49. var timestamp: UInt64
  50. /** @property accessToken
  51. @brief The STS Access Token for the authenticated user, only needed for linking the user.
  52. */
  53. var accessToken: String?
  54. /** @property displayName
  55. @brief The display name of the local Game Center player.
  56. */
  57. var displayName: String?
  58. /** @fn initWithPlayerID:publicKeyURL:signature:salt:timestamp:displayName:requestConfiguration:
  59. @brief Designated initializer.
  60. @param playerID The ID of the Game Center player.
  61. @param teamPlayerID The teamPlayerID of the Game Center local player.
  62. @param gamePlayerID The gamePlayerID of the Game Center local player.
  63. @param publicKeyURL The URL for the encryption key.
  64. @param signature The verification signature generated.
  65. @param salt A random string used to compute the hash and keep it randomized.
  66. @param timestamp The date and time that the signature was created.
  67. @param displayName The display name of the Game Center player.
  68. */
  69. init(playerID: String, teamPlayerID: String?, gamePlayerID: String?,
  70. publicKeyURL: URL,
  71. signature: Data, salt: Data,
  72. timestamp: UInt64, displayName: String?,
  73. requestConfiguration: AuthRequestConfiguration) {
  74. self.playerID = playerID
  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. func unencodedHTTPRequestBody() throws -> [String: AnyHashable] {
  88. var postBody: [String: AnyHashable] = [
  89. "playerId": playerID,
  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. }