Last active
May 25, 2016 18:50
-
-
Save seaders/785cb1318fcbaee41094 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function AuthiOSGameCenter(data, callback) | |
{ | |
if(data.bundle_id !== 'com.example.game') | |
{ | |
callback(false); | |
return; | |
} | |
function handleResponse(key) | |
{ | |
var crypto = require('crypto'), | |
ref = require('ref'); | |
var valid = false; | |
var user = null; | |
if(null !== key) | |
{ | |
var verifier = crypto.createVerify('sha256'); | |
verifier.update(data.player_id, 'utf8'); | |
verifier.update(data.bundle_id, 'utf8'); | |
var buf = ref.alloc('uint64'); | |
ref.writeUInt64BE(buf, 0, data.timestamp.toString()); | |
verifier.update(buf); | |
verifier.update(data.salt, 'base64'); | |
var pmd = '-----BEGIN CERTIFICATE-----'; | |
var base64 = key.toString('base64'); | |
var size = base64.length; | |
for (var i = 0; i < size; i = i + 64) { | |
var end = i + 64 < size ? i + 64 : size; | |
pmd = pmd + '\n' + base64.substring(i, end); | |
} | |
pmd = pmd + '\n-----END CERTIFICATE-----'; | |
valid = verifier.verify(pmd, data.signature, 'base64'); | |
user = valid ? data.player_id : false; | |
} | |
callback(valid, user); | |
} | |
var url = data.public_key_url; | |
var http = 0 === url.indexOf('https') ? | |
require('https') : | |
require('http'); | |
http.get( | |
url, | |
function(resp) | |
{ | |
if(resp.statusCode !== 200) | |
{ | |
callback(); | |
return; | |
} | |
resp.on('data', handleResponse); | |
} | |
); | |
} | |
AuthiOSGameCenter( | |
{ | |
public_key_url: 'https://sandbox.gc.apple.com/public-key/gc-sb-2.cer', | |
timestamp: 1234567890, | |
signature: 'nahtin==', | |
salt: 'nahtin==', | |
player_id: 'G:1234567890', | |
bundle_id: 'com.example.game' | |
}, | |
console.log | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)authenticateLocalPlayer | |
{ | |
__weak GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; | |
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) | |
{ | |
if (viewController != nil) | |
{ | |
[ [ [ [ [UIApplication sharedApplication] delegate] window] rootViewController] | |
presentViewController:viewController animated:YES completion:nil]; | |
} | |
else if (localPlayer.authenticated) | |
{ | |
NSLog(@"%@", localPlayer); | |
[localPlayer generateIdentityVerificationSignatureWithCompletionHandler: | |
^(NSURL *publicKeyUrl, NSData *signature, NSData *salt, uint64_t timestamp, NSError *error) | |
{ | |
NSDictionary *params = @{@"public_key_url": [publicKeyUrl absoluteString], | |
@"timestamp": [NSString stringWithFormat:@"%llu", timestamp], | |
@"signature": [signature base64EncodedStringWithOptions:0], | |
@"salt": [salt base64EncodedStringWithOptions:0], | |
@"player_id": [GKLocalPlayer localPlayer].playerID, | |
@"bundle_id": [[NSBundle mainBundle] bundleIdentifier]}; | |
NSLog(@"%@", params); | |
} | |
]; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment