Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generateIdentityVerification method added for third-party server authentication #39

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Mac crap
.DS_Store
.DS_Store
.idea
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ For more information, see the [PhoneGap build docs](http://docs.build.phonegap.c

## Usage

### Auth with Third Party backend

You should do this as soon as your deviceready event has been fired. The plug handles the various auth scenarios for you.
You need to enable game-center for your app from Itunes Connect
> Sometimes it's needed to add some dummy leaderboard from game-center tab in itunes connect to make it work
```
var successCallback = function (data) {
alert(data.alias);
// data.alias, data.bundleId, data.displayName, data.playerId, data.publicKeyUrl, data.salt, data.signature, data.timestamp
};

gamecenter.generateIdentityVerification(successCallback, failureCallback);
```

### Auth

You should do this as soon as your deviceready event has been fired. The plug handles the various auth scenarios for you.
Expand Down
1 change: 1 addition & 0 deletions src/ios/GameCenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@interface GameCenter : CDVPlugin <GKGameCenterControllerDelegate>

- (void) auth:(CDVInvokedUrlCommand*)command;
- (void) generateIdentityVerification:(CDVInvokedUrlCommand*)command;
- (void) getPlayerImage:(CDVInvokedUrlCommand*)command;
- (void) submitScore:(CDVInvokedUrlCommand*)command;
- (void) showLeaderboard:(CDVInvokedUrlCommand*)command;
Expand Down
98 changes: 92 additions & 6 deletions src/ios/GameCenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ - (void) auth:(CDVInvokedUrlCommand*)command;
{
[self.commandDelegate runInBackground:^{

__weak GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {
CDVPluginResult* pluginResult = nil;
Expand Down Expand Up @@ -47,9 +47,65 @@ - (void) auth:(CDVInvokedUrlCommand*)command;
}];
}

- (void) generateIdentityVerification:(CDVInvokedUrlCommand*)command;
{
[self.commandDelegate runInBackground:^{

GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {
CDVPluginResult* pluginResult = nil;
if (viewController != nil)
{
// Login required
[self.viewController presentViewController:viewController animated:YES completion:nil];
}
else
{
if (localPlayer.isAuthenticated)
{
[localPlayer generateIdentityVerificationSignatureWithCompletionHandler:^(NSURL *publicKeyUrl, NSData *signature, NSData *salt, uint64_t timestamp, NSError *error) {
__block CDVPluginResult* pluginResult = nil;
if(error != nil)
{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
} else {
NSDictionary* user = @{
@"playerId":localPlayer.playerID,
@"alias":localPlayer.alias,
@"displayName":localPlayer.displayName,
@"publicKeyUrl":[publicKeyUrl absoluteString],
@"signature":[self base64forData:signature],
@"salt":[self base64forData:salt],
@"timestamp":@(timestamp),
@"bundleId": [[NSBundle mainBundle] bundleIdentifier]
};
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:user];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

}];

}
else if (error != nil)
{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
else
{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}
};
}];
}

- (void) getPlayerImage:(CDVInvokedUrlCommand*)command;
{
__weak GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
__block CDVPluginResult* pluginResult = nil;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
Expand Down Expand Up @@ -188,7 +244,7 @@ - (void) resetAchievements:(CDVInvokedUrlCommand*)command;
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];

}
];
];

}

Expand Down Expand Up @@ -236,10 +292,10 @@ - (void) gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCen

- (void) getAchievements:(CDVInvokedUrlCommand*)command;
{
__block CDVPluginResult* pluginResult = nil;
NSMutableArray *earntAchievements = [NSMutableArray array];
__block CDVPluginResult* pluginResult = nil;
NSMutableArray *earntAchievements = [NSMutableArray array];

[GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error)
[GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error)
{
if (error == nil)
{
Expand All @@ -264,5 +320,35 @@ - (void) getAchievements:(CDVInvokedUrlCommand*)command;
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (NSString*)base64forData:(NSData*)theData {
const uint8_t* input = (const uint8_t*)[theData bytes];
NSInteger length = [theData length];

static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t* output = (uint8_t*)data.mutableBytes;

NSInteger i;
for (i=0; i < length; i += 3) {
NSInteger value = 0;
NSInteger j;
for (j = i; j < (i + 3); j++) {
value <<= 8;

if (j < length) {
value |= (0xFF & input[j]);
}
}

NSInteger theIndex = (i / 3) * 4;
output[theIndex + 0] = table[(value >> 18) & 0x3F];
output[theIndex + 1] = table[(value >> 12) & 0x3F];
output[theIndex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '=';
output[theIndex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '=';
}

return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}

@end
4 changes: 4 additions & 0 deletions www/gamecenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ GameCenter.prototype.auth = function (success, failure) {
exec(success, failure, "GameCenter", "auth", []);
};

GameCenter.prototype.generateIdentityVerification = function (success, failure) {
exec(success, failure, "GameCenter", "generateIdentityVerification", []);
};

GameCenter.prototype.getPlayerImage = function (success, failure) {
exec(success, failure, "GameCenter", "getPlayerImage", []);
};
Expand Down