forked from openid/AppAuth-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOIDFieldMapping.m
More file actions
133 lines (117 loc) · 4.32 KB
/
OIDFieldMapping.m
File metadata and controls
133 lines (117 loc) · 4.32 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
/*! @file OIDFieldMapping.m
@brief AppAuth iOS SDK
@copyright
Copyright 2015 Google Inc. All Rights Reserved.
@copydetails
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 "OIDFieldMapping.h"
#import "OIDDefines.h"
@implementation OIDFieldMapping
- (nonnull instancetype)init
OID_UNAVAILABLE_USE_INITIALIZER(@selector(initWithName:type:conversion:))
- (instancetype)initWithName:(NSString *)name
type:(Class)type {
return [self initWithName:name type:type conversion:nil];
}
- (instancetype)initWithName:(NSString *)name
type:(Class)type
conversion:(nullable OIDFieldMappingConversionFunction)conversion {
self = [super init];
if (self) {
_name = [name copy];
_expectedType = type;
_conversion = conversion;
}
return self;
}
+ (NSDictionary<NSString *, NSObject<NSCopying> *> *)remainingParametersWithMap:
(NSDictionary<NSString *, OIDFieldMapping *> *)map
parameters:(NSDictionary<NSString *, NSObject<NSCopying> *> *)parameters
instance:(id)instance {
NSMutableDictionary *additionalParameters = [NSMutableDictionary dictionary];
for (NSString *key in parameters) {
NSObject<NSCopying> *value = [parameters[key] copy];
OIDFieldMapping *mapping = map[key];
// If the field doesn't appear in the mapping, we add it to the additional parameters
// dictionary.
if (!mapping) {
additionalParameters[key] = value;
continue;
}
// If the field mapping specifies a conversion function, apply the conversion to the value.
if (mapping.conversion) {
value = mapping.conversion(value);
}
// Check the type of the value and make sure it matches the type we expected. If it doesn't we
// add the value to the additional parameters dictionary but don't assign the instance variable.
if (![value isKindOfClass:mapping.expectedType]) {
additionalParameters[key] = value;
continue;
}
// Assign the instance variable.
[instance setValue:value forKey:mapping.name];
}
return additionalParameters;
}
+ (void)encodeWithCoder:(NSCoder *)aCoder
map:(NSDictionary<NSString *, OIDFieldMapping *> *)map
instance:(id)instance {
for (NSString *key in map) {
id value = [instance valueForKey:map[key].name];
[aCoder encodeObject:value forKey:key];
}
}
+ (void)decodeWithCoder:(NSCoder *)aCoder
map:(NSDictionary<NSString *, OIDFieldMapping *> *)map
instance:(id)instance {
for (NSString *key in map) {
OIDFieldMapping *mapping = map[key];
id value = [aCoder decodeObjectOfClass:mapping.expectedType forKey:key];
[instance setValue:value forKey:mapping.name];
}
}
+ (NSSet *)JSONTypes {
return [NSSet setWithArray:@[
[NSDictionary class],
[NSArray class],
[NSString class],
[NSNumber class],
[NSNull class],
]];
}
+ (OIDFieldMappingConversionFunction)URLConversion {
return ^id _Nullable(NSObject *_Nullable value) {
if ([value isKindOfClass:[NSString class]]) {
return [NSURL URLWithString:(NSString *)value];
}
return value;
};
}
+ (OIDFieldMappingConversionFunction)dateSinceNowConversion {
return ^id _Nullable(NSObject *_Nullable value) {
if (![value isKindOfClass:[NSNumber class]]) {
return value;
}
NSNumber *valueAsNumber = (NSNumber *)value;
return [NSDate dateWithTimeIntervalSinceNow:[valueAsNumber longLongValue]];
};
}
+ (OIDFieldMappingConversionFunction)dateEpochConversion {
return ^id _Nullable(NSObject *_Nullable value) {
if (![value isKindOfClass:[NSNumber class]]) {
return value;
}
NSNumber *valueAsNumber = (NSNumber *) value;
return [NSDate dateWithTimeIntervalSince1970:[valueAsNumber longLongValue]];
};
}
@end