GIDSaveAuthOperation.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // GIDSaveAuthOperation.m
  3. // GoogleSignIn
  4. //
  5. // Created by Matt Mathias on 4/3/25.
  6. //
  7. #import "GIDSaveAuthOperation.h"
  8. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h"
  9. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDGoogleUser.h"
  10. #import "GoogleSignIn/Sources/GIDAuthorizationFlow/Implementations/Operations/GIDDecodeIDTokenOperation.h"
  11. #import "GoogleSignIn/Sources/GIDGoogleUser_Private.h"
  12. #import "GoogleSignIn/Sources/GIDSignInConstants.h"
  13. #import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
  14. #ifdef SWIFT_PACKAGE
  15. @import AppAuth;
  16. @import GTMAppAuth;
  17. #else
  18. #import <AppAuth/OIDAuthState.h>
  19. #import <GTMAppAuth/GTMAuthSession>
  20. #endif
  21. @interface GIDSaveAuthOperation ()
  22. @property(nonatomic, readwrite, nullable) NSError *error;
  23. @property(nonatomic, readwrite, nullable) GIDGoogleUser *currentUser;
  24. @end
  25. @implementation GIDSaveAuthOperation
  26. - (void)main {
  27. GIDDecodeIDTokenOperation *idToken = (GIDDecodeIDTokenOperation *)self.dependencies.firstObject;
  28. NSError *error = idToken.error;
  29. OIDAuthState *authState = idToken.authState;
  30. if (authState && !error) {
  31. if (![self saveAuthState:authState]) {
  32. self.error = [self errorWithString:kKeychainError code:kGIDSignInErrorCodeKeychain];
  33. return;
  34. }
  35. GIDProfileData *profileData = idToken.profileData;
  36. if (self.options.addScopesFlow) {
  37. [self.currentUser updateWithTokenResponse:authState.lastTokenResponse
  38. authorizationResponse:authState.lastAuthorizationResponse
  39. profileData:profileData];
  40. } else {
  41. GIDGoogleUser *user = [[GIDGoogleUser alloc] initWithAuthState:authState
  42. profileData:profileData];
  43. self.currentUser = user;
  44. }
  45. }
  46. }
  47. - (BOOL)saveAuthState:(OIDAuthState *)authState {
  48. GTMAuthSession *authorization = [[GTMAuthSession alloc] initWithAuthState:authState];
  49. NSError *error;
  50. GTMKeychainStore *keychainStore =
  51. [[GTMKeychainStore alloc] initWithItemName:kGTMAppAuthKeychainName];
  52. [keychainStore saveAuthSession:authorization error:&error];
  53. return error == nil;
  54. }
  55. // TODO: Extract this to an error class
  56. - (NSError *)errorWithString:(NSString *)errorString code:(GIDSignInErrorCode)code {
  57. if (errorString == nil) {
  58. errorString = @"Unknown error";
  59. }
  60. NSDictionary<NSString *, NSString *> *errorDict = @{ NSLocalizedDescriptionKey : errorString };
  61. return [NSError errorWithDomain:kGIDSignInErrorDomain
  62. code:code
  63. userInfo:errorDict];
  64. }
  65. @end