1-
21#import " RNAppAuth.h"
32#import < AppAuth/AppAuth.h>
43#import < React/RCTLog.h>
54#import < React/RCTConvert.h>
6- #import " AppDelegate .h"
5+ #import " RNAppAuthAuthorizationFlowManager .h"
76
87@implementation RNAppAuth
98
@@ -36,15 +35,15 @@ - (dispatch_queue_t)methodQueue
3635 additionalParameters: additionalParameters
3736 resolve: resolve
3837 reject: reject];
39-
38+
4039 } else {
4140 [OIDAuthorizationService discoverServiceConfigurationForIssuer: [NSURL URLWithString: issuer]
4241 completion: ^(OIDServiceConfiguration *_Nullable configuration, NSError *_Nullable error) {
4342 if (!configuration) {
4443 reject (@" RNAppAuth Error" , [error localizedDescription ], error);
4544 return ;
4645 }
47-
46+
4847 [self authorizeWithConfiguration: configuration
4948 redirectUrl: redirectUrl
5049 clientId: clientId
@@ -73,15 +72,15 @@ - (dispatch_queue_t)methodQueue
7372 if (serviceConfiguration) {
7473 OIDServiceConfiguration *configuration = [self createServiceConfiguration: serviceConfiguration];
7574 [self refreshWithConfiguration: configuration
76- redirectUrl: redirectUrl
77- clientId: clientId
78- clientSecret: clientSecret
79- refreshToken: refreshToken
80- scopes: scopes
81- additionalParameters: additionalParameters
82- resolve: resolve
83- reject: reject];
84-
75+ redirectUrl: redirectUrl
76+ clientId: clientId
77+ clientSecret: clientSecret
78+ refreshToken: refreshToken
79+ scopes: scopes
80+ additionalParameters: additionalParameters
81+ resolve: resolve
82+ reject: reject];
83+
8584 } else {
8685 // otherwise hit up the discovery endpoint
8786 [OIDAuthorizationService discoverServiceConfigurationForIssuer: [NSURL URLWithString: issuer]
@@ -111,13 +110,13 @@ - (OIDServiceConfiguration *) createServiceConfiguration: (NSDictionary *) servi
111110 NSURL *authorizationEndpoint = [NSURL URLWithString: [serviceConfiguration objectForKey: @" authorizationEndpoint" ]];
112111 NSURL *tokenEndpoint = [NSURL URLWithString: [serviceConfiguration objectForKey: @" tokenEndpoint" ]];
113112 NSURL *registrationEndpoint = [NSURL URLWithString: [serviceConfiguration objectForKey: @" registrationEndpoint" ]];
114-
113+
115114 OIDServiceConfiguration *configuration =
116115 [[OIDServiceConfiguration alloc ]
117116 initWithAuthorizationEndpoint: authorizationEndpoint
118117 tokenEndpoint: tokenEndpoint
119118 registrationEndpoint: registrationEndpoint];
120-
119+
121120 return configuration;
122121}
123122
@@ -142,12 +141,11 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
142141 redirectURL: [NSURL URLWithString: redirectUrl]
143142 responseType: OIDResponseTypeCode
144143 additionalParameters: additionalParameters];
145-
146-
144+
147145 // performs authentication request
148- AppDelegate * appDelegate = (AppDelegate * )[UIApplication sharedApplication ].delegate ;
149-
150- appDelegate. currentAuthorizationFlow =
146+ id <UIApplicationDelegate, RNAppAuthAuthorizationFlowManager> appDelegate = (id <UIApplicationDelegate, RNAppAuthAuthorizationFlowManager> )[UIApplication sharedApplication ].delegate ;
147+
148+ id <OIDAuthorizationFlowSession> currentSession =
151149 [OIDAuthState authStateByPresentingAuthorizationRequest: request
152150 presentingViewController: appDelegate.window.rootViewController
153151 callback: ^(OIDAuthState *_Nullable authState,
@@ -157,8 +155,15 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
157155 } else {
158156 reject (@" RNAppAuth Error" , [error localizedDescription ], error);
159157 }
160-
158+
161159 }]; // end [OIDAuthState authStateByPresentingAuthorizationRequest:request
160+ if ([[appDelegate class ] conformsToProtocol: @protocol (RNAppAuthAuthorizationFlowManager)]
161+ && [appDelegate respondsToSelector: @selector (setCurrentAuthorizationFlowSession: )]) {
162+ [appDelegate setCurrentAuthorizationFlowSession: currentSession];
163+ } else {
164+ [NSException raise: @" RNAppAuth Missing protocol conformance"
165+ format: @" %@ does not conform to RNAppAuthAuthorizationFlowManager" , appDelegate];
166+ }
162167}
163168
164169
@@ -174,7 +179,7 @@ - (void)refreshWithConfiguration: (OIDServiceConfiguration *)configuration
174179 additionalParameters : (NSDictionary *_Nullable) additionalParameters
175180 resolve : (RCTPromiseResolveBlock) resolve
176181 reject : (RCTPromiseRejectBlock) reject {
177-
182+
178183 OIDTokenRequest *tokenRefreshRequest =
179184 [[OIDTokenRequest alloc ] initWithConfiguration: configuration
180185 grantType: @" refresh_token"
@@ -186,7 +191,7 @@ - (void)refreshWithConfiguration: (OIDServiceConfiguration *)configuration
186191 refreshToken: refreshToken
187192 codeVerifier: nil
188193 additionalParameters: additionalParameters];
189-
194+
190195 [OIDAuthorizationService performTokenRequest: tokenRefreshRequest
191196 callback: ^(OIDTokenResponse *_Nullable response,
192197 NSError *_Nullable error) {
@@ -196,7 +201,7 @@ - (void)refreshWithConfiguration: (OIDServiceConfiguration *)configuration
196201 reject (@" RNAppAuth Error" , [error localizedDescription ], error);
197202 }
198203 }];
199-
204+
200205}
201206
202207/*
@@ -207,7 +212,7 @@ - (NSDictionary*)formatResponse: (OIDTokenResponse*) response {
207212 dateFormat.timeZone = [NSTimeZone timeZoneWithAbbreviation: @" UTC" ];
208213 [dateFormat setLocale: [NSLocale localeWithLocaleIdentifier: @" en_US_POSIX" ]];
209214 [dateFormat setDateFormat: @" yyyy-MM-dd'T'HH:mm:ss'Z'" ];
210-
215+
211216 return @{@" accessToken" : response.accessToken ? response.accessToken : @" " ,
212217 @" accessTokenExpirationDate" : response.accessTokenExpirationDate ? [dateFormat stringFromDate: response.accessTokenExpirationDate] : @" " ,
213218 @" additionalParameters" : response.additionalParameters ,
0 commit comments