Skip to content

Commit 6329e71

Browse files
authored
Merge pull request #125 from AzureAD/jason/embeddedWebview
Embedded Webview
2 parents c6b1322 + e439cfc commit 6329e71

File tree

59 files changed

+3726
-122
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+3726
-122
lines changed

IdentityCore/IdentityCore.xcodeproj/project.pbxproj

Lines changed: 228 additions & 22 deletions
Large diffs are not rendered by default.
File renamed without changes.

IdentityCore/src/MSIDContants.m renamed to IdentityCore/src/MSIDConstants.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
// THE SOFTWARE.
2323

24-
#import "MSIDContants.h"
24+
#import "MSIDConstants.h"
2525

2626
NSString *const MSID_PLATFORM_KEY = @"x-client-SKU";
2727
NSString *const MSID_VERSION_KEY = @"x-client-Ver";

IdentityCore/src/MSIDDeviceId.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#import "MSIDDeviceId.h"
2525
#import "MSIDVersion.h"
26-
#import "MSIDContants.h"
26+
#import "MSIDConstants.h"
2727
#import "MSIDOAuth2Constants.h"
2828

2929
#if !TARGET_OS_IPHONE

IdentityCore/src/MSIDError.h

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,26 @@ typedef NS_ENUM(NSInteger, MSIDErrorCode)
7070
MSIDErrorInvalidGrant = -51016,
7171
MSIDErrorInvalidScope = -51017,
7272
MSIDErrorInvalidParameter = -51018,
73+
MSIDErrorUserCancel = -51019,
74+
/*!
75+
The authentication request was cancelled programmatically.
76+
*/
77+
MSIDErrorSessionCanceled = -51020,
78+
/*!
79+
An interactive authentication session is already running with the
80+
SafariViewController visible. Another authentication session can not be
81+
launched yet.
82+
*/
83+
MSIDErrorInteractiveSessionAlreadyRunning = -51021,
84+
/*!
85+
An interactive authentication session failed to start.
86+
*/
87+
MSIDErrorInteractiveSessionStartFailure = -51022,
88+
89+
MSIDErrorNoMainViewController = -51023,
90+
MSIDServerNonHttpsRedirect = -51024,
91+
92+
MSIDErrorUnsupportedFunctionality = -51025,
7393

7494
/*!
7595
The user or application failed to authenticate in the interactive flow.
@@ -78,43 +98,24 @@ typedef NS_ENUM(NSInteger, MSIDErrorCode)
7898
*/
7999
MSIDErrorAuthorizationFailed = -52020,
80100

81-
/*!
82-
The state returned by the server does not match the state that was sent to
83-
the server at the beginning of the authorization attempt.
84-
*/
85-
MSIDErrorInvalidState = -52501,
86101
/*!
87102
Interaction required errors occur because of a wide variety of errors
88103
returned by the authentication service.
89104
*/
90105
MSIDErrorMismatchedUser = -52101,
91106
MSIDErrorNoAuthorizationResponse = -52102,
92107
MSIDErrorBadAuthorizationResponse = -52103,
93-
94108

95-
MSIDErrorUserCancel = -51019,
96-
/*!
97-
The authentication request was cancelled programmatically.
98-
*/
99-
MSIDErrorSessionCanceled = -51020,
100-
/*!
101-
An interactive authentication session is already running with the
102-
SafariViewController visible. Another authentication session can not be
103-
launched yet.
104-
*/
105-
MSIDErrorInteractiveSessionAlreadyRunning = -51021,
106109
/*!
107-
An interactive authentication session failed to start.
110+
The state returned by the server does not match the state that was sent to
111+
the server at the beginning of the authorization attempt.
108112
*/
109-
MSIDErrorInteractiveSessionStartFailure = -51022,
110-
111-
MSIDErrorUnsupportedFunctionality = -51018,
113+
MSIDErrorInvalidState = -52501,
112114

113115
MSIDErrorCodeFirst = MSIDErrorInternal,
114-
MSIDErrorCodeLast = MSIDErrorUnsupportedFunctionality
116+
MSIDErrorCodeLast = MSIDErrorInvalidState
115117
};
116118

117119
extern NSError *MSIDCreateError(NSString *domain, NSInteger code, NSString *errorDescription, NSString *oauthError, NSString *subError, NSError *underlyingError, NSUUID *correlationId, NSDictionary *additionalUserInfo);
118120

119121
extern MSIDErrorCode MSIDErrorCodeForOAuthError(NSString *oauthError, MSIDErrorCode defaultCode);
120-

IdentityCore/src/configuration/webview/MSIDWebviewConfiguration.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
@property (readwrite) NSDictionary<NSString *, NSString *> *sliceParameters;
4444
@property (readwrite) NSString *promptBehavior;
4545
@property (readwrite) NSString *claims;
46+
@property (readwrite) NSDictionary<NSString *, NSString *> *customHeaders;
4647

4748
// State verifier: Recommended verifier for state value of the response.
4849
// Set to YES to stop if verifying state fails
@@ -59,6 +60,11 @@
5960
// Priority start URL
6061
@property (readwrite) NSURL *explicitStartURL;
6162

63+
#if TARGET_OS_IPHONE
64+
@property (weak) UIViewController *parentController;
65+
@property (readwrite)UIModalPresentationStyle presentationType;
66+
#endif
67+
6268
- (instancetype)initWithAuthorizationEndpoint:(NSURL *)authorizationEndpoint
6369
redirectUri:(NSString *)redirectUri
6470
clientId:(NSString *)clientId
@@ -68,5 +74,4 @@
6874
verifyState:(BOOL)verifyState
6975
enablePkce:(BOOL)enablePkce;
7076

71-
7277
@end

IdentityCore/src/network/request_configurator/MSIDAADRequestConfigurator.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#import "MSIDDeviceId.h"
3030
#import "NSDictionary+MSIDExtensions.h"
3131
#import "MSIDVersion.h"
32-
#import "MSIDContants.h"
32+
#import "MSIDConstants.h"
3333

3434
static NSTimeInterval const s_defaultTimeoutInterval = 300;
3535

IdentityCore/src/oauth2/MSIDOauth2Factory.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,5 +399,3 @@ - (MSIDWebviewFactory *)webviewFactory
399399
}
400400

401401
@end
402-
403-

IdentityCore/src/oauth2/MSIDWebviewFactory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,5 @@
5454
- (BOOL)verifyRequestState:(NSString *)requestState
5555
responseURL:(NSURL *)url
5656
error:(NSError **)error;
57+
- (NSString *)generateStateValue;
5758
@end

IdentityCore/src/oauth2/MSIDWebviewFactory.m

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,30 @@
2929
#import "MSIDSystemWebviewController.h"
3030
#import "MSIDPkce.h"
3131
#import "NSOrderedSet+MSIDExtensions.h"
32+
#import "MSIDOAuth2EmbeddedWebviewController.h"
3233

3334
@implementation MSIDWebviewFactory
3435

3536
#pragma mark - Webview creation
3637

3738
- (MSIDWebviewSession *)embeddedWebviewSessionFromConfiguration:(MSIDWebviewConfiguration *)configuration customWebview:(WKWebView *)webview context:(id<MSIDRequestContext>)context
3839
{
39-
return nil;
40+
NSString *state = [self generateStateValue];
41+
NSURL *startURL = [self startURLFromConfiguration:configuration requestState:state];
42+
NSURL *redirectURL = [NSURL URLWithString:configuration.redirectUri];
43+
44+
MSIDOAuth2EmbeddedWebviewController *embeddedWebviewController
45+
= [[MSIDOAuth2EmbeddedWebviewController alloc] initWithStartURL:startURL
46+
endURL:redirectURL
47+
webview:webview
48+
configuration:configuration
49+
context:context];
50+
51+
MSIDWebviewSession *session = [[MSIDWebviewSession alloc] initWithWebviewController:embeddedWebviewController
52+
factory:self
53+
requestState:state
54+
verifyState:configuration.verifyState];
55+
return session;
4056
}
4157

4258
#if TARGET_OS_IPHONE

IdentityCore/src/oauth2/aad_base/MSIDAADWebviewFactory.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#import "MSIDWebWPJAuthResponse.h"
2828
#import "MSIDWebAADAuthResponse.h"
2929
#import "MSIDDeviceId.h"
30+
#import "MSIDAADOAuthEmbeddedWebviewController.h"
31+
#import "MSIDWebviewSession.h"
3032

3133
@implementation MSIDAADWebviewFactory
3234

@@ -68,6 +70,26 @@ @implementation MSIDAADWebviewFactory
6870
return parameters;
6971
}
7072

73+
- (MSIDWebviewSession *)embeddedWebviewSessionFromConfiguration:(MSIDWebviewConfiguration *)configuration customWebview:(WKWebView *)webview context:(id<MSIDRequestContext>)context
74+
{
75+
NSString *state = [self generateStateValue];
76+
NSURL *startURL = [self startURLFromConfiguration:configuration requestState:state];
77+
NSURL *redirectURL = [NSURL URLWithString:configuration.redirectUri];
78+
79+
MSIDAADOAuthEmbeddedWebviewController *embeddedWebviewController
80+
= [[MSIDAADOAuthEmbeddedWebviewController alloc] initWithStartURL:startURL
81+
endURL:redirectURL
82+
webview:webview
83+
configuration:configuration
84+
context:context];
85+
86+
MSIDWebviewSession *session = [[MSIDWebviewSession alloc] initWithWebviewController:embeddedWebviewController
87+
factory:self
88+
requestState:state
89+
verifyState:configuration.verifyState];
90+
return session;
91+
}
92+
7193
- (MSIDWebviewResponse *)responseWithURL:(NSURL *)url
7294
context:(id<MSIDRequestContext>)context
7395
error:(NSError **)error

IdentityCore/src/oauth2/aad_v1/MSIDAADV1Oauth2Factory.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#import "MSIDRefreshToken.h"
2929
#import "MSIDLegacySingleResourceToken.h"
3030
#import "MSIDAccount.h"
31+
#import "MSIDWebviewConfiguration.h"
3132

3233
#import "MSIDAADV1IdTokenClaims.h"
3334
#import "MSIDOauth2Factory+Internal.h"

IdentityCore/src/oauth2/aad_v1/MSIDAADV1WebviewFactory.m

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,17 @@
2222
// THE SOFTWARE.
2323

2424
#import "MSIDAADV1WebviewFactory.h"
25+
#import "MSIDWebviewConfiguration.h"
2526

2627
@implementation MSIDAADV1WebviewFactory
2728

28-
- (MSIDWebviewSession *)embeddedWebviewSessionFromConfiguration:(MSIDWebviewConfiguration *)configuration verifyState:(BOOL)verifyState customWebview:(WKWebView *)webview context:(id<MSIDRequestContext>)context
29-
{
30-
return nil;
31-
}
32-
3329
- (NSMutableDictionary<NSString *,NSString *> *)authorizationParametersFromConfiguration:(MSIDWebviewConfiguration *)configuration requestState:(NSString *)state
3430
{
3531
NSMutableDictionary<NSString *, NSString *> *parameters = [super authorizationParametersFromConfiguration:configuration
3632
requestState:state];
3733

38-
// Do custom parameters for AAD V1
34+
parameters[MSID_OAUTH2_RESOURCE] = configuration.resource;
35+
3936
return parameters;
4037
}
4138

IdentityCore/src/oauth2/aad_v2/MSIDAADV2Oauth2Factory.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,4 @@ - (MSIDWebviewFactory *)webviewFactory
188188
return _webviewFactory;
189189
}
190190

191-
192191
@end

IdentityCore/src/oauth2/aad_v2/MSIDAADV2WebviewFactory.m

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@
2929

3030
@implementation MSIDAADV2WebviewFactory
3131

32-
- (MSIDWebviewSession *)embeddedWebviewSessionFromConfiguration:(MSIDWebviewConfiguration *)configuration verifyState:(BOOL)verifyState customWebview:(WKWebView *)webview context:(id<MSIDRequestContext>)context
33-
{
34-
return nil;
35-
}
36-
3732
- (NSMutableDictionary<NSString *,NSString *> *)authorizationParametersFromConfiguration:(MSIDWebviewConfiguration *)configuration requestState:(NSString *)state
3833
{
3934
NSMutableDictionary<NSString *, NSString *> *parameters = [super authorizationParametersFromConfiguration:configuration

IdentityCore/src/util/ios/MSIDAppExtensionUtil.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
// Copyright (c) Microsoft Corporation.
32
// All rights reserved.
43
//

IdentityCore/src/util/ios/MSIDAppExtensionUtil.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ + (BOOL)isExecutingInAppExtension
3636
return NO;
3737
}
3838

39-
4039
return [mainBundlePath hasSuffix:@"appex"];
4140
}
4241

IdentityCore/src/util/ios/UIApplication+MSIDExtensions.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,3 @@ + (UIViewController*)msidCurrentViewControllerWithRootViewController:(UIViewCont
6363
}
6464

6565
@end
66-
67-

IdentityCore/src/validation/MSIDAuthority.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,6 @@
4444
+ (NSURL *)cacheUrlForAuthority:(NSURL *)authority
4545
tenantId:(NSString *)tenantId;
4646

47+
+ (BOOL)isKnownHost:(NSURL *)url;
48+
4749
@end

IdentityCore/src/validation/MSIDAuthority.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,31 @@
2929

3030
@implementation MSIDAuthority
3131

32+
static NSSet<NSString *> *s_trustedHostList;
33+
34+
// Trusted authorities
35+
NSString *const MSIDTrustedAuthority = @"login.windows.net";
36+
NSString *const MSIDTrustedAuthorityUS = @"login.microsoftonline.us";
37+
NSString *const MSIDTrustedAuthorityChina = @"login.chinacloudapi.cn";
38+
NSString *const MSIDTrustedAuthorityGermany = @"login.microsoftonline.de";
39+
NSString *const MSIDTrustedAuthorityWorldWide = @"login.microsoftonline.com";
40+
NSString *const MSIDTrustedAuthorityUSGovernment = @"login-us.microsoftonline.com";
41+
NSString *const MSIDTrustedAuthorityCloudGovApi = @"login.cloudgovapi.us";
42+
43+
+ (void)initialize
44+
{
45+
s_trustedHostList = [NSSet setWithObjects:MSIDTrustedAuthority, MSIDTrustedAuthorityUS,
46+
MSIDTrustedAuthorityChina, MSIDTrustedAuthorityGermany,
47+
MSIDTrustedAuthorityWorldWide, MSIDTrustedAuthorityUSGovernment, MSIDTrustedAuthorityCloudGovApi, nil];
48+
}
49+
50+
+ (BOOL)isKnownHost:(NSURL *)url
51+
{
52+
if (!url) return NO;
53+
54+
return [s_trustedHostList containsObject:url.host.lowercaseString];
55+
}
56+
3257
+ (BOOL)isADFSInstance:(NSString *)endpoint
3358
{
3459
if ([NSString msidIsStringNilOrBlank:endpoint])

IdentityCore/src/webview/MSIDWebviewAuthorization.m

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#import "MSIDError.h"
3232
#import "NSURL+MSIDExtensions.h"
3333
#import "MSIDTelemetry.h"
34-
#import "MSIDOAuth2EmbeddedWebviewController.h"
34+
#import "MSIDAADOAuthEmbeddedWebviewController.h"
3535
#import "MSIDSystemWebviewController.h"
3636
#import "MSIDWebviewFactory.h"
3737

@@ -44,17 +44,11 @@ + (void)startEmbeddedWebviewAuthWithConfiguration:(MSIDWebviewConfiguration *)co
4444
context:(id<MSIDRequestContext>)context
4545
completionHandler:(MSIDWebviewAuthCompletionHandler)completionHandler
4646
{
47-
// NSString *state = [factory generateStateValue];
48-
// NSURL *startURL = [factory startURLFromConfiguration:configuration requestState:state];
49-
// (void)startURL;
50-
// (void)state;
51-
//
52-
// MSIDOAuth2EmbeddedWebviewController *embeddedWebviewController = [[MSIDOAuth2EmbeddedWebviewController alloc] init];
53-
// [self startWebviewAuth:embeddedWebviewController
54-
// factory:factory
55-
// requestState:state
56-
// context:context
57-
// completionHandler:completionHandler];
47+
[self startEmbeddedWebviewWebviewAuthWithConfiguration:configuration
48+
oauth2Factory:oauth2Factory
49+
webview:nil
50+
context:context
51+
completionHandler:completionHandler];
5852
}
5953

6054
+ (void)startEmbeddedWebviewWebviewAuthWithConfiguration:(MSIDWebviewConfiguration *)configuration
@@ -63,17 +57,10 @@ + (void)startEmbeddedWebviewWebviewAuthWithConfiguration:(MSIDWebviewConfigurati
6357
context:(id<MSIDRequestContext>)context
6458
completionHandler:(MSIDWebviewAuthCompletionHandler)completionHandler
6559
{
66-
// NSString *state = [factory generateStateValue];
67-
// NSURL *startURL = [factory startURLFromConfiguration:configuration requestState:state];
68-
// (void)startURL;
69-
// (void)state;
70-
//
71-
// MSIDOAuth2EmbeddedWebviewController *embeddedWebviewController = [[MSIDOAuth2EmbeddedWebviewController alloc] init];
72-
// [self startWebviewAuth:embeddedWebviewController
73-
// factory:factory
74-
// requestState:state
75-
// context:context
76-
// completionHandler:completionHandler];
60+
MSIDWebviewFactory *webviewFactory = [oauth2Factory webviewFactory];
61+
MSIDWebviewSession *session = [webviewFactory embeddedWebviewSessionFromConfiguration:configuration customWebview:webview context:context];
62+
63+
[self startSession:session context:context completionHandler:completionHandler];
7764
}
7865

7966
#if TARGET_OS_IPHONE
@@ -182,7 +169,6 @@ + (void)cancelCurrentSession
182169
}
183170
}
184171

185-
186172
+ (BOOL)handleURLResponseForSystemWebviewController:(NSURL *)url;
187173
{
188174
#if TARGET_OS_IPHONE
@@ -200,7 +186,3 @@ + (BOOL)handleURLResponseForSystemWebviewController:(NSURL *)url;
200186

201187

202188
@end
203-
204-
205-
206-

IdentityCore/src/webview/MSIDWebviewInteracting.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,4 @@ typedef void (^MSIDWebUICompletionHandler)(NSURL *callbackURL, NSError *error);
3838

3939
- (NSURL *)startURL;
4040

41-
@optional
42-
#if TARGET_OS_IPHONE
43-
@property UIViewController *parentViewController;
44-
#endif
45-
4641
@end

0 commit comments

Comments
 (0)