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

Add support for auth claims. #8

Merged
merged 1 commit into from
May 10, 2023
Merged
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
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;
}
}
}
}