Skip to content

Commit f03fe37

Browse files
authored
Expose inits that allow the clients to be created with access token so short lived tokens can be used (#326)
1 parent e3a42c1 commit f03fe37

File tree

9 files changed

+78
-12
lines changed

9 files changed

+78
-12
lines changed

Source/ObjectiveDropboxOfficial/Shared/Handwritten/DBTeamClient.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
@class DBTransportDefaultClient;
1111
@class DBTransportDefaultConfig;
1212
@protocol DBAccessTokenProvider;
13+
@class DBAccessToken;
14+
@class DBOAuthManager;
1315

1416
NS_ASSUME_NONNULL_BEGIN
1517

@@ -79,6 +81,20 @@ NS_ASSUME_NONNULL_BEGIN
7981
tokenUid:(nullable NSString *)tokenUid
8082
transportConfig:(nullable DBTransportDefaultConfig *)transportConfig;
8183

84+
///
85+
/// Convenience initializer.
86+
///
87+
/// @param accessToken An access token object.
88+
/// @param oauthManager The oauthManager instance.
89+
/// @param transportConfig A wrapper around the different parameters that can be set to change network calling behavior.
90+
/// `DBTransportDefaultConfig` offers a number of different constructors to customize networking settings.
91+
///
92+
/// @return An initialized instance.
93+
///
94+
- (instancetype)initWithAccessToken:(DBAccessToken *)accessToken
95+
oauthManager:(DBOAuthManager *)oauthManager
96+
transportConfig:(nullable DBTransportDefaultConfig *)transportConfig;
97+
8298
/// Designated initializer.
8399
///
84100
/// @param client A `DBTransportDefaultClient` used to make network requests.

Source/ObjectiveDropboxOfficial/Shared/Handwritten/DBTeamClient.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#import "DBTransportDefaultClient.h"
99
#import "DBTransportDefaultConfig.h"
1010
#import "DBUserClient.h"
11+
#import "DBOAuthManager+Protected.h"
1112

1213
@implementation DBTeamClient
1314

@@ -39,6 +40,21 @@ - (instancetype)initWithAccessTokenProvider:(id<DBAccessTokenProvider>)accessTok
3940
return [self initWithTransportClient:transportClient];
4041
}
4142

43+
- (instancetype)initWithAccessToken:(DBAccessToken *)accessToken
44+
oauthManager:(DBOAuthManager *)oauthManager
45+
transportConfig:(DBTransportDefaultConfig *)transportConfig {
46+
NSCParameterAssert(oauthManager);
47+
NSCParameterAssert(accessToken);
48+
49+
id<DBAccessTokenProvider> tokenProvider = [oauthManager accessTokenProviderForToken:accessToken];
50+
51+
DBTransportDefaultClient *transportClient =
52+
[[DBTransportDefaultClient alloc] initWithAccessTokenProvider:tokenProvider
53+
tokenUid:accessToken.uid
54+
transportConfig:transportConfig];
55+
return [self initWithTransportClient:transportClient];
56+
}
57+
4258
- (instancetype)initWithTransportClient:(DBTransportDefaultClient *)client {
4359
if (self = [super initWithTransportClient:client]) {
4460
_tokenUid = client.tokenUid;

Source/ObjectiveDropboxOfficial/Shared/Handwritten/DBUserClient.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
@class DBTransportDefaultClient;
1111
@class DBTransportDefaultConfig;
1212
@protocol DBAccessTokenProvider;
13+
@class DBAccessToken;
14+
@class DBOAuthManager;
1315

1416
NS_ASSUME_NONNULL_BEGIN
1517

@@ -79,6 +81,20 @@ NS_ASSUME_NONNULL_BEGIN
7981
tokenUid:(nullable NSString *)tokenUid
8082
transportConfig:(nullable DBTransportDefaultConfig *)transportConfig;
8183

84+
///
85+
/// Convenience initializer.
86+
///
87+
/// @param accessToken An access token object.
88+
/// @param oauthManager The oauthManager instance.
89+
/// @param transportConfig A wrapper around the different parameters that can be set to change network calling behavior.
90+
/// `DBTransportDefaultConfig` offers a number of different constructors to customize networking settings.
91+
///
92+
/// @return An initialized instance.
93+
///
94+
- (instancetype)initWithAccessToken:(DBAccessToken *)accessToken
95+
oauthManager:(DBOAuthManager *)oauthManager
96+
transportConfig:(nullable DBTransportDefaultConfig *)transportConfig;
97+
8298
/// Designated initializer.
8399
///
84100
/// @param client A `DBTransportDefaultClient` used to make network requests.

Source/ObjectiveDropboxOfficial/Shared/Handwritten/DBUserClient.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#import "DBAccessTokenProvider.h"
88
#import "DBTransportDefaultClient.h"
99
#import "DBTransportDefaultConfig.h"
10+
#import "DBOAuthManager+Protected.h"
1011

1112
@implementation DBUserClient
1213

@@ -38,6 +39,20 @@ - (instancetype)initWithAccessTokenProvider:(id<DBAccessTokenProvider>)accessTok
3839
return [self initWithTransportClient:transportClient];
3940
}
4041

42+
- (instancetype)initWithAccessToken:(DBAccessToken *)accessToken
43+
oauthManager:(DBOAuthManager *)oauthManager
44+
transportConfig:(DBTransportDefaultConfig *)transportConfig {
45+
NSCParameterAssert(oauthManager);
46+
NSCParameterAssert(accessToken);
47+
id<DBAccessTokenProvider> tokenProvider = [oauthManager accessTokenProviderForToken:accessToken];
48+
49+
DBTransportDefaultClient *transportClient =
50+
[[DBTransportDefaultClient alloc] initWithAccessTokenProvider:tokenProvider
51+
tokenUid:accessToken.uid
52+
transportConfig:transportConfig];
53+
return [self initWithTransportClient:transportClient];
54+
}
55+
4156
- (instancetype)initWithTransportClient:(DBTransportDefaultClient *)client {
4257
if (self = [super initWithTransportClient:client]) {
4358
_tokenUid = client.tokenUid;

TestObjectiveDropbox/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PODS:
2-
- ObjectiveDropboxOfficial (6.0.1)
2+
- ObjectiveDropboxOfficial (6.1.0)
33

44
DEPENDENCIES:
55
- ObjectiveDropboxOfficial (from `../`)
@@ -9,7 +9,7 @@ EXTERNAL SOURCES:
99
:path: "../"
1010

1111
SPEC CHECKSUMS:
12-
ObjectiveDropboxOfficial: f6357b632e763f3f6bec2df41d9e66372f5fa401
12+
ObjectiveDropboxOfficial: b4765572e334d6fc6214b43a7595510324bbbbaa
1313

1414
PODFILE CHECKSUM: 9fd03646bd8426ab0dfe9b4eaa0407fd51d7b8ff
1515

TestObjectiveDropbox/TestObjectiveDropbox_iOSTests/FileRoutesTests.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ - (DBUserClient *)createUserClient {
2222
// Then follow https://dropbox.tech/developers/pkce--what-and-why- to get a refresh token using the PKCE flow
2323
NSString *apiAppKey = [TestAuthTokenGenerator environmentVariableForKey:@"FULL_DROPBOX_API_APP_KEY"];
2424

25-
NSString *fileRoutesTestsAuthToken = [TestAuthTokenGenerator
25+
DBAccessToken *fileRoutesTestsAuthToken = [TestAuthTokenGenerator
2626
refreshToken:[TestAuthTokenGenerator environmentVariableForKey:@"FULL_DROPBOX_TESTER_USER_REFRESH_TOKEN"]
2727
apiKey:apiAppKey
2828
scopes:[DropboxTester scopesForTests]];
@@ -38,8 +38,8 @@ - (DBUserClient *)createUserClient {
3838
forceForegroundSession:YES // NO here will cause downloadURL to fail on OSX
3939
sharedContainerIdentifier:nil];
4040

41-
return [[DBUserClient alloc] initWithAccessToken:fileRoutesTestsAuthToken
42-
transportConfig:transportConfigFullDropbox];
41+
DBOAuthManager *manager = [[DBOAuthManager alloc] initWithAppKey:transportConfigFullDropbox.appKey];
42+
return [[DBUserClient alloc] initWithAccessToken:fileRoutesTestsAuthToken oauthManager:manager transportConfig:transportConfigFullDropbox];
4343
}
4444

4545
- (void)setUp {

TestObjectiveDropbox/TestObjectiveDropbox_iOSTests/TeamRoutesTests.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ - (DBTeamClient *)createTeamClient {
3434
// Then follow https://dropbox.tech/developers/pkce--what-and-why- to get a refresh token using the PKCE flow
3535

3636
NSString *apiAppKey = [TestAuthTokenGenerator environmentVariableForKey:@"FULL_DROPBOX_API_APP_KEY"];
37-
NSString *teamRoutesTestsAuthToken = [TestAuthTokenGenerator
37+
DBAccessToken *teamRoutesTestsAuthToken = [TestAuthTokenGenerator
3838
refreshToken:[TestAuthTokenGenerator environmentVariableForKey:@"FULL_DROPBOX_TESTER_TEAM_REFRESH_TOKEN"]
3939
apiKey:apiAppKey
4040
scopes:[DropboxTeamTester scopesForTests]];
@@ -50,7 +50,9 @@ - (DBTeamClient *)createTeamClient {
5050
forceForegroundSession:YES // NO here will cause downloadURL to fail on OSX
5151
sharedContainerIdentifier:nil];
5252

53-
return [[DBTeamClient alloc] initWithAccessToken:teamRoutesTestsAuthToken transportConfig:transportConfigFullDropbox];
53+
54+
DBOAuthManager *manager = [[DBOAuthManager alloc] initWithAppKey:transportConfigFullDropbox.appKey];
55+
return [[DBTeamClient alloc] initWithAccessToken:teamRoutesTestsAuthToken oauthManager:manager transportConfig:transportConfigFullDropbox];
5456
}
5557

5658
- (void)testTeammemberManagement {
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
@class DBAccessToken;
12

23
@interface TestAuthTokenGenerator : NSObject
3-
+ (nonnull NSString *)environmentVariableForKey:(NSString *)key;
4+
+ (nonnull NSString *)environmentVariableForKey:(nonnull NSString *)key;
45

5-
+ (nullable NSString *)refreshToken:(nullable NSString *)refreshToken
6+
+ (nullable DBAccessToken *)refreshToken:(nullable NSString *)refreshToken
67
apiKey:(nullable NSString *)apiKey
78
scopes:(nonnull NSArray<NSString *>*)scopes;
89
@end

TestObjectiveDropbox/TestObjectiveDropbox_iOSTests/TestAuthTokenGenerator.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ + (NSString *)environmentVariableForKey:(NSString *)key {
1414
}
1515

1616
// Easy way for all tests to get an auth token for the scopes they use.
17-
+ (nullable NSString *)refreshToken:(nullable NSString *)refreshToken
17+
+ (nullable DBAccessToken *)refreshToken:(nullable NSString *)refreshToken
1818
apiKey:(nullable NSString *)apiKey
1919
scopes:(nonnull NSArray<NSString *>*)scopes {
2020
XCTAssertNotEqual(refreshToken.length, 0, @"Error: refreshToken needs to be set");
@@ -34,15 +34,15 @@ + (nullable NSString *)refreshToken:(nullable NSString *)refreshToken
3434

3535
XCTestExpectation *flag = [[XCTestExpectation alloc] init];
3636
DBOAuthManager *manager = [[DBOAuthManager alloc] initWithAppKey:apiKey];
37-
__block NSString *authToken = nil;
37+
__block DBAccessToken *authToken = nil;
3838
[manager refreshAccessToken:defaultToken
3939
scopes:scopes
4040
queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)
4141
completion:^(DBOAuthResult * result) {
4242
if(!result.isSuccess) {
4343
XCTFail(@"Error: failed to refresh access token (%@)", result.errorDescription);
4444
} else {
45-
authToken = result.accessToken.accessToken;
45+
authToken = result.accessToken;
4646
}
4747
[flag fulfill];
4848
}];

0 commit comments

Comments
 (0)