Skip to content

Commit 24085c9

Browse files
Merge pull request #52 from gsmedley/master
allow wildcard domains
2 parents c000a29 + cb0d5e3 commit 24085c9

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,13 @@ export interface HttpsSSLPinningOptions {
103103
certificate: string
104104
allowInvalidCertificates?: boolean
105105
validatesDomainName?: boolean
106+
commonName?: string
106107
}
107108
```
108109
Option | Description
109110
------------ | -------------
110-
`host: string` | This must be the top level domain name eg `httpbin.org`.
111+
`host: string` | This must be the request domain name eg `sales.company.org`.
112+
`commonName?: string` | Default: options.host, set if certificate CN is different from the host eg `*.company.org`
111113
`certificate: string` | The uri path to your `.cer` certificate file.
112114
`allowInvalidCertificates?: boolean` | Default: `false`. This should **always** be `false` if you are using SSL pinning. Set this to `true` if you're using a self-signed certificate.
113115
`validatesDomainName?: boolean` | Default: `true`. Determines if the domain name should be validated with your pinned certificate.

demo/app/main-page.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function getHttpbin() {
4747
}
4848

4949
export function getHttpbinLargeResponse() {
50-
getRequest('https://httpbin.org/bytes/100000', true);
50+
getRequest('https://httpbin.org/bytes/100000', true);
5151
}
5252

5353
export function getMockbin() {
@@ -57,7 +57,7 @@ export function getMockbin() {
5757
export function enableSSLPinning(args: Observable.EventData) {
5858
let dir = fs.knownFolders.currentApp().getFolder('assets');
5959
let certificate = dir.getFile('httpbin.org.cer').path;
60-
Https.enableSSLPinning({host: 'httpbin.org', certificate});
60+
Https.enableSSLPinning({host: 'httpbin.org', commonName: "httpbin.org", certificate});
6161
console.log('enabled');
6262
}
6363

src/https.android.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface Ipeer {
66
allowInvalidCertificates: boolean;
77
validatesDomainName: boolean;
88
host?: string;
9+
commonName?: string;
910
certificate?: string;
1011
x509Certificate?: java.security.cert.Certificate;
1112
}
@@ -41,6 +42,10 @@ export function enableSSLPinning(options: Https.HttpsSSLPinningOptions) {
4142
return;
4243
}
4344
peer.host = options.host;
45+
peer.commonName = options.host;
46+
if ( options.commonName != null ) {
47+
peer.commonName = options.commonName;
48+
}
4449
peer.certificate = certificate;
4550
if (options.allowInvalidCertificates === true) {
4651
peer.allowInvalidCertificates = true;
@@ -130,7 +135,7 @@ function getClient(reload: boolean = false, timeout: number = 10): okhttp3.OkHtt
130135
hv.verify(peer.host, session) &&
131136
peer.host === hostname &&
132137
peer.host === session.getPeerHost() &&
133-
pp.indexOf(peer.host) !== -1
138+
pp.indexOf(peer.commonName) !== -1
134139
);
135140
},
136141
}));
@@ -180,7 +185,7 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
180185
if ((['GET', 'HEAD'].indexOf(opts.method) !== -1) || (opts.method === 'DELETE' && !isDefined(opts.body))) {
181186
request[methods[opts.method]]();
182187
} else {
183-
let type = <string>opts.headers['Content-Type'] || 'application/json';
188+
let type = opts.headers && opts.headers['Content-Type'] ? <string>opts.headers['Content-Type'] : 'application/json';
184189
let body = <any>opts.body || {};
185190
try {
186191
body = JSON.stringify(body);
@@ -223,10 +228,10 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
223228
// }
224229

225230
let content = response.body().string();
226-
try {
227-
content = JSON.parse(content);
228-
} catch (e) {
229-
}
231+
try {
232+
content = JSON.parse(content);
233+
} catch (e) {
234+
}
230235

231236
let statusCode = response.code();
232237

src/https.common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface HttpsSSLPinningOptions {
55
certificate: string;
66
allowInvalidCertificates?: boolean;
77
validatesDomainName?: boolean;
8+
commonName?: string;
89
}
910

1011
export interface HttpsRequestObject {

0 commit comments

Comments
 (0)