Repository URL to install this package:
|
Version:
1.0.0 ▾
|
<?php
namespace Modules\Core\Http\Controllers\Api;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Modules\Core\Contracts\Repositories\ApiTokenRepository;
use Modules\Core\Http\Controllers\ApiController;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Facades\JWTAuth;
class ApiBaseController extends ApiController
{
/**
* @var ApiTokenRepository
*/
protected $api;
/**
* @param ApiTokenRepository $api
*/
public function __construct(ApiTokenRepository $apiTokenRepository)
{
parent::__construct();
$this->api = $apiTokenRepository;
$this->middleware('jwt.auth', ['except' => ['authenticate', 'refresh']]);
}
/**
* @return \Illuminate\Http\JsonResponse
*/
public function checkToken()
{
return response()->json(['status' => true]);
}
/**
* @api {put} /authenticate/refresh Regenerate Token
* @apiVersion 1.0.0
* @apiName RefreshToken
* @apiGroup Authenticate
* @apiDescription Receive valid token, but recently expired, and generates a new
* @apiHeader {String} Authorization Authorization bearer token.
* @apiHeader {String} Accept Set to accept application/json
* @apiHeader {String} Content-Type Set to application/json
*
* @apiHeaderExample {json} Header-Example:
* {
* "Accept": "application/json",
* "Content-Type": "application/json",
* "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cL3RyYWhlbnRlbS5kZXZcL2FwaVwvYXV0aGVudGljYXRlXC9yZWZyZXNoIiwiaWF0IjoxNDcwMTU5NTcxLCJleHAiOjE0NzAyNTMzMTksIm5iZiI6MTQ3MDI0OTcxOSwianRpIjoiNDUyNWEyMTE0ODQ1ZWQzYTkzZjZiZGFkMmMwNTE2MWUifQ.Hj7qAXP1mcruivvzyWxfCppiZTa3vW4dLk-tszhKHcM"
* }
*
* @apiSuccess {String} token Refreshed access token
*
* @apiSuccessExample {json} Success Response
* HTTP/1.1 200 OK
* {
* "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cL3RyYWhlbnRlbS5kZXZcL2FwaVwvYXV0aGVudGljYXRlXC9yZWZyZXNoIiwiaWF0IjoxNDcwMTU5NTcxLCJleHAiOjE0NzAyNTMzMTksIm5iZiI6MTQ3MDI0OTcxOSwianRpIjoiNDUyNWEyMTE0ODQ1ZWQzYTkzZjZiZGFkMmMwNTE2MWUifQ.Hj7qAXP1mcruivvzyWxfCppiZTa3vW4dLk-tszhKHcM"
* }
*
* @apiErrorExample {json} Error Absent Token
* HTTP/1.1 500 Internal Server Error
* {
* "error": "token_absent"
* }
* @apiErrorExample {json} Error Expired Token
* HTTP/1.1 500 Internal Server Error
* {
* "error": "token_expired"
* }
* @apiErrorExample {json} Error Invalid Token
* HTTP/1.1 500 Internal Server Error
* {
* "error": "token_invalid"
* }
* @apiErrorExample {json} Error Not Found
* HTTP/1.1 404 Not Found
* {
* "error": "user_not_found"
* }
*/
/**
* Regenerate auth token
*
* @return \Illuminate\Http\JsonResponse
*/
public function refresh()
{
try {
$currentToken = JWTAuth::getToken();
$token = JWTAuth::refresh($currentToken);
return response()->json(compact('token'));
} catch (JWTException $e) {
// something went wrong
return response()->json(['error' => $e->getMessage()], $e->getStatusCode());
}
}
/**
* @api {post} /authenticate Generate Token
* @apiVersion 1.0.0
* @apiName AuthenticateToken
* @apiGroup Authenticate
* @apiDescription Generate an access token on basis of the Private Key Authentication
* @apiHeader {String} Accept Set to accept application/json
* @apiHeader {String} Content-Type Set to application/json
*
* @apiHeaderExample {json} Header-Example:
* {
* "Accept": "application/json",
* "Content-Type": "application/json"
* }
*
* @apiParam {String} key Private access key
*
* @apiExample {json} Request Example - Raw Body
* {
* "key": "3dbe97d5d4c8a87378734ecc8d4e4559d1bf7b88"
* }
*
* @apiSuccess {String} token Temporary access token
*
* @apiSuccessExample {json} Success Response
* HTTP/1.1 200 OK
* {
* "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cL3RyYWhlbnRlbS5kZXZcL2FwaVwvYXV0aGVudGljYXRlXC9yZWZyZXNoIiwiaWF0IjoxNDcwMTU5NTcxLCJleHAiOjE0NzAyNTMzMTksIm5iZiI6MTQ3MDI0OTcxOSwianRpIjoiNDUyNWEyMTE0ODQ1ZWQzYTkzZjZiZGFkMmMwNTE2MWUifQ.Hj7qAXP1mcruivvzyWxfCppiZTa3vW4dLk-tszhKHcM"
* }
*
* @apiErrorExample {json} Error Could Not Create a Token
* HTTP/1.1 500 Internal Server Error
* {
* "error": "could_not_create_token"
* }
* @apiErrorExample {json} Error Invalid Credentials
* HTTP/1.1 401 Unauthorized
* {
* "error": "invalid_credentials",
* "credentials": {
* "key": "3dbe97d5d4c8a87378734ecc8d4e4559d1bf7b88"
* }
* }
*/
/**
* Authenticates a user on basis of access key
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
*/
public function authenticate(Request $request)
{
$credentials = ['token' => $request->get($this->authKeyField)];
$throttles = $this->isUsingThrottlesLoginsTrait();
try {
// verify the credentials and create a token for the user
if ($token = Auth::guard($this->getGuard())->attempt($credentials)) {
return $this->handleApiWasAuthenticated($request, $throttles, $token);
}
} catch (JWTException $e) {
// something went wrong
return response()->json(['error' => 'could_not_create_token'], 500);
}
// if no errors are encountered we can return a JWT
return response()->json(['error' => 'invalid_credentials', 'credentials' => [$this->authKeyField => $credentials['token']]], 401);
}
/**
* Determine if the class is using the ThrottlesLogins trait.
*
* @return bool
*/
protected function isUsingThrottlesLoginsTrait()
{
return in_array(
ThrottlesLogins::class,
class_uses_recursive(static::class)
);
}
protected function handleApiWasAuthenticated(Request $request, $throttles, $token)
{
if ($throttles) {
$this->clearLoginAttempts($request);
}
if (method_exists($this, 'authenticated')) {
return $this->authenticated($request, $token);
}
return redirect()->intended($this->redirectPath());
}
protected function authenticated($request, $token)
{
return response()->json([
'request' => $request->all(),
'token' => $token
]);
}
}