fix adapter
This commit is contained in:
@@ -1644,8 +1644,41 @@ describe('apple signin auth adapter', () => {
|
|||||||
|
|
||||||
describe('Apple Game Center Auth adapter', () => {
|
describe('Apple Game Center Auth adapter', () => {
|
||||||
const gcenter = require('../lib/Adapters/Auth/gcenter');
|
const gcenter = require('../lib/Adapters/Auth/gcenter');
|
||||||
|
const fs = require('fs');
|
||||||
|
const testCert = fs.readFileSync(__dirname + '/support/cert/game_center.pem');
|
||||||
|
it('can load adapter', async () => {
|
||||||
|
const options = {
|
||||||
|
gcenter: {
|
||||||
|
rootCertificateUrl:
|
||||||
|
'https://cacerts.digicert.com/DigiCertTrustedG4CodeSigningRSA4096SHA3842021CA1.crt.pem',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter(
|
||||||
|
'gcenter',
|
||||||
|
options
|
||||||
|
);
|
||||||
|
await adapter.validateAppId(
|
||||||
|
appIds,
|
||||||
|
{ publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer' },
|
||||||
|
providerOptions
|
||||||
|
);
|
||||||
|
});
|
||||||
it('validateAuthData should validate', async () => {
|
it('validateAuthData should validate', async () => {
|
||||||
|
const options = {
|
||||||
|
gcenter: {
|
||||||
|
rootCertificateUrl:
|
||||||
|
'https://cacerts.digicert.com/DigiCertTrustedG4CodeSigningRSA4096SHA3842021CA1.crt.pem',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter(
|
||||||
|
'gcenter',
|
||||||
|
options
|
||||||
|
);
|
||||||
|
await adapter.validateAppId(
|
||||||
|
appIds,
|
||||||
|
{ publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer' },
|
||||||
|
providerOptions
|
||||||
|
);
|
||||||
// real token is used
|
// real token is used
|
||||||
const authData = {
|
const authData = {
|
||||||
id: 'G:1965586982',
|
id: 'G:1965586982',
|
||||||
@@ -1656,29 +1689,49 @@ describe('Apple Game Center Auth adapter', () => {
|
|||||||
salt: 'DzqqrQ==',
|
salt: 'DzqqrQ==',
|
||||||
bundleId: 'cloud.xtralife.gamecenterauth',
|
bundleId: 'cloud.xtralife.gamecenterauth',
|
||||||
};
|
};
|
||||||
|
gcenter.cache['https://static.gc.apple.com/public-key/gc-prod-4.cer'] = testCert;
|
||||||
await gcenter.validateAuthData(authData);
|
await gcenter.validateAuthData(authData);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('validateAuthData invalid signature id', async () => {
|
it('validateAuthData invalid signature id', async () => {
|
||||||
|
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter(
|
||||||
|
'gcenter',
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
await adapter.validateAppId(
|
||||||
|
appIds,
|
||||||
|
{ publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer' },
|
||||||
|
providerOptions
|
||||||
|
);
|
||||||
const authData = {
|
const authData = {
|
||||||
id: 'G:1965586982',
|
id: 'G:1965586982',
|
||||||
publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer',
|
publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-6.cer',
|
||||||
timestamp: 1565257031287,
|
timestamp: 1565257031287,
|
||||||
signature: '1234',
|
signature: '1234',
|
||||||
salt: 'DzqqrQ==',
|
salt: 'DzqqrQ==',
|
||||||
bundleId: 'cloud.xtralife.gamecenterauth',
|
bundleId: 'com.example.com',
|
||||||
};
|
};
|
||||||
|
await expectAsync(gcenter.validateAuthData(authData)).toBeRejectedWith(
|
||||||
try {
|
new Parse.Error(Parse.Error.SCRIPT_FAILED, 'Apple Game Center - invalid signature')
|
||||||
await gcenter.validateAuthData(authData);
|
);
|
||||||
fail();
|
|
||||||
} catch (e) {
|
|
||||||
expect(e.message).toBe('Apple Game Center - invalid signature');
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('validateAuthData invalid public key http url', async () => {
|
it('validateAuthData invalid public key http url', async () => {
|
||||||
|
const options = {
|
||||||
|
gcenter: {
|
||||||
|
rootCertificateUrl:
|
||||||
|
'https://cacerts.digicert.com/DigiCertTrustedG4CodeSigningRSA4096SHA3842021CA1.crt.pem',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter(
|
||||||
|
'gcenter',
|
||||||
|
options
|
||||||
|
);
|
||||||
|
await adapter.validateAppId(
|
||||||
|
appIds,
|
||||||
|
{ publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer' },
|
||||||
|
providerOptions
|
||||||
|
);
|
||||||
const publicKeyUrls = [
|
const publicKeyUrls = [
|
||||||
'example.com',
|
'example.com',
|
||||||
'http://static.gc.apple.com/public-key/gc-prod-4.cer',
|
'http://static.gc.apple.com/public-key/gc-prod-4.cer',
|
||||||
@@ -1706,6 +1759,78 @@ describe('Apple Game Center Auth adapter', () => {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should not validate Symantec Cert', async () => {
|
||||||
|
const options = {
|
||||||
|
gcenter: {
|
||||||
|
rootCertificateUrl:
|
||||||
|
'https://cacerts.digicert.com/DigiCertTrustedG4CodeSigningRSA4096SHA3842021CA1.crt.pem',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter(
|
||||||
|
'gcenter',
|
||||||
|
options
|
||||||
|
);
|
||||||
|
await adapter.validateAppId(
|
||||||
|
appIds,
|
||||||
|
{ publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer' },
|
||||||
|
providerOptions
|
||||||
|
);
|
||||||
|
expect(() =>
|
||||||
|
gcenter.verifyPublicKeyIssuer(
|
||||||
|
testCert,
|
||||||
|
'https://static.gc.apple.com/public-key/gc-prod-4.cer'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('adapter should load default cert', async () => {
|
||||||
|
const options = {
|
||||||
|
gcenter: {},
|
||||||
|
};
|
||||||
|
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter(
|
||||||
|
'gcenter',
|
||||||
|
options
|
||||||
|
);
|
||||||
|
await adapter.validateAppId(
|
||||||
|
appIds,
|
||||||
|
{ publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer' },
|
||||||
|
providerOptions
|
||||||
|
);
|
||||||
|
const previous = new Date();
|
||||||
|
await adapter.validateAppId(
|
||||||
|
appIds,
|
||||||
|
{ publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer' },
|
||||||
|
providerOptions
|
||||||
|
);
|
||||||
|
|
||||||
|
const duration = new Date().getTime() - previous.getTime();
|
||||||
|
expect(duration).toEqual(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('adapter should throw', async () => {
|
||||||
|
const options = {
|
||||||
|
gcenter: {
|
||||||
|
rootCertificateUrl: 'https://example.com',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter(
|
||||||
|
'gcenter',
|
||||||
|
options
|
||||||
|
);
|
||||||
|
await expectAsync(
|
||||||
|
adapter.validateAppId(
|
||||||
|
appIds,
|
||||||
|
{ publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer' },
|
||||||
|
providerOptions
|
||||||
|
)
|
||||||
|
).toBeRejectedWith(
|
||||||
|
new Parse.Error(
|
||||||
|
Parse.Error.OBJECT_NOT_FOUND,
|
||||||
|
'Apple Game Center auth adapter parameter `rootCertificateURL` is invalid.'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('phant auth adapter', () => {
|
describe('phant auth adapter', () => {
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ const authData = {
|
|||||||
const { Parse } = require('parse/node');
|
const { Parse } = require('parse/node');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const https = require('https');
|
const https = require('https');
|
||||||
|
const { pki } = require('node-forge');
|
||||||
|
const ca = { cert: null, url: null };
|
||||||
const cache = {}; // (publicKey -> cert) cache
|
const cache = {}; // (publicKey -> cert) cache
|
||||||
|
|
||||||
function verifyPublicKeyUrl(publicKeyUrl) {
|
function verifyPublicKeyUrl(publicKeyUrl) {
|
||||||
@@ -52,39 +53,52 @@ async function getAppleCertificate(publicKeyUrl) {
|
|||||||
path: url.pathname,
|
path: url.pathname,
|
||||||
method: 'HEAD',
|
method: 'HEAD',
|
||||||
};
|
};
|
||||||
const headers = await new Promise((resolve, reject) =>
|
const cert_headers = await new Promise((resolve, reject) =>
|
||||||
https.get(headOptions, res => resolve(res.headers)).on('error', reject)
|
https.get(headOptions, res => resolve(res.headers)).on('error', reject)
|
||||||
);
|
);
|
||||||
if (
|
if (
|
||||||
headers['content-type'] !== 'application/pkix-cert' ||
|
cert_headers['content-type'] !== 'application/pkix-cert' ||
|
||||||
headers['content-length'] == null ||
|
cert_headers['content-length'] == null ||
|
||||||
headers['content-length'] > 10000
|
cert_headers['content-length'] > 10000
|
||||||
) {
|
) {
|
||||||
throw new Parse.Error(
|
throw new Parse.Error(
|
||||||
Parse.Error.OBJECT_NOT_FOUND,
|
Parse.Error.OBJECT_NOT_FOUND,
|
||||||
`Apple Game Center - invalid publicKeyUrl: ${publicKeyUrl}`
|
`Apple Game Center - invalid publicKeyUrl: ${publicKeyUrl}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return new Promise((resolve, reject) => {
|
const {certificate, headers} = await getCertificate(publicKeyUrl);
|
||||||
https
|
if (headers['cache-control']) {
|
||||||
.get(publicKeyUrl, res => {
|
const expire = headers['cache-control'].match(/max-age=([0-9]+)/);
|
||||||
let data = '';
|
|
||||||
res.on('data', chunk => {
|
|
||||||
data += chunk.toString('base64');
|
|
||||||
});
|
|
||||||
res.on('end', () => {
|
|
||||||
const cert = convertX509CertToPEM(data);
|
|
||||||
if (res.headers['cache-control']) {
|
|
||||||
var expire = res.headers['cache-control'].match(/max-age=([0-9]+)/);
|
|
||||||
if (expire) {
|
if (expire) {
|
||||||
cache[publicKeyUrl] = cert;
|
cache[publicKeyUrl] = certificate;
|
||||||
// we'll expire the cache entry later, as per max-age
|
// we'll expire the cache entry later, as per max-age
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
delete cache[publicKeyUrl];
|
delete cache[publicKeyUrl];
|
||||||
}, parseInt(expire[1], 10) * 1000);
|
}, parseInt(expire[1], 10) * 1000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resolve(cert);
|
return verifyPublicKeyIssuer(certificate, publicKeyUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCertificate(url, buffer) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
https
|
||||||
|
.get(url, res => {
|
||||||
|
const data = [];
|
||||||
|
res.on('data', chunk => {
|
||||||
|
data.push(chunk);
|
||||||
|
});
|
||||||
|
res.on('end', () => {
|
||||||
|
if (buffer) {
|
||||||
|
resolve({certificate: Buffer.concat(data), headers: res.headers});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let cert = '';
|
||||||
|
for (const chunk of data) {
|
||||||
|
cert += chunk.toString('base64');
|
||||||
|
}
|
||||||
|
const certificate = convertX509CertToPEM(cert);
|
||||||
|
resolve({certificate, headers: res.headers});
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.on('error', reject);
|
.on('error', reject);
|
||||||
@@ -115,6 +129,27 @@ function verifySignature(publicKey, authData) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function verifyPublicKeyIssuer(cert, publicKeyUrl) {
|
||||||
|
const publicKeyCert = pki.certificateFromPem(cert);
|
||||||
|
if (!ca.cert) {
|
||||||
|
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Apple Game Center auth adapter parameter `rootCertificateURL` is invalid.');
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (!ca.cert.verify(publicKeyCert)) {
|
||||||
|
throw new Parse.Error(
|
||||||
|
Parse.Error.OBJECT_NOT_FOUND,
|
||||||
|
`Apple Game Center - invalid publicKeyUrl: ${publicKeyUrl}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
throw new Parse.Error(
|
||||||
|
Parse.Error.OBJECT_NOT_FOUND,
|
||||||
|
`Apple Game Center - invalid publicKeyUrl: ${publicKeyUrl}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return cert;
|
||||||
|
}
|
||||||
|
|
||||||
// Returns a promise that fulfills if this user id is valid.
|
// Returns a promise that fulfills if this user id is valid.
|
||||||
async function validateAuthData(authData) {
|
async function validateAuthData(authData) {
|
||||||
if (!authData.id) {
|
if (!authData.id) {
|
||||||
@@ -126,11 +161,27 @@ async function validateAuthData(authData) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Returns a promise that fulfills if this app id is valid.
|
// Returns a promise that fulfills if this app id is valid.
|
||||||
function validateAppId() {
|
async function validateAppId(appIds, authData, options = {}) {
|
||||||
return Promise.resolve();
|
if (!options.rootCertificateUrl) {
|
||||||
|
options.rootCertificateUrl = 'https://cacerts.digicert.com/DigiCertTrustedG4CodeSigningRSA4096SHA3842021CA1.crt.pem'
|
||||||
|
}
|
||||||
|
if (ca.url === options.rootCertificateUrl) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const {certificate, headers} = await getCertificate(options.rootCertificateUrl, true);
|
||||||
|
if (
|
||||||
|
headers['content-type'] !== 'application/x-pem-file' ||
|
||||||
|
headers['content-length'] == null ||
|
||||||
|
headers['content-length'] > 10000
|
||||||
|
) {
|
||||||
|
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Apple Game Center auth adapter parameter `rootCertificateURL` is invalid.');
|
||||||
|
}
|
||||||
|
ca.cert = pki.certificateFromPem(certificate);
|
||||||
|
ca.url = options.rootCertificateUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
validateAppId,
|
validateAppId,
|
||||||
validateAuthData,
|
validateAuthData,
|
||||||
|
cache,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user