Skip to content

Commit

Permalink
Add request trace logging enabled via the preprocessor define _APPAUT…
Browse files Browse the repository at this point in the history
…HTRACE

Useful when debugging IdP issues.
  • Loading branch information
WilliamDenniss committed Jul 11, 2018
1 parent 67716e0 commit 66a5a12
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Source/OIDAuthorizationService.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ - (BOOL)resumeExternalUserAgentFlowWithURL:(NSURL *)URL {
if (![self shouldHandleURL:URL]) {
return NO;
}

AppAuthRequestTrace(@"Authorization Response: %@", URL);

// checks for an invalid state
if (!_pendingauthorizationFlowCallback) {
[NSException raise:OIDOAuthExceptionInvalidAuthorizationFlow
Expand Down Expand Up @@ -278,6 +281,9 @@ + (void)discoverServiceConfigurationForDiscoveryURL:(NSURL *)discoveryURL
presentAuthorizationRequest:(OIDAuthorizationRequest *)request
externalUserAgent:(id<OIDExternalUserAgent>)externalUserAgent
callback:(OIDAuthorizationCallback)callback {

AppAuthRequestTrace(@"Authorization Request: %@", request);

OIDAuthorizationFlowSessionImplementation *flowSession =
[[OIDAuthorizationFlowSessionImplementation alloc] initWithRequest:request];
[flowSession presentAuthorizationWithExternalUserAgent:externalUserAgent callback:callback];
Expand All @@ -299,6 +305,12 @@ + (void)performTokenRequest:(OIDTokenRequest *)request
callback:(OIDTokenCallback)callback {

NSURLRequest *URLRequest = [request URLRequest];

AppAuthRequestTrace(@"Token Request: %@\nHTTPBody: %@",
URLRequest.URL,
[[NSString alloc] initWithData:URLRequest.HTTPBody
encoding:NSUTF8StringEncoding]);

NSURLSession *session = [OIDURLSessionProvider session];
[[session dataTaskWithRequest:URLRequest
completionHandler:^(NSData *_Nullable data,
Expand All @@ -322,6 +334,9 @@ + (void)performTokenRequest:(OIDTokenRequest *)request

NSHTTPURLResponse *HTTPURLResponse = (NSHTTPURLResponse *)response;
NSInteger statusCode = HTTPURLResponse.statusCode;
AppAuthRequestTrace(@"Token Response: HTTP Status %d\nHTTPBody: %@",
(int)statusCode,
[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
if (statusCode != 200) {
// A server error occurred.
NSError *serverError =
Expand Down
7 changes: 7 additions & 0 deletions Source/OIDDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@
reason:reason \
userInfo:nil]; \
}

#ifdef _APPAUTHTRACE
# define AppAuthRequestTrace(fmt, ...) NSLog(fmt, ##__VA_ARGS__);
#else // _APPAUTHTRACE
# define AppAuthRequestTrace(...)
#endif // _APPAUTHTRACE

0 comments on commit 66a5a12

Please sign in to comment.