Commit e2e0568e by Евгений

Улучшение #19447

Добавление токенов
1 parent 2aa7c79c
...@@ -2,15 +2,29 @@ ...@@ -2,15 +2,29 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\User; use App\Models\Tokens;
use Illuminate\Support\Facades\App; use App\Service\API;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Request; use Illuminate\Support\Facades\Request;
use Illuminate\Validation\Rule;
use Inertia\Inertia; use Inertia\Inertia;
class TokensController extends Controller class TokensController extends Controller
{ {
function get($api){
return Inertia::location(API::getInstance($api)->getAuthLink());
}
function token($api){
$token = API::getInstance($api)->getToken(Request::get('code'));
$tokens = Tokens::firstOrNew(['token'=>$token['token']]);
$tokens->token = $token['token'];
$tokens->login = $token['login'];
$tokens->api = $api;
$tokens->created_by = Auth::user()->id;
$tokens->save();
return Redirect::route('dashboard')->with('success', 'Token added.');
}
} }
...@@ -7,5 +7,5 @@ use Illuminate\Database\Eloquent\Model; ...@@ -7,5 +7,5 @@ use Illuminate\Database\Eloquent\Model;
class Tokens extends Model class Tokens extends Model
{ {
use HasFactory; protected $fillable = ['token', 'login'];
} }
<?php <?php
namespace App\Service; namespace App\Service;
interface API{ use GuzzleHttp\Client;
function getAuthLink();
function getToken($code); class API implements \App\Service\Contract\API {
protected $client;
protected function __construct(){
$this->client = new Client();
}
static function getInstance($api){
switch($api){
case 'yd':
return new YandexDirect();
break;
}
}
function getAuthLink()
{
return '';
}
function getToken($code)
{
}
protected function getTokenUrl(){
return '';
}
function extractToken($data){
return '';
}
} }
<?php
namespace App\Service;
class ApiCommon implements API {
function getAuthLink()
{
return 'https://oauth.yandex.ru/authorize?response_type=code&client_id=' . config('api.yandex.id');
}
function getToken($code)
{
// TODO: Implement getToken() method.
}
}
<?php
namespace App\Service\Contract;
interface API{
function getAuthLink();
function getToken($code);
}
<?php <?php
namespace App\Service; namespace App\Service;
class YandexDirect extends ApiCommon{
class YandexDirect extends API{
private $url = 'https://oauth.yandex.ru/token';
function getAuthLink() function getAuthLink()
{ {
return 'https://oauth.yandex.ru/authorize?response_type=code&client_id=' . config('api.yandex.id'); return 'https://oauth.yandex.ru/authorize?response_type=code&client_id=' . config('api.yandex.id');
} }
function getToken($code) function getToken($code) {
{ $data = $this->client->post($this->getTokenUrl(), [
// TODO: Implement getToken() method. 'form_params' => [
'grant_type' => 'authorization_code',
'code' => $code,
'client_id' => config('api.yandex.id'),
'client_secret' => config('api.yandex.password'),
]
]);
return $this->extractToken($data);
}
protected function getTokenUrl() {
return $this->url;
}
function extractToken($data){
$token = json_decode($data->getBody())->access_token;
$login = $this->getLoginByToken($token);
return ['token' => $token, 'login' => $login];
}
public function getLoginByToken($token) {
$url = "https://login.yandex.ru/info?format=json&oauth_token={$token}";
$data = json_decode($this->client->get($url)->getBody());
return isset($data->login) ? $data->login : false;
} }
} }
...@@ -15,7 +15,14 @@ class CreateTokensTable extends Migration ...@@ -15,7 +15,14 @@ class CreateTokensTable extends Migration
{ {
Schema::create('tokens', function (Blueprint $table) { Schema::create('tokens', function (Blueprint $table) {
$table->id(); $table->id();
$table->string('token', 128);
$table->string('login', 128);
$table->enum('api', ['yd']);
$table->enum('type', ['main', 'goal'])->nullable();
$table->integer('created_by');
$table->timestamps(); $table->timestamps();
$table->unique('token');
}); });
} }
......
...@@ -5,9 +5,8 @@ ...@@ -5,9 +5,8 @@
<div class="mb-8 flex"> <div class="mb-8 flex">
<inertia-link class="btn-indigo" href="/500">500 error</inertia-link> <inertia-link class="btn-indigo" href="/500">500 error</inertia-link>
<inertia-link class="btn-indigo ml-1" href="/404">404 error</inertia-link> <inertia-link class="btn-indigo ml-1" href="/404">404 error</inertia-link>
<a class="btn-indigo ml-1" href="https://oauth.yandex.ru/authorize?response_type=code&client_id=41fef5d911a54f63b685c8155d189b61">
Add Token <inertia-link class="btn-indigo ml-1" :href="route('token.get', {'api': 'yd'})">Add yandex token</inertia-link>
</a>
</div> </div>
<p class="leading-normal">👆 These links are intended to be broken to illustrate how error handling works with Inertia.js.</p> <p class="leading-normal">👆 These links are intended to be broken to illustrate how error handling works with Inertia.js.</p>
</div> </div>
......
...@@ -151,6 +151,10 @@ Route::get('500', function () { ...@@ -151,6 +151,10 @@ Route::get('500', function () {
echo $fail; echo $fail;
}); });
Route::get('/token/{type}', [TokensController::class, 'token']) Route::get('/token/{api}', [TokensController::class, 'token'])
->name('token') ->name('token')
->middleware('auth'); ->middleware('auth');
Route::get('/token/{api}/get', [TokensController::class, 'get'])
->name('token.get')
->middleware('auth');
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!