Skip to content

Commit 4030dc2

Browse files
committed
Created authenticate with client credentials method, for account creation.
1 parent 59373e8 commit 4030dc2

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

Sources/OAuth2Client/NXOAuth2AccountStore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ typedef void(^NXOAuth2PreparedAuthorizationURLHandler)(NSURL *preparedURL);
114114
- (void)requestAccessToAccountWithType:(NSString *)accountType withPreparedAuthorizationURLHandler:(NXOAuth2PreparedAuthorizationURLHandler)aPreparedAuthorizationURLHandler;
115115
- (void)requestAccessToAccountWithType:(NSString *)accountType username:(NSString *)username password:(NSString *)password;
116116
- (void)requestAccessToAccountWithType:(NSString *)accountType assertionType:(NSURL *)assertionType assertion:(NSString *)assertion;
117+
- (void)requestClientCredentialsAccessWithType:(NSString *)accountType;
117118
- (void)removeAccount:(NXOAuth2Account *)account;
118119

119120

Sources/OAuth2Client/NXOAuth2AccountStore.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ - (void)requestAccessToAccountWithType:(NSString *)accountType assertionType:(NS
204204
[client authenticateWithAssertionType:assertionType assertion:assertion];
205205
}
206206

207+
- (void)requestClientCredentialsAccessWithType:(NSString *)accountType;
208+
{
209+
NXOAuth2Client *client = [self pendingOAuthClientForAccountType:accountType];
210+
[client authenticateWithClientCredentials];
211+
}
212+
207213
- (void)removeAccount:(NXOAuth2Account *)account;
208214
{
209215
if (account) {

Sources/OAuth2Client/NXOAuth2Client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ extern NSString * const NXOAuth2ClientConnectionContextTokenRefresh;
120120
/*!
121121
* Authenticate with username & password (User Credentials Flow)
122122
*/
123+
- (void)authenticateWithClientCredentials;
123124
- (void)authenticateWithUsername:(NSString *)username password:(NSString *)password;
124125

125126
/*!

Sources/OAuth2Client/NXOAuth2Client.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,31 @@ - (void)requestTokenWithAuthGrant:(NSString *)authGrant redirectURL:(NSURL *)red
301301
authConnection.context = NXOAuth2ClientConnectionContextTokenRequest;
302302
}
303303

304+
// Client Credential Flow
305+
- (void)authenticateWithClientCredentials;
306+
{
307+
NSAssert1(!authConnection, @"authConnection already running with: %@", authConnection);
308+
309+
NSMutableURLRequest *tokenRequest = [NSMutableURLRequest requestWithURL:tokenURL];
310+
[tokenRequest setHTTPMethod:@"POST"];
311+
[authConnection cancel]; // just to be sure
312+
313+
self.authenticating = YES;
314+
315+
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
316+
@"client_credentials", @"grant_type",
317+
clientId, @"client_id",
318+
clientSecret, @"client_secret",
319+
nil];
320+
if (self.desiredScope) {
321+
[parameters setObject:[[self.desiredScope allObjects] componentsJoinedByString:@" "] forKey:@"scope"];
322+
}
323+
authConnection = [[NXOAuth2Connection alloc] initWithRequest:tokenRequest
324+
requestParameters:parameters
325+
oauthClient:self
326+
delegate:self];
327+
authConnection.context = NXOAuth2ClientConnectionContextTokenRequest;
328+
}
304329

305330
// User Password Flow Only
306331
- (void)authenticateWithUsername:(NSString *)username password:(NSString *)password;

0 commit comments

Comments
 (0)