13
13
// limitations under the License.
14
14
15
15
#import " FIRAppDistribution+Private.h"
16
+ #import " FIRAppDistributionAuthPersistence+Private.h"
16
17
#import " FIRAppDistributionMachO+Private.h"
17
18
#import " FIRAppDistributionRelease+Private.h"
18
19
@@ -60,8 +61,15 @@ - (instancetype)initWithApp:(FIRApp *)app appInfo:(NSDictionary *)appInfo {
60
61
[GULAppDelegateSwizzler registerAppDelegateInterceptor: interceptor];
61
62
}
62
63
63
- // TODO: Lookup keychain to load auth state on init
64
+ NSError *authRetrievalError;
65
+ self.authState = [FIRAppDistributionAuthPersistence retrieveAuthState: &authRetrievalError];
66
+ // TODO (schnecle): replace NSLog statement with FIRLogger log statement
67
+ if (authRetrievalError) {
68
+ NSLog (@" Error retrieving token from keychain: %@ " , [authRetrievalError localizedDescription ]);
69
+ }
70
+
64
71
self.isTesterSignedIn = self.authState ? YES : NO ;
72
+
65
73
return self;
66
74
}
67
75
@@ -128,50 +136,70 @@ - (void)signInTesterWithCompletion:(void (^)(NSError *_Nullable error))completio
128
136
}
129
137
130
138
- (void )signOutTester {
139
+ NSError *error;
140
+ BOOL didClearAuthState = [FIRAppDistributionAuthPersistence clearAuthState: &error];
141
+ // TODO (schnecle): Add in FIRLogger to report when we have failed to clear auth state
142
+ if (!didClearAuthState) {
143
+ NSLog (@" Error clearing token from keychain: %@ " , [error localizedDescription ]);
144
+ }
145
+
131
146
self.authState = nil ;
132
147
self.isTesterSignedIn = false ;
133
148
}
134
149
135
150
- (void )fetchReleases : (FIRAppDistributionUpdateCheckCompletion)completion {
136
- NSURLSession *URLSession = [NSURLSession sharedSession ];
137
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc ] init ];
138
- NSString *URLString =
139
- [NSString stringWithFormat: kReleasesEndpointURL , [[FIRApp defaultApp ] options ].googleAppID];
140
- [request setURL: [NSURL URLWithString: URLString]];
141
- [request setHTTPMethod: @" GET" ];
142
- [request setValue: [NSString
143
- stringWithFormat: @" Bearer %@ " , self .authState.lastTokenResponse.accessToken]
144
- forHTTPHeaderField: @" Authorization" ];
145
-
146
- NSURLSessionDataTask *listReleasesDataTask = [URLSession
147
- dataTaskWithRequest: request
148
- completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
149
- if (error) {
150
- // TODO: Reformat error into error code
151
- completion (nil , error);
152
- return ;
153
- }
154
-
155
- NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
156
-
157
- if (HTTPResponse.statusCode == 200 ) {
158
- [self handleReleasesAPIResponseWithData: data completion: completion];
159
- } else {
160
- // TODO: Handle non-200 http response
161
- @throw ([NSException exceptionWithName: @" NotImplementedException"
162
- reason: @" This code path is not implemented yet"
163
- userInfo: nil ]);
164
- }
165
- }];
166
-
167
- [listReleasesDataTask resume ];
151
+ [self .authState performActionWithFreshTokens: ^(NSString *_Nonnull accessToken,
152
+ NSString *_Nonnull idToken,
153
+ NSError *_Nullable error) {
154
+ if (error) {
155
+ // TODO (schnecle): Add in FIRLogger log statement
156
+ NSLog (@" Error fetching fresh tokens: %@ " , [error localizedDescription ]);
157
+ [self signOutTester ];
158
+ return ;
159
+ }
160
+
161
+ // perform your API request using the tokens
162
+ NSURLSession *URLSession = [NSURLSession sharedSession ];
163
+ NSMutableURLRequest *request = [[NSMutableURLRequest alloc ] init ];
164
+ NSString *URLString =
165
+ [NSString stringWithFormat: kReleasesEndpointURL , [[FIRApp defaultApp ] options ].googleAppID];
166
+ [request setURL: [NSURL URLWithString: URLString]];
167
+ [request setHTTPMethod: @" GET" ];
168
+ [request setValue: [NSString stringWithFormat: @" Bearer %@ " , accessToken]
169
+ forHTTPHeaderField: @" Authorization" ];
170
+
171
+ NSURLSessionDataTask *listReleasesDataTask = [URLSession
172
+ dataTaskWithRequest: request
173
+ completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
174
+ if (error) {
175
+ // TODO: Reformat error into error code
176
+ completion (nil , error);
177
+ return ;
178
+ }
179
+
180
+ NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
181
+
182
+ if (HTTPResponse.statusCode == 200 ) {
183
+ [self handleReleasesAPIResponseWithData: data completion: completion];
184
+ } else {
185
+ // TODO: Handle non-200 http response
186
+ NSLog (@" ERROR - Non 200 service response - %@ " , HTTPResponse);
187
+ @throw ([NSException exceptionWithName: @" NotImplementedException"
188
+ reason: @" This code path is not implemented yet"
189
+ userInfo: nil ]);
190
+ }
191
+ }];
192
+
193
+ [listReleasesDataTask resume ];
194
+ }];
168
195
}
169
196
170
197
- (void )handleOauthDiscoveryCompletion : (OIDServiceConfiguration *_Nullable)configuration
171
198
error : (NSError *_Nullable)error
172
199
appDistributionSignInCompletion : (void (^)(NSError *_Nullable error))completion {
173
200
if (!configuration) {
174
201
// TODO: Handle when we cannot get configuration
202
+ NSLog (@" ERROR - Cannot discover oauth config" );
175
203
@throw ([NSException exceptionWithName: @" NotImplementedException"
176
204
reason: @" This code path is not implemented yet"
177
205
userInfo: nil ]);
@@ -191,19 +219,32 @@ - (void)handleOauthDiscoveryCompletion:(OIDServiceConfiguration *_Nullable)confi
191
219
additionalParameters: nil ];
192
220
193
221
[self setupUIWindowForLogin ];
222
+
223
+ void (^processAuthState)(OIDAuthState *_Nullable authState, NSError *_Nullable error) = ^void (
224
+ OIDAuthState *_Nullable authState, NSError *_Nullable error) {
225
+ self.authState = authState;
226
+
227
+ // Capture errors in persistence but do not bubble them
228
+ // up
229
+ NSError *authPersistenceError;
230
+ if (authState) {
231
+ [FIRAppDistributionAuthPersistence persistAuthState: authState error: &authPersistenceError];
232
+ }
233
+
234
+ // TODO (schnecle): Log errors in persistence using
235
+ // FIRLogger
236
+ if (authPersistenceError) {
237
+ NSLog (@" Error persisting token to keychain: %@ " , [error localizedDescription ]);
238
+ }
239
+ self.isTesterSignedIn = self.authState ? YES : NO ;
240
+ completion (error);
241
+ };
242
+
194
243
// performs authentication request
195
244
[FIRAppDistributionAppDelegatorInterceptor sharedInstance ].currentAuthorizationFlow =
196
245
[OIDAuthState authStateByPresentingAuthorizationRequest: request
197
246
presentingViewController: self .safariHostingViewController
198
- callback: ^(OIDAuthState *_Nullable authState,
199
- NSError *_Nullable error) {
200
- [self cleanupUIWindow ];
201
-
202
- self.authState = authState;
203
- self.isTesterSignedIn =
204
- self.authState ? YES : NO ;
205
- completion (error);
206
- }];
247
+ callback: processAuthState];
207
248
}
208
249
209
250
- (void )setupUIWindowForLogin {
0 commit comments