FIRGameCenterAuthProvider.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, tvOS 13.4.8, *)) {
  50. [localPlayer fetchItemsForIdentityVerificationSignature:^(
  51. NSURL *_Nullable publicKeyURL, NSData *_Nullable signature,
  52. NSData *_Nullable salt, uint64_t timestamp, NSError *_Nullable error) {
  53. if (error) {
  54. if (completion) {
  55. completion(nil, error);
  56. }
  57. } else {
  58. FIRGameCenterAuthCredential *credential =
  59. [[FIRGameCenterAuthCredential alloc] initWithPlayerID:localPlayer.playerID
  60. teamPlayerID:localPlayer.teamPlayerID
  61. gamePlayerID:localPlayer.gamePlayerID
  62. publicKeyURL:publicKeyURL
  63. signature:signature
  64. salt:salt
  65. timestamp:timestamp
  66. displayName:localPlayer.displayName];
  67. completion(credential, nil);
  68. }
  69. }];
  70. } else {
  71. [localPlayer generateIdentityVerificationSignatureWithCompletionHandler:^(
  72. NSURL *publicKeyURL, NSData *signature, NSData *salt, uint64_t timestamp,
  73. NSError *error) {
  74. if (error) {
  75. if (completion) {
  76. completion(nil, error);
  77. }
  78. } else {
  79. if (completion) {
  80. /**
  81. @c `localPlayer.alias` is actually the displayname needed, instead of
  82. `localPlayer.displayname`. For more information, check
  83. https://developer.apple.com/documentation/gamekit/gkplayer
  84. **/
  85. NSString *displayName = localPlayer.alias;
  86. // iOS 13 deprecation
  87. #pragma clang diagnostic push
  88. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  89. FIRGameCenterAuthCredential *credential =
  90. [[FIRGameCenterAuthCredential alloc] initWithPlayerID:localPlayer.playerID
  91. teamPlayerID:nil
  92. gamePlayerID:nil
  93. publicKeyURL:publicKeyURL
  94. signature:signature
  95. salt:salt
  96. timestamp:timestamp
  97. displayName:displayName];
  98. #pragma clang diagnostic pop
  99. completion(credential, nil);
  100. }
  101. }
  102. }];
  103. }
  104. }
  105. @end
  106. NS_ASSUME_NONNULL_END