Skip to content

Commit

Permalink
add support for auth claims. (#8)
Browse files Browse the repository at this point in the history
Co-authored-by: David Regimbal <dave@bitboss.org>
  • Loading branch information
dregimbal1 and David Regimbal authored May 10, 2023
1 parent cc913d2 commit 9f904a6
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ print_r($client->get('account'));
?>
```

Making a Claims Request
----------

```php
<?php
// Instantiate.
require __DIR__ . '/vendor/autoload.php';
use OpenXBL\Auth;

$auth = new Auth('YOUR_PUBLIC_KEY');

$response = $auth->claim('CODE_VALUE');
?>
```

Optional Parameters
----------
```php
Expand Down
46 changes: 46 additions & 0 deletions src/Auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace OpenXBL;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\GuzzleException;
use OpenXBL\Models\ClaimsResponse;

class Auth extends GuzzleClient
{
private string $publicKey;
private string $baseUrl = 'https://xbl.io/app/';

public function __construct(string $publicKey)
{
$this->publicKey = $publicKey;
parent::__construct([
'base_uri' => $this->baseUrl,
]);
}

/**
* @throws GuzzleException
*/
public function claim(string $code): ClaimsResponse
{
$options = [
'headers' => [
'Accept' => 'application/json',
],
'json' => [
'code' => $code,
'app_key' => $this->publicKey,
],
];

$request = $this->request('POST', 'claim', $options);

return new ClaimsResponse((string) $request->getBody());
}

public function getLoginUrl(): string
{
return "https://xbl.io/app/auth/{$this->publicKey}";
}
}
2 changes: 1 addition & 1 deletion src/HttpService.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ protected function request(): ?string

$request = $this->client->request($this->method, $this->endpoint, $options);

return $request->getBody()->getContents();
return (string) $request->getBody();
}
}
21 changes: 21 additions & 0 deletions src/Models/ClaimsResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace OpenXBL\Models;

class ClaimsResponse
{
public string $appKey;
public string $xuid;
public string $gamertag;
public string $email;
public ?string $avatar = null;

public function __construct(string $json) {
$json = json_decode($json, true);
foreach ($json as $key => $value) {
if (property_exists(__CLASS__, $key)) {
$this->$key = $value;
}
}
}
}

0 comments on commit 9f904a6

Please sign in to comment.