-
Notifications
You must be signed in to change notification settings - Fork 773
/
AppAuthExampleViewController.m
474 lines (400 loc) · 19.7 KB
/
AppAuthExampleViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*! @file AppAuthExampleViewController.m
@brief AppAuth iOS SDK Example
@copyright
Copyright 2015 Google Inc. All Rights Reserved.
@copydetails
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "AppAuthExampleViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "AppAuth.h"
#import "AppDelegate.h"
typedef void (^PostRegistrationCallback)(OIDServiceConfiguration *configuration,
OIDRegistrationResponse *registrationResponse);
/*! @brief The OIDC issuer from which the configuration will be discovered.
*/
static NSString *const kIssuer = @"https://issuer.example.com";
/*! @brief The OAuth client ID.
@discussion For client configuration instructions, see the README.
Set to nil to use dynamic registration with this example.
@see https://github.com/openid/AppAuth-iOS/blob/master/Examples/Example-iOS_ObjC/README.md
*/
static NSString *const kClientID = @"YOUR_CLIENT_ID";
/*! @brief The OAuth redirect URI for the client @c kClientID.
@discussion For client configuration instructions, see the README.
@see https://github.com/openid/AppAuth-iOS/blob/master/Examples/Example-iOS_ObjC/README.md
*/
static NSString *const kRedirectURI = @"com.example.app:/oauth2redirect/example-provider";
/*! @brief NSCoding key for the authState property.
*/
static NSString *const kAppAuthExampleAuthStateKey = @"authState";
@interface AppAuthExampleViewController () <OIDAuthStateChangeDelegate, OIDAuthStateErrorDelegate>
@end
@implementation AppAuthExampleViewController
- (void)viewDidLoad {
[super viewDidLoad];
_logTextView.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:1.0].CGColor;
_logTextView.layer.borderWidth = 1.0f;
_logTextView.alwaysBounceVertical = true;
_logTextView.textContainer.lineBreakMode = NSLineBreakByCharWrapping;
_logTextView.text = @"";
[self loadState];
[self updateUI];
}
- (void)verifyConfig {
#if !defined(NS_BLOCK_ASSERTIONS)
// The example needs to be configured with your own client details.
// See: https://github.com/openid/AppAuth-iOS/blob/master/Examples/Example-iOS_ObjC/README.md
NSAssert(![kIssuer isEqualToString:@"https://issuer.example.com"],
@"Update kIssuer with your own issuer. "
"Instructions: https://github.com/openid/AppAuth-iOS/blob/master/Examples/Example-iOS_ObjC/README.md");
NSAssert(![kClientID isEqualToString:@"YOUR_CLIENT_ID"],
@"Update kClientID with your own client ID. "
"Instructions: https://github.com/openid/AppAuth-iOS/blob/master/Examples/Example-iOS_ObjC/README.md");
NSAssert(![kRedirectURI isEqualToString:@"com.example.app:/oauth2redirect/example-provider"],
@"Update kRedirectURI with your own redirect URI. "
"Instructions: https://github.com/openid/AppAuth-iOS/blob/master/Examples/Example-iOS_ObjC/README.md");
// verifies that the custom URI scheme has been updated in the Info.plist
NSArray __unused* urlTypes =
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"];
NSAssert([urlTypes count] > 0, @"No custom URI scheme has been configured for the project.");
NSArray *urlSchemes =
[(NSDictionary *)[urlTypes objectAtIndex:0] objectForKey:@"CFBundleURLSchemes"];
NSAssert([urlSchemes count] > 0, @"No custom URI scheme has been configured for the project.");
NSString *urlScheme = [urlSchemes objectAtIndex:0];
NSAssert(![urlScheme isEqualToString:@"com.example.app"],
@"Configure the URI scheme in Info.plist (URL Types -> Item 0 -> URL Schemes -> Item 0) "
"with the scheme of your redirect URI. Full instructions: "
"https://github.com/openid/AppAuth-iOS/blob/master/Examples/Example-iOS_ObjC/README.md");
#endif // !defined(NS_BLOCK_ASSERTIONS)
}
/*! @brief Saves the @c OIDAuthState to @c NSUSerDefaults.
*/
- (void)saveState {
// for production usage consider using the OS Keychain instead
NSData *archivedAuthState = [ NSKeyedArchiver archivedDataWithRootObject:_authState];
[[NSUserDefaults standardUserDefaults] setObject:archivedAuthState
forKey:kAppAuthExampleAuthStateKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
/*! @brief Loads the @c OIDAuthState from @c NSUSerDefaults.
*/
- (void)loadState {
// loads OIDAuthState from NSUSerDefaults
NSData *archivedAuthState =
[[NSUserDefaults standardUserDefaults] objectForKey:kAppAuthExampleAuthStateKey];
OIDAuthState *authState = [NSKeyedUnarchiver unarchiveObjectWithData:archivedAuthState];
[self setAuthState:authState];
}
- (void)setAuthState:(nullable OIDAuthState *)authState {
if (_authState == authState) {
return;
}
_authState = authState;
_authState.stateChangeDelegate = self;
[self stateChanged];
}
/*! @brief Refreshes UI, typically called after the auth state changed.
*/
- (void)updateUI {
_userinfoButton.enabled = [_authState isAuthorized];
_clearAuthStateButton.enabled = _authState != nil;
_codeExchangeButton.enabled = _authState.lastAuthorizationResponse.authorizationCode
&& !_authState.lastTokenResponse;
// dynamically changes authorize button text depending on authorized state
if (!_authState) {
[_authAutoButton setTitle:@"Authorize" forState:UIControlStateNormal];
[_authAutoButton setTitle:@"Authorize" forState:UIControlStateHighlighted];
[_authManual setTitle:@"Authorize (Manual)" forState:UIControlStateNormal];
[_authManual setTitle:@"Authorize (Manual)" forState:UIControlStateHighlighted];
} else {
[_authAutoButton setTitle:@"Re-authorize" forState:UIControlStateNormal];
[_authAutoButton setTitle:@"Re-authorize" forState:UIControlStateHighlighted];
[_authManual setTitle:@"Re-authorize (Manual)" forState:UIControlStateNormal];
[_authManual setTitle:@"Re-authorize (Manual)" forState:UIControlStateHighlighted];
}
}
- (void)stateChanged {
[self saveState];
[self updateUI];
}
- (void)didChangeState:(OIDAuthState *)state {
[self stateChanged];
}
- (void)authState:(OIDAuthState *)state didEncounterAuthorizationError:(nonnull NSError *)error {
[self logMessage:@"Received authorization error: %@", error];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)doClientRegistration:(OIDServiceConfiguration *)configuration
callback:(PostRegistrationCallback)callback {
NSURL *redirectURI = [NSURL URLWithString:kRedirectURI];
OIDRegistrationRequest *request =
[[OIDRegistrationRequest alloc] initWithConfiguration:configuration
redirectURIs:@[ redirectURI ]
responseTypes:nil
grantTypes:nil
subjectType:nil
tokenEndpointAuthMethod:@"client_secret_post"
additionalParameters:nil];
// performs registration request
[self logMessage:@"Initiating registration request"];
[OIDAuthorizationService performRegistrationRequest:request
completion:^(OIDRegistrationResponse *_Nullable regResp, NSError *_Nullable error) {
if (regResp) {
[self setAuthState:[[OIDAuthState alloc] initWithRegistrationResponse:regResp]];
[self logMessage:@"Got registration response: [%@]", regResp];
callback(configuration, regResp);
} else {
[self logMessage:@"Registration error: %@", [error localizedDescription]];
[self setAuthState:nil];
}
}];
}
- (void)doAuthWithAutoCodeExchange:(OIDServiceConfiguration *)configuration
clientID:(NSString *)clientID
clientSecret:(NSString *)clientSecret {
NSURL *redirectURI = [NSURL URLWithString:kRedirectURI];
// builds authentication request
OIDAuthorizationRequest *request =
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
clientId:clientID
clientSecret:clientSecret
scopes:@[ OIDScopeOpenID, OIDScopeProfile ]
redirectURL:redirectURI
responseType:OIDResponseTypeCode
additionalParameters:nil];
// performs authentication request
AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
[self logMessage:@"Initiating authorization request with scope: %@", request.scope];
appDelegate.currentAuthorizationFlow =
[OIDAuthState authStateByPresentingAuthorizationRequest:request
presentingViewController:self
callback:^(OIDAuthState *_Nullable authState, NSError *_Nullable error) {
if (authState) {
[self setAuthState:authState];
[self logMessage:@"Got authorization tokens. Access token: %@",
authState.lastTokenResponse.accessToken];
} else {
[self logMessage:@"Authorization error: %@", [error localizedDescription]];
[self setAuthState:nil];
}
}];
}
- (void)doAuthWithoutCodeExchange:(OIDServiceConfiguration *)configuration
clientID:(NSString *)clientID
clientSecret:(NSString *)clientSecret {
NSURL *redirectURI = [NSURL URLWithString:kRedirectURI];
// builds authentication request
OIDAuthorizationRequest *request =
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
clientId:clientID
clientSecret:clientSecret
scopes:@[ OIDScopeOpenID, OIDScopeProfile ]
redirectURL:redirectURI
responseType:OIDResponseTypeCode
additionalParameters:nil];
// performs authentication request
AppDelegate *appDelegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
[self logMessage:@"Initiating authorization request %@", request];
appDelegate.currentAuthorizationFlow =
[OIDAuthorizationService presentAuthorizationRequest:request
presentingViewController:self
callback:^(OIDAuthorizationResponse *_Nullable authorizationResponse,
NSError *_Nullable error) {
if (authorizationResponse) {
OIDAuthState *authState =
[[OIDAuthState alloc] initWithAuthorizationResponse:authorizationResponse];
[self setAuthState:authState];
[self logMessage:@"Authorization response with code: %@",
authorizationResponse.authorizationCode];
// could just call [self tokenExchange:nil] directly, but will let the user initiate it.
} else {
[self logMessage:@"Authorization error: %@", [error localizedDescription]];
}
}];
}
- (IBAction)authWithAutoCodeExchange:(nullable id)sender {
[self verifyConfig];
NSURL *issuer = [NSURL URLWithString:kIssuer];
[self logMessage:@"Fetching configuration for issuer: %@", issuer];
// discovers endpoints
[OIDAuthorizationService discoverServiceConfigurationForIssuer:issuer
completion:^(OIDServiceConfiguration *_Nullable configuration, NSError *_Nullable error) {
if (!configuration) {
[self logMessage:@"Error retrieving discovery document: %@", [error localizedDescription]];
[self setAuthState:nil];
return;
}
[self logMessage:@"Got configuration: %@", configuration];
if (!kClientID) {
[self doClientRegistration:configuration
callback:^(OIDServiceConfiguration *configuration,
OIDRegistrationResponse *registrationResponse) {
[self doAuthWithAutoCodeExchange:configuration
clientID:registrationResponse.clientID
clientSecret:registrationResponse.clientSecret];
}];
} else {
[self doAuthWithAutoCodeExchange:configuration clientID:kClientID clientSecret:nil];
}
}];
}
- (IBAction)authNoCodeExchange:(nullable id)sender {
[self verifyConfig];
NSURL *issuer = [NSURL URLWithString:kIssuer];
[self logMessage:@"Fetching configuration for issuer: %@", issuer];
// discovers endpoints
[OIDAuthorizationService discoverServiceConfigurationForIssuer:issuer
completion:^(OIDServiceConfiguration *_Nullable configuration, NSError *_Nullable error) {
if (!configuration) {
[self logMessage:@"Error retrieving discovery document: %@", [error localizedDescription]];
return;
}
[self logMessage:@"Got configuration: %@", configuration];
if (!kClientID) {
[self doClientRegistration:configuration
callback:^(OIDServiceConfiguration *configuration,
OIDRegistrationResponse *registrationResponse) {
[self doAuthWithoutCodeExchange:configuration
clientID:registrationResponse.clientID
clientSecret:registrationResponse.clientSecret];
}];
} else {
[self doAuthWithoutCodeExchange:configuration clientID:kClientID clientSecret:nil];
}
}];
}
- (IBAction)codeExchange:(nullable id)sender {
// performs code exchange request
OIDTokenRequest *tokenExchangeRequest =
[_authState.lastAuthorizationResponse tokenExchangeRequest];
[self logMessage:@"Performing authorization code exchange with request [%@]",
tokenExchangeRequest];
[OIDAuthorizationService performTokenRequest:tokenExchangeRequest
callback:^(OIDTokenResponse *_Nullable tokenResponse,
NSError *_Nullable error) {
if (!tokenResponse) {
[self logMessage:@"Token exchange error: %@", [error localizedDescription]];
} else {
[self logMessage:@"Received token response with accessToken: %@", tokenResponse.accessToken];
}
[_authState updateWithTokenResponse:tokenResponse error:error];
}];
}
- (IBAction)clearAuthState:(nullable id)sender {
[self setAuthState:nil];
}
- (IBAction)clearLog:(nullable id)sender {
_logTextView.text = @"";
}
- (IBAction)userinfo:(nullable id)sender {
NSURL *userinfoEndpoint =
_authState.lastAuthorizationResponse.request.configuration.discoveryDocument.userinfoEndpoint;
if (!userinfoEndpoint) {
[self logMessage:@"Userinfo endpoint not declared in discovery document"];
return;
}
NSString *currentAccessToken = _authState.lastTokenResponse.accessToken;
[self logMessage:@"Performing userinfo request"];
[_authState performActionWithFreshTokens:^(NSString *_Nonnull accessToken,
NSString *_Nonnull idToken,
NSError *_Nullable error) {
if (error) {
[self logMessage:@"Error fetching fresh tokens: %@", [error localizedDescription]];
return;
}
// log whether a token refresh occurred
if (![currentAccessToken isEqual:accessToken]) {
[self logMessage:@"Access token was refreshed automatically (%@ to %@)",
currentAccessToken,
accessToken];
} else {
[self logMessage:@"Access token was fresh and not updated [%@]", accessToken];
}
// creates request to the userinfo endpoint, with access token in the Authorization header
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:userinfoEndpoint];
NSString *authorizationHeaderValue = [NSString stringWithFormat:@"Bearer %@", accessToken];
[request addValue:authorizationHeaderValue forHTTPHeaderField:@"Authorization"];
NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:nil
delegateQueue:nil];
// performs HTTP request
NSURLSessionDataTask *postDataTask =
[session dataTaskWithRequest:request
completionHandler:^(NSData *_Nullable data,
NSURLResponse *_Nullable response,
NSError *_Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^() {
if (error) {
[self logMessage:@"HTTP request failed %@", error];
return;
}
if (![response isKindOfClass:[NSHTTPURLResponse class]]) {
[self logMessage:@"Non-HTTP response"];
return;
}
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
id jsonDictionaryOrArray =
[NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
if (httpResponse.statusCode != 200) {
// server replied with an error
NSString *responseText = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
if (httpResponse.statusCode == 401) {
// "401 Unauthorized" generally indicates there is an issue with the authorization
// grant. Puts OIDAuthState into an error state.
NSError *oauthError =
[OIDErrorUtilities resourceServerAuthorizationErrorWithCode:0
errorResponse:jsonDictionaryOrArray
underlyingError:error];
[_authState updateWithAuthorizationError:oauthError];
// log error
[self logMessage:@"Authorization Error (%@). Response: %@", oauthError, responseText];
} else {
[self logMessage:@"HTTP: %d. Response: %@",
(int)httpResponse.statusCode,
responseText];
}
return;
}
// success response
[self logMessage:@"Success: %@", jsonDictionaryOrArray];
});
}];
[postDataTask resume];
}];
}
/*! @brief Logs a message to stdout and the textfield.
@param format The format string and arguments.
*/
- (void)logMessage:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2) {
// gets message as string
va_list argp;
va_start(argp, format);
NSString *log = [[NSString alloc] initWithFormat:format arguments:argp];
va_end(argp);
// outputs to stdout
NSLog(@"%@", log);
// appends to output log
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"hh:mm:ss";
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
_logTextView.text = [NSString stringWithFormat:@"%@%@%@: %@",
_logTextView.text,
([_logTextView.text length] > 0) ? @"\n" : @"",
dateString,
log];
}
@end