Skip to content

Commit

Permalink
Expose inits that allow the clients to be created with access token s…
Browse files Browse the repository at this point in the history
…o short lived tokens can be used (#326)
  • Loading branch information
YufeiG committed Sep 7, 2021
1 parent e3a42c1 commit f03fe37
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 12 deletions.
16 changes: 16 additions & 0 deletions Source/ObjectiveDropboxOfficial/Shared/Handwritten/DBTeamClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
@class DBTransportDefaultClient;
@class DBTransportDefaultConfig;
@protocol DBAccessTokenProvider;
@class DBAccessToken;
@class DBOAuthManager;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -79,6 +81,20 @@ NS_ASSUME_NONNULL_BEGIN
tokenUid:(nullable NSString *)tokenUid
transportConfig:(nullable DBTransportDefaultConfig *)transportConfig;

///
/// Convenience initializer.
///
/// @param accessToken An access token object.
/// @param oauthManager The oauthManager instance.
/// @param transportConfig A wrapper around the different parameters that can be set to change network calling behavior.
/// `DBTransportDefaultConfig` offers a number of different constructors to customize networking settings.
///
/// @return An initialized instance.
///
- (instancetype)initWithAccessToken:(DBAccessToken *)accessToken
oauthManager:(DBOAuthManager *)oauthManager
transportConfig:(nullable DBTransportDefaultConfig *)transportConfig;

/// Designated initializer.
///
/// @param client A `DBTransportDefaultClient` used to make network requests.
Expand Down
16 changes: 16 additions & 0 deletions Source/ObjectiveDropboxOfficial/Shared/Handwritten/DBTeamClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import "DBTransportDefaultClient.h"
#import "DBTransportDefaultConfig.h"
#import "DBUserClient.h"
#import "DBOAuthManager+Protected.h"

@implementation DBTeamClient

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

- (instancetype)initWithAccessToken:(DBAccessToken *)accessToken
oauthManager:(DBOAuthManager *)oauthManager
transportConfig:(DBTransportDefaultConfig *)transportConfig {
NSCParameterAssert(oauthManager);
NSCParameterAssert(accessToken);

id<DBAccessTokenProvider> tokenProvider = [oauthManager accessTokenProviderForToken:accessToken];

DBTransportDefaultClient *transportClient =
[[DBTransportDefaultClient alloc] initWithAccessTokenProvider:tokenProvider
tokenUid:accessToken.uid
transportConfig:transportConfig];
return [self initWithTransportClient:transportClient];
}

- (instancetype)initWithTransportClient:(DBTransportDefaultClient *)client {
if (self = [super initWithTransportClient:client]) {
_tokenUid = client.tokenUid;
Expand Down
16 changes: 16 additions & 0 deletions Source/ObjectiveDropboxOfficial/Shared/Handwritten/DBUserClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
@class DBTransportDefaultClient;
@class DBTransportDefaultConfig;
@protocol DBAccessTokenProvider;
@class DBAccessToken;
@class DBOAuthManager;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -79,6 +81,20 @@ NS_ASSUME_NONNULL_BEGIN
tokenUid:(nullable NSString *)tokenUid
transportConfig:(nullable DBTransportDefaultConfig *)transportConfig;

///
/// Convenience initializer.
///
/// @param accessToken An access token object.
/// @param oauthManager The oauthManager instance.
/// @param transportConfig A wrapper around the different parameters that can be set to change network calling behavior.
/// `DBTransportDefaultConfig` offers a number of different constructors to customize networking settings.
///
/// @return An initialized instance.
///
- (instancetype)initWithAccessToken:(DBAccessToken *)accessToken
oauthManager:(DBOAuthManager *)oauthManager
transportConfig:(nullable DBTransportDefaultConfig *)transportConfig;

/// Designated initializer.
///
/// @param client A `DBTransportDefaultClient` used to make network requests.
Expand Down
15 changes: 15 additions & 0 deletions Source/ObjectiveDropboxOfficial/Shared/Handwritten/DBUserClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#import "DBAccessTokenProvider.h"
#import "DBTransportDefaultClient.h"
#import "DBTransportDefaultConfig.h"
#import "DBOAuthManager+Protected.h"

@implementation DBUserClient

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

- (instancetype)initWithAccessToken:(DBAccessToken *)accessToken
oauthManager:(DBOAuthManager *)oauthManager
transportConfig:(DBTransportDefaultConfig *)transportConfig {
NSCParameterAssert(oauthManager);
NSCParameterAssert(accessToken);
id<DBAccessTokenProvider> tokenProvider = [oauthManager accessTokenProviderForToken:accessToken];

DBTransportDefaultClient *transportClient =
[[DBTransportDefaultClient alloc] initWithAccessTokenProvider:tokenProvider
tokenUid:accessToken.uid
transportConfig:transportConfig];
return [self initWithTransportClient:transportClient];
}

- (instancetype)initWithTransportClient:(DBTransportDefaultClient *)client {
if (self = [super initWithTransportClient:client]) {
_tokenUid = client.tokenUid;
Expand Down
4 changes: 2 additions & 2 deletions TestObjectiveDropbox/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- ObjectiveDropboxOfficial (6.0.1)
- ObjectiveDropboxOfficial (6.1.0)

DEPENDENCIES:
- ObjectiveDropboxOfficial (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
ObjectiveDropboxOfficial: f6357b632e763f3f6bec2df41d9e66372f5fa401
ObjectiveDropboxOfficial: b4765572e334d6fc6214b43a7595510324bbbbaa

PODFILE CHECKSUM: 9fd03646bd8426ab0dfe9b4eaa0407fd51d7b8ff

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ - (DBUserClient *)createUserClient {
// Then follow https://dropbox.tech/developers/pkce--what-and-why- to get a refresh token using the PKCE flow
NSString *apiAppKey = [TestAuthTokenGenerator environmentVariableForKey:@"FULL_DROPBOX_API_APP_KEY"];

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

return [[DBUserClient alloc] initWithAccessToken:fileRoutesTestsAuthToken
transportConfig:transportConfigFullDropbox];
DBOAuthManager *manager = [[DBOAuthManager alloc] initWithAppKey:transportConfigFullDropbox.appKey];
return [[DBUserClient alloc] initWithAccessToken:fileRoutesTestsAuthToken oauthManager:manager transportConfig:transportConfigFullDropbox];
}

- (void)setUp {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ - (DBTeamClient *)createTeamClient {
// Then follow https://dropbox.tech/developers/pkce--what-and-why- to get a refresh token using the PKCE flow

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

return [[DBTeamClient alloc] initWithAccessToken:teamRoutesTestsAuthToken transportConfig:transportConfigFullDropbox];

DBOAuthManager *manager = [[DBOAuthManager alloc] initWithAppKey:transportConfigFullDropbox.appKey];
return [[DBTeamClient alloc] initWithAccessToken:teamRoutesTestsAuthToken oauthManager:manager transportConfig:transportConfigFullDropbox];
}

- (void)testTeammemberManagement {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
@class DBAccessToken;

@interface TestAuthTokenGenerator : NSObject
+ (nonnull NSString *)environmentVariableForKey:(NSString *)key;
+ (nonnull NSString *)environmentVariableForKey:(nonnull NSString *)key;

+ (nullable NSString *)refreshToken:(nullable NSString *)refreshToken
+ (nullable DBAccessToken *)refreshToken:(nullable NSString *)refreshToken
apiKey:(nullable NSString *)apiKey
scopes:(nonnull NSArray<NSString *>*)scopes;
@end
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ + (NSString *)environmentVariableForKey:(NSString *)key {
}

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

XCTestExpectation *flag = [[XCTestExpectation alloc] init];
DBOAuthManager *manager = [[DBOAuthManager alloc] initWithAppKey:apiKey];
__block NSString *authToken = nil;
__block DBAccessToken *authToken = nil;
[manager refreshAccessToken:defaultToken
scopes:scopes
queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)
completion:^(DBOAuthResult * result) {
if(!result.isSuccess) {
XCTFail(@"Error: failed to refresh access token (%@)", result.errorDescription);
} else {
authToken = result.accessToken.accessToken;
authToken = result.accessToken;
}
[flag fulfill];
}];
Expand Down

0 comments on commit f03fe37

Please sign in to comment.