Skip to content

Commit 6a46f70

Browse files
committed
Merge pull request #44 from nxtbgthng/feature/scopeSupport
Feature/scope support
2 parents 2277568 + 437be3d commit 6a46f70

File tree

8 files changed

+164
-11
lines changed

8 files changed

+164
-11
lines changed

Sources/OAuth2Client/NXOAuth2AccessToken.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@
3434
@property (nonatomic, readonly) NSString *responseBody;
3535

3636
+ (id)tokenWithResponseBody:(NSString *)responseBody;
37+
+ (id)tokenWithResponseBody:(NSString *)responseBody tokenType:(NSString *)tokenType;
3738

3839
- (id)initWithAccessToken:(NSString *)accessToken;
3940
- (id)initWithAccessToken:(NSString *)accessToken refreshToken:(NSString *)refreshToken expiresAt:(NSDate *)expiryDate;
4041
- (id)initWithAccessToken:(NSString *)accessToken refreshToken:(NSString *)refreshToken expiresAt:(NSDate *)expiryDate scope:(NSSet *)scope;
4142
- (id)initWithAccessToken:(NSString *)accessToken refreshToken:(NSString *)refreshToken expiresAt:(NSDate *)expiryDate scope:(NSSet *)scope responseBody:(NSString *)responseBody;
4243
- (id)initWithAccessToken:(NSString *)accessToken refreshToken:(NSString *)refreshToken expiresAt:(NSDate *)expiryDate scope:(NSSet *)scope responseBody:(NSString *)responseBody tokenType:(NSString*)tokenType; // designated
4344

45+
- (void)restoreWithOldToken:(NXOAuth2AccessToken *)oldToken;
4446

4547
#pragma mark Keychain Support
4648

Sources/OAuth2Client/NXOAuth2AccessToken.m

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ @implementation NXOAuth2AccessToken
2121
#pragma mark Lifecycle
2222

2323
+ (id)tokenWithResponseBody:(NSString *)theResponseBody;
24+
{
25+
return [self tokenWithResponseBody:theResponseBody tokenType:nil];
26+
}
27+
28+
+ (id)tokenWithResponseBody:(NSString *)theResponseBody tokenType:(NSString *)tokenType;
2429
{
2530
NSDictionary *jsonDict = nil;
2631
Class jsonSerializationClass = NSClassFromString(@"NSJSONSerialization");
@@ -50,7 +55,11 @@ + (id)tokenWithResponseBody:(NSString *)theResponseBody;
5055
NSString *anAccessToken = [jsonDict objectForKey:@"access_token"];
5156
NSString *aRefreshToken = [jsonDict objectForKey:@"refresh_token"];
5257
NSString *scopeString = [jsonDict objectForKey:@"scope"];
53-
NSString *tokenType = [jsonDict objectForKey:@"token_type"];
58+
59+
// if the response overrides token_type we take it from the response
60+
if ([jsonDict objectForKey:@"token_type"]) {
61+
tokenType = [jsonDict objectForKey:@"token_type"];
62+
}
5463

5564
NSSet *scope = nil;
5665
if (scopeString) {
@@ -121,6 +130,12 @@ - (id)initWithAccessToken:(NSString *)anAccessToken refreshToken:(NSString *)aRe
121130
return self;
122131
}
123132

133+
- (void)restoreWithOldToken:(NXOAuth2AccessToken *)oldToken;
134+
{
135+
if (self.refreshToken == nil) {
136+
refreshToken = oldToken.refreshToken;
137+
}
138+
}
124139

125140

126141
#pragma mark Accessors
@@ -156,10 +171,9 @@ - (BOOL)hasExpired;
156171
return ([[NSDate date] earlierDate:expiresAt] == expiresAt);
157172
}
158173

159-
160174
- (NSString *)description;
161175
{
162-
return [NSString stringWithFormat:@"<NXOAuth2Token token:%@ refreshToken:%@ expiresAt:%@>", self.accessToken, self.refreshToken, self.expiresAt];
176+
return [NSString stringWithFormat:@"<NXOAuth2Token token:%@ refreshToken:%@ expiresAt:%@ tokenType: %@>", self.accessToken, self.refreshToken, self.expiresAt, self.tokenType];
163177
}
164178

165179

@@ -172,6 +186,9 @@ - (void)encodeWithCoder:(NSCoder *)aCoder
172186
[aCoder encodeObject:expiresAt forKey:@"expiresAt"];
173187
[aCoder encodeObject:scope forKey:@"scope"];
174188
[aCoder encodeObject:responseBody forKey:@"responseBody"];
189+
if (tokenType) {
190+
[aCoder encodeObject:tokenType forKey:@"tokenType"];
191+
}
175192
}
176193

177194
- (id)initWithCoder:(NSCoder *)aDecoder
@@ -190,6 +207,7 @@ - (id)initWithCoder:(NSCoder *)aDecoder
190207
expiresAt = [[aDecoder decodeObjectForKey:@"expiresAt"] copy];
191208
scope = [[aDecoder decodeObjectForKey:@"scope"] copy];
192209
responseBody = [[aDecoder decodeObjectForKey:@"responseBody"] copy];
210+
tokenType = [[aDecoder decodeObjectForKey:@"tokenType"] copy];
193211
}
194212
return self;
195213
}

Sources/OAuth2Client/NXOAuth2Account.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ - (NXOAuth2Client *)oauthClient;
8080
NSString *clientSecret = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationSecret];
8181
NSURL *authorizeURL = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationAuthorizeURL];
8282
NSURL *tokenURL = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationTokenURL];
83+
NSString *tokenType = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationTokenType];
8384

8485
oauthClient = [[NXOAuth2Client alloc] initWithClientID:clientID
8586
clientSecret:clientSecret
8687
authorizeURL:authorizeURL
8788
tokenURL:tokenURL
8889
accessToken:self.accessToken
90+
tokenType:tokenType
8991
persistent:NO
9092
delegate:self];
9193
}

Sources/OAuth2Client/NXOAuth2AccountStore.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ extern NSString * const kNXOAuth2AccountStoreConfigurationSecret;
3232
extern NSString * const kNXOAuth2AccountStoreConfigurationAuthorizeURL;
3333
extern NSString * const kNXOAuth2AccountStoreConfigurationTokenURL;
3434
extern NSString * const kNXOAuth2AccountStoreConfigurationRedirectURL;
35+
extern NSString * const kNXOAuth2AccountStoreConfigurationScope;
36+
extern NSString * const kNXOAuth2AccountStoreConfigurationTokenType;
3537

3638

3739
#pragma mark Account Type
@@ -75,6 +77,23 @@ typedef void(^NXOAuth2PreparedAuthorizationURLHandler)(NSURL *preparedURL);
7577
redirectURL:(NSURL *)aRedirectURL
7678
forAccountType:(NSString *)anAccountType;
7779

80+
- (void)setClientID:(NSString *)aClientID
81+
secret:(NSString *)aSecret
82+
scope:(NSSet *)theScope
83+
authorizationURL:(NSURL *)anAuthorizationURL
84+
tokenURL:(NSURL *)aTokenURL
85+
redirectURL:(NSURL *)aRedirectURL
86+
forAccountType:(NSString *)anAccountType;
87+
88+
- (void)setClientID:(NSString *)aClientID
89+
secret:(NSString *)aSecret
90+
scope:(NSSet *)theScope
91+
authorizationURL:(NSURL *)anAuthorizationURL
92+
tokenURL:(NSURL *)aTokenURL
93+
redirectURL:(NSURL *)aRedirectURL
94+
tokenType:(NSString *)aTokenType
95+
forAccountType:(NSString *)anAccountType;
96+
7897
- (void)setConfiguration:(NSDictionary *)configuration forAccountType:(NSString *)accountType;
7998

8099
- (NSDictionary *)configurationForAccountType:(NSString *)accountType;

Sources/OAuth2Client/NXOAuth2AccountStore.m

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
NSString * const kNXOAuth2AccountStoreConfigurationAuthorizeURL = @"kNXOAuth2AccountStoreConfigurationAuthorizeURL";
4141
NSString * const kNXOAuth2AccountStoreConfigurationTokenURL = @"kNXOAuth2AccountStoreConfigurationTokenURL";
4242
NSString * const kNXOAuth2AccountStoreConfigurationRedirectURL = @"kNXOAuth2AccountStoreConfigurationRedirectURL";
43+
NSString * const kNXOAuth2AccountStoreConfigurationScope = @"kNXOAuth2AccountStoreConfigurationScope";
44+
NSString * const kNXOAuth2AccountStoreConfigurationTokenType = @"kNXOAuth2AccountStoreConfigurationTokenType";
4345

4446
#pragma mark Account Type
4547

@@ -225,6 +227,44 @@ - (void)setClientID:(NSString *)aClientID
225227
forAccountType:anAccountType];
226228
}
227229

230+
- (void)setClientID:(NSString *)aClientID
231+
secret:(NSString *)aSecret
232+
scope:(NSSet *)theScope
233+
authorizationURL:(NSURL *)anAuthorizationURL
234+
tokenURL:(NSURL *)aTokenURL
235+
redirectURL:(NSURL *)aRedirectURL
236+
forAccountType:(NSString *)anAccountType;
237+
{
238+
[self setConfiguration:[NSDictionary dictionaryWithObjectsAndKeys:
239+
aClientID, kNXOAuth2AccountStoreConfigurationClientID,
240+
aSecret, kNXOAuth2AccountStoreConfigurationSecret,
241+
theScope, kNXOAuth2AccountStoreConfigurationScope,
242+
anAuthorizationURL, kNXOAuth2AccountStoreConfigurationAuthorizeURL,
243+
aTokenURL, kNXOAuth2AccountStoreConfigurationTokenURL,
244+
aRedirectURL, kNXOAuth2AccountStoreConfigurationRedirectURL, nil]
245+
forAccountType:anAccountType];
246+
}
247+
248+
- (void)setClientID:(NSString *)aClientID
249+
secret:(NSString *)aSecret
250+
scope:(NSSet *)theScope
251+
authorizationURL:(NSURL *)anAuthorizationURL
252+
tokenURL:(NSURL *)aTokenURL
253+
redirectURL:(NSURL *)aRedirectURL
254+
tokenType:(NSString *)aTokenType
255+
forAccountType:(NSString *)anAccountType;
256+
{
257+
[self setConfiguration:[NSDictionary dictionaryWithObjectsAndKeys:
258+
aClientID, kNXOAuth2AccountStoreConfigurationClientID,
259+
aSecret, kNXOAuth2AccountStoreConfigurationSecret,
260+
theScope, kNXOAuth2AccountStoreConfigurationScope,
261+
anAuthorizationURL, kNXOAuth2AccountStoreConfigurationAuthorizeURL,
262+
aTokenURL, kNXOAuth2AccountStoreConfigurationTokenURL,
263+
aTokenType, kNXOAuth2AccountStoreConfigurationTokenType,
264+
aRedirectURL, kNXOAuth2AccountStoreConfigurationRedirectURL, nil]
265+
forAccountType:anAccountType];
266+
}
267+
228268
- (void)setConfiguration:(NSDictionary *)configuration
229269
forAccountType:(NSString *)accountType;
230270
{
@@ -338,16 +378,26 @@ - (NXOAuth2Client *)pendingOAuthClientForAccountType:(NSString *)accountType;
338378

339379
NSString *clientID = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationClientID];
340380
NSString *clientSecret = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationSecret];
381+
NSSet *scope = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationScope];
341382
NSURL *authorizeURL = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationAuthorizeURL];
342383
NSURL *tokenURL = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationTokenURL];
384+
NSString *tokenType = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationTokenType];
343385

344386
client = [[NXOAuth2Client alloc] initWithClientID:clientID
345387
clientSecret:clientSecret
346388
authorizeURL:authorizeURL
347389
tokenURL:tokenURL
390+
accessToken:nil
391+
tokenType:tokenType
392+
persistent:YES
348393
delegate:self];
394+
349395
client.persistent = NO;
350396

397+
if (scope != nil) {
398+
client.desiredScope = scope;
399+
}
400+
351401
[self.pendingOAuthClients setObject:client forKey:accountType];
352402
}
353403
}

Sources/OAuth2Client/NXOAuth2Client.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extern NSString * const NXOAuth2ClientConnectionContextTokenRefresh;
4545
// server information
4646
NSURL *authorizeURL;
4747
NSURL *tokenURL;
48+
NSString *tokenType;
4849

4950
// token exchange
5051
NXOAuth2Connection *authConnection;
@@ -60,6 +61,7 @@ extern NSString * const NXOAuth2ClientConnectionContextTokenRefresh;
6061

6162
@property (nonatomic, copy, readonly) NSString *clientId;
6263
@property (nonatomic, copy, readonly) NSString *clientSecret;
64+
@property (nonatomic, copy, readonly) NSString *tokenType;
6365

6466
@property (nonatomic, copy) NSSet *desiredScope;
6567
@property (nonatomic, copy) NSString *userAgent;
@@ -90,6 +92,15 @@ extern NSString * const NXOAuth2ClientConnectionContextTokenRefresh;
9092
persistent:(BOOL)shouldPersist
9193
delegate:(NSObject<NXOAuth2ClientDelegate> *)delegate;
9294

95+
- (id)initWithClientID:(NSString *)clientId
96+
clientSecret:(NSString *)clientSecret
97+
authorizeURL:(NSURL *)authorizeURL
98+
tokenURL:(NSURL *)tokenURL
99+
accessToken:(NXOAuth2AccessToken *)accessToken
100+
tokenType:(NSString *)tokenType
101+
persistent:(BOOL)shouldPersist
102+
delegate:(NSObject<NXOAuth2ClientDelegate> *)delegate;
103+
93104
- (BOOL)openRedirectURL:(NSURL *)URL;
94105

95106

Sources/OAuth2Client/NXOAuth2Client.m

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,25 @@ - (id)initWithClientID:(NSString *)aClientId
5959
accessToken:(NXOAuth2AccessToken *)anAccessToken
6060
persistent:(BOOL)shouldPersist
6161
delegate:(NSObject<NXOAuth2ClientDelegate> *)aDelegate;
62+
{
63+
return [self initWithClientID:aClientId
64+
clientSecret:aClientSecret
65+
authorizeURL:anAuthorizeURL
66+
tokenURL:aTokenURL
67+
accessToken:anAccessToken
68+
tokenType:nil
69+
persistent:shouldPersist
70+
delegate:aDelegate];
71+
}
72+
73+
- (id)initWithClientID:(NSString *)aClientId
74+
clientSecret:(NSString *)aClientSecret
75+
authorizeURL:(NSURL *)anAuthorizeURL
76+
tokenURL:(NSURL *)aTokenURL
77+
accessToken:(NXOAuth2AccessToken *)anAccessToken
78+
tokenType:(NSString *)aTokenType
79+
persistent:(BOOL)shouldPersist
80+
delegate:(NSObject<NXOAuth2ClientDelegate> *)aDelegate;
6281
{
6382
NSAssert(aTokenURL != nil && anAuthorizeURL != nil, @"No token or no authorize URL");
6483
self = [super init];
@@ -69,6 +88,7 @@ - (id)initWithClientID:(NSString *)aClientId
6988
clientSecret = [aClientSecret copy];
7089
authorizeURL = [anAuthorizeURL copy];
7190
tokenURL = [aTokenURL copy];
91+
tokenType = [aTokenType copy];
7292
accessToken = anAccessToken;
7393

7494
self.persistent = shouldPersist;
@@ -85,7 +105,7 @@ - (void)dealloc;
85105

86106
#pragma mark Accessors
87107

88-
@synthesize clientId, clientSecret;
108+
@synthesize clientId, clientSecret, tokenType;
89109
@synthesize desiredScope, userAgent;
90110
@synthesize delegate, persistent, accessToken, authenticating;
91111

@@ -157,6 +177,15 @@ - (void)setAccessToken:(NXOAuth2AccessToken *)value;
157177
}
158178
}
159179

180+
- (void)setDesiredScope:(NSSet *)aDesiredScope;
181+
{
182+
if (desiredScope == aDesiredScope) {
183+
return;
184+
}
185+
186+
desiredScope = [aDesiredScope copy];
187+
}
188+
160189

161190
#pragma mark Flow
162191

@@ -169,11 +198,17 @@ - (void)requestAccess;
169198

170199
- (NSURL *)authorizationURLWithRedirectURL:(NSURL *)redirectURL;
171200
{
172-
return [authorizeURL nxoauth2_URLByAddingParameters:[NSDictionary dictionaryWithObjectsAndKeys:
173-
@"code", @"response_type",
174-
clientId, @"client_id",
175-
[redirectURL absoluteString], @"redirect_uri",
176-
nil]];
201+
NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
202+
@"code", @"response_type",
203+
clientId, @"client_id",
204+
[redirectURL absoluteString], @"redirect_uri",
205+
nil];
206+
207+
if (self.desiredScope.count > 0) {
208+
[parameters setObject:[[self.desiredScope allObjects] componentsJoinedByString:@" "] forKey:@"scope"];
209+
}
210+
211+
return [authorizeURL nxoauth2_URLByAddingParameters:parameters];
177212
}
178213

179214

@@ -346,8 +381,12 @@ - (void)oauthConnection:(NXOAuth2Connection *)connection didFinishWithData:(NSDa
346381
self.authenticating = NO;
347382

348383
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
349-
NXOAuth2AccessToken *newToken = [NXOAuth2AccessToken tokenWithResponseBody:result];
384+
NXOAuth2AccessToken *newToken = [NXOAuth2AccessToken tokenWithResponseBody:result tokenType:self.tokenType
385+
];
350386
NSAssert(newToken != nil, @"invalid response?");
387+
388+
[newToken restoreWithOldToken:self.accessToken];
389+
351390
self.accessToken = newToken;
352391

353392
for (NXOAuth2Connection *retryConnection in waitingConnections) {
@@ -363,6 +402,10 @@ - (void)oauthConnection:(NXOAuth2Connection *)connection didFinishWithData:(NSDa
363402

364403
- (void)oauthConnection:(NXOAuth2Connection *)connection didFailWithError:(NSError *)error;
365404
{
405+
NSString *body = [[NSString alloc] initWithData:connection.data encoding:NSUTF8StringEncoding];
406+
NSLog(@"oauthConnection Error: %@", body);
407+
408+
366409
if (connection == authConnection) {
367410
self.authenticating = NO;
368411

Sources/OAuth2Client/NXOAuth2Connection.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,15 @@ - (NSURLConnection *)createConnection;
175175
return nil;
176176
}
177177

178-
oauthAuthorizationHeader = [NSString stringWithFormat:@"%@ %@", client.accessToken.tokenType, client.accessToken.accessToken];
178+
NSString *tokenType = client.accessToken.tokenType;
179+
if (tokenType == nil) {
180+
tokenType = client.tokenType;
181+
}
182+
if (tokenType == nil) {
183+
tokenType = @"OAuth";
184+
}
185+
186+
oauthAuthorizationHeader = [NSString stringWithFormat:@"%@ %@", tokenType, client.accessToken.accessToken];
179187
}
180188

181189
NSMutableURLRequest *startRequest = [request mutableCopy];

0 commit comments

Comments
 (0)