FIRGameCenterAuthProvider.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRGameCenterAuthProvider.h"
  17. #import <GameKit/GameKit.h>
  18. #import "FirebaseAuth/Sources/AuthProvider/GameCenter/FIRGameCenterAuthCredential.h"
  19. #import "FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h"
  20. #import "FirebaseAuth/Sources/Utilities/FIRAuthExceptionUtils.h"
  21. NS_ASSUME_NONNULL_BEGIN
  22. @implementation FIRGameCenterAuthProvider
  23. - (instancetype)init {
  24. [FIRAuthExceptionUtils
  25. raiseMethodNotImplementedExceptionWithReason:@"This class is not meant to be initialized."];
  26. return nil;
  27. }
  28. + (void)getCredentialWithCompletion:(FIRGameCenterCredentialCallback)completion {
  29. /**
  30. Linking GameKit.framework without using it on macOS results in App Store rejection.
  31. Thus we don't link GameKit.framework to our SDK directly. `optionalLocalPlayer` is used for
  32. checking whether the APP that consuming our SDK has linked GameKit.framework. If not, a
  33. `GameKitNotLinkedError` will be raised.
  34. **/
  35. GKLocalPlayer *_Nullable optionalLocalPlayer = [[NSClassFromString(@"GKLocalPlayer") alloc] init];
  36. if (!optionalLocalPlayer) {
  37. if (completion) {
  38. completion(nil, [FIRAuthErrorUtils gameKitNotLinkedError]);
  39. }
  40. return;
  41. }
  42. __weak GKLocalPlayer *localPlayer = [[optionalLocalPlayer class] localPlayer];
  43. if (!localPlayer.isAuthenticated) {
  44. if (completion) {
  45. completion(nil, [FIRAuthErrorUtils localPlayerNotAuthenticatedError]);
  46. }
  47. return;
  48. }
  49. if (@available(iOS 13.5, macOS 10.15.5, macCatalyst 13.5,
  50. // Availability of fetchItemsForIdentityVerificationSignature was extended to tvOS 13.4.8 in
  51. // Xcode 14 (from tvOS 13.5 in Xcode 13).
  52. // TODO: Remove this check when we no longer support Xcode 13.
  53. #if defined(__TVOS_16_0)
  54. tvOS 13.4.8,
  55. #else
  56. tvOS 13.5,
  57. #endif
  58. *)) {
  59. [localPlayer fetchItemsForIdentityVerificationSignature:^(
  60. NSURL *_Nullable publicKeyURL, NSData *_Nullable signature,
  61. NSData *_Nullable salt, uint64_t timestamp, NSError *_Nullable error) {
  62. if (error) {
  63. if (completion) {
  64. completion(nil, error);
  65. }
  66. } else {
  67. FIRGameCenterAuthCredential *credential =
  68. [[FIRGameCenterAuthCredential alloc] initWithPlayerID:localPlayer.playerID
  69. teamPlayerID:localPlayer.teamPlayerID
  70. gamePlayerID:localPlayer.gamePlayerID
  71. publicKeyURL:publicKeyURL
  72. signature:signature
  73. salt:salt
  74. timestamp:timestamp
  75. displayName:localPlayer.displayName];
  76. completion(credential, nil);
  77. }
  78. }];
  79. } else {
  80. [localPlayer generateIdentityVerificationSignatureWithCompletionHandler:^(
  81. NSURL *publicKeyURL, NSData *signature, NSData *salt, uint64_t timestamp,
  82. NSError *error) {
  83. if (error) {
  84. if (completion) {
  85. completion(nil, error);
  86. }
  87. } else {
  88. if (completion) {
  89. /**
  90. @c `localPlayer.alias` is actually the displayname needed, instead of
  91. `localPlayer.displayname`. For more information, check
  92. https://developer.apple.com/documentation/gamekit/gkplayer
  93. **/
  94. NSString *displayName = localPlayer.alias;
  95. // iOS 13 deprecation
  96. #pragma clang diagnostic push
  97. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  98. FIRGameCenterAuthCredential *credential =
  99. [[FIRGameCenterAuthCredential alloc] initWithPlayerID:localPlayer.playerID
  100. teamPlayerID:nil
  101. gamePlayerID:nil
  102. publicKeyURL:publicKeyURL
  103. signature:signature
  104. salt:salt
  105. timestamp:timestamp
  106. displayName:displayName];
  107. #pragma clang diagnostic pop
  108. completion(credential, nil);
  109. }
  110. }
  111. }];
  112. }
  113. }
  114. @end
  115. NS_ASSUME_NONNULL_END