init
This commit is contained in:
54 files changed
+4604
No files matched your search
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Config;
|
||||
use App\Eitaa\Client;
|
||||
use App\Http\HttpException;
|
||||
use App\Http\Request;
|
||||
use App\Security\InitDataValidator;
|
||||
use App\Security\RateLimiter;
|
||||
use App\Security\Session;
|
||||
|
||||
final class DemoController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Config $config,
|
||||
private readonly Session $session,
|
||||
private readonly RateLimiter $limiter,
|
||||
private readonly Client $eitaa,
|
||||
) {
|
||||
}
|
||||
|
||||
public function dispatch(Request $request): array
|
||||
{
|
||||
$request->requireSecurePost($this->config->get('allow_local_http'), $this->session->csrf());
|
||||
$body = $request->json();
|
||||
$action = $body['action'] ?? '';
|
||||
if (!is_string($action)) {
|
||||
throw new HttpException('عملیات نامعتبر است.');
|
||||
}
|
||||
$this->limiter->hit('ip:' . $request->ip(), 60, 60);
|
||||
if ($action === 'logout') {
|
||||
$this->session->logout();
|
||||
return ['ok' => true, 'csrf' => $this->session->csrf()];
|
||||
}
|
||||
if ($this->config->get('token') === '') {
|
||||
throw new HttpException('توکن برنامه هنوز در فضای خصوصی هاست تنظیم نشده است.', 503);
|
||||
}
|
||||
if ($action === 'authenticate') {
|
||||
return $this->authenticate($request, $body);
|
||||
}
|
||||
$user = $this->session->user();
|
||||
return match ($action) {
|
||||
'verify' => $this->verify($request),
|
||||
'contact' => $this->contact($body, $user),
|
||||
'sendMessage' => $this->sendMessage($body, $user),
|
||||
default => throw new HttpException('عملیات پیدا نشد.', 404),
|
||||
};
|
||||
}
|
||||
|
||||
private function authenticate(Request $request, array $body): array
|
||||
{
|
||||
$this->limiter->hit('login:' . $request->ip(), 20, 300);
|
||||
if (!is_string($body['initData'] ?? null)) {
|
||||
throw new HttpException('دادهٔ ورود وجود ندارد.', 401);
|
||||
}
|
||||
$data = InitDataValidator::validate(
|
||||
$body['initData'], $this->config->get('token'), $this->config->get('init_data_max_age'),
|
||||
);
|
||||
$user = InitDataValidator::user($data);
|
||||
$alreadyVerified = ($this->session->launch()['hash'] ?? '') === $data['hash']
|
||||
&& $this->session->isRemoteVerified();
|
||||
if ($this->config->get('verify_on_login') && !$alreadyVerified) {
|
||||
$this->remoteVerify($request, $data);
|
||||
}
|
||||
$this->session->login($user, $data, $this->config->get('verify_on_login') || $alreadyVerified);
|
||||
return [
|
||||
'ok' => true,
|
||||
'user' => $user,
|
||||
'expires' => $this->session->expiresAt(),
|
||||
'remoteVerified' => $this->session->isRemoteVerified(),
|
||||
'context' => array_intersect_key($data, array_flip([
|
||||
'auth_date', 'query_id', 'chat_type', 'chat_instance', 'start_param', 'device_id',
|
||||
])),
|
||||
];
|
||||
}
|
||||
|
||||
private function verify(Request $request): array
|
||||
{
|
||||
if (!$this->session->isRemoteVerified()) {
|
||||
$this->remoteVerify($request, $this->session->launch());
|
||||
$this->session->markRemoteVerified();
|
||||
}
|
||||
return ['ok' => true, 'message' => 'اصالت ورود توسط ایتایار تأیید شد.'];
|
||||
}
|
||||
|
||||
private function remoteVerify(Request $request, array $launch): void
|
||||
{
|
||||
if (time() - (int) $launch['auth_date'] > 60) {
|
||||
throw new HttpException('اصالتسنجی فقط در دقیقهٔ اول ورود ممکن است.', 422);
|
||||
}
|
||||
$this->limiter->hit('verify-global', 10, 1);
|
||||
$this->eitaa->verify($launch['hash'], $this->config->get('verify_ip') ? $request->ip() : null);
|
||||
}
|
||||
|
||||
private function contact(array $body, array $user): array
|
||||
{
|
||||
if (!is_string($body['contactData'] ?? null)) {
|
||||
throw new HttpException('دادهٔ امضاشدهٔ تماس وجود ندارد.');
|
||||
}
|
||||
$data = InitDataValidator::validate($body['contactData'], $this->config->get('token'), 300);
|
||||
$contact = json_decode($data['contact'] ?? '', true, 16, JSON_BIGINT_AS_STRING);
|
||||
if (!is_array($contact) || (string) ($contact['user_id'] ?? '') !== (string) $user['id']) {
|
||||
throw new HttpException('تماس متعلق به کاربر واردشده نیست.', 401);
|
||||
}
|
||||
return [
|
||||
'ok' => true,
|
||||
'contact' => array_intersect_key($contact, array_flip(['first_name', 'last_name', 'phone_number', 'user_id'])),
|
||||
];
|
||||
}
|
||||
|
||||
private function sendMessage(array $body, array $user): array
|
||||
{
|
||||
$message = $body['text'] ?? null;
|
||||
if (!is_string($message) || trim($message) === '' || strlen($message) > 4000) {
|
||||
throw new HttpException('متن پیام لازم است و باید کمتر از ۴۰۰۰ بایت باشد.');
|
||||
}
|
||||
// No client-selected recipient. Quotas persist across sessions.
|
||||
$this->limiter->hit('message:' . $user['id'], 5, 3600);
|
||||
$this->limiter->hit('message-global', 100, 3600);
|
||||
$this->eitaa->sendMessage((string) $user['id'], trim($message));
|
||||
return ['ok' => true, 'message' => 'پیام به حساب ایتای شما ارسال شد.'];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user