Skip to content

Commit 5933e6e

Browse files
author
hakonk
committed
fix: podfile source path. feat: add protocol for managing auth flow session and calling on id instance conforming to UIApplicationDelegate in order to get reference to rootViewController
1 parent 23ad92f commit 5933e6e

File tree

5 files changed

+42
-33
lines changed

5 files changed

+42
-33
lines changed

ios/RNAppAuth.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
2-
#if __has_include("RCTBridgeModule.h")
3-
#import "RCTBridgeModule.h"
4-
#else
51
#import <React/RCTBridgeModule.h>
6-
#endif
72

83
@interface RNAppAuth : NSObject <RCTBridgeModule>
94

105
@end
11-
6+

ios/RNAppAuth.m

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
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,

ios/RNAppAuth.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Pod::Spec.new do |s|
1212
s.author = { "author" => "kadi.kraman@formidable.com" }
1313
s.platform = :ios, "7.0"
1414
s.source = { :git => "https://github.com/FormidableLabs/react-native-app-auth.git", :tag => "master" }
15-
s.source_files = "RNAppAuth/**/*.{h,m}"
15+
s.source_files = "**/*.{h,m}"
1616
s.requires_arc = true
1717

1818
s.dependency "React"

ios/RNAppAuth.xcodeproj/project.pbxproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
/* Begin PBXFileReference section */
2626
134814201AA4EA6300B7C361 /* libRNAppAuth.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNAppAuth.a; sourceTree = BUILT_PRODUCTS_DIR; };
27+
19C8EE5020E3796B00C4D522 /* RNAppAuthAuthorizationFlowManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNAppAuthAuthorizationFlowManager.h; sourceTree = "<group>"; };
2728
B3E7B5881CC2AC0600A0062D /* RNAppAuth.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNAppAuth.h; sourceTree = "<group>"; };
2829
B3E7B5891CC2AC0600A0062D /* RNAppAuth.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNAppAuth.m; sourceTree = "<group>"; };
2930
/* End PBXFileReference section */
@@ -52,6 +53,7 @@
5253
children = (
5354
B3E7B5881CC2AC0600A0062D /* RNAppAuth.h */,
5455
B3E7B5891CC2AC0600A0062D /* RNAppAuth.m */,
56+
19C8EE5020E3796B00C4D522 /* RNAppAuthAuthorizationFlowManager.h */,
5557
134814211AA4EA7D00B7C361 /* Products */,
5658
);
5759
sourceTree = "<group>";
@@ -204,7 +206,7 @@
204206
isa = XCBuildConfiguration;
205207
buildSettings = {
206208
HEADER_SEARCH_PATHS = (
207-
"$(inherited)",
209+
"$(inherited)",
208210
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
209211
"$(SRCROOT)/../../../React/**",
210212
"$(SRCROOT)/../../react-native/React/**",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#import <Foundation/Foundation.h>
2+
#import <AppAuth/AppAuth.h>
3+
4+
@protocol RNAppAuthAuthorizationFlowManager <NSObject>
5+
@required
6+
-(void)setCurrentAuthorizationFlowSession:(id<OIDAuthorizationFlowSession>)session;
7+
@end

0 commit comments

Comments
 (0)