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

Commit 82ef637

Browse files
author
Vikas Dadheech
committed
Change Authentication Handler options to incorporate provider options
1 parent 655bc44 commit 82ef637

File tree

12 files changed

+77
-78
lines changed

12 files changed

+77
-78
lines changed

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

MSGraphClientSDK/MSGraphClientSDK/HTTPClient/MSClientFactory.m

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
#import "MSMiddlewareFactory.h"
99
#import "MSRedirectHandler.h"
1010
#import "MSRetryHandler.h"
11-
#import "MSAuthenticationHandlerOptions.h"
12-
#import "MSRedirectHandlerOptions.h"
13-
#import "MSRetryHandlerOptions.h"
1411

1512
@implementation MSClientFactory
1613

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

2320
//Initializing different default middlewares
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];
21+
MSAuthenticationHandler *authenticationHandler = (MSAuthenticationHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeAuthentication];
22+
authenticationHandler.authenticationProvider = authenticationProvider;
23+
MSRedirectHandler *redirectHandler = (MSRedirectHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRedirect];
24+
MSRetryHandler *retryHandler = (MSRetryHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRetry];
25+
MSURLSessionManager *sessionManager = (MSURLSessionManager *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeHTTP];
2826
//Creating a default chain
2927
[authenticationHandler setNext:redirectHandler];
3028
[redirectHandler setNext:retryHandler];
@@ -41,9 +39,10 @@ +(MSHTTPClient *)createHTTPClientWithAuthenticationProvider:(id<MSAuthentication
4139
//Creating a default chain of middlewares starting from Authentication
4240

4341
//Initializing different default middlewares
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]];
42+
MSAuthenticationHandler *authenticationHandler = (MSAuthenticationHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeAuthentication];
43+
authenticationHandler.authenticationProvider = authenticationProvider;
44+
MSRedirectHandler *redirectHandler = (MSRedirectHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRedirect];
45+
MSRetryHandler *retryHandler = (MSRetryHandler *)[MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRetry];
4746

4847
//Create session manager with custom session configuration
4948
MSURLSessionManager *sessionManager = [[MSURLSessionManager alloc] initWithSessionConfiguration:sessionConfiguration];

MSGraphClientSDK/MSGraphClientSDK/HTTPClient/MSMiddlewareFactory.h

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

55
#import <Foundation/Foundation.h>
66
#import "MSGraphMiddleware.h"
7-
#import "MSMiddlewareOptions.h"
87

98
@interface MSMiddlewareFactory : NSObject
109

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

2928
@end

MSGraphClientSDK/MSGraphClientSDK/HTTPClient/MSMiddlewareFactory.m

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

1111
@implementation MSMiddlewareFactory
1212

13-
+(id<MSGraphMiddleware>)createMiddleware:(MSMiddlewareType)middlewareType withOptions:(id<MSMiddlewareOptions>)middlewareOptions
13+
+(id<MSGraphMiddleware>)createMiddleware:(MSMiddlewareType)middlewareType
1414
{
1515
switch (middlewareType)
1616
{
@@ -21,37 +21,19 @@ @implementation MSMiddlewareFactory
2121
}
2222
case MSMiddlewareTypeRedirect:
2323
{
24-
MSRedirectHandler *redirectHandler;
25-
if(middlewareOptions)
26-
{
27-
redirectHandler = [[MSRedirectHandler alloc] initWithOptions:(MSRedirectHandlerOptions *)middlewareOptions];
28-
}else{
29-
redirectHandler = [[MSRedirectHandler alloc] init];
30-
}
24+
MSRedirectHandler *redirectHandler = [[MSRedirectHandler alloc] init];
3125
return redirectHandler;
3226
}
33-
case MSMiddlewareTypeRetry:
34-
{
35-
MSRetryHandler *retryHandler;
36-
if(middlewareOptions)
37-
{
38-
retryHandler = [[MSRetryHandler alloc] initWithOptions:(MSRetryHandlerOptions *)middlewareOptions];
39-
}else{
40-
retryHandler = [[MSRetryHandler alloc] init];
41-
}
42-
return retryHandler;
43-
}
4427
case MSMiddlewareTypeAuthentication:
4528
{
46-
MSAuthenticationHandler *authenticationHandler;
47-
if(middlewareOptions)
48-
{
49-
authenticationHandler = [[MSAuthenticationHandler alloc] initWithOptions:(MSAuthenticationHandlerOptions *)middlewareOptions];
50-
}else{
51-
authenticationHandler = [[MSAuthenticationHandler alloc] init];
52-
}
29+
MSAuthenticationHandler *authenticationHandler = [[MSAuthenticationHandler alloc] init];
5330
return authenticationHandler;
5431
}
32+
case MSMiddlewareTypeRetry:
33+
{
34+
MSRetryHandler *retryHandler = [[MSRetryHandler alloc] init];
35+
return retryHandler;
36+
}
5537
default:
5638
break;
5739
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@
66
#import "MSAuthenticationProvider.h"
77
#import "MSGraphMiddleware.h"
88

9-
@class MSAuthenticationHandlerOptions;
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+
*/
1012

1113
@interface MSAuthenticationHandler : NSObject<MSGraphMiddleware>
1214

13-
@property (nonatomic, strong, readonly) MSAuthenticationHandlerOptions *authHandlerOptions;
15+
//Authentication provider which will be used to get access token
16+
@property (nonatomic, strong) id<MSAuthenticationProvider> authenticationProvider;
1417

15-
- (instancetype)initWithOptions:(MSAuthenticationHandlerOptions *)authHandlerOptions;
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;
1624

1725
@end

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,28 @@ @interface MSAuthenticationHandler()
2121

2222
@implementation MSAuthenticationHandler
2323

24-
- (instancetype)initWithOptions:(MSAuthenticationHandlerOptions *)authHandlerOptions
24+
- (instancetype)initWithAuthenticationProvider:(id<MSAuthenticationProvider>)authProvider
2525
{
2626
self = [super init];
2727
if(self)
2828
{
29-
_authHandlerOptions = authHandlerOptions;
29+
_authenticationProvider = authProvider;
3030
}
3131
return self;
3232
}
3333

34+
- (void)setAuthenticationProvider:(id<MSAuthenticationProvider>)authProvider
35+
{
36+
_authenticationProvider = authProvider;
37+
}
38+
3439
- (void)execute:(MSURLSessionTask *)task withCompletionHandler:(HTTPRequestCompletionHandler)completionHandler
3540
{
3641
MSAuthenticationHandlerOptions *authHandlerOptions = [task getMiddlewareOptionWithType:MSMiddlewareOptionsTypeAuth];
37-
if(!authHandlerOptions)
38-
{
39-
authHandlerOptions = _authHandlerOptions;
40-
}
41-
[authHandlerOptions.authenticationProvider getAccessTokenWithCompletion:^(NSString *accessToken, NSError *error) {
42+
43+
id<MSAuthenticationProvider> authProvider = authHandlerOptions.authenticationProvider?authHandlerOptions.authenticationProvider:_authenticationProvider;
44+
45+
[authProvider getAccessTokenForProviderOptions:authHandlerOptions.authenticationProviderOptions andCompletion:^(NSString *accessToken, NSError *error) {
4246
if(!error)
4347
{
4448
NSMutableURLRequest *urlRequest = [task request];

MSGraphClientSDK/MSGraphClientSDK/Middleware/Options/MSAuthenticationHandlerOptions.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#import <Foundation/Foundation.h>
66
#import "MSMiddlewareOptions.h"
77
#import "MSAuthenticationProvider.h"
8-
8+
#import "MSAuthenticationProviderOptions.h"
99
/*
1010
This class provides options to control the behaviour of Authentication Handler.
1111
*/
@@ -15,13 +15,16 @@
1515
/*
1616
Authentication Provider instance which will be used to authenticate the ongoing requests.
1717
*/
18-
@property (nonatomic, readonly) id<MSAuthenticationProvider> authenticationProvider;
18+
@property (nonatomic, strong, readonly) id<MSAuthenticationProvider> authenticationProvider;
19+
20+
@property (nonatomic, strong, readonly) id<MSAuthenticationProviderOptions> authenticationProviderOptions;
1921

2022
/*
2123
This method initializes an instance of MSAuthenticationHandlerOptions with the provided parameter.
2224
@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.
2326
@return The MSAuthenticationHandlerOptions instance with the provided value.
2427
*/
25-
- (instancetype)initWithAuthenticationProvider:(nonnull id<MSAuthenticationProvider>)authProvider;
28+
- (instancetype)initWithAuthenticationProvider:(id<MSAuthenticationProvider>)authProvider andAuthProviderOptions:(id<MSAuthenticationProviderOptions>)authProviderOptions;
2629

2730
@end

MSGraphClientSDK/MSGraphClientSDK/Middleware/Options/MSAuthenticationHandlerOptions.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ @implementation MSAuthenticationHandlerOptions
99

1010
@synthesize middlewareOptionsType;
1111

12-
- (instancetype)initWithAuthenticationProvider:(id<MSAuthenticationProvider>)authProvider
12+
- (instancetype)initWithAuthenticationProvider:(id<MSAuthenticationProvider>)authProvider andAuthProviderOptions:(id<MSAuthenticationProviderOptions>)authProviderOptions
1313
{
1414
self = [super init];
1515
if(self)
1616
{
17-
NSAssert(authProvider, @"Authentication Provider is requried to authenticate the requests.");
1817
_authenticationProvider = authProvider;
18+
_authenticationProviderOptions = authProviderOptions;
1919
middlewareOptionsType = MSMiddlewareOptionsTypeAuth;
2020
}
2121
return self;

MSGraphClientSDK/MSGraphClientSDKTests/HTTPClient/MSMiddlewareFactoryTests.m

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,11 @@ - (void)tearDown {
2323
}
2424

2525
- (void)testCreateMiddlewareMethod {
26-
id<MSGraphMiddleware>authHandler = [MSMiddlewareFactory createMiddleware:MSMiddlewareTypeAuthentication withOptions:nil];
27-
id<MSGraphMiddleware>httpMiddleware = [MSMiddlewareFactory createMiddleware:MSMiddlewareTypeHTTP withOptions:nil];
28-
id<MSGraphMiddleware>retryMiddleware = [MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRetry withOptions:nil];
29-
id<MSGraphMiddleware>redirectMiddleware = [MSMiddlewareFactory createMiddleware:MSMiddlewareTypeRedirect withOptions:nil];
30-
id<MSGraphMiddleware>randomMiddleware = [MSMiddlewareFactory createMiddleware:4 withOptions:nil];
26+
id<MSGraphMiddleware>authHandler = [MSMiddlewareFactory createMiddleware:MSMiddlewareTypeAuthentication];
27+
id<MSGraphMiddleware>httpMiddleware = [MSMiddlewareFactory createMiddleware:MSMiddlewareTypeHTTP];
28+
id<MSGraphMiddleware>randomMiddleware = [MSMiddlewareFactory createMiddleware:4];
3129
XCTAssertTrue([authHandler isKindOfClass:[MSAuthenticationHandler class]]);
3230
XCTAssertTrue([httpMiddleware isKindOfClass:[MSURLSessionManager class]]);
33-
XCTAssertTrue([retryMiddleware isKindOfClass:[MSRetryHandler class]]);
34-
XCTAssertTrue([redirectMiddleware isKindOfClass:[MSRedirectHandler class]]);
3531
XCTAssertNil(randomMiddleware);
3632
}
3733

MSGraphClientSDK/MSGraphClientSDKTests/MSGraphWorkloadsTests.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +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-
MSAuthenticationHandlerOptions *options = [[MSAuthenticationHandlerOptions alloc] initWithAuthenticationProvider:self.mockAuthProvider];
4544

46-
MSAuthenticationHandler *authHandler = [[MSAuthenticationHandler alloc] initWithOptions:options];
45+
MSAuthenticationHandler *authHandler = [[MSAuthenticationHandler alloc] initWithAuthenticationProvider:self.mockAuthProvider];
4746
[authHandler setNext:redirectHandler];
4847
[redirectHandler setNext:retryHandler];
4948
[retryHandler setNext:sessionManager];

0 commit comments

Comments
 (0)