This repository has been archived on 2024-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
keksAccount/app/Http/Controllers/API/oAuthController.php

122 lines
3.3 KiB
PHP
Raw Normal View History

2019-06-20 14:46:50 +00:00
<?php
namespace App\Http\Controllers\API;
use App\Entity\Token;
use App\Exceptions\HTTPException;
use App\Exceptions\NotLoggedInException;
use App\Exceptions\ResourceNotFound;
2019-09-25 07:56:36 +00:00
use App\Http\Resources\API\AppAccessDetails;
2019-06-20 14:46:50 +00:00
use App\Models\AccessToken;
use App\Models\App;
use App\Models\AppAccess;
use App\Models\AppCode;
use App\Models\RefreshToken;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use TaGeSo\APIResponse\Response;
2019-06-20 16:03:54 +00:00
use Laravel\Lumen\Routing\Controller as BaseController;
2019-06-20 14:46:50 +00:00
2019-06-20 16:03:54 +00:00
class oAuthController extends BaseController
2019-06-20 14:46:50 +00:00
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}
public function getAccess(Response $response, Request $request, $id) {
if(!Auth::check()) {
throw new NotLoggedInException();
}
2019-06-20 16:03:54 +00:00
$app = App::query()->where("id", "=", $id)->firstOrFail();
2019-06-20 14:46:50 +00:00
if($request->get("create", false)) {
$access = AppAccess::getOrCreate(Auth::user()->id, $id);
} else {
$access = AppAccess::query()
->where("user_id", "=", Auth::user()->id)
->where("app_id", "=", $id)->first();
}
if(empty($access)) {
throw new ResourceNotFound();
}
//Auto Allow
if($app->auto_accept) {
$access->status = "allowed";
}
2019-06-20 16:03:54 +00:00
return $response->withData(new \App\Http\Resources\API\AppAccess($access));
}
public function allowAccess(Response $response, Request $request, $id) {
if(!Auth::check()) {
throw new NotLoggedInException();
}
$this->validate($request, [
'redirect_uri' => 'required|url',
'state' => ''
]);
$app = App::query()->where("id", "=", $id);
$access = AppAccess::getOrCreate(Auth::user()->id, $id);
$access->status = "allowed";
$access->saveOrFail();
$appCode = AppCode::createCode($access);
//Create Redirect URL
$returnUrl = urldecode($request->input("redirect_uri"));
if(strpos($returnUrl, "?") > 0) {
Log::debug("Found questionmark in redirect_uri");
if(substr($returnUrl, -1, 1) != "&") {
Log::debug("Add & to the redirect_uri");
$returnUrl .= "&";
}
} else {
$returnUrl .= "?";
}
$returnUrl.="code=".$appCode->code;
if($request->input("state", null) !== null) {
$returnUrl .= "&state=".$request->input("state");
}
Log::debug("Return URL: ".$returnUrl);
$res = [];
$res["appCode"] = $appCode->code;
$res["redirectUrl"] = $returnUrl;
return $response->withData($res);
2019-06-20 14:46:50 +00:00
}
2019-09-25 07:56:36 +00:00
public function listAccess(Response $response) {
if(!Auth::check()) {
abort(401);
}
$access = AppAccess::query()->where("user_id", "=", Auth::user()->id)->get();
$visibleAccess = [];
foreach($access as $a) {
if(!$a->getApp()->hidden_in_app_list) {
$visibleAccess[] = $a;
}
}
return $response->withData(AppAccessDetails::collection(collect($visibleAccess)));
}
}