Skip to content

Commit 2706f9c

Browse files
committed
add option to supply additional params to authentication methods
1 parent 6a46f70 commit 2706f9c

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

Sources/OAuth2Client/NXOAuth2AccountStore.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ extern NSString * const kNXOAuth2AccountStoreConfigurationScope;
3636
extern NSString * const kNXOAuth2AccountStoreConfigurationTokenType;
3737

3838

39+
/*
40+
* Requires a NSDictionary as a value.
41+
* They are passed onto the authentication request as additional query parameters.
42+
* The dictionary may not contain the keys "grant_type", "client_id", "client_secret",
43+
* "username", "password", "redirect_uri", "code", "assertion_type" and "assertion" are not allowed.
44+
*/
45+
extern NSString * const kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters;
46+
47+
3948
#pragma mark Account Type
4049

4150
extern NSString * const kNXOAuth2AccountStoreAccountType;

Sources/OAuth2Client/NXOAuth2AccountStore.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
NSString * const kNXOAuth2AccountStoreConfigurationRedirectURL = @"kNXOAuth2AccountStoreConfigurationRedirectURL";
4343
NSString * const kNXOAuth2AccountStoreConfigurationScope = @"kNXOAuth2AccountStoreConfigurationScope";
4444
NSString * const kNXOAuth2AccountStoreConfigurationTokenType = @"kNXOAuth2AccountStoreConfigurationTokenType";
45+
NSString * const kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters = @"kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters";
4546

4647
#pragma mark Account Type
4748

@@ -382,6 +383,7 @@ - (NXOAuth2Client *)pendingOAuthClientForAccountType:(NSString *)accountType;
382383
NSURL *authorizeURL = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationAuthorizeURL];
383384
NSURL *tokenURL = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationTokenURL];
384385
NSString *tokenType = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationTokenType];
386+
NSDictionary *additionalAuthenticationParameters = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters];
385387

386388
client = [[NXOAuth2Client alloc] initWithClientID:clientID
387389
clientSecret:clientSecret
@@ -394,6 +396,11 @@ - (NXOAuth2Client *)pendingOAuthClientForAccountType:(NSString *)accountType;
394396

395397
client.persistent = NO;
396398

399+
if (additionalAuthenticationParameters != nil) {
400+
NSAssert([additionalAuthenticationParameters isKindOfClass:[NSDictionary class]], @"additionalAuthenticationParameters have to be a NSDictionary");
401+
client.additionalAuthenticationParameters = additionalAuthenticationParameters;
402+
}
403+
397404
if (scope != nil) {
398405
client.desiredScope = scope;
399406
}

Sources/OAuth2Client/NXOAuth2Client.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,15 @@ extern NSString * const NXOAuth2ClientConnectionContextTokenRefresh;
6262
@property (nonatomic, copy, readonly) NSString *clientId;
6363
@property (nonatomic, copy, readonly) NSString *clientSecret;
6464
@property (nonatomic, copy, readonly) NSString *tokenType;
65+
@property (nonatomic, strong, readwrite) NSDictionary *additionalAuthenticationParameters;
6566

6667
@property (nonatomic, copy) NSSet *desiredScope;
6768
@property (nonatomic, copy) NSString *userAgent;
6869

6970
@property (nonatomic, strong) NXOAuth2AccessToken *accessToken;
7071
@property (nonatomic, unsafe_unretained) NSObject<NXOAuth2ClientDelegate>* delegate;
7172

73+
7274
/*!
7375
* If set to NO, the access token is not stored any keychain, will be removed if it was.
7476
* Defaults to YES

Sources/OAuth2Client/NXOAuth2Client.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,26 @@ - (void)dealloc;
108108
@synthesize clientId, clientSecret, tokenType;
109109
@synthesize desiredScope, userAgent;
110110
@synthesize delegate, persistent, accessToken, authenticating;
111+
@synthesize additionalAuthenticationParameters;
112+
113+
- (void)setAdditionalAuthenticationParameters:(NSDictionary *)value;
114+
{
115+
if (value == additionalAuthenticationParameters) return;
116+
117+
NSArray *forbiddenKeys = @[ @"grant_type", @"client_id",
118+
@"client_secret",
119+
@"username", @"password",
120+
@"redirect_uri", @"code",
121+
@"assertion_type", @"assertion" ];
122+
123+
for (id key in value) {
124+
NSAssert1([forbiddenKeys containsObject:key] == NO, @"The key %@ may not be used in additionalAuthenticationParameters", key);
125+
}
126+
127+
additionalAuthenticationParameters = value;
128+
129+
130+
}
111131

112132
- (void)setPersistent:(BOOL)shouldPersist;
113133
{
@@ -204,6 +224,10 @@ - (NSURL *)authorizationURLWithRedirectURL:(NSURL *)redirectURL;
204224
[redirectURL absoluteString], @"redirect_uri",
205225
nil];
206226

227+
if (self.additionalAuthenticationParameters) {
228+
[parameters addEntriesFromDictionary:self.additionalAuthenticationParameters];
229+
}
230+
207231
if (self.desiredScope.count > 0) {
208232
[parameters setObject:[[self.desiredScope allObjects] componentsJoinedByString:@" "] forKey:@"scope"];
209233
}
@@ -322,6 +346,11 @@ - (void)authenticateWithUsername:(NSString *)username password:(NSString *)passw
322346
if (self.desiredScope) {
323347
[parameters setObject:[[self.desiredScope allObjects] componentsJoinedByString:@" "] forKey:@"scope"];
324348
}
349+
350+
if (self.additionalAuthenticationParameters) {
351+
[parameters addEntriesFromDictionary:self.additionalAuthenticationParameters];
352+
}
353+
325354
authConnection = [[NXOAuth2Connection alloc] initWithRequest:tokenRequest
326355
requestParameters:parameters
327356
oauthClient:self

0 commit comments

Comments
 (0)