Skip to content

Commit

Permalink
Add the request operation as a callback argument
Browse files Browse the repository at this point in the history
Adds AFHTTPRequestOperation arguments to the success and failure block signatures, as discussed in issue AFNetworking#84
  • Loading branch information
Nathan Racklyeft committed Mar 11, 2015
1 parent 30037a6 commit f6c4e48
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 53 deletions.
2 changes: 1 addition & 1 deletion AFOAuth2Manager.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'AFOAuth2Manager'
s.version = '2.2.0'
s.version = '3.0.0'
s.license = 'MIT'
s.summary = 'AFNetworking Extension for OAuth 2 Authentication.'
s.homepage = 'https://github.com/AFNetworking/AFOAuth2Manager'
Expand Down
30 changes: 15 additions & 15 deletions AFOAuth2Manager/AFOAuth2Manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,69 +95,69 @@
@param username The username used for authentication
@param password The password used for authentication
@param scope The authorization scope
@param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the OAuth credential returned by the server.
@param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the request operation, and the OAuth credential returned by the server.
@param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the error returned from the server.
*/
- (AFHTTPRequestOperation *)authenticateUsingOAuthWithURLString:(NSString *)URLString
username:(NSString *)username
password:(NSString *)password
scope:(NSString *)scope
success:(void (^)(AFOAuthCredential *credential))success
failure:(void (^)(NSError *error))failure;
success:(void (^)(AFHTTPRequestOperation *operation, AFOAuthCredential *credential))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

/**
Creates and enqueues an `AFHTTPRequestOperation` to authenticate against the server with a designated scope.
@param URLString The URL string used to create the request URL.
@param scope The authorization scope
@param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the OAuth credential returned by the server.
@param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the request operation, and the OAuth credential returned by the server.
@param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the error returned from the server.
*/
- (AFHTTPRequestOperation *)authenticateUsingOAuthWithURLString:(NSString *)URLString
scope:(NSString *)scope
success:(void (^)(AFOAuthCredential *credential))success
failure:(void (^)(NSError *error))failure;
success:(void (^)(AFHTTPRequestOperation *operation, AFOAuthCredential *credential))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

/**
Creates and enqueues an `AFHTTPRequestOperation` to authenticate against the server using the specified refresh token.
@param URLString The URL string used to create the request URL.
@param refreshToken The OAuth refresh token
@param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the OAuth credential returned by the server.
@param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the request operation, and the OAuth credential returned by the server.
@param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the error returned from the server.
*/
- (AFHTTPRequestOperation *)authenticateUsingOAuthWithURLString:(NSString *)URLString
refreshToken:(NSString *)refreshToken
success:(void (^)(AFOAuthCredential *credential))success
failure:(void (^)(NSError *error))failure;
success:(void (^)(AFHTTPRequestOperation *operation, AFOAuthCredential *credential))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

/**
Creates and enqueues an `AFHTTPRequestOperation` to authenticate against the server with an authorization code, redirecting to a specified URI upon successful authentication.
@param URLString The URL string used to create the request URL.
@param code The authorization code
@param uri The URI to redirect to after successful authentication
@param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the OAuth credential returned by the server.
@param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the request operation, and the OAuth credential returned by the server.
@param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the error returned from the server.
*/
- (AFHTTPRequestOperation *)authenticateUsingOAuthWithURLString:(NSString *)URLString
code:(NSString *)code
redirectURI:(NSString *)uri
success:(void (^)(AFOAuthCredential *credential))success
failure:(void (^)(NSError *error))failure;
success:(void (^)(AFHTTPRequestOperation *operation, AFOAuthCredential *credential))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

/**
Creates and enqueues an `AFHTTPRequestOperation` to authenticate against the server with the specified parameters.
@param URLString The URL string used to create the request URL.
@param parameters The parameters to be encoded and set in the request HTTP body.
@param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes a single argument: the OAuth credential returned by the server.
@param success A block object to be executed when the request operation finishes successfully. This block has no return value and takes two arguments: the request operation, and the OAuth credential returned by the server.
@param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the error returned from the server.
*/
- (AFHTTPRequestOperation *)authenticateUsingOAuthWithURLString:(NSString *)URLString
parameters:(NSDictionary *)parameters
success:(void (^)(AFOAuthCredential *credential))success
failure:(void (^)(NSError *error))failure;
success:(void (^)(AFHTTPRequestOperation *operation, AFOAuthCredential *credential))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

@end

Expand Down
70 changes: 35 additions & 35 deletions AFOAuth2Manager/AFOAuth2Manager.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
NSCParameterAssert(identifier);

return @{
(__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
(__bridge id)kSecAttrService: kAFOAuth2CredentialServiceName,
(__bridge id)kSecAttrAccount: identifier
};
(__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
(__bridge id)kSecAttrService: kAFOAuth2CredentialServiceName,
(__bridge id)kSecAttrAccount: identifier
};
}

// See: http://tools.ietf.org/html/rfc6749#section-5.2
Expand Down Expand Up @@ -106,15 +106,15 @@ - (id)initWithBaseURL:(NSURL *)url
if (!self) {
return nil;
}

self.serviceProviderIdentifier = [self.baseURL host];
self.clientID = clientID;
self.secret = secret;

self.useHTTPBasicAuthentication = YES;

[self.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

return self;
}

Expand All @@ -141,11 +141,11 @@ - (void)setSecret:(NSString *)secret {
#pragma mark -

- (AFHTTPRequestOperation *)authenticateUsingOAuthWithURLString:(NSString *)URLString
username:(NSString *)username
password:(NSString *)password
scope:(NSString *)scope
success:(void (^)(AFOAuthCredential *credential))success
failure:(void (^)(NSError *error))failure
username:(NSString *)username
password:(NSString *)password
scope:(NSString *)scope
success:(void (^)(AFHTTPRequestOperation *operation, AFOAuthCredential *credential))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
NSParameterAssert(username);
NSParameterAssert(password);
Expand All @@ -156,46 +156,46 @@ - (AFHTTPRequestOperation *)authenticateUsingOAuthWithURLString:(NSString *)URLS
@"username": username,
@"password": password,
@"scope": scope
};
};

return [self authenticateUsingOAuthWithURLString:URLString parameters:parameters success:success failure:failure];
}

- (AFHTTPRequestOperation *)authenticateUsingOAuthWithURLString:(NSString *)URLString
scope:(NSString *)scope
success:(void (^)(AFOAuthCredential *credential))success
failure:(void (^)(NSError *error))failure
scope:(NSString *)scope
success:(void (^)(AFHTTPRequestOperation *operation, AFOAuthCredential *credential))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
NSParameterAssert(scope);

NSDictionary *parameters = @{
@"grant_type": kAFOAuthClientCredentialsGrantType,
@"scope": scope
};
};

return [self authenticateUsingOAuthWithURLString:URLString parameters:parameters success:success failure:failure];
}

- (AFHTTPRequestOperation *)authenticateUsingOAuthWithURLString:(NSString *)URLString
refreshToken:(NSString *)refreshToken
success:(void (^)(AFOAuthCredential *credential))success
failure:(void (^)(NSError *error))failure
refreshToken:(NSString *)refreshToken
success:(void (^)(AFHTTPRequestOperation *operation, AFOAuthCredential *credential))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
NSParameterAssert(refreshToken);

NSDictionary *parameters = @{
@"grant_type": kAFOAuthRefreshGrantType,
@"refresh_token": refreshToken
};
};

return [self authenticateUsingOAuthWithURLString:URLString parameters:parameters success:success failure:failure];
}

- (AFHTTPRequestOperation *)authenticateUsingOAuthWithURLString:(NSString *)URLString
code:(NSString *)code
redirectURI:(NSString *)uri
success:(void (^)(AFOAuthCredential *credential))success
failure:(void (^)(NSError *error))failure
code:(NSString *)code
redirectURI:(NSString *)uri
success:(void (^)(AFHTTPRequestOperation *operation, AFOAuthCredential *credential))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
NSParameterAssert(code);
NSParameterAssert(uri);
Expand All @@ -204,15 +204,15 @@ - (AFHTTPRequestOperation *)authenticateUsingOAuthWithURLString:(NSString *)URLS
@"grant_type": kAFOAuthCodeGrantType,
@"code": code,
@"redirect_uri": uri
};
};

return [self authenticateUsingOAuthWithURLString:URLString parameters:parameters success:success failure:failure];
}

- (AFHTTPRequestOperation *)authenticateUsingOAuthWithURLString:(NSString *)URLString
parameters:(NSDictionary *)parameters
success:(void (^)(AFOAuthCredential *credential))success
failure:(void (^)(NSError *error))failure
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, AFOAuthCredential *credential))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
NSMutableDictionary *mutableParameters = [NSMutableDictionary dictionaryWithDictionary:parameters];
if (!self.useHTTPBasicAuthentication) {
Expand All @@ -221,18 +221,18 @@ - (AFHTTPRequestOperation *)authenticateUsingOAuthWithURLString:(NSString *)URLS
}
parameters = [NSDictionary dictionaryWithDictionary:mutableParameters];

AFHTTPRequestOperation *requestOperation = [self POST:URLString parameters:parameters success:^(__unused AFHTTPRequestOperation *operation, id responseObject) {
AFHTTPRequestOperation *requestOperation = [self POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (!responseObject) {
if (failure) {
failure(nil);
failure(operation, nil);
}

return;
}

if ([responseObject valueForKey:@"error"]) {
if (failure) {
failure(AFErrorFromRFC6749Section5_2Error(responseObject));
failure(operation, AFErrorFromRFC6749Section5_2Error(responseObject));
}

return;
Expand Down Expand Up @@ -262,14 +262,14 @@ - (AFHTTPRequestOperation *)authenticateUsingOAuthWithURLString:(NSString *)URLS
}

if (success) {
success(credential);
success(operation, credential);
}
} failure:^(__unused AFHTTPRequestOperation *operation, NSError *error) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
failure(error);
failure(operation, error);
}
}];

return requestOperation;
}

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ AFOAuth2Manager *OAuth2Manager =
username:@"username"
password:@"password"
scope:@"email"
success:^(AFOAuthCredential *credential) {
success:^(AFHTTPRequestOperation *operation, AFOAuthCredential *credential) {
NSLog(@"Token: %@", credential.accessToken);
}
failure:^(NSError *error) {
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
```
Expand Down

0 comments on commit f6c4e48

Please sign in to comment.