Skip to content
This repository was archived by the owner on Jan 17, 2023. It is now read-only.

Commit 412d256

Browse files
committed
[Issue #85][Issue #87] Adding useHTTPBasicAuthentication property to conditionally encode client credentials in Base64-encoded HTTP Authorization header field, rather than in body of request.
1 parent 910eb36 commit 412d256

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

AFOAuth2Manager/AFOAuth2Manager.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
*/
5050
@property (readonly, nonatomic, copy) NSString *clientID;
5151

52+
/**
53+
Whether to encode client credentials in a Base64-encoded HTTP `Authorization` header, as opposed to the request body. Defaults to `YES`.
54+
*/
55+
@property (nonatomic, assign) BOOL useHTTPBasicAuthentication;
56+
5257
///------------------------------------------------
5358
/// @name Creating and Initializing OAuth 2 Clients
5459
///------------------------------------------------
@@ -67,7 +72,7 @@
6772
secret:(NSString *)secret;
6873

6974
/**
70-
Initializes an `AFOAuth2Manager` object with the specified base URL, client identifier, and secret.
75+
Initializes an `AFOAuth2Manager` object with the specified base URL, client identifier, and secret. The communication to to the server will use HTTP basic auth by default (use `-(id)initWithBaseURL:clientID:secret:withBasicAuth:` to change this).
7176
7277
@param url The base URL for the HTTP client. This argument must not be `nil`.
7378
@param clientID The client identifier issued by the authorization server, uniquely representing the registration information provided by the client.

AFOAuth2Manager/AFOAuth2Manager.m

+22-5
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ @interface AFOAuth2Manager ()
8585
@property (readwrite, nonatomic, copy) NSString *serviceProviderIdentifier;
8686
@property (readwrite, nonatomic, copy) NSString *clientID;
8787
@property (readwrite, nonatomic, copy) NSString *secret;
88+
@property (readonly, nonatomic) BOOL basicAuth;
8889
@end
8990

9091
@implementation AFOAuth2Manager
@@ -101,23 +102,37 @@ - (id)initWithBaseURL:(NSURL *)url
101102
secret:(NSString *)secret
102103
{
103104
NSParameterAssert(clientID);
104-
105+
105106
self = [super initWithBaseURL:url];
106107
if (!self) {
107108
return nil;
108109
}
109-
110+
110111
self.serviceProviderIdentifier = [self.baseURL host];
111112
self.clientID = clientID;
112113
self.secret = secret;
113114

114-
[self.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
115+
self.useHTTPBasicAuthentication = YES;
115116

117+
[self.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
118+
116119
return self;
117120
}
118121

119122
#pragma mark -
120123

124+
- (void)setUseHTTPBasicAuthentication:(BOOL)useHTTPBasicAuthentication {
125+
_useHTTPBasicAuthentication = useHTTPBasicAuthentication;
126+
127+
if (self.useHTTPBasicAuthentication) {
128+
[self.requestSerializer setAuthorizationHeaderFieldWithUsername:self.clientID password:self.secret];
129+
} else {
130+
[self.requestSerializer setValue:nil forHTTPHeaderField:@"Authorization"];
131+
}
132+
}
133+
134+
#pragma mark -
135+
121136
- (AFHTTPRequestOperation *)authenticateUsingOAuthWithURLString:(NSString *)URLString
122137
username:(NSString *)username
123138
password:(NSString *)password
@@ -193,8 +208,10 @@ - (AFHTTPRequestOperation *)authenticateUsingOAuthWithURLString:(NSString *)URLS
193208
failure:(void (^)(NSError *error))failure
194209
{
195210
NSMutableDictionary *mutableParameters = [NSMutableDictionary dictionaryWithDictionary:parameters];
196-
mutableParameters[@"client_id"] = self.clientID;
197-
mutableParameters[@"client_secret"] = self.secret;
211+
if (!self.useHTTPBasicAuthentication) {
212+
mutableParameters[@"client_id"] = self.clientID;
213+
mutableParameters[@"client_secret"] = self.secret;
214+
}
198215
parameters = [NSDictionary dictionaryWithDictionary:mutableParameters];
199216

200217
AFHTTPRequestOperation *requestOperation = [self POST:URLString parameters:parameters success:^(__unused AFHTTPRequestOperation *operation, id responseObject) {

0 commit comments

Comments
 (0)