forked from cbellone/nativescript-https
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps.ios.js
More file actions
executable file
·154 lines (153 loc) · 6.18 KB
/
https.ios.js
File metadata and controls
executable file
·154 lines (153 loc) · 6.18 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var types_1 = require("utils/types");
var policies = {
def: AFSecurityPolicy.defaultPolicy(),
secured: false,
};
function enableSSLPinning(options) {
policies.secure = CustomAFSecurityPolicy.policyWithPinningMode(1);
var allowInvalidCertificates = (types_1.isDefined(options.allowInvalidCertificates)) ? options.allowInvalidCertificates : false;
policies.secure.allowInvalidCertificates = allowInvalidCertificates;
var validatesDomainName = (types_1.isDefined(options.validatesDomainName)) ? options.validatesDomainName : true;
policies.secure.validatesDomainName = validatesDomainName;
var data = NSData.dataWithContentsOfFile(options.certificate);
policies.secure.pinnedCertificates = NSSet.setWithObject(data);
policies.secured = true;
console.log('nativescript-https > Enabled SSL pinning');
}
exports.enableSSLPinning = enableSSLPinning;
function disableSSLPinning() {
policies.secured = false;
console.log('nativescript-https > Disabled SSL pinning');
}
exports.disableSSLPinning = disableSSLPinning;
console.info('nativescript-https > Disabled SSL pinning by default');
function AFSuccess(resolve, task, data) {
var content;
if (data && data.class) {
if (data.enumerateKeysAndObjectsUsingBlock || data.class().name == 'NSArray') {
var serial = NSJSONSerialization.dataWithJSONObjectOptionsError(data, 1);
content = NSString.alloc().initWithDataEncoding(serial, NSUTF8StringEncoding).toString();
}
else if (data.class().name == 'NSData') {
content = NSString.alloc().initWithDataEncoding(data, NSUTF8StringEncoding).toString();
}
else {
content = data;
}
}
else {
content = data;
}
resolve({ task: task, content: content });
}
function AFFailure(resolve, reject, task, error) {
var data = error.userInfo.valueForKey(AFNetworkingOperationFailingURLResponseDataErrorKey);
var body = NSString.alloc().initWithDataEncoding(data, NSUTF8StringEncoding).toString();
var content = {
body: body,
description: error.description,
reason: error.localizedDescription,
url: error.userInfo.objectForKey('NSErrorFailingURLKey').description
};
if (policies.secured == true) {
content.description = 'nativescript-https > Invalid SSL certificate! ' + content.description;
}
var reason = error.localizedDescription;
resolve({ task: task, content: body, reason: reason });
}
function request(opts) {
return new Promise(function (resolve, reject) {
try {
var manager_1 = AFHTTPSessionManager.manager();
var cTypeJSON = opts.headers && opts.headers['Content-Type'] == 'application/json';
if (cTypeJSON) {
manager_1.requestSerializer = AFJSONRequestSerializer.serializer();
}
else {
manager_1.requestSerializer = AFHTTPRequestSerializer.serializer();
}
manager_1.responseSerializer = AFHTTPResponseSerializer.serializer();
manager_1.requestSerializer.allowsCellularAccess = true;
manager_1.securityPolicy = (policies.secured == true) ? policies.secure : policies.def;
manager_1.requestSerializer.timeoutInterval = 60;
var heads_1 = opts.headers;
if (heads_1) {
Object.keys(heads_1).forEach(function (key) {
manager_1.requestSerializer.setValueForHTTPHeaderField(heads_1[key], key);
});
}
var methods = {
'GET': 'GETParametersSuccessFailure',
'POST': 'POSTParametersSuccessFailure',
'PUT': 'PUTParametersSuccessFailure',
'DELETE': 'DELETEParametersSuccessFailure',
'PATCH': 'PATCHParametersSuccessFailure',
'HEAD': 'HEADParametersSuccessFailure',
};
manager_1[methods[opts.method]](opts.url, serializeBody(opts.body, cTypeJSON), function success(task, data) {
AFSuccess(resolve, task, data);
}, function failure(task, error) {
AFFailure(resolve, reject, task, error);
});
}
catch (error) {
reject(error);
}
}).then(function (AFResponse) {
var sendi = {
content: AFResponse.content,
headers: {},
};
var response = AFResponse.task.response;
if (!types_1.isNullOrUndefined(response)) {
sendi.statusCode = response.statusCode;
var dict = response.allHeaderFields;
dict.enumerateKeysAndObjectsUsingBlock(function (k, v) {
sendi.headers[k] = v;
});
}
if (AFResponse.reason) {
sendi.reason = AFResponse.reason;
}
return Promise.resolve(sendi);
});
}
exports.request = request;
function serializeBody(body, isJSON) {
if (body) {
if (body.constructor === Array && isJSON) {
var arr_1 = NSMutableArray.new();
body.forEach(function (e) {
var dict = NSMutableDictionary.new();
Object.keys(e).forEach(function (key) {
dict.setValueForKey(e[key], key);
});
arr_1.addObject(dict);
});
return arr_1;
}
else if (types_1.isObject(body)) {
var dict_1 = NSMutableDictionary.new();
Object.keys(body).forEach(function (key) {
dict_1.setValueForKey(body[key], key);
});
return dict_1;
}
}
return null;
}
var CustomAFSecurityPolicy = (function (_super) {
__extends(CustomAFSecurityPolicy, _super);
function CustomAFSecurityPolicy() {
return _super !== null && _super.apply(this, arguments) || this;
}
CustomAFSecurityPolicy.prototype.evaluateServerTrustForDomain = function (serverTrust, domain) {
console.log("authorizing domain: " + domain);
return true;
};
return CustomAFSecurityPolicy;
}(AFSecurityPolicy));
exports.CustomAFSecurityPolicy = CustomAFSecurityPolicy;
//# sourceMappingURL=https.ios.js.map