-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathGIDEMMSupport.m
More file actions
159 lines (132 loc) · 5.42 KB
/
GIDEMMSupport.m
File metadata and controls
159 lines (132 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import <TargetConditionals.h>
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
#import "GoogleSignIn/Sources/GIDEMMSupport.h"
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h"
#import "GoogleSignIn/Sources/GIDEMMErrorHandler.h"
#import "GoogleSignIn/Sources/GIDMDMPasscodeState.h"
#ifdef SWIFT_PACKAGE
@import AppAuth;
#else
#import <AppAuth/AppAuth.h>
#endif
NS_ASSUME_NONNULL_BEGIN
// Additional parameter names for EMM.
static NSString *const kEMMSupportParameterName = @"emm_support";
static NSString *const kEMMOSVersionParameterName = @"device_os";
static NSString *const kEMMPasscodeInfoParameterName = @"emm_passcode_info";
// Old UIDevice system name for iOS.
static NSString *const kOldIOSSystemName = @"iPhone OS";
// New UIDevice system name for iOS.
static NSString *const kNewIOSSystemName = @"iOS";
// The error key in the server response.
static NSString *const kErrorKey = @"error";
// Optional separator between error prefix and the payload.
static NSString *const kErrorPayloadSeparator = @":";
// A list for recognized error codes.
typedef NS_ENUM(NSInteger, ErrorCode) {
ErrorCodeNone = 0,
ErrorCodeDeviceNotCompliant,
ErrorCodeScreenlockRequired,
ErrorCodeAppVerificationRequired,
};
@implementation GIDEMMSupport
- (instancetype)init {
return [super init];
}
+ (void)handleTokenFetchEMMError:(nullable NSError *)error
completion:(void (^)(NSError *_Nullable))completion {
NSDictionary *errorJSON = error.userInfo[OIDOAuthErrorResponseErrorKey];
if (errorJSON) {
__block BOOL handled = NO;
handled = [[GIDEMMErrorHandler sharedInstance] handleErrorFromResponse:errorJSON
completion:^() {
if (handled) {
completion([NSError errorWithDomain:kGIDSignInErrorDomain
code:kGIDSignInErrorCodeEMM
userInfo:error.userInfo]);
} else {
completion(error);
}
}];
} else {
completion(error);
}
}
+ (NSDictionary<NSString *,NSString *> *)updatedEMMParametersWithParameters:
(NSDictionary *)parameters {
return [self parametersWithParameters:parameters
emmSupport:parameters[kEMMSupportParameterName]
isPasscodeInfoRequired:parameters[kEMMPasscodeInfoParameterName] != nil];
}
+ (NSDictionary<NSString *,NSString *> *)parametersWithParameters:(NSDictionary *)parameters
emmSupport:(nullable NSString *)emmSupport
isPasscodeInfoRequired:(BOOL)isPasscodeInfoRequired {
if (!emmSupport) {
return parameters;
}
NSMutableDictionary *allParameters = [(parameters ?: @{}) mutableCopy];
allParameters[kEMMSupportParameterName] = emmSupport;
UIDevice *device = [UIDevice currentDevice];
NSString *systemName = device.systemName;
if ([systemName isEqualToString:kOldIOSSystemName]) {
systemName = kNewIOSSystemName;
}
allParameters[kEMMOSVersionParameterName] =
[NSString stringWithFormat:@"%@ %@", systemName, device.systemVersion];
if (isPasscodeInfoRequired) {
allParameters[kEMMPasscodeInfoParameterName] = [GIDMDMPasscodeState passcodeState].info;
}
return [GIDEMMSupport dictionaryWithStringValuesFromDictionary:allParameters];
}
#pragma mark - GTMAuthSessionDelegate
- (nullable NSDictionary<NSString *,NSString *> *)
additionalTokenRefreshParametersForAuthSession:(GTMAuthSession *)authSession {
return [GIDEMMSupport updatedEMMParametersWithParameters:
authSession.authState.lastTokenResponse.additionalParameters];
}
- (void)updateErrorForAuthSession:(GTMAuthSession *)authSession
originalError:(NSError *)originalError
completion:(void (^)(NSError * _Nullable))completion {
[GIDEMMSupport handleTokenFetchEMMError:originalError completion:^(NSError *_Nullable error) {
completion(error);
}];
}
#pragma mark - Private Helpers
+ (NSDictionary<NSString *, NSString *> *)
dictionaryWithStringValuesFromDictionary:(NSDictionary *)originalDictionary {
NSMutableDictionary<NSString *, NSString *> *stringifiedDictionary =
[NSMutableDictionary dictionaryWithCapacity:originalDictionary.count];
[originalDictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
if ([value isKindOfClass:[NSString class]]) {
stringifiedDictionary[key] = value;
return;
}
if ([value isKindOfClass:[NSNumber class]]) {
if (CFGetTypeID((__bridge CFTypeRef)value) == CFBooleanGetTypeID()) {
stringifiedDictionary[key] = [value boolValue] ? @"true" : @"false";
} else {
stringifiedDictionary[key] = [value stringValue];
}
return;
}
}];
return stringifiedDictionary;
}
@end
NS_ASSUME_NONNULL_END
#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST