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

Commit cf6b66f

Browse files
author
Vikas Dadheech
committed
Add authentication provider options protocol and auth handler options
1 parent cdfaa24 commit cf6b66f

File tree

14 files changed

+202
-28
lines changed

14 files changed

+202
-28
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 */,
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: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#import "MSMiddlewareFactory.h"
99
#import "MSRedirectHandler.h"
1010
#import "MSRetryHandler.h"
11+
#import "MSAuthenticationHandlerOptions.h"
12+
#import "MSRedirectHandlerOptions.h"
13+
#import "MSRetryHandlerOptions.h"
1114

1215
@implementation MSClientFactory
1316

@@ -18,11 +21,10 @@ +(MSHTTPClient *)createHTTPClientWithAuthenticationProvider:(id<MSAuthentication
1821
//Creating a default chain of middlewares starting from Authentication
1922

2023
//Initializing different default middlewares
21-
MSAuthenticationHandler *authenticationHandler = (MSAuthenticationHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeAuthentication];
22-
authenticationHandler.authProvider = authenticationProvider;
23-
MSRedirectHandler *redirectHandler = (MSRedirectHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRedirect];
24-
MSRetryHandler *retryHandler = (MSRetryHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRetry];
25-
MSURLSessionManager *sessionManager = (MSURLSessionManager *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeHTTP];
24+
MSAuthenticationHandler *authenticationHandler = (MSAuthenticationHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeAuthentication withOptions:[[MSAuthenticationHandlerOptions alloc] initWithAuthenticationProvider:authenticationProvider]];
25+
MSRedirectHandler *redirectHandler = (MSRedirectHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRedirect withOptions:[[MSRedirectHandlerOptions alloc] init]];
26+
MSRetryHandler *retryHandler = (MSRetryHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRetry withOptions:[[MSRetryHandlerOptions alloc] init]];
27+
MSURLSessionManager *sessionManager = (MSURLSessionManager *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeHTTP withOptions:nil];
2628
//Creating a default chain
2729
[authenticationHandler setNext:redirectHandler];
2830
[redirectHandler setNext:retryHandler];
@@ -39,11 +41,10 @@ +(MSHTTPClient *)createHTTPClientWithAuthenticationProvider:(id<MSAuthentication
3941
//Creating a default chain of middlewares starting from Authentication
4042

4143
//Initializing different default middlewares
42-
MSAuthenticationHandler *authenticationHandler = (MSAuthenticationHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeAuthentication];
43-
authenticationHandler.authProvider = authenticationProvider;
44-
MSRedirectHandler *redirectHandler = (MSRedirectHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRedirect];
45-
MSRetryHandler *retryHandler = (MSRetryHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRetry];
46-
44+
MSAuthenticationHandler *authenticationHandler = (MSAuthenticationHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeAuthentication withOptions:[[MSAuthenticationHandlerOptions alloc] initWithAuthenticationProvider:authenticationProvider]];
45+
MSRedirectHandler *redirectHandler = (MSRedirectHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRedirect withOptions:[[MSRedirectHandlerOptions alloc] init]];
46+
MSRetryHandler *retryHandler = (MSRetryHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRetry withOptions:[[MSRetryHandlerOptions alloc] init]];
47+
4748
//Create session manager with custom session configuration
4849
MSURLSessionManager *sessionManager = [[MSURLSessionManager alloc] initWithSessionConfiguration:sessionConfiguration];
4950

MSGraphClientSDK/MSGraphClientSDK/HTTPClient/MSMiddlewareFactory.h

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

55
#import <Foundation/Foundation.h>
66
#import "MSGraphMiddleware.h"
7+
#import "MSMiddlewareOptions.h"
78

89
@interface MSMiddlewareFactory : NSObject
910

@@ -23,6 +24,6 @@ typedef NS_ENUM(NSInteger, MSMiddlewareType)
2324
@param middlewareType Type of middleware which this method will create
2425
@return The middleware object of given type.
2526
*/
26-
+(id<MSGraphMiddleware>)createMiddleware:(MSMiddlewareType)middlewareType;
27+
+(id<MSGraphMiddleware>)createMiddleware:(MSMiddlewareType)middlewareType withOptions:(id<MSMiddlewareOptions>)middlewareOptions;
2728

2829
@end

MSGraphClientSDK/MSGraphClientSDK/HTTPClient/MSMiddlewareFactory.m

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
@implementation MSMiddlewareFactory
1212

13-
+(id<MSGraphMiddleware>)createMiddleware:(MSMiddlewareType)middlewareType
13+
+(id<MSGraphMiddleware>)createMiddleware:(MSMiddlewareType)middlewareType withOptions:(id<MSMiddlewareOptions>)middlewareOptions
1414
{
1515
switch (middlewareType)
1616
{
@@ -21,19 +21,37 @@ @implementation MSMiddlewareFactory
2121
}
2222
case MSMiddlewareTypeRedirect:
2323
{
24-
MSRedirectHandler *redirectHandler = [[MSRedirectHandler alloc] init];
24+
MSRedirectHandler *redirectHandler;
25+
if(middlewareOptions)
26+
{
27+
redirectHandler = [[MSRedirectHandler alloc] initWithOptions:(MSRedirectHandlerOptions *)middlewareOptions];
28+
}else{
29+
redirectHandler = [[MSRedirectHandler alloc] init];
30+
}
2531
return redirectHandler;
2632
}
27-
case MSMiddlewareTypeAuthentication:
28-
{
29-
MSAuthenticationHandler *authenticationHandler = [[MSAuthenticationHandler alloc] init];
30-
return authenticationHandler;
31-
}
3233
case MSMiddlewareTypeRetry:
3334
{
34-
MSRetryHandler *retryHandler = [[MSRetryHandler alloc] init];
35+
MSRetryHandler *retryHandler;
36+
if(middlewareOptions)
37+
{
38+
retryHandler = [[MSRetryHandler alloc] initWithOptions:(MSRetryHandlerOptions *)middlewareOptions];
39+
}else{
40+
retryHandler = [[MSRetryHandler alloc] init];
41+
}
3542
return retryHandler;
3643
}
44+
case MSMiddlewareTypeAuthentication:
45+
{
46+
MSAuthenticationHandler *authenticationHandler;
47+
if(middlewareOptions)
48+
{
49+
authenticationHandler = [[MSAuthenticationHandler alloc] initWithOptions:(MSAuthenticationHandlerOptions *)middlewareOptions];
50+
}else{
51+
authenticationHandler = [[MSAuthenticationHandler alloc] init];
52+
}
53+
return authenticationHandler;
54+
}
3755
default:
3856
break;
3957
}

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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
#import "MSAuthenticationProvider.h"
77
#import "MSGraphMiddleware.h"
88

9+
@class MSAuthenticationHandlerOptions;
10+
911
@interface MSAuthenticationHandler : NSObject<MSGraphMiddleware>
1012

11-
@property (nonatomic, strong) id<MSAuthenticationProvider> authProvider;
13+
@property (nonatomic, strong, readonly) MSAuthenticationHandlerOptions *authHandlerOptions;
14+
15+
- (instancetype)initWithOptions:(MSAuthenticationHandlerOptions *)authHandlerOptions;
1216

1317
@end

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

Lines changed: 17 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,24 @@ @interface MSAuthenticationHandler()
2021

2122
@implementation MSAuthenticationHandler
2223

24+
- (instancetype)initWithOptions:(MSAuthenticationHandlerOptions *)authHandlerOptions
25+
{
26+
self = [super init];
27+
if(self)
28+
{
29+
_authHandlerOptions = authHandlerOptions;
30+
}
31+
return self;
32+
}
33+
2334
- (void)execute:(MSURLSessionTask *)task withCompletionHandler:(HTTPRequestCompletionHandler)completionHandler
2435
{
25-
[self.authProvider getAccessTokenWithCompletion:^(NSString *accessToken, NSError *error) {
36+
MSAuthenticationHandlerOptions *authHandlerOptions = [task getMiddlewareOptionWithType:MSMiddlewareOptionsTypeAuth];
37+
if(!authHandlerOptions)
38+
{
39+
authHandlerOptions = _authHandlerOptions;
40+
}
41+
[authHandlerOptions.authenticationProvider getAccessTokenWithCompletion:^(NSString *accessToken, NSError *error) {
2642
if(!error)
2743
{
2844
NSMutableURLRequest *urlRequest = [task request];
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
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, readonly) id<MSAuthenticationProvider> authenticationProvider;
19+
20+
/*
21+
This method initializes an instance of MSAuthenticationHandlerOptions with the provided parameter.
22+
@param authProvider The authentication provider instance which will be used to authenticate the ongoing requests.
23+
@return The MSAuthenticationHandlerOptions instance with the provided value.
24+
*/
25+
- (instancetype)initWithAuthenticationProvider:(nonnull id<MSAuthenticationProvider>)authProvider;
26+
27+
@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
13+
{
14+
self = [super init];
15+
if(self)
16+
{
17+
NSAssert(authProvider, @"Authentication Provider is requried to authenticate the requests.");
18+
_authenticationProvider = authProvider;
19+
middlewareOptionsType = MSMiddlewareOptionsTypeAuth;
20+
}
21+
return self;
22+
}
23+
24+
25+
@end

0 commit comments

Comments
 (0)