-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1456dfc
commit 161f1f9
Showing
19 changed files
with
685 additions
and
520 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace KodiCMS\API\Contracts; | ||
|
||
use KodiCMS\API\Model\Token; | ||
|
||
interface Tokenable | ||
{ | ||
|
||
/** | ||
* Get all of the API tokens for the user. | ||
* | ||
* @return \Illuminate\Database\Eloquent\Relations\hasMany | ||
*/ | ||
public function tokens(); | ||
|
||
/** | ||
* Get the currently used API token for the user. | ||
* | ||
* @return Token | ||
*/ | ||
public function token(); | ||
|
||
/** | ||
* Set the current API token for the user. | ||
* | ||
* @param Token $token | ||
* | ||
* @return $this | ||
*/ | ||
public function setToken(Token $token); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace KodiCMS\API\Http\Controllers\API; | ||
|
||
use KodiCMS\API\Exceptions\Exception; | ||
use KodiCMS\API\Exceptions\PermissionException; | ||
use KodiCMS\API\Http\Controllers\System\Controller; | ||
use KodiCMS\API\Repositories\TokenRepository; | ||
|
||
class ApiTokensController extends Controller | ||
{ | ||
|
||
/** | ||
* @param TokenRepository $repository | ||
*/ | ||
public function getKeys(TokenRepository $repository) | ||
{ | ||
$keys = $repository->findAllByUser($this->currentUser); | ||
|
||
$this->setContent($keys); | ||
} | ||
|
||
/** | ||
* @param TokenRepository $repository | ||
*/ | ||
public function putKey(TokenRepository $repository) | ||
{ | ||
$description = $this->getRequiredParameter('description'); | ||
$this->setContent( | ||
$repository->createForUser($this->currentUser, $description) | ||
); | ||
} | ||
|
||
/** | ||
* @param TokenRepository $repository | ||
*/ | ||
public function deleteKey(TokenRepository $repository) | ||
{ | ||
$key = $this->getRequiredParameter('key'); | ||
|
||
$this->setContent( | ||
(bool) $repository->deleteForUser($this->currentUser, $key) | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace KodiCMS\API\Http\Middleware; | ||
|
||
use KodiCMS\API\Repositories\TokenRepository; | ||
|
||
class VerifyApiToken | ||
{ | ||
|
||
/** | ||
* @var TokenRepository | ||
*/ | ||
private $repository; | ||
|
||
public function __construct(TokenRepository $repository) | ||
{ | ||
$this->repository = $repository; | ||
} | ||
|
||
/** | ||
* Verify the incoming request's user belongs to team. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function handle($request, $next) | ||
{ | ||
if (! backend_auth()->check()) { | ||
if ($user = \Auth::guard('api')->user()) { | ||
backend_auth()->setUser($user); | ||
|
||
$response = $next($request); | ||
|
||
$response->withCookie( | ||
$this->repository->createCookie($user->token()) | ||
); | ||
|
||
return $response; | ||
} | ||
} | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
<?php | ||
|
||
Route::group(['as' => 'api.', 'middleware' => ['backend', 'api']], function () { | ||
RouteAPI::post('refresh.key', ['as' => 'refresh.key', 'uses' => 'API\KeysController@postRefresh']); | ||
RouteAPI::get('keys', ['as' => 'keys.list', 'uses' => 'API\KeysController@getKeys']); | ||
RouteAPI::put('key', ['as' => 'key.put', 'uses' => 'API\KeysController@putKey']); | ||
RouteAPI::delete('key', ['as' => 'key.delete', 'uses' => 'API\KeysController@deleteKey']); | ||
Route::group(['as' => 'api.', 'middleware' => ['api', 'backend']], function () { | ||
RouteAPI::post('refresh.key', ['as' => 'refresh.key', 'uses' => 'API\ApiTokensController@postRefresh']); | ||
RouteAPI::get('keys', ['as' => 'keys.list', 'uses' => 'API\ApiTokensController@getKeys']); | ||
RouteAPI::put('key', ['as' => 'key.put', 'uses' => 'API\ApiTokensController@putKey']); | ||
RouteAPI::delete('key', ['as' => 'key.delete', 'uses' => 'API\ApiTokensController@deleteKey']); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace KodiCMS\API; | ||
|
||
use Firebase\JWT\JWT as FirebaseJWT; | ||
use Illuminate\Support\Str; | ||
|
||
class JWT | ||
{ | ||
|
||
/** | ||
* Encode the given array as a JWT token. | ||
* | ||
* @param array $token | ||
* | ||
* @return string | ||
*/ | ||
public static function encode($token) | ||
{ | ||
return FirebaseJWT::encode($token, static::getKey()); | ||
} | ||
|
||
/** | ||
* Decode the given token to an array. | ||
* | ||
* @param string $token | ||
* | ||
* @return array | ||
*/ | ||
public static function decode($token) | ||
{ | ||
return (array) FirebaseJWT::decode($token, static::getKey(), ['HS256']); | ||
} | ||
|
||
/** | ||
* Get the encryption key for the application. | ||
* | ||
* @return string | ||
*/ | ||
protected static function getKey() | ||
{ | ||
$key = config('app.key'); | ||
|
||
if (Str::startsWith($key, 'base64:')) { | ||
$key = base64_decode(substr($key, 7)); | ||
} | ||
|
||
return $key; | ||
} | ||
} |
Oops, something went wrong.