Skip to content

Commit fb41357

Browse files
committed
Refreshing of expired access token
1 parent 51193ec commit fb41357

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

Sources/OAuth2Client/NXOAuth2Connection.m

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,18 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon
136136
}
137137

138138
NSString *authenticateHeader = nil;
139-
if ([response isKindOfClass:[NSHTTPURLResponse class]])
140-
authenticateHeader = [[(NSHTTPURLResponse *)response allHeaderFields] objectForKey:@"WWW-Authenticate"];
141-
if (self.statusCode == 401
142-
&& client.accessToken.refreshToken != nil
139+
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
140+
NSDictionary *headerFields = [(NSHTTPURLResponse *)response allHeaderFields];
141+
for (NSString *headerKey in headerFields.allKeys) {
142+
if ([[headerKey lowercaseString] isEqualToString:@"www-authenticate"]) {
143+
authenticateHeader = [headerFields objectForKey:headerKey];
144+
break;
145+
}
146+
}
147+
}
148+
if (/*self.statusCode == 401 // TODO: check for status code once the bug returning 500 is fixed
149+
&&*/ client.accessToken.refreshToken != nil
150+
&& authenticateHeader
143151
&& [authenticateHeader rangeOfString:@"expired_token"].location != NSNotFound) {
144152
[self cancel];
145153
[client refreshAccessTokenAndRetryConnection:self];
@@ -200,7 +208,6 @@ - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectio
200208

201209
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
202210
{
203-
NSLog(@"Auth error: ", [challenge.error localizedDescription]);
204211
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
205212
//if ([trustedHosts containsObject:challenge.protectionSpace.host])
206213
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

0 commit comments

Comments
 (0)