Skip to content

Commit 604d0e3

Browse files
committed
Merge branch 'dev' into mipetriu/update_min_version
merge dev to branch
2 parents 3b38519 + bd128ad commit 604d0e3

File tree

13 files changed

+864
-0
lines changed

13 files changed

+864
-0
lines changed

IdentityCore/IdentityCore.xcodeproj/project.pbxproj

Lines changed: 46 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Copyright (c) Microsoft Corporation.
3+
// All rights reserved.
4+
//
5+
// This code is licensed under the MIT License.
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files(the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions :
13+
//
14+
// The above copyright notice and this permission notice shall be included in
15+
// all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
// THE SOFTWARE.
24+
25+
NS_ASSUME_NONNULL_BEGIN
26+
@interface MSIDCachedNonce : NSObject
27+
28+
@property (nonatomic, readonly, nonnull) NSString *nonce;
29+
@property (nonatomic, readonly, nonnull) NSDate *cachedDate;
30+
31+
- (instancetype)init NS_UNAVAILABLE;
32+
+ (instancetype)new NS_UNAVAILABLE;
33+
- (instancetype)initWithNonce:(nonnull NSString *)nonce;
34+
35+
@end
36+
NS_ASSUME_NONNULL_END
37+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// Copyright (c) Microsoft Corporation.
3+
// All rights reserved.
4+
//
5+
// This code is licensed under the MIT License.
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files(the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions :
13+
//
14+
// The above copyright notice and this permission notice shall be included in
15+
// all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
// THE SOFTWARE.
24+
25+
#import "MSIDCachedNonce.h"
26+
@implementation MSIDCachedNonce
27+
28+
- (instancetype)initWithNonce:(NSString *)nonce
29+
{
30+
self = [super init];
31+
if (self)
32+
{
33+
_nonce = nonce;
34+
_cachedDate = [NSDate date];
35+
}
36+
return self;
37+
}
38+
@end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Copyright (c) Microsoft Corporation.
3+
// All rights reserved.
4+
//
5+
// This code is licensed under the MIT License.
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files(the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions :
13+
//
14+
// The above copyright notice and this permission notice shall be included in
15+
// all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
// THE SOFTWARE.
24+
25+
#import "MSIDHttpRequest.h"
26+
27+
NS_ASSUME_NONNULL_BEGIN
28+
@interface MSIDNonceHttpRequest : MSIDHttpRequest
29+
- (instancetype)init NS_UNAVAILABLE;
30+
+ (instancetype)new NS_UNAVAILABLE;
31+
- (instancetype) initWithTokenEndpoint:(NSURL *)tokenEndpoint context:(id<MSIDRequestContext>)context;
32+
@end
33+
NS_ASSUME_NONNULL_END
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// Copyright (c) Microsoft Corporation.
3+
// All rights reserved.
4+
//
5+
// This code is licensed under the MIT License.
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files(the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions :
13+
//
14+
// The above copyright notice and this permission notice shall be included in
15+
// all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
// THE SOFTWARE.
24+
#import "MSIDNonceHttpRequest.h"
25+
#import "MSIDAADRequestConfigurator.h"
26+
27+
@implementation MSIDNonceHttpRequest
28+
29+
- (nonnull instancetype)initWithTokenEndpoint:(nonnull NSURL *)tokenEndpoint context:(nonnull id<MSIDRequestContext>)context
30+
{
31+
self = [super init];
32+
if (self)
33+
{
34+
if (!tokenEndpoint)
35+
{
36+
MSID_LOG_WITH_CTX(MSIDLogLevelError, context, @"No endpoint provided to get nonce from!");
37+
NSParameterAssert(tokenEndpoint);
38+
return nil;
39+
}
40+
NSMutableURLRequest *urlRequest = [NSMutableURLRequest new];
41+
urlRequest.URL = tokenEndpoint;
42+
urlRequest.HTTPMethod = @"POST";
43+
_urlRequest = urlRequest;
44+
45+
__auto_type requestConfigurator = [MSIDAADRequestConfigurator new];
46+
[requestConfigurator configure:self];
47+
_parameters = @{MSID_OAUTH2_GRANT_TYPE : @"srv_challenge"};
48+
}
49+
50+
return self;
51+
}
52+
53+
@end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// Copyright (c) Microsoft Corporation.
3+
// All rights reserved.
4+
//
5+
// This code is licensed under the MIT License.
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files(the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions :
13+
//
14+
// The above copyright notice and this permission notice shall be included in
15+
// all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
// THE SOFTWARE.
24+
25+
@class MSIDRequestParameters;
26+
typedef void (^MSIDNonceRequestCompletion)(NSString * _Nullable resultNonce, NSError * _Nullable error);
27+
28+
NS_ASSUME_NONNULL_BEGIN
29+
30+
@interface MSIDNonceTokenRequest : NSObject
31+
32+
@property (nonatomic, readonly, nonnull) MSIDRequestParameters *requestParameters;
33+
34+
- (nullable instancetype)initWithRequestParameters:(nonnull MSIDRequestParameters *)parameters;
35+
36+
- (void)executeRequestWithCompletion:(nonnull MSIDNonceRequestCompletion)completionBlock;
37+
38+
@end
39+
40+
NS_ASSUME_NONNULL_END
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
//
2+
// Copyright (c) Microsoft Corporation.
3+
// All rights reserved.
4+
//
5+
// This code is licensed under the MIT License.
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files(the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions :
13+
//
14+
// The above copyright notice and this permission notice shall be included in
15+
// all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
// THE SOFTWARE.
24+
25+
#import "MSIDNonceTokenRequest.h"
26+
#import "MSIDRequestParameters.h"
27+
#import "MSIDAuthority.h"
28+
#import "MSIDOpenIdProviderMetadata.h"
29+
#import "MSIDAccountIdentifier.h"
30+
#import "MSIDNonceHttpRequest.h"
31+
#import "MSIDCachedNonce.h"
32+
33+
const static NSUInteger kMSIDNonceLifetimeInSeconds = 180;
34+
@implementation MSIDNonceTokenRequest
35+
36+
- (nullable instancetype)initWithRequestParameters:(nonnull MSIDRequestParameters *)parameters
37+
{
38+
self = [super init];
39+
if (self)
40+
{
41+
_requestParameters = parameters;
42+
}
43+
return self;
44+
}
45+
46+
- (void)executeRequestWithCompletion:(nonnull MSIDNonceRequestCompletion)completionBlock
47+
{
48+
MSIDCachedNonce *cachedNonce = [self.class getCachedNonceForKey:self.requestParameters.authority.environment];
49+
if (cachedNonce)
50+
{
51+
completionBlock(cachedNonce.nonce, nil);
52+
return;
53+
}
54+
55+
if (self.requestParameters.authority.metadata.tokenEndpoint)
56+
{
57+
[self executeNetworkRequestWithCompletion:completionBlock];
58+
return;
59+
}
60+
61+
[self.requestParameters.authority resolveAndValidate:YES
62+
userPrincipalName:_requestParameters.accountIdentifier.displayableId
63+
context:_requestParameters
64+
completionBlock:^(NSURL __unused *openIdConfigurationEndpoint, BOOL __unused validated, NSError *error)
65+
{
66+
if (error)
67+
{
68+
completionBlock(nil, error);
69+
return;
70+
}
71+
72+
[self.requestParameters.authority loadOpenIdMetadataWithContext:self.requestParameters
73+
completionBlock:^(__unused MSIDOpenIdProviderMetadata *metadata, NSError *openIdError)
74+
{
75+
76+
if (openIdError)
77+
{
78+
completionBlock(nil, openIdError);
79+
return;
80+
}
81+
82+
[self executeNetworkRequestWithCompletion:completionBlock];
83+
}];
84+
}];
85+
}
86+
87+
- (void)executeNetworkRequestWithCompletion:(nonnull MSIDNonceRequestCompletion)completionBlock
88+
{
89+
MSIDNonceHttpRequest *nonceRequest = [[MSIDNonceHttpRequest alloc] initWithTokenEndpoint:self.requestParameters.tokenEndpoint
90+
context:self.requestParameters];
91+
[nonceRequest sendWithBlock:^(NSDictionary *response, NSError *error)
92+
{
93+
if (error)
94+
{
95+
if (completionBlock) completionBlock(nil, error);
96+
return;
97+
}
98+
99+
if (![response isKindOfClass:[NSDictionary class]])
100+
{
101+
MSID_LOG_WITH_CTX(MSIDLogLevelError, self.requestParameters, @"Unexpected nonce response received");
102+
NSError *nwError = MSIDCreateError(MSIDErrorDomain, MSIDErrorServerInvalidResponse, @"Unexpected nonce response", nil, nil, nil, nil, nil, YES);
103+
if (completionBlock) completionBlock(nil, nwError);
104+
return;
105+
}
106+
107+
NSString *nonce = [response msidStringObjectForKey:@"Nonce"];
108+
109+
if ([NSString msidIsStringNilOrBlank:nonce])
110+
{
111+
MSID_LOG_WITH_CTX(MSIDLogLevelError, self.requestParameters, @"Didn't receive valid nonce in response");
112+
NSError *nwError = MSIDCreateError(MSIDErrorDomain, MSIDErrorServerInvalidResponse, @"Didn't receive valid nonce in response", nil, nil, nil, nil, nil, YES);
113+
if (completionBlock) completionBlock(nil, nwError);
114+
return;
115+
}
116+
117+
[self.class cacheNonceForKey:self.requestParameters.authority.environment nonce:nonce];
118+
if (completionBlock)
119+
{
120+
completionBlock(nonce, nil);
121+
}
122+
}];
123+
}
124+
125+
#pragma mark - Cache
126+
127+
+ (MSIDCache *)nonceCache
128+
{
129+
static MSIDCache *k_nonceCache;
130+
static dispatch_once_t once_token;
131+
dispatch_once(&once_token, ^{
132+
k_nonceCache = [MSIDCache new];
133+
});
134+
135+
return k_nonceCache;
136+
}
137+
138+
+ (nullable MSIDCachedNonce *)getCachedNonceForKey:(NSString *)key
139+
{
140+
MSIDCache *cache = [self.class nonceCache];
141+
MSIDCachedNonce *cachedNonce = [cache objectForKey:key];
142+
if (cachedNonce)
143+
{
144+
NSTimeInterval ti = [[NSDate date] timeIntervalSinceDate:cachedNonce.cachedDate];
145+
if (ti > 0 && ti < kMSIDNonceLifetimeInSeconds)
146+
{
147+
return cachedNonce;
148+
}
149+
}
150+
151+
return nil;
152+
}
153+
154+
+ (BOOL)cacheNonceForKey:(NSString *)key nonce:(NSString *)nonce
155+
{
156+
if (!nonce || !key)
157+
{
158+
return NO;
159+
}
160+
161+
MSIDCachedNonce *cachedNonce = [[MSIDCachedNonce alloc] initWithNonce:nonce];
162+
[self.class.nonceCache setObject:cachedNonce forKey:key];
163+
return YES;
164+
}
165+
@end

IdentityCore/src/util/mac/MSIDXpcSingleSignOnProvider.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ - (void)getXpcService:(id<MSIDXpcProviderCaching>)xpcProviderCache withContinueB
413413
{
414414
isConnectionErroredOut = YES;
415415
continueBlock(nil, nil, xpcError);
416+
return;
416417
}
417418

418419
if (continueBlock) continueBlock(nil, nil, xpcError);

0 commit comments

Comments
 (0)