GIDAuthorizationResponseHandler.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright 2024 Google LLC
  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 "GoogleSignIn/Sources/GIDAuthorizationResponse/Implementations/GIDAuthorizationResponseHandler.h"
  17. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
  18. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h"
  19. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDVerifyAccountDetail.h"
  20. #import "GoogleSignIn/Sources/GIDAuthorizationResponse/GIDAuthorizationResponseHelper.h"
  21. #import "GoogleSignIn/Sources/GIDAuthFlow.h"
  22. #import "GoogleSignIn/Sources/GIDSignInConstants.h"
  23. #import "GoogleSignIn/Sources/GIDEMMErrorHandler.h"
  24. #import "GoogleSignIn/Sources/GIDEMMSupport.h"
  25. #import "GoogleSignIn/Sources/GIDSignInPreferences.h"
  26. #ifdef SWIFT_PACKAGE
  27. @import AppAuth;
  28. #else
  29. #import <AppAuth/OIDAuthState.h>
  30. #import <AppAuth/OIDAuthorizationResponse.h>
  31. #import <AppAuth/OIDAuthorizationService.h>
  32. #import <AppAuth/OIDError.h>
  33. #import <AppAuth/OIDTokenRequest.h>
  34. #import <AppAuth/OIDTokenResponse.h>
  35. #endif
  36. @interface GIDAuthorizationResponseHandler ()
  37. /// The authorization response to process.
  38. @property(nonatomic, nullable) OIDAuthorizationResponse *authorizationResponse;
  39. /// The EMM support version.
  40. @property(nonatomic, nullable) NSString *emmSupport;
  41. /// The name of the current flow.
  42. @property(nonatomic) GIDFlowName flowName;
  43. /// The configuration for the current flow.
  44. @property(nonatomic, nullable) GIDConfiguration *configuration;
  45. /// The configuration for the current flow.
  46. @property(nonatomic, nullable) NSError *error;
  47. @end
  48. @implementation GIDAuthorizationResponseHandler
  49. - (instancetype)
  50. initWithAuthorizationResponse:(nullable OIDAuthorizationResponse *)authorizationResponse
  51. emmSupport:(nullable NSString *)emmSupport
  52. flowName:(GIDFlowName)flowName
  53. configuration:(nullable GIDConfiguration *)configuration
  54. error:(nullable NSError *)error {
  55. self = [super init];
  56. if (self) {
  57. _authorizationResponse = authorizationResponse;
  58. _emmSupport = emmSupport;
  59. _flowName = flowName;
  60. _configuration = configuration;
  61. _error = error;
  62. }
  63. return self;
  64. }
  65. - (GIDAuthFlow *)generateAuthFlowFromAuthorizationResponse {
  66. GIDAuthFlow *authFlow = [[GIDAuthFlow alloc] initWithAuthState:nil
  67. error:nil
  68. emmSupport:_emmSupport
  69. profileData:nil];
  70. if (_authorizationResponse) {
  71. if (_authorizationResponse.authorizationCode.length) {
  72. authFlow.authState =
  73. [[OIDAuthState alloc] initWithAuthorizationResponse:_authorizationResponse];
  74. [self maybeFetchToken:authFlow];
  75. } else {
  76. [self authorizationCodeErrorToAuthFlow:authFlow];
  77. }
  78. } else {
  79. [self authorizationResponseErrorToAuthFlow:authFlow error:_error];
  80. }
  81. return authFlow;
  82. }
  83. - (void)maybeFetchToken:(GIDAuthFlow *)authFlow {
  84. OIDAuthState *authState = authFlow.authState;
  85. // Do nothing if we have an auth flow error or a restored access token that isn't near expiration.
  86. if (authFlow.error ||
  87. (authState.lastTokenResponse.accessToken &&
  88. [authState.lastTokenResponse.accessTokenExpirationDate timeIntervalSinceNow] >
  89. kMinimumRestoredAccessTokenTimeToExpire)) {
  90. return;
  91. }
  92. NSMutableDictionary<NSString *, NSString *> *additionalParameters = [@{} mutableCopy];
  93. if (_configuration.serverClientID) {
  94. additionalParameters[kAudienceParameter] = _configuration.serverClientID;
  95. }
  96. if (_configuration.openIDRealm) {
  97. additionalParameters[kOpenIDRealmParameter] = _configuration.openIDRealm;
  98. }
  99. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  100. if (_flowName == GIDFlowNameSignIn) {
  101. NSDictionary<NSString *, NSObject *> *params =
  102. authState.lastAuthorizationResponse.additionalParameters;
  103. NSString *passcodeInfoRequired = (NSString *)params[kEMMPasscodeInfoRequiredKeyName];
  104. [additionalParameters addEntriesFromDictionary:
  105. [GIDEMMSupport parametersWithParameters:@{}
  106. emmSupport:authFlow.emmSupport
  107. isPasscodeInfoRequired:passcodeInfoRequired.length > 0]];
  108. }
  109. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  110. additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
  111. additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();
  112. OIDTokenRequest *tokenRequest;
  113. if (!authState.lastTokenResponse.accessToken &&
  114. authState.lastAuthorizationResponse.authorizationCode) {
  115. tokenRequest = [authState.lastAuthorizationResponse
  116. tokenExchangeRequestWithAdditionalParameters:additionalParameters];
  117. } else {
  118. [additionalParameters
  119. addEntriesFromDictionary:authState.lastTokenResponse.request.additionalParameters];
  120. tokenRequest = [authState tokenRefreshRequestWithAdditionalParameters:additionalParameters];
  121. }
  122. // TODO: Clean up callback flow (#427).
  123. [authFlow wait];
  124. [OIDAuthorizationService performTokenRequest:tokenRequest
  125. originalAuthorizationResponse:authFlow.authState.lastAuthorizationResponse
  126. callback:^(OIDTokenResponse *_Nullable tokenResponse,
  127. NSError *_Nullable error) {
  128. [authState updateWithTokenResponse:tokenResponse error:error];
  129. authFlow.error = error;
  130. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  131. if (authFlow.emmSupport) {
  132. [GIDEMMSupport handleTokenFetchEMMError:error completion:^(NSError *error) {
  133. authFlow.error = error;
  134. [authFlow next];
  135. }];
  136. } else {
  137. [authFlow next];
  138. }
  139. #elif TARGET_OS_OSX || TARGET_OS_MACCATALYST
  140. [authFlow next];
  141. #endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST
  142. }];
  143. }
  144. - (void)authorizationCodeErrorToAuthFlow:(GIDAuthFlow *)authFlow {
  145. NSDictionary<NSString *, NSObject *> *params = _authorizationResponse.additionalParameters;
  146. NSString *errorString = (NSString *)params[kOAuth2ErrorKeyName];
  147. switch (_flowName) {
  148. case GIDFlowNameSignIn: {
  149. GIDSignInErrorCode errorCode = kGIDSignInErrorCodeUnknown;
  150. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  151. if (authFlow.emmSupport) {
  152. [authFlow wait];
  153. BOOL isEMMError = [[GIDEMMErrorHandler sharedInstance]
  154. handleErrorFromResponse:params
  155. completion:^{
  156. [authFlow next];
  157. }];
  158. if (isEMMError) {
  159. errorCode = kGIDSignInErrorCodeEMM;
  160. }
  161. }
  162. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  163. if ([errorString isEqualToString:kOAuth2AccessDenied]) {
  164. errorCode = kGIDSignInErrorCodeCanceled;
  165. }
  166. authFlow.error = [self errorWithString:errorString code:errorCode];
  167. }
  168. break;
  169. case GIDFlowNameVerifyAccountDetail: {
  170. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  171. GIDVerifyErrorCode errorCode = GIDVerifyErrorCodeUnknown;
  172. if ([errorString isEqualToString:kOAuth2AccessDenied]) {
  173. errorCode = GIDVerifyErrorCodeCanceled;
  174. }
  175. authFlow.error = [self errorWithString:errorString code:errorCode];
  176. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  177. break;
  178. }
  179. }
  180. }
  181. - (void)authorizationResponseErrorToAuthFlow:(GIDAuthFlow *)authFlow
  182. error:(NSError *)error {
  183. NSString *errorString = [error localizedDescription];
  184. switch (_flowName) {
  185. case GIDFlowNameSignIn: {
  186. GIDSignInErrorCode errorCode = kGIDSignInErrorCodeUnknown;
  187. if (error.code == OIDErrorCodeUserCanceledAuthorizationFlow ||
  188. error.code == OIDErrorCodeProgramCanceledAuthorizationFlow) {
  189. // The user has canceled the flow at the iOS modal dialog.
  190. errorString = kUserCanceledSignInError;
  191. errorCode = kGIDSignInErrorCodeCanceled;
  192. }
  193. authFlow.error = [self errorWithString:errorString code:errorCode];
  194. break;
  195. }
  196. case GIDFlowNameVerifyAccountDetail: {
  197. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  198. GIDVerifyErrorCode errorCode = GIDVerifyErrorCodeUnknown;
  199. if (error.code == OIDErrorCodeUserCanceledAuthorizationFlow) {
  200. errorString = kUserCanceledVerifyError;
  201. errorCode = GIDVerifyErrorCodeCanceled;
  202. }
  203. authFlow.error = [self errorWithString:errorString code:errorCode];
  204. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  205. break;
  206. }
  207. }
  208. }
  209. - (NSError *)errorWithString:(nullable NSString *)errorString code:(NSInteger)code {
  210. if (errorString == nil) {
  211. errorString = @"Unknown error";
  212. }
  213. if (!_flowName) {
  214. errorString = @"No specified flow";
  215. }
  216. NSDictionary<NSString *, NSString *> *errorDict = @{ NSLocalizedDescriptionKey : errorString };
  217. switch (_flowName) {
  218. case GIDFlowNameSignIn:
  219. return [NSError errorWithDomain:kGIDSignInErrorDomain
  220. code:code
  221. userInfo:errorDict];
  222. break;
  223. case GIDFlowNameVerifyAccountDetail:
  224. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  225. return [NSError errorWithDomain:kGIDVerifyErrorDomain
  226. code:code
  227. userInfo:errorDict];
  228. break;
  229. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  230. default:
  231. break;
  232. }
  233. return [NSError errorWithDomain:kGIDSignInErrorDomain
  234. code:code
  235. userInfo:errorDict];
  236. }
  237. @end