Skip to content

Commit

Permalink
Merge pull request #1417 from AzureAD/sedemche/merge_2.6.10_in_2.7.x
Browse files Browse the repository at this point in the history
Merge 2.6.10 into 2.7.x
  • Loading branch information
antrix1989 authored May 14, 2019
2 parents 2298675 + 652b9ac commit d643d3d
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 35 deletions.
2 changes: 1 addition & 1 deletion ADAL.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "ADAL"
s.module_name = "ADAL"
s.version = "2.7.10"
s.version = "2.7.11"
s.summary = "The ADAL SDK for iOS gives you the ability to add Azure Identity authentication to your application"

s.description = <<-DESC
Expand Down
2 changes: 1 addition & 1 deletion ADAL/resources/ios/Framework/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>2.7.10</string>
<string>2.7.11</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion ADAL/resources/mac/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>2.7.10</string>
<string>2.7.11</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion ADAL/src/ADAL_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// through build script. Don't change its format unless changing build script as well.)
#define ADAL_VER_HIGH 2
#define ADAL_VER_LOW 7
#define ADAL_VER_PATCH 10
#define ADAL_VER_PATCH 11

#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
Expand Down
3 changes: 3 additions & 0 deletions ADAL/src/ADAuthenticationContext+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,8 @@ extern NSString* const ADRedirectUriInvalidError;
toUser:(ADUserIdentifier*)userId
verifyUserId:(BOOL)verifyUserId;

+ (BOOL)canHandleResponse:(NSURL *)response
sourceApplication:(NSString *)sourceApplication;

@end

31 changes: 31 additions & 0 deletions ADAL/src/ADAuthenticationContext+Internal.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#import "ADTokenCacheItem+Internal.h"
#import "ADHelpers.h"
#import "NSDictionary+MSIDExtensions.h"
#if TARGET_OS_IPHONE
#import "ADBrokerNotificationManager.h"
#endif

NSString* const ADUnknownError = @"Uknown error.";
NSString* const ADCredentialsNeeded = @"The user credentials are needed to obtain access token. Please call the non-silent acquireTokenWithResource methods.";
Expand Down Expand Up @@ -174,4 +177,32 @@ + (ADAuthenticationResult*)updateResult:(ADAuthenticationResult*)result
return result;
}

+ (BOOL)canHandleResponse:(NSURL *)response
sourceApplication:(NSString *)sourceApplication
{
#if TARGET_OS_IPHONE
BOOL isResponseFromBroker = [self isResponseFromBroker:sourceApplication response:response];
if (!isResponseFromBroker) { return NO; }

NSURLComponents *components = [NSURLComponents componentsWithURL:response resolvingAgainstBaseURL:NO];
NSString *qp = [components percentEncodedQuery];
NSDictionary* queryParamsMap = [NSDictionary msidDictionaryFromWWWFormURLEncodedString:qp];

NSString *protocolVersion = queryParamsMap[ADAL_BROKER_MESSAGE_VERSION];
BOOL isValidVersion = [protocolVersion isEqualToString:@"2"];

NSDictionary *resumeDictionary = [[NSUserDefaults standardUserDefaults] objectForKey:kAdalResumeDictionaryKey];

if (!resumeDictionary) MSID_LOG_INFO(nil, @"No resume dictionary found.");

BOOL isADALInitiatedRequest = [resumeDictionary[kAdalSDKNameKey] isEqualToString:kAdalSDKObjc] || [[ADBrokerNotificationManager sharedInstance] hasCallback];

return isValidVersion && isADALInitiatedRequest;
#else
(void)response;
(void)sourceApplication;
return NO;
#endif
}

@end
1 change: 1 addition & 0 deletions ADAL/src/ADAuthenticationContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#import "MSIDTelemetryEventStrings.h"
#import "ADUserIdentifier.h"
#import "ADTokenCacheItem.h"
#import "ADAuthenticationRequest+Broker.h"
#import "MSIDLegacyTokenCacheAccessor.h"
#import "ADHelpers.h"
#import "MSIDMacTokenCache.h"
Expand Down
10 changes: 7 additions & 3 deletions ADAL/src/broker/ios/ADBrokerHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#import "ADBrokerNotificationManager.h"
#import "ADWebAuthController+Internal.h"
#import "ADAppExtensionUtil.h"
#import "ADAuthenticationContext+Internal.h"

typedef BOOL (*applicationHandleOpenURLPtr)(id, SEL, UIApplication*, NSURL*);
IMP __original_ApplicationHandleOpenURL = NULL;
Expand All @@ -36,7 +37,7 @@

BOOL __swizzle_ApplicationOpenURL(id self, SEL _cmd, UIApplication* application, NSURL* url, NSString* sourceApplication, id annotation)
{
if ([ADAuthenticationContext isResponseFromBroker:sourceApplication response:url])
if ([ADAuthenticationContext canHandleResponse:url sourceApplication:sourceApplication])
{
// Attempt to handle response from broker
BOOL result = [ADAuthenticationContext handleBrokerResponse:url];
Expand All @@ -47,6 +48,8 @@ BOOL __swizzle_ApplicationOpenURL(id self, SEL _cmd, UIApplication* application,
return YES;
}
}

MSID_LOG_INFO(nil, @"This url cannot be handled by ADAL. Skipping it.");

// Fallback to original delegate if defined
if (__original_ApplicationOpenURL)
Expand All @@ -70,7 +73,7 @@ BOOL __swizzle_ApplicationOpenURLiOS9(id self, SEL _cmd, UIApplication* applicat
{
NSString* sourceApplication = [options objectForKey:UIApplicationOpenURLOptionsSourceApplicationKey];

if ([ADAuthenticationContext isResponseFromBroker:sourceApplication response:url])
if ([ADAuthenticationContext canHandleResponse:url sourceApplication:sourceApplication])
{
// Attempt to handle response from broker
BOOL result = [ADAuthenticationContext handleBrokerResponse:url];
Expand All @@ -80,8 +83,9 @@ BOOL __swizzle_ApplicationOpenURLiOS9(id self, SEL _cmd, UIApplication* applicat
// Successfully handled broker response
return YES;
}

}

MSID_LOG_INFO(nil, @"This url cannot be handled by ADAL. Skipping it.");

// Fallback to original delegate if defined
if (__original_ApplicationOpenURLiOS9)
Expand Down
2 changes: 2 additions & 0 deletions ADAL/src/broker/ios/ADBrokerNotificationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@

- (ADAuthenticationCallback)copyAndClearCallback;

- (BOOL)hasCallback;

@end
10 changes: 10 additions & 0 deletions ADAL/src/broker/ios/ADBrokerNotificationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,15 @@ - (void)callbackCleanup:(NSNotification*)aNotification
}
}

- (BOOL)hasCallback
{
BOOL result = NO;
@synchronized(self)
{
result = _callbackForBroker != nil;
}

return result;
}

@end
8 changes: 5 additions & 3 deletions ADAL/src/request/ADAuthenticationRequest+Broker.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@

typedef void(^ADAuthorizationCodeCallback)(NSString*, ADAuthenticationError*);

extern NSString* kAdalResumeDictionaryKey;
extern NSString* s_brokerAppVersion;
extern NSString* s_brokerProtocolVersion;
extern NSString *kAdalResumeDictionaryKey;
extern NSString *s_brokerAppVersion;
extern NSString *s_brokerProtocolVersion;
extern NSString *kAdalSDKNameKey;
extern NSString *kAdalSDKObjc;

@interface ADAuthenticationRequest (Broker)

Expand Down
41 changes: 16 additions & 25 deletions ADAL/src/request/ADAuthenticationRequest+Broker.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@
#import "MSIDBrokerResponse+ADAL.h"
#endif // TARGET_OS_IPHONE

NSString* s_brokerAppVersion = nil;
NSString* s_brokerProtocolVersion = nil;

NSString* kAdalResumeDictionaryKey = @"adal-broker-resume-dictionary";
NSString *s_brokerAppVersion = nil;
NSString *s_brokerProtocolVersion = nil;
NSString *kAdalResumeDictionaryKey = @"adal-broker-resume-dictionary";
NSString *kAdalSDKNameKey = @"sdk_name";
NSString *kAdalSDKObjc = @"adal-objc";

@implementation ADAuthenticationRequest (Broker)

Expand Down Expand Up @@ -410,29 +411,19 @@ - (NSURL *)composeBrokerRequest:(ADAuthenticationError* __autoreleasing *)error
@"client_app_version": clientMetadata[MSID_APP_VER_KEY]
};

NSDictionary<NSString *, NSString *> *resumeDictionary = nil;
NSMutableDictionary *resumeDictionary = [@{
@"authority" : _requestParams.authority,
@"resource" : _requestParams.resource,
@"client_id" : _requestParams.clientId,
@"redirect_uri" : _requestParams.redirectUri,
@"correlation_id" : _requestParams.correlationId.UUIDString,
kAdalSDKNameKey : kAdalSDKObjc
} mutableCopy];
#if TARGET_OS_IPHONE
NSString *sharedGroup = self.sharedGroup ? self.sharedGroup : MSIDKeychainTokenCache.defaultKeychainGroup;

resumeDictionary =
@{
@"authority" : _requestParams.authority,
@"resource" : _requestParams.resource,
@"client_id" : _requestParams.clientId,
@"redirect_uri" : _requestParams.redirectUri,
@"correlation_id" : _requestParams.correlationId.UUIDString,
@"keychain_group" : sharedGroup
};
#else
resumeDictionary =
@{
@"authority" : _requestParams.authority,
@"resource" : _requestParams.resource,
@"client_id" : _requestParams.clientId,
@"redirect_uri" : _requestParams.redirectUri,
@"correlation_id" : _requestParams.correlationId.UUIDString,
};
NSString *keychainGroup = self.sharedGroup ? self.sharedGroup : MSIDKeychainTokenCache.defaultKeychainGroup;
resumeDictionary[@"keychain_group"] = keychainGroup;
#endif

[[NSUserDefaults standardUserDefaults] setObject:resumeDictionary forKey:kAdalResumeDictionaryKey];
[[NSUserDefaults standardUserDefaults] synchronize];

Expand Down
81 changes: 81 additions & 0 deletions ADAL/tests/unit/ADAuthenticationContextTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#import "ADTokenCacheItem+Internal.h"
#import "ADUserIdentifier.h"
#import "ADAuthenticationRequest.h"
#import "ADAuthenticationRequest+Broker.h"
#if TARGET_OS_IPHONE
#import "ADBrokerNotificationManager.h"
#endif

@implementation ADAuthenticationContextTests

Expand All @@ -40,6 +44,11 @@ - (void)setUp
- (void)tearDown
{
[super tearDown];

#if TARGET_OS_IPHONE
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kAdalResumeDictionaryKey];
[[ADBrokerNotificationManager sharedInstance] copyAndClearCallback];
#endif
}

#pragma mark - Initialization
Expand Down Expand Up @@ -132,4 +141,76 @@ - (void)testAuthenticationContextWithAuthority_whenAuthorityIsValidValidateAutho
XCTAssertNil(error);
}

#if TARGET_OS_IPHONE

- (void)testCanHandleResponse_whenProtocolVersionIs2AndRequestIntiatedByAdal_shouldReturnYes
{
NSDictionary *resumeDictionary = @{kAdalSDKNameKey: kAdalSDKObjc};
[[NSUserDefaults standardUserDefaults] setObject:resumeDictionary forKey:kAdalResumeDictionaryKey];
NSURL *url = [[NSURL alloc] initWithString:@"testapp://com.microsoft.testapp/broker?msg_protocol_ver=2&response=someEncryptedResponse"];
NSString *sourceApp = @"com.microsoft.azureauthenticator";

BOOL result = [ADAuthenticationContext canHandleResponse:url sourceApplication:sourceApp];

XCTAssertTrue(result);
}

- (void)testCanHandleResponse_whenProtocolVersionIs2AndRequestIsNotIntiatedByAdal_shouldReturnNo
{
NSDictionary *resumeDictionary = @{kAdalSDKNameKey: @"msal-objc"};
[[NSUserDefaults standardUserDefaults] setObject:resumeDictionary forKey:kAdalResumeDictionaryKey];
NSURL *url = [[NSURL alloc] initWithString:@"testapp://com.microsoft.testapp/broker?msg_protocol_ver=2&response=someEncryptedResponse"];
NSString *sourceApp = @"com.microsoft.azureauthenticator";

BOOL result = [ADAuthenticationContext canHandleResponse:url sourceApplication:sourceApp];

XCTAssertFalse(result);
}

- (void)testCanHandleResponse_whenProtocolVersionIs2AndThereIsNoCallbackAndNoResumeDictionary_shouldReturnNo
{
NSURL *url = [[NSURL alloc] initWithString:@"testapp://com.microsoft.testapp/broker?msg_protocol_ver=2&response=someEncryptedResponse"];
NSString *sourceApp = @"com.microsoft.azureauthenticator";

BOOL result = [ADAuthenticationContext canHandleResponse:url sourceApplication:sourceApp];

XCTAssertFalse(result);
}

- (void)testCanHandleResponse_whenProtocolVersionIs2AndThereIsCallbackAndNoResumeDictionary_shouldReturnYes
{
[[ADBrokerNotificationManager sharedInstance] enableNotifications:^(__unused ADAuthenticationResult *result) { }];
NSURL *url = [[NSURL alloc] initWithString:@"testapp://com.microsoft.testapp/broker?msg_protocol_ver=2&response=someEncryptedResponse"];
NSString *sourceApp = @"com.microsoft.azureauthenticator";

BOOL result = [ADAuthenticationContext canHandleResponse:url sourceApplication:sourceApp];

XCTAssertTrue(result);
}

- (void)testCanHandleResponse_whenProtocolVersionIs3AndRequestIntiatedByAdal_shouldReturnNo
{
NSDictionary *resumeDictionary = @{kAdalSDKNameKey: kAdalSDKObjc};
[[NSUserDefaults standardUserDefaults] setObject:resumeDictionary forKey:kAdalResumeDictionaryKey];
NSURL *url = [[NSURL alloc] initWithString:@"testapp://com.microsoft.testapp/broker?msg_protocol_ver=3&response=someEncryptedResponse"];
NSString *sourceApp = @"com.microsoft.azureauthenticator";

BOOL result = [ADAuthenticationContext canHandleResponse:url sourceApplication:sourceApp];

XCTAssertFalse(result);
}
#else

- (void)testCanHandleResponse_shouldReturnNo
{
NSURL *url = [[NSURL alloc] initWithString:@"testapp://com.microsoft.testapp/broker?msg_protocol_ver=2&response=someEncryptedResponse"];
NSString *sourceApp = @"com.microsoft.azureauthenticator";

BOOL result = [ADAuthenticationContext canHandleResponse:url sourceApplication:sourceApp];

XCTAssertFalse(result);
}

#endif

@end
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Version 2.7.11 (05.14.2019)
-----------
* Fix issues when ADAL was trying to handle MSAL broker responses.

Version 2.7.10 (04.24.2018)
-----------
* Hotfix to add compiler flag to disable Kerberos (#1401)
Expand Down

0 comments on commit d643d3d

Please sign in to comment.