Skip to content

Commit

Permalink
New authentification workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
SRWieZ committed May 8, 2024
1 parent e7c1567 commit d0b39de
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 15 deletions.
12 changes: 7 additions & 5 deletions app/Commands/DomainWatchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ public function handle()
$interval = intval($this->option('interval')) <= 0 ? 2 : intval($this->option('interval'));

$record = $response->json('data');

$start = time();
$this->line(''); // Blank line
$this->line(' Watching the following record: '.implode(' | ', [
$record_string = implode(' | ', [
$record['name'],
$record['type'],
$record['value'],
]));
]);

$start = time();
$this->line(''); // Blank line
$this->line(' Watching the following record: '.$record_string);

$this->line(''); // Blank line

Expand All @@ -59,6 +60,7 @@ public function handle()

if ($record['state'] === 'verified') {
$this->info(' Record is active');
$this->notify('Record is active!', $record_string);
break;
}

Expand Down
66 changes: 60 additions & 6 deletions app/Commands/LoginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Unolia\UnoliaCLI\Commands;

use Exception;
use LaravelZero\Framework\Commands\Command;
use Saloon\Exceptions\Request\Statuses\UnprocessableEntityException;
use Unolia\UnoliaCLI\Helpers\Config;
use Unolia\UnoliaCLI\Helpers\Helpers;
use Unolia\UnoliaCLI\Http\Integrations\Unolia\Requests\CurrentAuthenticated;
use Unolia\UnoliaCLI\Http\Integrations\Unolia\Requests\CurrentToken;
use Unolia\UnoliaCLI\Http\Integrations\Unolia\Requests\Login;

use function Laravel\Prompts\password;
use function Laravel\Prompts\text;

class LoginCommand extends Command
{
Expand All @@ -19,18 +23,68 @@ class LoginCommand extends Command
public function handle()
{
$token = Helpers::getApiToken();
$connector = Helpers::getApiConnector($token ?: '');

if (! empty($token)) {
$this->line('You are already logged in.');
try {
$verify = $connector->send(new CurrentToken());
$verify->throw();

$this->line('You are already logged in.');
} catch (Exception) {
}

return;
}

$token_name = 'Token for '.get_current_user().'@'.gethostname();

$email = text('Email');
$password = password('Password');
$connector_auth = Helpers::getAuthConnector();

try {
$login = $connector_auth->send(new Login(
email: $email,
password: $password,
token_name: $token_name,
));

$login->throw();

} catch (UnprocessableEntityException $e) {

$two_fa_error = $e->getResponse()->json('errors.two_factor_code');

if (! empty($two_fa_error)) {
$two_factor_code = text('Two factor authentication code');

$login = $connector_auth->send(new Login(
email: $email, password: $password,
two_factor_code: $two_factor_code,
token_name: $token_name,
));

try {
$login->throw();
} catch (Exception $e) {
$this->error($e->getMessage());

return;
}
} else {
$this->error($e->getMessage());

return;
}

} catch (Exception $e) {
$this->error($e->getMessage());

return;
}

$token = password(
label: 'API Token',
placeholder: '',
hint: 'You can find your API token in your unolia.com account settings',
);
$token = $login->json('data.token');

$connector = Helpers::getApiConnector($token);
try {
Expand Down
12 changes: 11 additions & 1 deletion app/Commands/LogoutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
use LaravelZero\Framework\Commands\Command;
use Unolia\UnoliaCLI\Helpers\Config;
use Unolia\UnoliaCLI\Helpers\Helpers;
use Unolia\UnoliaCLI\Http\Integrations\Unolia\Requests\Logout;

class LogoutCommand extends Command
{
protected $signature = 'logout';
protected $signature = 'logout {--force : Force the operation when the actual token is already deleted from your account}';

protected $description = 'Logout from unolia.com';

Expand All @@ -22,6 +23,15 @@ public function handle()
return;
}

$connector = Helpers::getAuthConnector();
$logout = $connector->send(new Logout());

if ($logout->failed() && ! $this->option('force')) {
$this->error('Logout failed: '.$logout->json('message'));

return;
}

Config::set('api.token', null);

$this->info('You are now logged out.');
Expand Down
25 changes: 24 additions & 1 deletion app/Helpers/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,43 @@
namespace Unolia\UnoliaCLI\Helpers;

use Unolia\UnoliaCLI\Http\Integrations\Unolia\Unolia;
use Unolia\UnoliaCLI\Http\Integrations\Unolia\UnoliaAuth;

class Helpers
{
public static function getApiConnector($token = null): Unolia
{
return new Unolia(
token: $token ?: self::getApiToken(),
token: $token ?? self::getApiToken(),
api_url: config('settings.api.url'),
);
}

public static function getAuthConnector($token = null): UnoliaAuth
{
return new UnoliaAuth(
token: $token ?? self::getApiToken(),
api_url: config('settings.api.auth_url'),
);
}

public static function getApiToken(): ?string
{
// Check environment variable, else use config file
return config('settings.api.token') ?: Config::get('api.token');
}

public static function getUserAgent()
{
$phpVersion = 'PHP '.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION;

return sprintf(
'User-Agent: UnoliaCLI/%s (%s; %s; %s; %s)',
config('app.version'),
function_exists('php_uname') ? php_uname('s') : 'Unknown',
function_exists('php_uname') ? php_uname('m') : 'Unknown',
function_exists('php_uname') ? php_uname('r') : 'Unknown',
$phpVersion,
);
}
}
38 changes: 38 additions & 0 deletions app/Http/Integrations/Unolia/Requests/Login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Unolia\UnoliaCLI\Http\Integrations\Unolia\Requests;

use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Traits\Body\HasJsonBody;

class Login extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::POST;

public function __construct(
public readonly string $email,
public readonly string $password,
public readonly ?string $two_factor_code = null,
public readonly ?string $token_name = null,
) {
}

public function defaultBody(): array
{
return array_filter([
'email' => $this->email,
'password' => $this->password,
'two_factor_code' => $this->two_factor_code,
'token_name' => $this->token_name,
]);
}

public function resolveEndpoint(): string
{
return 'login';
}
}
16 changes: 16 additions & 0 deletions app/Http/Integrations/Unolia/Requests/Logout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Unolia\UnoliaCLI\Http\Integrations\Unolia\Requests;

use Saloon\Enums\Method;
use Saloon\Http\Request;

class Logout extends Request
{
protected Method $method = Method::DELETE;

public function resolveEndpoint(): string
{
return 'logout';
}
}
5 changes: 3 additions & 2 deletions app/Http/Integrations/Unolia/Unolia.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Saloon\Http\Request;
use Saloon\PaginationPlugin\Contracts\HasPagination;
use Saloon\Traits\Plugins\AcceptsJson;
use Unolia\UnoliaCLI\Helpers\Helpers;
use Unolia\UnoliaCLI\Http\Integrations\Unolia\Paginator\UnoliaPaginator;

class Unolia extends Connector implements HasPagination
Expand All @@ -27,13 +28,13 @@ protected function defaultAuth(): TokenAuthenticator
protected function defaultHeaders(): array
{
return [
// 'User-Agent' => 'IntelliJ HTTP Client/PhpStorm 2024.1.1',
'User-Agent' => Helpers::getUserAgent(),
];
}

public function resolveBaseUrl(): string
{
return $this->api_url ?? 'https://api.unolia.com/v1/';
return $this->api_url ?? 'https://app.unolia.com/api/v1/';
}

public function paginate(Request $request): UnoliaPaginator
Expand Down
36 changes: 36 additions & 0 deletions app/Http/Integrations/Unolia/UnoliaAuth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Unolia\UnoliaCLI\Http\Integrations\Unolia;

use Saloon\Http\Auth\TokenAuthenticator;
use Saloon\Http\Connector;
use Saloon\Traits\Plugins\AcceptsJson;
use Unolia\UnoliaCLI\Helpers\Helpers;

class UnoliaAuth extends Connector
{
use AcceptsJson;

public function __construct(
public readonly ?string $token = null,
public readonly ?string $api_url = null,
) {
}

protected function defaultAuth(): ?TokenAuthenticator
{
return $this->token ? new TokenAuthenticator($this->token) : null;
}

protected function defaultHeaders(): array
{
return [
'User-Agent' => Helpers::getUserAgent(),
];
}

public function resolveBaseUrl(): string
{
return $this->api_url ?? 'https://app.unolia.com/api/';
}
}
1 change: 1 addition & 0 deletions config/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
|
*/
'url' => env('UNOLIA_API_URL', 'https://app.unolia.com/api/v1/'),
'auth_url' => env('UNOLIA_AUTH_URL', 'https://app.unolia.com/api/'),
],
];

0 comments on commit d0b39de

Please sign in to comment.