Skip to content

Commit

Permalink
Add: RequestLogin, RegisterDevice 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
nnnlog committed May 10, 2020
1 parent a6db41a commit 40c3671
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 19 deletions.
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function autoLoad(string $path) {
}
}

autoLoad('./src/');
autoLoad(__DIR__ . '/src/');
/*
$parallelManager = new \loco\parallel\ParallelManager();
Expand Down
78 changes: 75 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


use loco\protocol\ProtocolInfo;
use loco\structure\RegisterDeviceData;
use loco\structure\LoginData;
use loco\utils\request;

Expand All @@ -23,20 +24,91 @@ public function __construct(string $email, string $password, string $client_name
/**
* 인증코드를 요청합니다.
*
* @param bool $permanent
* @param string $device_uuid
* @param string $os_version
* @param string $device_name
* @param bool $permanent
*
* @param bool $forced
*
* @return array
*/
public function requestPasscode(string $device_uuid, string $os_version, string $device_name, bool $permanent = false, bool $forced = false): array {
return json_decode(request::postURL(ProtocolInfo::getAccountInternalURL(ProtocolInfo::Account["LOGIN"]), $field = request::queryToString(LoginData::create(
return json_decode(request::postURL(ProtocolInfo::getAccountInternalURL(ProtocolInfo::Account["REQUEST_PASSCODE"]), request::queryToString(LoginData::create(
$this->email,
$this->password,
$device_uuid,
$os_version,
$device_name,
$permanent,
$forced
)->__toArray()), ProtocolInfo::getAuthHeader(
ProtocolInfo::calculateXVCKey(ProtocolInfo::AuthUserAgent, $this->email, $device_uuid)
)), true);
}

/**
* 인증코드를 사용해 기기를 등록합니다.
*
* @param string $passcode
* @param string $device_uuid
* @param string $os_version
* @param string $device_name
* @param bool $permanent
*
* @return array
*/
public function registerDevice(string $passcode, string $device_uuid, string $os_version, string $device_name, bool $permanent = false): array {
return json_decode(request::postURL(ProtocolInfo::getAccountInternalURL(ProtocolInfo::Account["REGISTER_DEVICE"]), request::queryToString(RegisterDeviceData::create(
$passcode,
$this->email,
$this->password,
$device_uuid,
$os_version,
$device_name,
$permanent
)->__toArray()), ProtocolInfo::getAuthHeader(
ProtocolInfo::calculateXVCKey(ProtocolInfo::AuthUserAgent, $this->email, $device_uuid)
)), true);
}

/**
* @internal
*
* @param string $device_uuid
* @param string $os_version
* @param string $device_name
* @param bool $permanent
* @param bool $forced
*
* @return array
*/
private function requestLogin(string $device_uuid, string $os_version, string $device_name, bool $permanent = false, bool $forced = false): array {
return json_decode(request::postURL(ProtocolInfo::getAccountInternalURL(ProtocolInfo::Account["LOGIN"]), request::queryToString(LoginData::create(
$this->email,
$this->password,
$device_uuid,
$os_version,
$device_name
$device_name,
$permanent,
$forced
)->__toArray()), ProtocolInfo::getAuthHeader(
ProtocolInfo::calculateXVCKey(ProtocolInfo::AuthUserAgent, $this->email, $device_uuid)
)), true);
}

/**
* 로그인을 시도합니다.
*
* @param string $device_uuid
* @param string $os_version
* @param string $device_name
* @param bool $permanent
* @param bool $forced
*/
public function login(string $device_uuid, string $os_version, string $device_name, bool $permanent = false, bool $forced = false): void {
//TODO: initialize loco socket
$loginResult = $this->requestLogin($device_uuid, $os_version, $device_name, $permanent, $forced);
}

}
3 changes: 1 addition & 2 deletions src/protocol/ProtocolInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ public static function calculateXVCKey(string $aHeader, string $email, string $d
return substr(ProtocolInfo::calculateFullXVCKey($aHeader, $email, $deviceUUID), 0, 16);
}

public static function getAuthHeader(string $verifyCodeExtra/*, int $contentLength*/): array {
public static function getAuthHeader(string $verifyCodeExtra): array {
return [
"Content-Type: application/x-www-form-urlencoded",
//"Content-Length: {$contentLength}",
"Host: " . ProtocolInfo::AccountInternalHost,
"A: " . ProtocolInfo::AuthHeaderAgent,
"X-VC: ${verifyCodeExtra}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace loco\structure;


class DeviceRegisterData {
class RegisterDeviceData {
public string $passcode;
public string $email;
public string $password;
Expand All @@ -25,4 +25,16 @@ public static function create(string $passcode, string $email, string $password,

return $obj;
}

public function __toArray(): array {
return [
"passcode" => $this->passcode,
"email" => $this->email,
"password" => $this->password,
"device_uuid" => $this->device_uuid,
"os_version" => $this->os_version,
"device_name" => $this->device_name,
"permanent" => $this->permanent
];
}
}
12 changes: 0 additions & 12 deletions test.php

This file was deleted.

12 changes: 12 additions & 0 deletions tests/CREDENTIALS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

interface CREDENTIALS {
public const EMAIL = "test@test";
public const PASSWORD = "password";
public const DEVICE_UUID = "ecf2b95c-688a-4805-a49b-8d9c62a7149e";
public const OS_VERSION = "0.0.1";
public const DEVICE_NAME = "loco.php";
public const PASSCODE = "0000";
public const PERMANENT = false;
public const FORCED = false;
}
13 changes: 13 additions & 0 deletions tests/login/RegisterDevice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

require_once "../CREDENTIALS.php";
require_once "../../index.php";

$client = new \loco\Client(CREDENTIALS::EMAIL, CREDENTIALS::PASSWORD, "loco.php");
var_dump($client->registerDevice(
CREDENTIALS::PASSCODE,
base64_encode(CREDENTIALS::DEVICE_UUID),
CREDENTIALS::OS_VERSION,
CREDENTIALS::DEVICE_NAME,
CREDENTIALS::PERMANENT
));
17 changes: 17 additions & 0 deletions tests/login/RequestLogin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

require_once "../CREDENTIALS.php";
require_once "../../index.php";

$client = new \loco\Client(CREDENTIALS::EMAIL, CREDENTIALS::PASSWORD, "loco.php");

$requestLogin = (new ReflectionClass($client))->getMethod("requestLogin");
$requestLogin->setAccessible(true);

var_dump($requestLogin->invoke($client,
base64_encode(CREDENTIALS::DEVICE_UUID),
CREDENTIALS::OS_VERSION,
CREDENTIALS::DEVICE_NAME,
CREDENTIALS::PERMANENT,
CREDENTIALS::FORCED
));
13 changes: 13 additions & 0 deletions tests/login/RequestPasscode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

require_once "../CREDENTIALS.php";
require_once "../../index.php";

$client = new \loco\Client(CREDENTIALS::EMAIL, CREDENTIALS::PASSWORD, "loco.php");
var_dump($client->requestPasscode(
base64_encode(CREDENTIALS::DEVICE_UUID),
CREDENTIALS::OS_VERSION,
CREDENTIALS::DEVICE_NAME,
CREDENTIALS::PERMANENT,
CREDENTIALS::FORCED
));

0 comments on commit 40c3671

Please sign in to comment.