Skip to content
This repository was archived by the owner on Apr 18, 2023. It is now read-only.

Commit f0274de

Browse files
authored
Merge pull request #25 from microsoftgraph/vidadhee/AuthProviderOptions
Add authentication provider options protocol and auth handler options
2 parents 7f22fea + 82ef637 commit f0274de

File tree

12 files changed

+189
-16
lines changed

12 files changed

+189
-16
lines changed

MSGraphClientSDK/MSGraphClientSDK.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
E54D1237226472FB00EB517A /* MSLargeFileUploadTaskTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E54D1236226472FB00EB517A /* MSLargeFileUploadTaskTests.m */; };
4444
E54D123B2264858100EB517A /* LargeFileUploadResource.bmp in Resources */ = {isa = PBXBuildFile; fileRef = E54D123A2264858100EB517A /* LargeFileUploadResource.bmp */; };
4545
E54D123D2264AEBD00EB517A /* MSGraphOneDriveLargeFileUploadTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E54D123C2264AEBD00EB517A /* MSGraphOneDriveLargeFileUploadTests.m */; };
46+
E54D12772270A39200EB517A /* MSAuthenticationHandlerOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = E54D12752270A39200EB517A /* MSAuthenticationHandlerOptions.h */; settings = {ATTRIBUTES = (Public, ); }; };
47+
E54D12782270A39200EB517A /* MSAuthenticationHandlerOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = E54D12762270A39200EB517A /* MSAuthenticationHandlerOptions.m */; };
48+
E54D12892271C91D00EB517A /* MSAuthenticationProviderOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = E54D123E226EF79C00EB517A /* MSAuthenticationProviderOptions.h */; settings = {ATTRIBUTES = (Public, ); }; };
49+
E54D128F22733FCD00EB517A /* MSAuthenticationHandlerOptionsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E54D128E22733FCD00EB517A /* MSAuthenticationHandlerOptionsTests.m */; };
4650
E566782E2150BC4F00C6B7AE /* MSRedirectHandlerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E566782D2150BC4F00C6B7AE /* MSRedirectHandlerTests.m */; };
4751
E56678302150E1AF00C6B7AE /* MSAuthenticationHandlerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E566782F2150E1AF00C6B7AE /* MSAuthenticationHandlerTests.m */; };
4852
E56B101C2220086300ED640F /* MSRedirectHandlerOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = E56B101A2220086300ED640F /* MSRedirectHandlerOptions.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -135,6 +139,10 @@
135139
E54D1236226472FB00EB517A /* MSLargeFileUploadTaskTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MSLargeFileUploadTaskTests.m; sourceTree = "<group>"; };
136140
E54D123A2264858100EB517A /* LargeFileUploadResource.bmp */ = {isa = PBXFileReference; lastKnownFileType = image.bmp; path = LargeFileUploadResource.bmp; sourceTree = "<group>"; };
137141
E54D123C2264AEBD00EB517A /* MSGraphOneDriveLargeFileUploadTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MSGraphOneDriveLargeFileUploadTests.m; sourceTree = "<group>"; };
142+
E54D123E226EF79C00EB517A /* MSAuthenticationProviderOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MSAuthenticationProviderOptions.h; sourceTree = "<group>"; };
143+
E54D12752270A39200EB517A /* MSAuthenticationHandlerOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MSAuthenticationHandlerOptions.h; sourceTree = "<group>"; };
144+
E54D12762270A39200EB517A /* MSAuthenticationHandlerOptions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MSAuthenticationHandlerOptions.m; sourceTree = "<group>"; };
145+
E54D128E22733FCD00EB517A /* MSAuthenticationHandlerOptionsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MSAuthenticationHandlerOptionsTests.m; sourceTree = "<group>"; };
138146
E566782D2150BC4F00C6B7AE /* MSRedirectHandlerTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MSRedirectHandlerTests.m; sourceTree = "<group>"; };
139147
E566782F2150E1AF00C6B7AE /* MSAuthenticationHandlerTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MSAuthenticationHandlerTests.m; sourceTree = "<group>"; };
140148
E56B101A2220086300ED640F /* MSRedirectHandlerOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MSRedirectHandlerOptions.h; sourceTree = "<group>"; };
@@ -215,6 +223,7 @@
215223
isa = PBXGroup;
216224
children = (
217225
E5DC73F6211418040040BAB6 /* MSAuthenticationProvider.h */,
226+
E54D123E226EF79C00EB517A /* MSAuthenticationProviderOptions.h */,
218227
);
219228
path = Authentication;
220229
sourceTree = "<group>";
@@ -363,6 +372,7 @@
363372
E56B101E22237F3900ED640F /* Options */ = {
364373
isa = PBXGroup;
365374
children = (
375+
E54D128E22733FCD00EB517A /* MSAuthenticationHandlerOptionsTests.m */,
366376
E56B101F22237F6500ED640F /* MSRetryHandlerOptionsTests.m */,
367377
E56B1021222382E400ED640F /* MSRedirectHandlerOptionsTests.m */,
368378
);
@@ -425,6 +435,8 @@
425435
E59EBFED221D3B6700B4B279 /* MSRetryHandlerOptions.m */,
426436
E56B101A2220086300ED640F /* MSRedirectHandlerOptions.h */,
427437
E56B101B2220086300ED640F /* MSRedirectHandlerOptions.m */,
438+
E54D12752270A39200EB517A /* MSAuthenticationHandlerOptions.h */,
439+
E54D12762270A39200EB517A /* MSAuthenticationHandlerOptions.m */,
428440
);
429441
path = Options;
430442
sourceTree = "<group>";
@@ -557,6 +569,7 @@
557569
files = (
558570
E5DC7407211418050040BAB6 /* MSURLSessionManager.h in Headers */,
559571
E5DC741C21142C430040BAB6 /* MSClientFactory.h in Headers */,
572+
E54D12892271C91D00EB517A /* MSAuthenticationProviderOptions.h in Headers */,
560573
E5DC740E211418050040BAB6 /* MSHttpProvider.h in Headers */,
561574
E5DC740C211418050040BAB6 /* MSAuthenticationProvider.h in Headers */,
562575
E5DC7410211418050040BAB6 /* MSGraphMiddleware.h in Headers */,
@@ -580,6 +593,7 @@
580593
E56B101C2220086300ED640F /* MSRedirectHandlerOptions.h in Headers */,
581594
E54D123222646CA900EB517A /* MSGraphOneDriveLargeFileUploadTask.h in Headers */,
582595
E54D123322646CA900EB517A /* MSLargeFileUploadTask.h in Headers */,
596+
E54D12772270A39200EB517A /* MSAuthenticationHandlerOptions.h in Headers */,
583597
E54D1220225B59B300EB517A /* MSPageIterator.h in Headers */,
584598
E5DC73DF211412BF0040BAB6 /* MSGraphClientSDK.h in Headers */,
585599
);
@@ -692,6 +706,7 @@
692706
E5204FFF2173A7C200E64160 /* MSBatchRequestStepTests.m in Sources */,
693707
E52050042174C1DB00E64160 /* MSBatchResponseContentTests.m in Sources */,
694708
E51E616F216E073F00F1B3C0 /* MSRetryHandlerTests.m in Sources */,
709+
E54D128F22733FCD00EB517A /* MSAuthenticationHandlerOptionsTests.m in Sources */,
695710
E54D123D2264AEBD00EB517A /* MSGraphOneDriveLargeFileUploadTests.m in Sources */,
696711
E56B1022222382E400ED640F /* MSRedirectHandlerOptionsTests.m in Sources */,
697712
E573EE072133FFF200C5E884 /* MSHTTPClientTests.m in Sources */,
@@ -724,6 +739,7 @@
724739
E56B101D2220086300ED640F /* MSRedirectHandlerOptions.m in Sources */,
725740
E5DC741A21142C430040BAB6 /* MSClientFactory.m in Sources */,
726741
E5069AC9211AAEC900432372 /* MSConstants.m in Sources */,
742+
E54D12782270A39200EB517A /* MSAuthenticationHandlerOptions.m in Sources */,
727743
E56FDB9821D36A98001DFE68 /* MSURLSessionDownloadTask.m in Sources */,
728744
E56FDB9921D36A98001DFE68 /* MSURLSessionUploadTask.m in Sources */,
729745
E5ECE0A52146657900A289C2 /* MSRedirectHandler.m in Sources */,

MSGraphClientSDK/MSGraphClientSDK/Authentication/MSAuthenticationProvider.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44

55
#import <Foundation/Foundation.h>
6+
#import "MSAuthenticationProviderOptions.h"
67

78
typedef void(^MSAuthenticationCompletion)(NSMutableURLRequest *request, NSError *error);
89

@@ -14,9 +15,10 @@ typedef void(^MSAuthenticationCompletion)(NSMutableURLRequest *request, NSError
1415

1516
/**
1617
Gets the access token. This method should be implemented by a class which should provide the capability of providing the access token.
18+
@param authProviderOptions Options which can be used to control the behaviour of AuthenticationProvider
1719
@param completion The completion handler to be called when access token or an error can be returned.
1820
*/
1921

20-
- (void) getAccessTokenWithCompletion:(void (^)(NSString *accessToken, NSError *error))completion;
22+
- (void) getAccessTokenForProviderOptions:(id<MSAuthenticationProviderOptions>)authProviderOptions andCompletion:(void (^)(NSString *accessToken, NSError *error))completion;
2123

2224
@end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
3+
//
4+
5+
#import <Foundation/Foundation.h>
6+
7+
/**
8+
The `MSAuthenticationProviderOptions` is a protocol that is used to define the contract for the options required by AuthenticationProvider */
9+
@protocol MSAuthenticationProviderOptions <NSObject>
10+
11+
@required
12+
13+
/*
14+
This property is added to ensure that every option class sets the type of it so that during execution middlewares can pick up the option which is requried by it's implementation.
15+
*/
16+
@property NSArray<NSString *> *scopesArray;
17+
18+
@end

MSGraphClientSDK/MSGraphClientSDK/HTTPClient/MSClientFactory.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ +(MSHTTPClient *)createHTTPClientWithAuthenticationProvider:(id<MSAuthentication
1919

2020
//Initializing different default middlewares
2121
MSAuthenticationHandler *authenticationHandler = (MSAuthenticationHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeAuthentication];
22-
authenticationHandler.authProvider = authenticationProvider;
22+
authenticationHandler.authenticationProvider = authenticationProvider;
2323
MSRedirectHandler *redirectHandler = (MSRedirectHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRedirect];
2424
MSRetryHandler *retryHandler = (MSRetryHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRetry];
2525
MSURLSessionManager *sessionManager = (MSURLSessionManager *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeHTTP];
@@ -40,10 +40,10 @@ +(MSHTTPClient *)createHTTPClientWithAuthenticationProvider:(id<MSAuthentication
4040

4141
//Initializing different default middlewares
4242
MSAuthenticationHandler *authenticationHandler = (MSAuthenticationHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeAuthentication];
43-
authenticationHandler.authProvider = authenticationProvider;
43+
authenticationHandler.authenticationProvider = authenticationProvider;
4444
MSRedirectHandler *redirectHandler = (MSRedirectHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRedirect];
4545
MSRetryHandler *retryHandler = (MSRetryHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRetry];
46-
46+
4747
//Create session manager with custom session configuration
4848
MSURLSessionManager *sessionManager = [[MSURLSessionManager alloc] initWithSessionConfiguration:sessionConfiguration];
4949

MSGraphClientSDK/MSGraphClientSDK/MSGraphClientSDK.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ FOUNDATION_EXPORT const unsigned char MSGraphClientSDKVersionString[];
1818
#define MSSDK_h
1919

2020
#import "MSGraphClientSDK/MSAuthenticationProvider.h"
21+
#import "MSGraphClientSDK/MSAuthenticationProviderOptions.h"
2122
#import "MSGraphClientSDK/MSHttpProvider.h"
2223
#import "MSGraphClientSDK/MSGraphMiddleware.h"
2324
#import "MSGraphClientSDK/MSMiddlewareOptions.h"
@@ -43,6 +44,7 @@ FOUNDATION_EXPORT const unsigned char MSGraphClientSDKVersionString[];
4344

4445
#import "MSGraphClientSDK/MSRetryHandlerOptions.h"
4546
#import "MSGraphClientSDK/MSRedirectHandlerOptions.h"
47+
#import "MSGraphClientSDK/MSAuthenticationHandlerOptions.h"
4648

4749
#import "MSGraphClientSDK/MSPageIterator.h"
4850
#import "MSGraphClientSDK/MSLargeFileUploadTask.h"

MSGraphClientSDK/MSGraphClientSDK/Middleware/Implementations/Authentication/MSAuthenticationHandler.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,20 @@
66
#import "MSAuthenticationProvider.h"
77
#import "MSGraphMiddleware.h"
88

9+
/*
10+
This class provides the mechanism to communicate with authentication provider. It is implmented as a middleware so it will be called during the execution of network calls.
11+
*/
12+
913
@interface MSAuthenticationHandler : NSObject<MSGraphMiddleware>
1014

11-
@property (nonatomic, strong) id<MSAuthenticationProvider> authProvider;
15+
//Authentication provider which will be used to get access token
16+
@property (nonatomic, strong) id<MSAuthenticationProvider> authenticationProvider;
17+
18+
/*
19+
This method creates and returns an instance of MSAuthenticationHandler
20+
@param authProvider Authentication Provider instance
21+
@return An instance of MSAuthenticationHandler
22+
*/
23+
- (instancetype)initWithAuthenticationProvider:(id<MSAuthenticationProvider>)authProvider;
1224

1325
@end

MSGraphClientSDK/MSGraphClientSDK/Middleware/Implementations/Authentication/MSAuthenticationHandler.m

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#import "MSAuthenticationHandler.h"
66
#import "MSURLSessionTask.h"
7+
#import "MSAuthenticationHandlerOptions.h"
78

89
@interface MSURLSessionTask()
910

@@ -20,9 +21,28 @@ @interface MSAuthenticationHandler()
2021

2122
@implementation MSAuthenticationHandler
2223

24+
- (instancetype)initWithAuthenticationProvider:(id<MSAuthenticationProvider>)authProvider
25+
{
26+
self = [super init];
27+
if(self)
28+
{
29+
_authenticationProvider = authProvider;
30+
}
31+
return self;
32+
}
33+
34+
- (void)setAuthenticationProvider:(id<MSAuthenticationProvider>)authProvider
35+
{
36+
_authenticationProvider = authProvider;
37+
}
38+
2339
- (void)execute:(MSURLSessionTask *)task withCompletionHandler:(HTTPRequestCompletionHandler)completionHandler
2440
{
25-
[self.authProvider getAccessTokenWithCompletion:^(NSString *accessToken, NSError *error) {
41+
MSAuthenticationHandlerOptions *authHandlerOptions = [task getMiddlewareOptionWithType:MSMiddlewareOptionsTypeAuth];
42+
43+
id<MSAuthenticationProvider> authProvider = authHandlerOptions.authenticationProvider?authHandlerOptions.authenticationProvider:_authenticationProvider;
44+
45+
[authProvider getAccessTokenForProviderOptions:authHandlerOptions.authenticationProviderOptions andCompletion:^(NSString *accessToken, NSError *error) {
2646
if(!error)
2747
{
2848
NSMutableURLRequest *urlRequest = [task request];
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
3+
//
4+
5+
#import <Foundation/Foundation.h>
6+
#import "MSMiddlewareOptions.h"
7+
#import "MSAuthenticationProvider.h"
8+
#import "MSAuthenticationProviderOptions.h"
9+
/*
10+
This class provides options to control the behaviour of Authentication Handler.
11+
*/
12+
13+
@interface MSAuthenticationHandlerOptions : NSObject <MSMiddlewareOptions>
14+
15+
/*
16+
Authentication Provider instance which will be used to authenticate the ongoing requests.
17+
*/
18+
@property (nonatomic, strong, readonly) id<MSAuthenticationProvider> authenticationProvider;
19+
20+
@property (nonatomic, strong, readonly) id<MSAuthenticationProviderOptions> authenticationProviderOptions;
21+
22+
/*
23+
This method initializes an instance of MSAuthenticationHandlerOptions with the provided parameter.
24+
@param authProvider The authentication provider instance which will be used to authenticate the ongoing requests.
25+
@param authProviderOptions The authentication provider options which will be used to control the behaviour of authentication provider being used to authenticate the ongoing requests.
26+
@return The MSAuthenticationHandlerOptions instance with the provided value.
27+
*/
28+
- (instancetype)initWithAuthenticationProvider:(id<MSAuthenticationProvider>)authProvider andAuthProviderOptions:(id<MSAuthenticationProviderOptions>)authProviderOptions;
29+
30+
@end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
3+
//
4+
5+
6+
#import "MSAuthenticationHandlerOptions.h"
7+
8+
@implementation MSAuthenticationHandlerOptions
9+
10+
@synthesize middlewareOptionsType;
11+
12+
- (instancetype)initWithAuthenticationProvider:(id<MSAuthenticationProvider>)authProvider andAuthProviderOptions:(id<MSAuthenticationProviderOptions>)authProviderOptions
13+
{
14+
self = [super init];
15+
if(self)
16+
{
17+
_authenticationProvider = authProvider;
18+
_authenticationProviderOptions = authProviderOptions;
19+
middlewareOptionsType = MSMiddlewareOptionsTypeAuth;
20+
}
21+
return self;
22+
}
23+
24+
25+
@end

MSGraphClientSDK/MSGraphClientSDKTests/MSGraphWorkloadsTests.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ @implementation MSGraphWorkloadsTests
3030

3131
- (void)setUp {
3232
[super setUp];
33-
OCMStub([self.mockAuthProvider getAccessTokenWithCompletion:[OCMArg any]])
33+
OCMStub([self.mockAuthProvider getAccessTokenForProviderOptions:[OCMArg any] andCompletion:[OCMArg any]])
3434
.andDo(^(NSInvocation *invocation){
3535
void (^completionHandler)(NSString *accessToken, NSError *error);
36-
[invocation getArgument:&completionHandler atIndex:2];
36+
[invocation getArgument:&completionHandler atIndex:3];
3737
completionHandler(@"abcdefg",nil);
3838
});
3939
_OKresponse = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:MSGraphBaseURL] statusCode:MSExpectedResponseCodesOK HTTPVersion:@"foo" headerFields:nil];
4040

4141
MSURLSessionManager *sessionManager = [[MSURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
4242
MSRedirectHandler *redirectHandler = [[MSRedirectHandler alloc] init];
4343
MSRetryHandler *retryHandler = [[MSRetryHandler alloc] init];
44-
MSAuthenticationHandler *authHandler = [[MSAuthenticationHandler alloc] init];
45-
authHandler.authProvider = self.mockAuthProvider;
44+
45+
MSAuthenticationHandler *authHandler = [[MSAuthenticationHandler alloc] initWithAuthenticationProvider:self.mockAuthProvider];
4646
[authHandler setNext:redirectHandler];
4747
[redirectHandler setNext:retryHandler];
4848
[retryHandler setNext:sessionManager];

0 commit comments

Comments
 (0)