1
-
2
1
#import " RNAppAuth.h"
3
2
#import < AppAuth/AppAuth.h>
4
3
#import < React/RCTLog.h>
5
4
#import < React/RCTConvert.h>
6
- #import " AppDelegate .h"
5
+ #import " RNAppAuthAuthorizationFlowManager .h"
7
6
8
7
@implementation RNAppAuth
9
8
@@ -36,15 +35,15 @@ - (dispatch_queue_t)methodQueue
36
35
additionalParameters: additionalParameters
37
36
resolve: resolve
38
37
reject: reject];
39
-
38
+
40
39
} else {
41
40
[OIDAuthorizationService discoverServiceConfigurationForIssuer: [NSURL URLWithString: issuer]
42
41
completion: ^(OIDServiceConfiguration *_Nullable configuration, NSError *_Nullable error) {
43
42
if (!configuration) {
44
43
reject (@" RNAppAuth Error" , [error localizedDescription ], error);
45
44
return ;
46
45
}
47
-
46
+
48
47
[self authorizeWithConfiguration: configuration
49
48
redirectUrl: redirectUrl
50
49
clientId: clientId
@@ -73,15 +72,15 @@ - (dispatch_queue_t)methodQueue
73
72
if (serviceConfiguration) {
74
73
OIDServiceConfiguration *configuration = [self createServiceConfiguration: serviceConfiguration];
75
74
[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
+
85
84
} else {
86
85
// otherwise hit up the discovery endpoint
87
86
[OIDAuthorizationService discoverServiceConfigurationForIssuer: [NSURL URLWithString: issuer]
@@ -111,13 +110,13 @@ - (OIDServiceConfiguration *) createServiceConfiguration: (NSDictionary *) servi
111
110
NSURL *authorizationEndpoint = [NSURL URLWithString: [serviceConfiguration objectForKey: @" authorizationEndpoint" ]];
112
111
NSURL *tokenEndpoint = [NSURL URLWithString: [serviceConfiguration objectForKey: @" tokenEndpoint" ]];
113
112
NSURL *registrationEndpoint = [NSURL URLWithString: [serviceConfiguration objectForKey: @" registrationEndpoint" ]];
114
-
113
+
115
114
OIDServiceConfiguration *configuration =
116
115
[[OIDServiceConfiguration alloc ]
117
116
initWithAuthorizationEndpoint: authorizationEndpoint
118
117
tokenEndpoint: tokenEndpoint
119
118
registrationEndpoint: registrationEndpoint];
120
-
119
+
121
120
return configuration;
122
121
}
123
122
@@ -142,12 +141,11 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
142
141
redirectURL: [NSURL URLWithString: redirectUrl]
143
142
responseType: OIDResponseTypeCode
144
143
additionalParameters: additionalParameters];
145
-
146
-
144
+
147
145
// 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 =
151
149
[OIDAuthState authStateByPresentingAuthorizationRequest: request
152
150
presentingViewController: appDelegate.window.rootViewController
153
151
callback: ^(OIDAuthState *_Nullable authState,
@@ -157,8 +155,15 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
157
155
} else {
158
156
reject (@" RNAppAuth Error" , [error localizedDescription ], error);
159
157
}
160
-
158
+
161
159
}]; // 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
+ }
162
167
}
163
168
164
169
@@ -174,7 +179,7 @@ - (void)refreshWithConfiguration: (OIDServiceConfiguration *)configuration
174
179
additionalParameters : (NSDictionary *_Nullable) additionalParameters
175
180
resolve : (RCTPromiseResolveBlock) resolve
176
181
reject : (RCTPromiseRejectBlock) reject {
177
-
182
+
178
183
OIDTokenRequest *tokenRefreshRequest =
179
184
[[OIDTokenRequest alloc ] initWithConfiguration: configuration
180
185
grantType: @" refresh_token"
@@ -186,7 +191,7 @@ - (void)refreshWithConfiguration: (OIDServiceConfiguration *)configuration
186
191
refreshToken: refreshToken
187
192
codeVerifier: nil
188
193
additionalParameters: additionalParameters];
189
-
194
+
190
195
[OIDAuthorizationService performTokenRequest: tokenRefreshRequest
191
196
callback: ^(OIDTokenResponse *_Nullable response,
192
197
NSError *_Nullable error) {
@@ -196,7 +201,7 @@ - (void)refreshWithConfiguration: (OIDServiceConfiguration *)configuration
196
201
reject (@" RNAppAuth Error" , [error localizedDescription ], error);
197
202
}
198
203
}];
199
-
204
+
200
205
}
201
206
202
207
/*
@@ -207,7 +212,7 @@ - (NSDictionary*)formatResponse: (OIDTokenResponse*) response {
207
212
dateFormat.timeZone = [NSTimeZone timeZoneWithAbbreviation: @" UTC" ];
208
213
[dateFormat setLocale: [NSLocale localeWithLocaleIdentifier: @" en_US_POSIX" ]];
209
214
[dateFormat setDateFormat: @" yyyy-MM-dd'T'HH:mm:ss'Z'" ];
210
-
215
+
211
216
return @{@" accessToken" : response.accessToken ? response.accessToken : @" " ,
212
217
@" accessTokenExpirationDate" : response.accessTokenExpirationDate ? [dateFormat stringFromDate: response.accessTokenExpirationDate] : @" " ,
213
218
@" additionalParameters" : response.additionalParameters ,
0 commit comments