Skip to content
This repository has been archived by the owner on Feb 15, 2020. It is now read-only.

Updated login mechanism #65

Open
wants to merge 2 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
57 changes: 38 additions & 19 deletions src/Account.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
<?php

namespace Fortnite;

use Fortnite\FortniteClient;
use Fortnite\Model\FortniteStats;
use GuzzleHttp\Exception\GuzzleException;
use Fortnite\Exception\UserNotFoundException;
use Fortnite\Exception\StatsNotFoundException;

use GuzzleHttp\Exception\GuzzleException;
use Fortnite\Exception\StatsNotFoundException;

class Account {
class Account
{

private $account_id;

public function __construct($access_token,$account_id) {
public function __construct($access_token, $account_id)
{
$this->access_token = $access_token;
$this->account_id = $account_id;
}

public static function getDisplayNameFromID($id, $access_token) {
public static function getDisplayNameFromID($id, $access_token)
{
try {
$data = FortniteClient::sendFortniteGetRequest(FortniteClient::FORTNITE_ACCOUNT_API . "public/account?accountId={$id}",
$access_token);
$data = FortniteClient::sendFortniteGetRequest(
FortniteClient::FORTNITE_ACCOUNT_API . "public/account?accountId={$id}",
$access_token
);

return $data[0]->displayName;
} catch (GuzzleException $e) {
Expand All @@ -29,10 +35,13 @@ public static function getDisplayNameFromID($id, $access_token) {
}
}

public function getDisplayNamesFromID($id) {
public function getDisplayNamesFromID($id)
{
try {
$data = FortniteClient::sendFortniteGetRequest(FortniteClient::FORTNITE_ACCOUNT_API . "public/account?accountId=" . join('&accountId=', $id),
$this->access_token);
$data = FortniteClient::sendFortniteGetRequest(
FortniteClient::FORTNITE_ACCOUNT_API . "public/account?accountId=" . join('&accountId=', $id),
$this->access_token
);

return $data;
} catch (GuzzleException $e) {
Expand All @@ -41,25 +50,35 @@ public function getDisplayNamesFromID($id) {
}
}

public function killSession() {
public function killSession()
{
FortniteClient::sendFortniteDeleteRequest(FortniteClient::FORTNITE_ACCOUNT_API . "oauth/sessions/kill/" . $this->access_token, $this->access_token);
}

public function acceptEULA(){
public function acceptEULA()
{
try {
$data = FortniteClient::sendFortniteGetRequest(FortniteClient::FORTNITE_EULA_API . "public/agreements/fn/account/" . $this->account_id .'?locale=en-US',
$this->access_token);
$data = FortniteClient::sendFortniteGetRequest(
FortniteClient::FORTNITE_EULA_API . "public/agreements/fn/account/" . $this->account_id . '?locale=en-US',
$this->access_token
);

FortniteClient::sendFortnitePostRequest(FortniteClient::FORTNITE_EULA_API . "public/agreements/fn/version/".$data->version."/account/".$this->account_id."/accept?locale=en",
$this->access_token,new \StdClass());
FortniteClient::sendFortnitePostRequest(
FortniteClient::FORTNITE_EULA_API . "public/agreements/fn/version/" . $data->version . "/account/" . $this->account_id . "/accept?locale=en",
$this->access_token,
new \StdClass()
);

FortniteClient::sendFortnitePostRequest(FortniteClient::FORTNITE_API.'game/v2/grant_access/'.$this->account_id,
$this->access_token,new \StdClass());
FortniteClient::sendFortnitePostRequest(
FortniteClient::FORTNITE_API . 'game/v2/grant_access/' . $this->account_id,
$this->access_token,
new \StdClass()
);

return true;
} catch (GuzzleException $e) {
if ($e->getResponse()->getStatusCode() == 404) throw new \Exception('Could not read or accept EULA for account id ' . $this->account_id);
throw $e;
}
}
}
}
66 changes: 46 additions & 20 deletions src/Auth.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

namespace Fortnite;

use Fortnite\FortniteClient;
use Fortnite\Profile;
use Fortnite\Status;
use Fortnite\Profile;
use GuzzleHttp\Client;
use Fortnite\FortniteClient;
use Fortnite\Exception\TwoFactorAuthRequiredException;

class Auth {
class Auth
{
private $access_token;
private $in_app_id;
private $refresh_token;
Expand All @@ -16,7 +19,6 @@ class Auth {
public $profile;



// TODO: Probably want to lazy load all of these object initializations. Although currently I'm not sure how to go about that with PHP.
// @Tustin 7/28/2018
/**
Expand All @@ -26,15 +28,16 @@ class Auth {
* @param string $account_id Unreal Engine account id
* @param string $expires_in OAuth2 token expiration time
*/
private function __construct($access_token, $in_app_id, $refresh_token, $account_id, $expires_in) {
private function __construct($access_token, $in_app_id, $refresh_token, $account_id, $expires_in)
{
$this->access_token = $access_token;
$this->in_app_id = $in_app_id;
$this->refresh_token = $refresh_token;
$this->account_id = $account_id;
$this->expires_in = $expires_in;
$this->account = new Account($this->access_token,$this->account_id);
$this->account = new Account($this->access_token, $this->account_id);
$this->status = new Status($this->access_token);
if ($this->status->allowedToPlay() === false){
if ($this->status->allowedToPlay() === false) {
$this->account->acceptEULA();
}
$this->profile = new Profile($this->access_token, $this->account_id);
Expand All @@ -53,7 +56,8 @@ private function __construct($access_token, $in_app_id, $refresh_token, $account
*
* @return self New Auth instance
*/
public static function login($email, $password, $challenge = '', $code = '') {
public static function login($email, $password, $challenge = '', $code = '')
{

$requestParams = [
'includePerms' => 'false', // We don't need these here
Expand All @@ -63,7 +67,8 @@ public static function login($email, $password, $challenge = '', $code = '') {
if (empty($challenge) && empty($code)) {
// Regular login
$requestParams = array_merge($requestParams, [
'grant_type' => 'password',
'grant_type' => 'exchange_code',
'exchange_code' => 'test',
'username' => $email,
'password' => $password,
]);
Expand All @@ -74,9 +79,23 @@ public static function login($email, $password, $challenge = '', $code = '') {
'challenge' => $challenge,
]);
}
$client = new Client(['cookies' => true]);

$dataToken = FortniteClient::sendUnrealXSRFClientPostRequest($client);

$data = FortniteClient::sendUnrealClientLoginRequestPostRequest($client, $dataToken, $email, $password);
TimVerheul marked this conversation as resolved.
Show resolved Hide resolved

$dataParam = FortniteClient::sendUnrealClientExchangePostRequest($client, $dataToken);

$requestParams = array_merge($requestParams, [
'grant_type' => 'exchange_code',
'exchange_code' => $dataParam->code,
'includePerms' => 'true',
'token_type' => 'eg1',
]);

// First, we need to get a token for the Unreal Engine client
$data = FortniteClient::sendUnrealClientPostRequest(FortniteClient::EPIC_OAUTH_TOKEN_ENDPOINT, $requestParams);
$data = FortniteClient::sendUnrealClientPostRequest($client, FortniteClient::EPIC_OAUTH_TOKEN_ENDPOINT, $requestParams);

if (!isset($data->access_token)) {
if ($data->errorCode === 'errors.com.epicgames.common.two_factor_authentication.required') {
Expand All @@ -87,14 +106,14 @@ public static function login($email, $password, $challenge = '', $code = '') {
}

// Now that we've got our Unreal Client launcher token, let's get an exchange token for Fortnite
$data = FortniteClient::sendUnrealClientGetRequest(FortniteClient::EPIC_OAUTH_EXCHANGE_ENDPOINT, $data->access_token, true);
$data = FortniteClient::sendUnrealClientGetRequest($client, FortniteClient::EPIC_OAUTH_EXCHANGE_ENDPOINT, $data->access_token, true);

if (!isset($data->code)) {
throw new \Exception($data->errorMessage);
}

// Should be good. Let's get our tokens for the Fortnite API
$data = FortniteClient::sendUnrealClientPostRequest(FortniteClient::EPIC_OAUTH_TOKEN_ENDPOINT, [
$data = FortniteClient::sendUnrealClientPostRequest($client, FortniteClient::EPIC_OAUTH_TOKEN_ENDPOINT, [
'grant_type' => 'exchange_code',
'exchange_code' => $data->code,
'includePerms' => false, // We don't need these here
Expand All @@ -113,8 +132,11 @@ public static function login($email, $password, $challenge = '', $code = '') {
* @param string $refresh_token Exisiting OAuth2 refresh token
* @return self New Auth instance
*/
public static function refresh($refresh_token) {
$data = FortniteClient::sendUnrealClientPostRequest(FortniteClient::EPIC_OAUTH_TOKEN_ENDPOINT, [
public static function refresh($refresh_token)
{
$client = new Client(['cookies' => true]);

$data = FortniteClient::sendUnrealClientPostRequest($client, FortniteClient::EPIC_OAUTH_TOKEN_ENDPOINT, [
'grant_type' => 'refresh_token',
'refresh_token' => $refresh_token,
'includePerms' => "false", // We don't need these here
Expand All @@ -125,34 +147,38 @@ public static function refresh($refresh_token) {
throw new \Exception($data->errorMessage);
}

return new self($data->access_token, $data->in_app_id, $data->refresh_token, $data->account_id, $data->expires_in);
return new self($data->access_token, $data->in_app_id, $data->refresh_token, $data->account_id, $data->expires_in);
}

/**
* Returns current refresh token.
* @return string OAuth2 refresh token
*/
public function refreshToken() {
public function refreshToken()
{
return $this->refresh_token;
}

/**
* Returns the time until the OAuth2 access token expires.
* @return string Time until OAuth2 access token expires (in seconds)
*/
public function expiresIn() {
public function expiresIn()
{
return $this->expires_in;
}

/**
* Returns current access token.
* @return string OAuth2 access token
*/
public function accessToken() {
public function accessToken()
{
return $this->access_token;
}

public function inAppId() {
public function inAppId()
{
return $this->in_app_id;
}
}
}
Loading