Skip to content

Commit b7d7c55

Browse files
authored
Merge pull request #151 from hakonk/refactor/auth-session-handling
Uniform auth session handling (iOS)
2 parents 1ce4c69 + b5cd4d0 commit b7d7c55

File tree

7 files changed

+41
-58
lines changed

7 files changed

+41
-58
lines changed

Example/ios/AppAuthExample/AppDelegate.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
*/
99

1010
#import <UIKit/UIKit.h>
11+
#import <RNAppAuth/RNAppAuthAuthorizationFlowManager.h>
1112

12-
@interface AppDelegate : UIResponder <UIApplicationDelegate>
13+
@interface AppDelegate : UIResponder <UIApplicationDelegate, RNAppAuthAuthorizationFlowManager>
1314
@property (nonatomic, strong, nullable) UIWindow *window;
15+
@property(nonatomic, weak)id<RNAppAuthAuthorizationFlowManagerDelegate>authorizationFlowManagerDelegate;
1416
@end
1517

Example/ios/AppAuthExample/AppDelegate.m

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,9 @@
1010
#import "AppDelegate.h"
1111
#import <React/RCTBundleURLProvider.h>
1212
#import <React/RCTRootView.h>
13-
#import <AppAuth/AppAuth.h>
14-
#import <RNAppAuth/RNAppAuthAuthorizationFlowManager.h>
15-
16-
@interface AppDelegate()<RNAppAuthAuthorizationFlowManager> {
17-
id <OIDAuthorizationFlowSession> _currentSession;
18-
}
19-
@end
2013

2114
@implementation AppDelegate
2215

23-
-(void)setCurrentAuthorizationFlowSession:(id<OIDAuthorizationFlowSession>)session {
24-
_currentSession = session;
25-
}
26-
2716
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2817
{
2918
NSURL *jsCodeLocation;
@@ -45,9 +34,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
4534
}
4635

4736
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *, id> *)options {
48-
BOOL shouldOpenUrl = [_currentSession resumeAuthorizationFlowWithURL:url];
49-
_currentSession = nil;
50-
return shouldOpenUrl;
37+
return [self.authorizationFlowManagerDelegate resumeExternalUserAgentFlowWithURL:url];
5138
}
5239

5340
@end

README.md

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ AppAuth supports three options for dependency management.
213213

214214
pod 'AppAuth', '>= 0.94'
215215

216-
Then run `pod install`. Note that version 0.91 is the first of the library to support iOS 11.
216+
Then run `pod install`. Note that version 0.94 is the first of the library to support iOS 11.
217217

218218
2. **Carthage**
219219

@@ -265,58 +265,39 @@ authorization flow from the redirect. Follow these steps:
265265

266266
`RNAppAuth` will call on the given app's delegate via `[UIApplication sharedApplication].delegate`.
267267
Furthermore, `RNAppAuth` expects the delegate instance to conform to the protocol `RNAppAuthAuthorizationFlowManager`.
268-
Make `AppDelegate` conform to `RNAppAuthAuthorizationFlowManager`:
268+
Make `AppDelegate` conform to `RNAppAuthAuthorizationFlowManager` with the following changes to `AppDelegate.h`:
269269

270270
```diff
271-
+ // Depending on build configurations, import either with:
272-
+@import RNAppAuth;
273-
+ // or:
274-
+import <AppAuth/AppAuth.h>
275-
+import "RNAppAuthAuthorizationFlowManager.h"
276-
277-
+ @interface AppDelegate()<RNAppAuthAuthorizationFlowManager> {
278-
+ id <OIDAuthorizationFlowSession> _currentSession;
279-
+ }
280-
+ @end
281-
```
282-
283-
Implement the required method of `RNAppAuthAuthorizationFlowManager` in `AppDelegate`:
284-
285-
```diff
286-
+ -(void)setCurrentAuthorizationFlowSession:(id<OIDAuthorizationFlowSession>)session {
287-
+ // retain session for further use
288-
+ _currentSession = session;
289-
+ }
271+
+#import <RNAppAuth/RNAppAuthAuthorizationFlowManager.h>
272+
+@interface AppDelegate : UIResponder <UIApplicationDelegate, RNAppAuthAuthorizationFlowManager>
273+
+@property(nonatomic, weak)id<RNAppAuthAuthorizationFlowManagerDelegate>authorizationFlowManagerDelegate;
290274
```
291275

292276
The authorization response URL is returned to the app via the iOS openURL app delegate method, so
293277
you need to pipe this through to the current authorization session (created in the previous
294-
instruction). Thus, implement the following method from `UIApplicationDelegate` in `AppDelegate`:
278+
instruction). Thus, implement the following method from `UIApplicationDelegate` in `AppDelegate.m`:
295279

296280
```diff
297281
+ - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *, id> *)options {
298-
+ BOOL shouldOpenUrl = [_currentSession resumeAuthorizationFlowWithURL:url];
299-
+ _currentSession = nil;
300-
+ return shouldOpenUrl;
282+
+ return return [self.authorizationFlowManagerDelegate resumeExternalUserAgentFlowWithURL:url];
301283
+ }
302284
```
303285

304286
#### Integration of the library with a Swift iOS project
305287

306288
The approach mentioned above should also be possible to employ with Swift. In this case one should have to import `RNAppAuth`
307289
and make `AppDelegate` conform to `RNAppAuthAuthorizationFlowManager`. Note that this has not been tested.
308-
`AppDelegate` should look something like this:
290+
`AppDelegate.swift` should look something like this:
309291

310292
```swift
311293
@import RNAppAuth
312294
class AppDelegate: UIApplicationDelegate, RNAppAuthAuthorizationFlowManager {
313-
private var currentAuthorizationFlow: OIDAuthorizationFlowSession?
295+
public weak var authorizationFlowManagerDelegate: RNAppAuthAuthorizationFlowManagerDelegate?
314296
func application(
315297
_ app: UIApplication,
316298
open url: URL,
317299
options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool {
318-
defer { currentAuthorizationFlow = nil }
319-
return currentAuthorizationFlow?.resumeAuthorizationFlow(with: url) ?? false
300+
return authorizationFlowManagerDelegate?.resumeExternalUserAgentFlowWithURL(with: url) ?? false
320301
}
321302
}
322303
```

ios/RNAppAuth.m

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@
44
#import <React/RCTConvert.h>
55
#import "RNAppAuthAuthorizationFlowManager.h"
66

7+
@interface RNAppAuth()<RNAppAuthAuthorizationFlowManagerDelegate> {
8+
id<OIDExternalUserAgentSession> _currentSession;
9+
}
10+
@end
11+
712
@implementation RNAppAuth
813

14+
-(BOOL)resumeExternalUserAgentFlowWithURL:(NSURL *)url {
15+
return [_currentSession resumeExternalUserAgentFlowWithURL:url];
16+
}
17+
918
- (dispatch_queue_t)methodQueue
1019
{
1120
return dispatch_get_main_queue();
@@ -141,26 +150,24 @@ - (void)authorizeWithConfiguration: (OIDServiceConfiguration *) configuration
141150

142151
// performs authentication request
143152
id<UIApplicationDelegate, RNAppAuthAuthorizationFlowManager> appDelegate = (id<UIApplicationDelegate, RNAppAuthAuthorizationFlowManager>)[UIApplication sharedApplication].delegate;
144-
145-
id<OIDAuthorizationFlowSession> currentSession =
146-
[OIDAuthState authStateByPresentingAuthorizationRequest:request
153+
if (![[appDelegate class] conformsToProtocol:@protocol(RNAppAuthAuthorizationFlowManager)]) {
154+
[NSException raise:@"RNAppAuth Missing protocol conformance"
155+
format:@"%@ does not conform to RNAppAuthAuthorizationFlowManager", appDelegate];
156+
}
157+
appDelegate.authorizationFlowManagerDelegate = self;
158+
__weak typeof(self) weakSelf = self;
159+
_currentSession = [OIDAuthState authStateByPresentingAuthorizationRequest:request
147160
presentingViewController:appDelegate.window.rootViewController
148161
callback:^(OIDAuthState *_Nullable authState,
149162
NSError *_Nullable error) {
163+
typeof(self) strongSelf = weakSelf;
164+
strongSelf->_currentSession = nil;
150165
if (authState) {
151166
resolve([self formatResponse:authState.lastTokenResponse]);
152167
} else {
153168
reject(@"RNAppAuth Error", [error localizedDescription], error);
154169
}
155-
156170
}]; // end [OIDAuthState authStateByPresentingAuthorizationRequest:request
157-
if ([[appDelegate class] conformsToProtocol:@protocol(RNAppAuthAuthorizationFlowManager)]
158-
&& [appDelegate respondsToSelector: @selector(setCurrentAuthorizationFlowSession:)]) {
159-
[appDelegate setCurrentAuthorizationFlowSession:currentSession];
160-
} else {
161-
[NSException raise:@"RNAppAuth Missing protocol conformance"
162-
format:@"%@ does not conform to RNAppAuthAuthorizationFlowManager", appDelegate];
163-
}
164171
}
165172

166173

ios/RNAppAuth.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ Pod::Spec.new do |s|
1616
s.requires_arc = true
1717

1818
s.dependency "React"
19-
s.dependency "AppAuth"
19+
s.dependency "AppAuth", "0.94.0"
2020
end
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#import <Foundation/Foundation.h>
2-
#import <AppAuth/AppAuth.h>
2+
#import "RNAppAuthAuthorizationFlowManagerDelegate.h"
33

44
@protocol RNAppAuthAuthorizationFlowManager <NSObject>
55
@required
6-
-(void)setCurrentAuthorizationFlowSession:(id<OIDAuthorizationFlowSession>)session;
6+
@property(nonatomic, weak)id<RNAppAuthAuthorizationFlowManagerDelegate>authorizationFlowManagerDelegate;
77
@end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#import <Foundation/Foundation.h>
2+
3+
@protocol RNAppAuthAuthorizationFlowManagerDelegate <NSObject>
4+
@required
5+
-(BOOL)resumeExternalUserAgentFlowWithURL:(NSURL *)url;
6+
@end

0 commit comments

Comments
 (0)