@appzung/react-native-code-push 5.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. package/AlertAdapter.js +24 -0
  2. package/CONTRIBUTING.md +136 -0
  3. package/CodePush.js +666 -0
  4. package/CodePush.podspec +27 -0
  5. package/LICENSE.md +13 -0
  6. package/README.md +386 -0
  7. package/RestartManager.js +59 -0
  8. package/android/app/build.gradle +31 -0
  9. package/android/app/proguard-rules.pro +25 -0
  10. package/android/app/src/main/AndroidManifest.xml +10 -0
  11. package/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java +395 -0
  12. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushBuilder.java +37 -0
  13. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushConstants.java +34 -0
  14. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushDialog.java +102 -0
  15. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushInstallMode.java +16 -0
  16. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushInvalidPublicKeyException.java +12 -0
  17. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushInvalidUpdateException.java +7 -0
  18. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushMalformedDataException.java +12 -0
  19. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java +641 -0
  20. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushNotInitializedException.java +12 -0
  21. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushTelemetryManager.java +175 -0
  22. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushUnknownException.java +12 -0
  23. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateManager.java +369 -0
  24. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateState.java +15 -0
  25. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateUtils.java +267 -0
  26. package/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java +236 -0
  27. package/android/app/src/main/java/com/microsoft/codepush/react/DownloadProgress.java +30 -0
  28. package/android/app/src/main/java/com/microsoft/codepush/react/DownloadProgressCallback.java +5 -0
  29. package/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java +190 -0
  30. package/android/app/src/main/java/com/microsoft/codepush/react/ReactInstanceHolder.java +17 -0
  31. package/android/app/src/main/java/com/microsoft/codepush/react/SettingsManager.java +173 -0
  32. package/android/build.gradle +20 -0
  33. package/android/codepush.gradle +113 -0
  34. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  35. package/android/gradle/wrapper/gradle-wrapper.properties +5 -0
  36. package/android/gradle.properties +20 -0
  37. package/android/gradlew +164 -0
  38. package/android/gradlew.bat +90 -0
  39. package/android/settings.gradle +1 -0
  40. package/docs/api-android.md +65 -0
  41. package/docs/api-ios.md +31 -0
  42. package/docs/api-js.md +592 -0
  43. package/docs/multi-deployment-testing-android.md +107 -0
  44. package/docs/multi-deployment-testing-ios.md +53 -0
  45. package/docs/setup-android.md +406 -0
  46. package/docs/setup-ios.md +229 -0
  47. package/docs/setup-windows.md +65 -0
  48. package/gulpfile.js +302 -0
  49. package/ios/CodePush/Base64/Base64/MF_Base64Additions.h +34 -0
  50. package/ios/CodePush/Base64/Base64/MF_Base64Additions.m +252 -0
  51. package/ios/CodePush/Base64/README.md +47 -0
  52. package/ios/CodePush/CodePush.h +235 -0
  53. package/ios/CodePush/CodePush.m +1041 -0
  54. package/ios/CodePush/CodePushConfig.m +116 -0
  55. package/ios/CodePush/CodePushDownloadHandler.m +130 -0
  56. package/ios/CodePush/CodePushErrorUtils.m +20 -0
  57. package/ios/CodePush/CodePushPackage.m +598 -0
  58. package/ios/CodePush/CodePushTelemetryManager.m +175 -0
  59. package/ios/CodePush/CodePushUpdateUtils.m +376 -0
  60. package/ios/CodePush/CodePushUtils.m +9 -0
  61. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithm.h +69 -0
  62. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithmFactory.h +16 -0
  63. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithmFactory.m +51 -0
  64. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithmNone.h +15 -0
  65. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithmNone.m +55 -0
  66. package/ios/CodePush/JWT/Core/Algorithms/ESFamily/JWTAlgorithmESBase.h +24 -0
  67. package/ios/CodePush/JWT/Core/Algorithms/ESFamily/JWTAlgorithmESBase.m +41 -0
  68. package/ios/CodePush/JWT/Core/Algorithms/HSFamily/JWTAlgorithmHSBase.h +28 -0
  69. package/ios/CodePush/JWT/Core/Algorithms/HSFamily/JWTAlgorithmHSBase.m +205 -0
  70. package/ios/CodePush/JWT/Core/Algorithms/Holders/JWTAlgorithmDataHolder.h +103 -0
  71. package/ios/CodePush/JWT/Core/Algorithms/Holders/JWTAlgorithmDataHolder.m +322 -0
  72. package/ios/CodePush/JWT/Core/Algorithms/Holders/JWTAlgorithmDataHolderChain.h +37 -0
  73. package/ios/CodePush/JWT/Core/Algorithms/Holders/JWTAlgorithmDataHolderChain.m +145 -0
  74. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/JWTAlgorithmRSBase.h +35 -0
  75. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/JWTAlgorithmRSBase.m +551 -0
  76. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/JWTRSAlgorithm.h +23 -0
  77. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoKey.h +43 -0
  78. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoKey.m +230 -0
  79. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoKeyExtractor.h +31 -0
  80. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoKeyExtractor.m +113 -0
  81. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoSecurity.h +38 -0
  82. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoSecurity.m +500 -0
  83. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaim.h +18 -0
  84. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaim.m +214 -0
  85. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSet.h +23 -0
  86. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSet.m +29 -0
  87. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSetSerializer.h +19 -0
  88. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSetSerializer.m +68 -0
  89. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSetVerifier.h +18 -0
  90. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSetVerifier.m +72 -0
  91. package/ios/CodePush/JWT/Core/Coding/JWTCoding+ResultTypes.h +67 -0
  92. package/ios/CodePush/JWT/Core/Coding/JWTCoding+ResultTypes.m +111 -0
  93. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionOne.h +119 -0
  94. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionOne.m +307 -0
  95. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionThree.h +94 -0
  96. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionThree.m +619 -0
  97. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionTwo.h +164 -0
  98. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionTwo.m +514 -0
  99. package/ios/CodePush/JWT/Core/Coding/JWTCoding.h +24 -0
  100. package/ios/CodePush/JWT/Core/Coding/JWTCoding.m +11 -0
  101. package/ios/CodePush/JWT/Core/FrameworkSupplement/JWT.h +52 -0
  102. package/ios/CodePush/JWT/Core/FrameworkSupplement/Map.modulemap +5 -0
  103. package/ios/CodePush/JWT/Core/Supplement/JWTBase64Coder.h +28 -0
  104. package/ios/CodePush/JWT/Core/Supplement/JWTBase64Coder.m +70 -0
  105. package/ios/CodePush/JWT/Core/Supplement/JWTDeprecations.h +22 -0
  106. package/ios/CodePush/JWT/Core/Supplement/JWTErrorDescription.h +34 -0
  107. package/ios/CodePush/JWT/Core/Supplement/JWTErrorDescription.m +73 -0
  108. package/ios/CodePush/JWT/LICENSE +19 -0
  109. package/ios/CodePush/JWT/README.md +489 -0
  110. package/ios/CodePush/RCTConvert+CodePushInstallMode.m +20 -0
  111. package/ios/CodePush/RCTConvert+CodePushUpdateState.m +20 -0
  112. package/ios/CodePush/SSZipArchive/Common.h +81 -0
  113. package/ios/CodePush/SSZipArchive/README.md +1 -0
  114. package/ios/CodePush/SSZipArchive/SSZipArchive.h +76 -0
  115. package/ios/CodePush/SSZipArchive/SSZipArchive.m +691 -0
  116. package/ios/CodePush/SSZipArchive/aes/aes.h +198 -0
  117. package/ios/CodePush/SSZipArchive/aes/aes_via_ace.h +541 -0
  118. package/ios/CodePush/SSZipArchive/aes/aescrypt.c +294 -0
  119. package/ios/CodePush/SSZipArchive/aes/aeskey.c +548 -0
  120. package/ios/CodePush/SSZipArchive/aes/aesopt.h +739 -0
  121. package/ios/CodePush/SSZipArchive/aes/aestab.c +391 -0
  122. package/ios/CodePush/SSZipArchive/aes/aestab.h +173 -0
  123. package/ios/CodePush/SSZipArchive/aes/brg_endian.h +126 -0
  124. package/ios/CodePush/SSZipArchive/aes/brg_types.h +219 -0
  125. package/ios/CodePush/SSZipArchive/aes/entropy.c +54 -0
  126. package/ios/CodePush/SSZipArchive/aes/entropy.h +16 -0
  127. package/ios/CodePush/SSZipArchive/aes/fileenc.c +144 -0
  128. package/ios/CodePush/SSZipArchive/aes/fileenc.h +121 -0
  129. package/ios/CodePush/SSZipArchive/aes/hmac.c +145 -0
  130. package/ios/CodePush/SSZipArchive/aes/hmac.h +103 -0
  131. package/ios/CodePush/SSZipArchive/aes/prng.c +155 -0
  132. package/ios/CodePush/SSZipArchive/aes/prng.h +82 -0
  133. package/ios/CodePush/SSZipArchive/aes/pwd2key.c +103 -0
  134. package/ios/CodePush/SSZipArchive/aes/pwd2key.h +57 -0
  135. package/ios/CodePush/SSZipArchive/aes/sha1.c +258 -0
  136. package/ios/CodePush/SSZipArchive/aes/sha1.h +73 -0
  137. package/ios/CodePush/SSZipArchive/minizip/crypt.h +130 -0
  138. package/ios/CodePush/SSZipArchive/minizip/ioapi.c +369 -0
  139. package/ios/CodePush/SSZipArchive/minizip/ioapi.h +175 -0
  140. package/ios/CodePush/SSZipArchive/minizip/mztools.c +284 -0
  141. package/ios/CodePush/SSZipArchive/minizip/mztools.h +31 -0
  142. package/ios/CodePush/SSZipArchive/minizip/unzip.c +1839 -0
  143. package/ios/CodePush/SSZipArchive/minizip/unzip.h +248 -0
  144. package/ios/CodePush/SSZipArchive/minizip/zip.c +1910 -0
  145. package/ios/CodePush/SSZipArchive/minizip/zip.h +202 -0
  146. package/ios/CodePush.xcodeproj/project.pbxproj +937 -0
  147. package/ios/CodePush.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  148. package/ios/CodePush.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  149. package/logging.js +6 -0
  150. package/package-mixins.js +69 -0
  151. package/package.json +47 -0
  152. package/request-fetch-adapter.js +52 -0
  153. package/scripts/generateBundledResourcesHash.js +125 -0
  154. package/scripts/getFilesInFolder.js +19 -0
  155. package/scripts/postlink/android/postlink.js +87 -0
  156. package/scripts/postlink/ios/postlink.js +116 -0
  157. package/scripts/postlink/run.js +11 -0
  158. package/scripts/postunlink/android/postunlink.js +74 -0
  159. package/scripts/postunlink/ios/postunlink.js +87 -0
  160. package/scripts/postunlink/run.js +11 -0
  161. package/scripts/recordFilesBeforeBundleCommand.js +41 -0
  162. package/scripts/tools/linkToolsAndroid.js +57 -0
  163. package/scripts/tools/linkToolsIos.js +130 -0
  164. package/typings/react-native-code-push.d.ts +446 -0
  165. package/windows/CodePush/CodePush.csproj +128 -0
  166. package/windows/CodePush/CodePushUtils.cs +47 -0
  167. package/windows/CodePush/FileUtils.cs +40 -0
  168. package/windows/CodePush/Properties/AssemblyInfo.cs +29 -0
  169. package/windows/CodePush/Properties/CodePush.rd.xml +33 -0
  170. package/windows/CodePush/UpdateManager.cs +305 -0
  171. package/windows/CodePush/UpdateUtils.cs +46 -0
  172. package/windows/CodePush.Net46/Adapters/Http/HttpProgress.cs +28 -0
  173. package/windows/CodePush.Net46/Adapters/Storage/ApplicationDataContainer.cs +106 -0
  174. package/windows/CodePush.Net46/CodePush.Net46.csproj +103 -0
  175. package/windows/CodePush.Net46/CodePushUtils.cs +158 -0
  176. package/windows/CodePush.Net46/FileUtils.cs +55 -0
  177. package/windows/CodePush.Net46/Properties/AssemblyInfo.cs +36 -0
  178. package/windows/CodePush.Net46/UpdateManager.cs +330 -0
  179. package/windows/CodePush.Net46/UpdateUtils.cs +70 -0
  180. package/windows/CodePush.Net46/packages.config +5 -0
  181. package/windows/CodePush.Shared/CodePush.Shared.projitems +22 -0
  182. package/windows/CodePush.Shared/CodePush.Shared.shproj +13 -0
  183. package/windows/CodePush.Shared/CodePushConstants.cs +35 -0
  184. package/windows/CodePush.Shared/CodePushNativeModule.cs +329 -0
  185. package/windows/CodePush.Shared/CodePushReactPackage.cs +235 -0
  186. package/windows/CodePush.Shared/CodePushUtils.cs +70 -0
  187. package/windows/CodePush.Shared/InstallMode.cs +9 -0
  188. package/windows/CodePush.Shared/MinimumBackgroundListener.cs +44 -0
  189. package/windows/CodePush.Shared/SettingsManager.cs +148 -0
  190. package/windows/CodePush.Shared/TelemetryManager.cs +250 -0
  191. package/windows/CodePush.Shared/UpdateState.cs +9 -0
@@ -0,0 +1,619 @@
1
+ //
2
+ // JWTCoding+VersionThree.m
3
+ // JWT
4
+ //
5
+ // Created by Lobanov Dmitry on 27.11.16.
6
+ // Copyright © 2016 JWTIO. All rights reserved.
7
+ //
8
+
9
+ #import "JWTCoding+VersionThree.h"
10
+ #import "JWTAlgorithmDataHolderChain.h"
11
+ #import "JWTRSAlgorithm.h"
12
+ #import "JWTCoding+ResultTypes.h"
13
+ #import "JWTAlgorithmFactory.h"
14
+ #import "JWTErrorDescription.h"
15
+ #import "JWTBase64Coder.h"
16
+ #import "JWTClaimsSetSerializer.h"
17
+ #import "JWTClaimsSetVerifier.h"
18
+
19
+ @implementation JWT (VersionThree)
20
+ + (JWTEncodingBuilder *)encodeWithHolders:(NSArray *)holders {
21
+ return [JWTEncodingBuilder createWithHolders:holders];
22
+ }
23
+ + (JWTEncodingBuilder *)encodeWithChain:(JWTAlgorithmDataHolderChain *)chain {
24
+ return [JWTEncodingBuilder createWithChain:chain];
25
+ }
26
+ + (JWTDecodingBuilder *)decodeWithHolders:(NSArray *)holders {
27
+ return [JWTDecodingBuilder createWithHolders:holders];
28
+ }
29
+ + (JWTDecodingBuilder *)decodeWithChain:(JWTAlgorithmDataHolderChain *)chain {
30
+ return [JWTDecodingBuilder createWithChain:chain];
31
+ }
32
+ @end
33
+
34
+ @interface JWTCodingBuilder ()
35
+ #pragma mark - Internal
36
+ @property (strong, nonatomic, readwrite) JWTAlgorithmDataHolderChain *internalChain;
37
+ @property (copy, nonatomic, readwrite) NSNumber *internalOptions;
38
+
39
+ #pragma mark - Fluent
40
+ @property (copy, nonatomic, readwrite) JWTCodingBuilder *(^chain)(JWTAlgorithmDataHolderChain *chain);
41
+ @property (copy, nonatomic, readwrite) JWTCodingBuilder *(^constructChain)(JWTAlgorithmDataHolderChain *(^block)());
42
+ @property (copy, nonatomic, readwrite) JWTCodingBuilder *(^modifyChain)(JWTAlgorithmDataHolderChain *(^block)(JWTAlgorithmDataHolderChain * chain));
43
+ @property (copy, nonatomic, readwrite) JWTCodingBuilder *(^options)(NSNumber *options);
44
+ @property (copy, nonatomic, readwrite) JWTCodingBuilder *(^addHolder)(id<JWTAlgorithmDataHolderProtocol> holder);
45
+ @property (copy, nonatomic, readwrite) JWTCodingBuilder *(^constructHolder)(id<JWTAlgorithmDataHolderProtocol>(^block)(id<JWTAlgorithmDataHolderProtocol> holder));
46
+
47
+ @end
48
+ @interface JWTCodingBuilder (Fluent_Setup)
49
+ - (instancetype)chain:(JWTAlgorithmDataHolderChain *)chain;
50
+ - (instancetype)options:(NSNumber *)options;
51
+ - (instancetype)addHolder:(id<JWTAlgorithmDataHolderProtocol>)holder;
52
+ - (void)setupFluent;
53
+ @end
54
+
55
+ @implementation JWTCodingBuilder (Fluent_Setup)
56
+ - (instancetype)chain:(JWTAlgorithmDataHolderChain *)chain {
57
+ self.internalChain = chain;
58
+ return self;
59
+ }
60
+ - (instancetype)options:(NSNumber *)options {
61
+ self.internalOptions = options;
62
+ return self;
63
+ }
64
+ - (instancetype)addHolder:(id<JWTAlgorithmDataHolderProtocol>)holder {
65
+ self.internalChain = [self.internalChain chainByAppendingHolder:holder];
66
+ return self;
67
+ }
68
+ - (void)setupFluent {
69
+ __weak typeof(self) weakSelf = self;
70
+ self.chain = ^(JWTAlgorithmDataHolderChain *chain) {
71
+ return [weakSelf chain:chain];
72
+ };
73
+
74
+ self.constructChain = ^(JWTAlgorithmDataHolderChain *(^block)()) {
75
+ if (block) {
76
+ JWTAlgorithmDataHolderChain *chain = block();
77
+ return [weakSelf chain:chain];
78
+ }
79
+ return weakSelf;
80
+ };
81
+
82
+ self.modifyChain = ^(JWTAlgorithmDataHolderChain *(^block)(JWTAlgorithmDataHolderChain *chain)) {
83
+ if (block) {
84
+ JWTAlgorithmDataHolderChain *chain = block(weakSelf.internalChain);
85
+ return [weakSelf chain:chain];
86
+ }
87
+ return weakSelf;
88
+ };
89
+
90
+
91
+ self.options = ^(NSNumber *options) {
92
+ return [weakSelf options:options];
93
+ };
94
+
95
+ self.addHolder = ^(id<JWTAlgorithmDataHolderProtocol> holder) {
96
+ return [weakSelf addHolder:holder];
97
+ };
98
+
99
+ self.constructHolder = ^(id<JWTAlgorithmDataHolderProtocol> (^block)(id<JWTAlgorithmDataHolderProtocol> holder)) {
100
+ if (block) {
101
+ [weakSelf addHolder:block([JWTAlgorithmBaseDataHolder new])];
102
+ }
103
+ return weakSelf;
104
+ };
105
+ }
106
+ @end
107
+
108
+ @implementation JWTCodingBuilder
109
+ #pragma mark - Getters
110
+ // Chain always exists
111
+ - (JWTAlgorithmDataHolderChain *)internalChain {
112
+ return _internalChain ?: (_internalChain = [JWTAlgorithmDataHolderChain new], _internalChain);
113
+ }
114
+ #pragma mark - Create
115
+ - (instancetype)initWithChain:(JWTAlgorithmDataHolderChain *)chain {
116
+ if (self = [super init]) {
117
+ self.internalChain = chain;
118
+ [self setupFluent];
119
+ }
120
+ return self;
121
+ }
122
+ + (instancetype)createWithHolders:(NSArray *)items {
123
+ return [self createWithChain:[[JWTAlgorithmDataHolderChain alloc] initWithHolders:items]];
124
+ }
125
+ + (instancetype)createWithChain:(JWTAlgorithmDataHolderChain *)chain {
126
+ return [[self alloc] initWithChain:chain];
127
+ }
128
+ + (instancetype)createWithEmptyChain {
129
+ return [self createWithChain:nil];
130
+ }
131
+ @end
132
+
133
+ @implementation JWTCodingBuilder (Sugar)
134
+ - (instancetype)and {
135
+ return self;
136
+ }
137
+ - (instancetype)with {
138
+ return self;
139
+ }
140
+ @end
141
+
142
+ @interface JWTEncodingBuilder ()
143
+ #pragma mark - Internal
144
+ @property (copy, nonatomic, readwrite) NSDictionary *internalPayload;
145
+ @property (copy, nonatomic, readwrite) NSDictionary *internalHeaders;
146
+ @property (strong, nonatomic, readwrite) JWTClaimsSet *internalClaimsSet;
147
+ @property (copy, nonatomic, readwrite) NSDictionary *internalMixingClaimsPayload;
148
+
149
+ #pragma mark - Fluent
150
+ @property (copy, nonatomic, readwrite) JWTEncodingBuilder *(^payload)(NSDictionary *payload);
151
+ @property (copy, nonatomic, readwrite) JWTEncodingBuilder *(^headers)(NSDictionary *headers);
152
+ @property (copy, nonatomic, readwrite) JWTEncodingBuilder *(^claimsSet)(JWTClaimsSet *claimsSet);
153
+ @end
154
+
155
+ @interface JWTEncodingBuilder (Fluent_Setup)
156
+ - (instancetype)payload:(NSDictionary *)payload;
157
+ - (instancetype)headers:(NSDictionary *)headers;
158
+ - (instancetype)claimsSet:(JWTClaimsSet *)claimsSet;
159
+ @end
160
+
161
+ @implementation JWTEncodingBuilder (Fluent_Setup)
162
+
163
+ - (instancetype)payload:(NSDictionary *)payload {
164
+ self.internalPayload = payload;
165
+ return self;
166
+ }
167
+ - (instancetype)headers:(NSDictionary *)headers {
168
+ self.internalHeaders = headers;
169
+ return self;
170
+ }
171
+
172
+ - (instancetype)claimsSet:(JWTClaimsSet *)claimsSet {
173
+ self.internalClaimsSet = claimsSet;
174
+ return self;
175
+ }
176
+
177
+ - (void)setupFluent {
178
+ [super setupFluent];
179
+ __weak typeof(self) weakSelf = self;
180
+ self.payload = ^(NSDictionary *payload) {
181
+ return [weakSelf payload:payload];
182
+ };
183
+ self.headers = ^(NSDictionary *headers) {
184
+ return [weakSelf headers:headers];
185
+ };
186
+ self.claimsSet = ^(JWTClaimsSet *claimsSet) {
187
+ return [weakSelf claimsSet:claimsSet];
188
+ };
189
+ }
190
+
191
+ @end
192
+
193
+ @implementation JWTEncodingBuilder
194
+ #pragma mark - Getters
195
+ - (NSDictionary *)internalMixingClaimsPayload {
196
+ NSMutableDictionary *dictionary = [@{} mutableCopy];
197
+ if (_internalPayload) {
198
+ [dictionary addEntriesFromDictionary:_internalPayload];
199
+ }
200
+
201
+ if (_internalClaimsSet) {
202
+ [dictionary addEntriesFromDictionary:[JWTClaimsSetSerializer dictionaryWithClaimsSet:_internalClaimsSet]];
203
+ }
204
+
205
+ return dictionary;
206
+ }
207
+
208
+ #pragma mark - Create
209
+ + (instancetype)encodePayload:(NSDictionary *)payload {
210
+ return ((JWTEncodingBuilder *)[self createWithEmptyChain]).payload(payload);
211
+ }
212
+
213
+ + (instancetype)encodeClaimsSet:(JWTClaimsSet *)claimsSet {
214
+ return ((JWTEncodingBuilder *)[self createWithEmptyChain]).claimsSet(claimsSet);
215
+ }
216
+
217
+ @end
218
+
219
+ @implementation JWTEncodingBuilder (Coding)
220
+ - (JWTCodingResultType *)encode {
221
+
222
+ NSDictionary *headers = self.internalHeaders;
223
+ NSDictionary *payload = self.internalMixingClaimsPayload;
224
+
225
+ NSString *encodedMessage = nil;
226
+ NSError *error = nil;
227
+
228
+ NSArray *holders = self.internalChain.holders;
229
+ // ERROR: HOLDERS ARE EMPTY.
230
+ if (holders.count == 0) {
231
+ error = [JWTErrorDescription errorWithCode:JWTDecodingHoldersChainEmptyError];
232
+ }
233
+
234
+ for (id <JWTAlgorithmDataHolderProtocol>holder in holders) {
235
+ id <JWTAlgorithm>algorithm = holder.internalAlgorithm;
236
+ NSData *secretData = holder.internalSecretData;
237
+ encodedMessage = [self encodeWithAlgorithm:algorithm withHeaders:headers withPayload:payload withSecretData:secretData withError:&error];
238
+ if (encodedMessage && (error == nil)) {
239
+ break;
240
+ }
241
+ }
242
+
243
+ JWTCodingResultType *result = nil;
244
+
245
+ if (error) {
246
+ result = [[JWTCodingResultType alloc] initWithErrorResult:[[JWTCodingResultTypeError alloc] initWithError:error]];
247
+ }
248
+ else if (encodedMessage) {
249
+ result = [[JWTCodingResultType alloc] initWithSuccessResult:[[JWTCodingResultTypeSuccess alloc] initWithEncoded:encodedMessage]];
250
+ }
251
+ else {
252
+ NSLog(@"%@ something went wrong! result is nil!", self.debugDescription);
253
+ }
254
+
255
+ return result;
256
+ }
257
+ - (NSString *)encodeWithAlgorithm:(id<JWTAlgorithm>)theAlgorithm withHeaders:(NSDictionary *)theHeaders withPayload:(NSDictionary *)thePayload withSecretData:(NSData *)theSecretData withError:(NSError *__autoreleasing *)theError {
258
+ // do it!
259
+
260
+ if (!theAlgorithm) {
261
+ if (theError) {
262
+ *theError = [JWTErrorDescription errorWithCode:JWTUnspecifiedAlgorithmError];
263
+ }
264
+ return nil;
265
+ }
266
+
267
+ NSString *theAlgorithmName = [theAlgorithm name];
268
+
269
+ if (!theAlgorithmName) {
270
+ if (theError) {
271
+ *theError = [JWTErrorDescription errorWithCode:JWTUnsupportedAlgorithmError];
272
+ }
273
+ return nil;
274
+ }
275
+
276
+ NSDictionary *header = @{
277
+ @"alg": theAlgorithmName,
278
+ @"typ": @"JWT"
279
+ };
280
+ NSMutableDictionary *allHeaders = [header mutableCopy];
281
+
282
+ if (theHeaders.allKeys.count > 0) {
283
+ [allHeaders addEntriesFromDictionary:theHeaders];
284
+ }
285
+
286
+ NSString *headerSegment = [self encodeSegment:[allHeaders copy] withError:nil];
287
+
288
+ if (!headerSegment) {
289
+ // encode header segment error
290
+ if (theError) {
291
+ *theError = [JWTErrorDescription errorWithCode:JWTEncodingHeaderError];
292
+ }
293
+ return nil;
294
+ }
295
+
296
+ NSString *payloadSegment = [self encodeSegment:thePayload withError:nil];
297
+
298
+ if (!payloadSegment) {
299
+ // encode payment segment error
300
+ if (theError) {
301
+ *theError = [JWTErrorDescription errorWithCode:JWTEncodingPayloadError];
302
+ }
303
+ return nil;
304
+ }
305
+
306
+ NSString *signingInput = [@[headerSegment, payloadSegment] componentsJoinedByString:@"."];
307
+
308
+ NSString *signedOutput = nil;
309
+
310
+ // this happens somewhere outside.
311
+
312
+ NSError *algorithmError = nil;
313
+ if (theSecretData && [theAlgorithm respondsToSelector:@selector(signHash:key:error:)]) {
314
+ NSData *signedOutputData = [theAlgorithm signHash:[signingInput dataUsingEncoding:NSUTF8StringEncoding] key:theSecretData error:&algorithmError];
315
+ signedOutput = [JWTBase64Coder base64UrlEncodedStringWithData:signedOutputData];
316
+ }
317
+ // if (theSecretData && [theAlgorithm respondsToSelector:@selector(encodePayloadData:withSecret:)]) {
318
+ // // not sure that it is correct.
319
+ // NSData *signedOutputData = [theAlgorithm encodePayloadData:[signingInput dataUsingEncoding:NSUTF8StringEncoding] withSecret:theSecretData];
320
+ // signedOutput = [JWTBase64Coder base64UrlEncodedStringWithData:signedOutputData];
321
+ // }
322
+ // not used now.
323
+ // else {
324
+ // NSData *signedOutputData = [theAlgorithm encodePayload:signingInput withSecret:self.jwtSecret];
325
+ // signedOutput = [JWTBase64Coder base64UrlEncodedStringWithData:signedOutputData];
326
+ // }
327
+
328
+ if (algorithmError) {
329
+ // algorithmError
330
+ if (theError) {
331
+ *theError = algorithmError;
332
+ }
333
+ return nil;
334
+ }
335
+ if (!signedOutput) {
336
+ // Make sure signing worked (e.g. we may have issues extracting the key from the PKCS12 bundle if passphrase is incorrect)
337
+ if (theError) {
338
+ *theError = [JWTErrorDescription errorWithCode:JWTEncodingSigningError];
339
+ }
340
+ return nil;
341
+ }
342
+
343
+ return [@[headerSegment, payloadSegment, signedOutput] componentsJoinedByString:@"."];
344
+ }
345
+
346
+ - (NSString *)encodeSegment:(id)theSegment withError:(NSError *__autoreleasing*)error {
347
+ NSData *encodedSegmentData = nil;
348
+
349
+ if (theSegment) {
350
+ encodedSegmentData = [NSJSONSerialization dataWithJSONObject:theSegment options:0 error:error];
351
+ }
352
+ else {
353
+ // error!
354
+ NSError *generatedError = [JWTErrorDescription errorWithCode:JWTInvalidSegmentSerializationError];
355
+ if (error) {
356
+ *error = generatedError;
357
+ }
358
+ NSLog(@"%@ Could not encode segment: %@", self.class, generatedError.localizedDescription);
359
+ return nil;
360
+ }
361
+
362
+ NSString *encodedSegment = nil;
363
+
364
+ if (encodedSegmentData) {
365
+ encodedSegment = [JWTBase64Coder base64UrlEncodedStringWithData:encodedSegmentData];
366
+ }
367
+
368
+ return encodedSegment;
369
+ }
370
+
371
+ - (JWTCodingResultType *)result {
372
+ return self.encode;
373
+ }
374
+ @end
375
+
376
+ @interface JWTDecodingBuilder ()
377
+ #pragma mark - Internal
378
+ @property (copy, nonatomic, readwrite) NSString *internalMessage;
379
+ @property (nonatomic, readwrite) JWTClaimsSet *internalClaimsSet;
380
+
381
+ #pragma mark - Fluent
382
+ @property (copy, nonatomic, readwrite) JWTDecodingBuilder *(^message)(NSString *message);
383
+ @property (copy, nonatomic, readwrite) JWTDecodingBuilder *(^claimsSet)(JWTClaimsSet *claimsSet);
384
+
385
+ @end
386
+
387
+ @interface JWTDecodingBuilder (Fluent_Setup)
388
+ - (instancetype)message:(NSString *)message;
389
+ - (instancetype)claimsSet:(JWTClaimsSet *)claimsSet;
390
+ @end
391
+ @implementation JWTDecodingBuilder (Fluent_Setup)
392
+ - (instancetype)message:(NSString *)message {
393
+ self.internalMessage = message;
394
+ return self;
395
+ }
396
+ - (instancetype)claimsSet:(JWTClaimsSet *)claimsSet {
397
+ self.internalClaimsSet = claimsSet;
398
+ return self;
399
+ }
400
+ - (void)setupFluent {
401
+ [super setupFluent];
402
+ __weak typeof(self) weakSelf = self;
403
+ self.message = ^(NSString *message) {
404
+ return [weakSelf message:message];
405
+ };
406
+ self.claimsSet = ^(JWTClaimsSet *claimsSet) {
407
+ return [weakSelf claimsSet:claimsSet];
408
+ };
409
+ }
410
+ @end
411
+
412
+ @implementation JWTDecodingBuilder
413
+ #pragma mark - Create
414
+ + (instancetype)decodeMessage:(NSString *)message {
415
+ return ((JWTDecodingBuilder *)[self createWithEmptyChain]).message(message);
416
+ }
417
+ @end
418
+
419
+ @implementation JWTDecodingBuilder (Coding)
420
+ - (JWTCodingResultType *)decode {
421
+ // do!
422
+ // iterate over items in chain!
423
+ // and return if everything ok!
424
+ // or return error!
425
+ NSError *error = nil;
426
+ NSDictionary *decodedDictionary = nil;
427
+ NSString *message = self.internalMessage;
428
+ NSNumber *options = self.internalOptions;
429
+ NSArray *holders = self.internalChain.holders;
430
+ JWTClaimsSet *claimsSet = self.internalClaimsSet;
431
+
432
+ // ERROR: HOLDERS ARE EMPTY.
433
+ if (holders.count == 0) {
434
+ error = [JWTErrorDescription errorWithCode:JWTDecodingHoldersChainEmptyError];
435
+ }
436
+
437
+ for (id <JWTAlgorithmDataHolderProtocol>holder in self.internalChain.holders) {
438
+ // try decode!
439
+ id <JWTAlgorithm> algorithm = holder.internalAlgorithm;
440
+ NSData *secretData = holder.internalSecretData;
441
+ // try to retrieve passphrase.
442
+ decodedDictionary = [self decodeMessage:message secretData:secretData algorithm:algorithm options:options error:&error];
443
+ if (decodedDictionary && (error == nil)) {
444
+ break;
445
+ }
446
+ }
447
+
448
+ // claimsSet verification.
449
+ JWTCodingResultType *result = nil;
450
+ if (error) {
451
+ return [[JWTCodingResultType alloc] initWithErrorResult:[[JWTCodingResultTypeError alloc] initWithError:error]];
452
+ }
453
+
454
+ if (claimsSet) {
455
+ BOOL claimsVerified = [JWTClaimsSetVerifier verifyClaimsSet:[JWTClaimsSetSerializer claimsSetWithDictionary:decodedDictionary[JWTCodingResultPayload]] withTrustedClaimsSet:claimsSet];
456
+ if (!claimsVerified){
457
+ error = [JWTErrorDescription errorWithCode:JWTClaimsSetVerificationFailed];
458
+ return [[JWTCodingResultType alloc] initWithErrorResult:[[JWTCodingResultTypeError alloc] initWithError:error]];
459
+ }
460
+ }
461
+
462
+ if (decodedDictionary) {
463
+ NSDictionary *headers = decodedDictionary[JWTCodingResultHeaders];
464
+ NSDictionary *payload = decodedDictionary[JWTCodingResultPayload];
465
+ result = [[JWTCodingResultType alloc] initWithSuccessResult:[[JWTCodingResultTypeSuccess alloc] initWithHeaders:headers withPayload:payload]];
466
+ }
467
+ else {
468
+ NSLog(@"%@ something went wrong! result is nil!", self.debugDescription);
469
+ }
470
+
471
+ return result;
472
+ }
473
+
474
+ // Maybe later add algorithmName
475
+ - (NSDictionary *)decodeMessage:(NSString *)theMessage secretData:(NSData *)theSecretData algorithm:(id<JWTAlgorithm>)theAlgorithm options:(NSNumber *)theOptions error:(NSError *__autoreleasing *)theError {
476
+
477
+ BOOL skipVerification = [theOptions boolValue];
478
+ NSString *theAlgorithmName = [theAlgorithm name];
479
+
480
+ NSArray *parts = [theMessage componentsSeparatedByString:@"."];
481
+
482
+ if (parts.count < 3) {
483
+ // generate error?
484
+ if (theError) {
485
+ *theError = [JWTErrorDescription errorWithCode:JWTInvalidFormatError];
486
+ }
487
+ return nil;
488
+ }
489
+
490
+ NSString *headerPart = parts[0];
491
+ NSString *payloadPart = parts[1];
492
+ NSString *signedPart = parts[2];
493
+
494
+ // decode headerPart
495
+ NSError *jsonError = nil;
496
+ NSData *headerData = [JWTBase64Coder dataWithBase64UrlEncodedString:headerPart];
497
+ id headerJSON = [NSJSONSerialization JSONObjectWithData:headerData
498
+ options:0
499
+ error:&jsonError];
500
+ if (jsonError) {
501
+ if (theError) {
502
+ *theError = [JWTErrorDescription errorWithCode:JWTDecodingHeaderError];
503
+ }
504
+ return nil;
505
+ }
506
+ NSDictionary *header = (NSDictionary *)headerJSON;
507
+ if (!header) {
508
+ if (theError) {
509
+ *theError = [JWTErrorDescription errorWithCode:JWTNoHeaderError];
510
+ }
511
+ return nil;
512
+ }
513
+
514
+ if (!skipVerification) {
515
+ // find algorithm
516
+
517
+ //It is insecure to trust the header's value for the algorithm, since
518
+ //the signature hasn't been verified yet, so an algorithm must be provided
519
+ if (!theAlgorithmName) {
520
+ if (theError) {
521
+ *theError = [JWTErrorDescription errorWithCode:JWTUnspecifiedAlgorithmError];
522
+ }
523
+ return nil;
524
+ }
525
+
526
+ NSString *headerAlgorithmName = header[@"alg"];
527
+
528
+ //If the algorithm in the header doesn't match what's expected, verification fails
529
+ if (![theAlgorithmName isEqualToString:headerAlgorithmName]) {
530
+ if (theError) {
531
+ *theError = [JWTErrorDescription errorWithCode:JWTAlgorithmNameMismatchError];
532
+ }
533
+ return nil;
534
+ }
535
+
536
+ // A shit logic, but...
537
+ // You should copy algorithm if this algorithm conforms to RSAlgorithm (NSCopying).
538
+ // Now RS Algorithm holds too much. ( All data about keys :/ )
539
+ // Need further investigation.
540
+ id<JWTAlgorithm> algorithm = nil;
541
+ if ([theAlgorithm conformsToProtocol:@protocol(JWTRSAlgorithm)]) {
542
+ algorithm = [(id<JWTRSAlgorithm>)theAlgorithm copyWithZone:nil];
543
+ }
544
+ else {
545
+ algorithm = [JWTAlgorithmFactory algorithmByName:theAlgorithmName];
546
+ }
547
+
548
+ if (!algorithm) {
549
+ if (theError) {
550
+ *theError = [JWTErrorDescription errorWithCode:JWTUnsupportedAlgorithmError];
551
+ }
552
+ return nil;
553
+ }
554
+
555
+ // Verify the signed part
556
+ NSString *signingInput = [@[headerPart, payloadPart] componentsJoinedByString:@"."];
557
+ BOOL signatureValid = NO;
558
+
559
+ NSError *algorithmError = nil;
560
+ if (theSecretData && [algorithm respondsToSelector:@selector(verifyHash:signature:key:error:)]) {
561
+ signatureValid =
562
+ //[algorithm verifySignedInput:signingInput withSignature:signedPart verificationKeyData:theSecretData];
563
+ [algorithm verifyHash:[signingInput dataUsingEncoding:NSUTF8StringEncoding] signature:[JWTBase64Coder dataWithBase64UrlEncodedString:signedPart] key:theSecretData error:&algorithmError];
564
+ }
565
+ // if (theSecretData && [algorithm respondsToSelector:@selector(verifySignedInput:withSignature:verificationKeyData:)]) {
566
+ // signatureValid = [algorithm verifySignedInput:signingInput withSignature:signedPart verificationKeyData:theSecretData];
567
+ //
568
+ // // Not used now.
569
+ //// } else {
570
+ //// signatureValid = [algorithm verifySignedInput:signingInput withSignature:signedPart verificationKey:theSecret];
571
+ // }
572
+
573
+ if (algorithmError) {
574
+ if (theError) {
575
+ *theError = algorithmError;
576
+ }
577
+ return nil;
578
+ }
579
+ if (!signatureValid) {
580
+ if (theError) {
581
+ *theError = [JWTErrorDescription errorWithCode:JWTInvalidSignatureError];
582
+ }
583
+ return nil;
584
+ }
585
+ }
586
+
587
+ // and decode payload
588
+ jsonError = nil;
589
+ NSData *payloadData = [JWTBase64Coder dataWithBase64UrlEncodedString:payloadPart];
590
+ id payloadJSON = [NSJSONSerialization JSONObjectWithData:payloadData
591
+ options:0
592
+ error:&jsonError];
593
+ if (jsonError) {
594
+ if (theError) {
595
+ *theError = [JWTErrorDescription errorWithCode:JWTDecodingPayloadError];
596
+ }
597
+ return nil;
598
+ }
599
+ NSDictionary *payload = (NSDictionary *)payloadJSON;
600
+
601
+ if (!payload) {
602
+ if (theError) {
603
+ *theError = [JWTErrorDescription errorWithCode:JWTNoPayloadError];
604
+ }
605
+ return nil;
606
+ }
607
+
608
+ NSDictionary *result = @{
609
+ JWTCodingResultHeaders : header,
610
+ JWTCodingResultPayload : payload
611
+ };
612
+
613
+ return result;
614
+ }
615
+
616
+ - (JWTCodingResultType *)result {
617
+ return self.decode;
618
+ }
619
+ @end