コード例 #1
0
 public function login()
 {
     $apps_key = Request::header('Application-Key');
     $auth_key = Request::header('Authorization');
     if (System::where('api_token', $apps_key)->get()->count() < 1) {
         return Response::json(['status' => 400, 'message' => http_codes(400)], 400);
     }
     if (!($credentials = array_filter(explode(" ", $auth_key)))) {
         return Response::json(['error' => 'invalid_credentials'], 401);
     }
     if (!($auth = array_filter(explode(":", base64_decode($credentials[1]))))) {
         return Response::json(['error' => 'invalid_credentials'], 401);
     }
     if (!Auth::attempt(['name' => $auth[0], 'password' => $auth[1]])) {
         return Response::json(['status' => 401, 'message' => http_codes(401)], 401);
     }
     try {
         $data = ['userId' => Auth::user()->id];
         if (!($token = JWT::setToken($data))) {
             return Response::json(['error' => 'invalid_credentials'], 401);
         }
     } catch (Exception $e) {
         return Response::json(['error' => 'could_not_create_token'], 500);
     }
     return Response::json(['status' => 200, 'message' => http_codes(200), 'data' => $token], 200);
 }
コード例 #2
0
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Closure  $next
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     // JWT Authentication and for getting data (e.g: userId, userName, ...etc)
     $jwt = Request::header('Token-Key');
     try {
         $data = JWT::getToken($jwt);
     } catch (Exception $e) {
         return Response::json(['error' => $e->getMessage()], 511);
     }
     $request->merge(array("data" => $data));
     return $next($request);
 }
コード例 #3
0
 /**
  * Encode a PHP object into a JSON string.
  *
  * @param object|array $input A PHP object or array
  *
  * @return string          JSON representation of the PHP object or array
  * @throws DomainException Provided object could not be encoded to valid JSON
  */
 public static function jsonEncode($input)
 {
     $json = json_encode($input);
     if (function_exists('json_last_error') && ($errno = json_last_error())) {
         JWT::_handleJsonError($errno);
     } else {
         if ($json === 'null' && $input !== null) {
             throw new DomainException('Null result with non-null input');
         }
     }
     return $json;
 }