Skip to content

Commit

Permalink
Create Api tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster committed May 27, 2016
1 parent 1456dfc commit 161f1f9
Show file tree
Hide file tree
Showing 19 changed files with 685 additions and 520 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"description": "The official KodiCMS Api module",
"homepage": "http://www.kodicms.ru/",
"license": "MIT",
"keywords": ["laravel", "framework", "kodicms", "cms", "module", "api"
],
"keywords": ["laravel", "framework", "kodicms", "cms", "module", "api"],
"module": {
"name": "API",
"priority": "-100"
Expand All @@ -29,6 +28,8 @@
},
"require": {
"kodicms/module-loader": "3.*",
"kodicms/core": "0.*"
"kodicms/core": "0.*",
"ramsey/uuid": "^3.1",
"firebase/php-jwt": "~3.0"
}
}
32 changes: 32 additions & 0 deletions src/Contracts/Tokenable.php
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);
}
14 changes: 0 additions & 14 deletions src/Facades/KeysHelper.php

This file was deleted.

76 changes: 0 additions & 76 deletions src/Helpers/Keys.php

This file was deleted.

45 changes: 45 additions & 0 deletions src/Http/Controllers/API/ApiTokensController.php
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)
);
}
}
72 changes: 0 additions & 72 deletions src/Http/Controllers/API/KeysController.php

This file was deleted.

46 changes: 46 additions & 0 deletions src/Http/Middleware/VerifyApiToken.php
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);
}
}
10 changes: 5 additions & 5 deletions src/Http/routes.php
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']);
});
50 changes: 50 additions & 0 deletions src/JWT.php
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;
}
}
Loading

0 comments on commit 161f1f9

Please sign in to comment.