SignInWithGameCenterRequest.swift 3.5 KB

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