Skip to content

Removing obstacles processing server responses #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions Sources/OAuth2Client/NXOAuth2AccessToken.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ - (id)initWithAccessToken:(NSString *)anAccessToken refreshToken:(NSString *)aRe

- (id)initWithAccessToken:(NSString *)anAccessToken refreshToken:(NSString *)aRefreshToken expiresAt:(NSDate *)anExpiryDate scope:(NSSet *)aScope responseBody:(NSString *)aResponseBody tokenType:(NSString *)aTokenType
{
// a token object without an actual token is not what we want!
NSAssert1(anAccessToken, @"No token from token response: %@", aResponseBody);
// a token object without an actual token is not what we want! >> But we must process response!
if (anAccessToken == nil) {
return nil;
}
Expand Down Expand Up @@ -238,7 +237,7 @@ + (id)tokenFromDefaultKeychainWithServiceProviderName:(NSString *)provider;
result = (__bridge_transfer NSDictionary *)cfResult;

if (status != noErr) {
NSAssert1(status == errSecItemNotFound, @"unexpected error while fetching token from keychain: %d", (int)status);
NSAssert1(status == errSecItemNotFound, @"unexpected error while fetching token from keychain: %ld", status);
return nil;
}

Expand All @@ -257,7 +256,7 @@ - (void)storeInDefaultKeychainWithServiceProviderName:(NSString *)provider;
nil];
[self removeFromDefaultKeychainWithServiceProviderName:provider];
OSStatus __attribute__((unused)) err = SecItemAdd((__bridge CFDictionaryRef)query, NULL);
NSAssert1(err == noErr, @"error while adding token to keychain: %d", (int)err);
NSAssert1(err == noErr, @"error while adding token to keychain: %ld", err);
}

- (void)removeFromDefaultKeychainWithServiceProviderName:(NSString *)provider;
Expand All @@ -268,7 +267,7 @@ - (void)removeFromDefaultKeychainWithServiceProviderName:(NSString *)provider;
serviceName, kSecAttrService,
nil];
OSStatus __attribute__((unused)) err = SecItemDelete((__bridge CFDictionaryRef)query);
NSAssert1((err == noErr || err == errSecItemNotFound), @"error while deleting token from keychain: %d", (int)err);
NSAssert1((err == noErr || err == errSecItemNotFound), @"error while deleting token from keychain: %ld", err);
}

#else
Expand Down
42 changes: 25 additions & 17 deletions Sources/OAuth2Client/NXOAuth2Client.m
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ - (void)setPersistent:(BOOL)shouldPersist;
- (NXOAuth2AccessToken *)accessToken;
{
if (accessToken) return accessToken;

if (persistent) {
accessToken = [NXOAuth2AccessToken tokenFromDefaultKeychainWithServiceProviderName:keyChainGroup ? keyChainGroup : [tokenURL host]];
if (accessToken) {
Expand All @@ -177,10 +177,10 @@ - (NXOAuth2AccessToken *)accessToken;

- (void)setAccessToken:(NXOAuth2AccessToken *)value;
{
if (self.accessToken == value) return;
if (self.accessToken == value && nil != value) return;
BOOL authorisationStatusChanged = ((accessToken == nil) || (value == nil)); //They can't both be nil, see one line above. So they have to have changed from or to nil.

if (!value) {
if (!value && nil != self.accessToken) {
[self.accessToken removeFromDefaultKeychainWithServiceProviderName:keyChainGroup ? keyChainGroup : [tokenURL host]];
}

Expand Down Expand Up @@ -329,13 +329,13 @@ - (void)requestTokenWithAuthGrant:(NSString *)authGrant redirectURL:(NSURL *)red
if (self.desiredScope) {
[parameters setObject:[[self.desiredScope allObjects] componentsJoinedByString:@" "] forKey:@"scope"];
}

if (self.customHeaderFields) {
[self.customHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
[tokenRequest addValue:obj forHTTPHeaderField:key];
}];
}

if (self.additionalAuthenticationParameters) {
[parameters addEntriesFromDictionary:self.additionalAuthenticationParameters];
}
Expand Down Expand Up @@ -366,13 +366,13 @@ - (void)authenticateWithClientCredentials;
if (self.desiredScope) {
[parameters setObject:[[self.desiredScope allObjects] componentsJoinedByString:@" "] forKey:@"scope"];
}

if (self.customHeaderFields) {
[self.customHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
[tokenRequest addValue:obj forHTTPHeaderField:key];
}];
}

authConnection = [[NXOAuth2Connection alloc] initWithRequest:tokenRequest
requestParameters:parameters
oauthClient:self
Expand Down Expand Up @@ -405,13 +405,13 @@ - (void)authenticateWithUsername:(NSString *)username password:(NSString *)passw
if (self.additionalAuthenticationParameters) {
[parameters addEntriesFromDictionary:self.additionalAuthenticationParameters];
}

if (self.customHeaderFields) {
[self.customHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
[tokenRequest addValue:obj forHTTPHeaderField:key];
}];
}

authConnection = [[NXOAuth2Connection alloc] initWithRequest:tokenRequest
requestParameters:parameters
oauthClient:self
Expand Down Expand Up @@ -442,6 +442,13 @@ - (void)authenticateWithAssertionType:(NSURL *)anAssertionType assertion:(NSStri
if (self.desiredScope) {
[parameters setObject:[[self.desiredScope allObjects] componentsJoinedByString:@" "] forKey:@"scope"];
}

if (self.customHeaderFields) {
[self.customHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
[tokenRequest addValue:obj forHTTPHeaderField:key];
}];
}

authConnection = [[NXOAuth2Connection alloc] initWithRequest:tokenRequest
requestParameters:parameters
oauthClient:self
Expand Down Expand Up @@ -500,14 +507,15 @@ - (void)oauthConnection:(NXOAuth2Connection *)connection didFinishWithData:(NSDa
self.authenticating = NO;

NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NXOAuth2AccessToken *newToken = [NXOAuth2AccessToken tokenWithResponseBody:result tokenType:self.tokenType
];
NSAssert(newToken != nil, @"invalid response?");

[newToken restoreWithOldToken:self.accessToken];

self.accessToken = newToken;

NXOAuth2AccessToken *newToken = [NXOAuth2AccessToken tokenWithResponseBody:result tokenType:self.tokenType];

if (nil != newToken) {
[newToken restoreWithOldToken:self.accessToken];
self.accessToken = newToken;
} else {
self.accessToken = nil;
}

for (NXOAuth2Connection *retryConnection in waitingConnections) {
[retryConnection retry];
}
Expand Down