FIRGameCenterAuthProvider.m 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. [localPlayer generateIdentityVerificationSignatureWithCompletionHandler:^(
  50. NSURL *publicKeyURL, NSData *signature, NSData *salt, uint64_t timestamp,
  51. NSError *error) {
  52. if (error) {
  53. if (completion) {
  54. completion(nil, error);
  55. }
  56. } else {
  57. if (completion) {
  58. /**
  59. @c `localPlayer.alias` is actually the displayname needed, instead of
  60. `localPlayer.displayname`. For more information, check
  61. https://developer.apple.com/documentation/gamekit/gkplayer
  62. **/
  63. NSString *displayName = localPlayer.alias;
  64. // iOS 13 deprecation
  65. #pragma clang diagnostic push
  66. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  67. FIRGameCenterAuthCredential *credential =
  68. [[FIRGameCenterAuthCredential alloc] initWithPlayerID:localPlayer.playerID
  69. publicKeyURL:publicKeyURL
  70. signature:signature
  71. salt:salt
  72. timestamp:timestamp
  73. displayName:displayName];
  74. #pragma clang diagnostic pop
  75. completion(credential, nil);
  76. }
  77. }
  78. }];
  79. }
  80. @end
  81. NS_ASSUME_NONNULL_END