diff --git a/README.md b/README.md index 14d3d9a..995f43b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,22 @@ -# Gear JWT +# Gear/Auth +```php +// routes that are ignored by auth +$routes = array( + '/', +); + +$app->register('auth', 'Gear\Middleware\AuthMiddleware', array($routes)); + +// adding jwtHelper +$options = array( + env('JWT_SECRET'), + env('JWT_URL'), + env('JWT_EXPIRED_AT'), +); + +$app->register('jwtHelper', 'Gear\Helpers\JwtHelper', $options); + +// adding filters +$app->before('start', array(new Gear\Filters\AuthFilter, 'before')); +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..358ce40 --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "gear/auth", + "type": "libary", + "license": "MIT", + "authors": [ + { "name": "Björn Hase", "email": "me@tentakelfabrik.de" } + ], + "require": { + "php": "^7.0", + "firebase/php-jwt": "^5.0" + }, + "autoload": { + "psr-4": { + "Gear\\": "src/" + } + } +} diff --git a/src/Gear/Auth/Filters/AuthFilter.php b/src/Gear/Auth/Filters/AuthFilter.php new file mode 100644 index 0000000..7fa276f --- /dev/null +++ b/src/Gear/Auth/Filters/AuthFilter.php @@ -0,0 +1,29 @@ +app->auth()->attempt()) { + $this->app->halt(403, 'Access Denied!'); + } + } +} diff --git a/src/Gear/Auth/Jwt.php b/src/Gear/Auth/Jwt.php new file mode 100644 index 0000000..7729181 --- /dev/null +++ b/src/Gear/Auth/Jwt.php @@ -0,0 +1,67 @@ +secret = $secret; + $this->url = $url; + $this->expiredAt = $expiredAt; + } + + /** + * encode JWT, adding Data + * + * @param array $data + * @return object + */ + public function encode($data = NULL) + { + // current time + $time = time(); + + // create token + $token = array( + 'iss' => $this->url, + 'iat' => $time, + 'nbf' => $time, + 'exp' => $time + $this->expiredAt + ); + + if ($data) { + $token['data'] = $data; + } + + return JWT::encode($token, $this->secret); + } + + /** + * decode JWT + * + * @param string $token + * @return array + */ + public function decode($token) + { + return JWT::decode($token, $this->secret, array('HS256')); + } +} diff --git a/src/Gear/Auth/Middleware/AuthInterface.php b/src/Gear/Auth/Middleware/AuthInterface.php new file mode 100644 index 0000000..34f7cc8 --- /dev/null +++ b/src/Gear/Auth/Middleware/AuthInterface.php @@ -0,0 +1,16 @@ +allowed = $allowed; + } + + /** + * attempt + * + * + * @return mixed + */ + public function attempt() + { + $match = NULL; + + // search for pattern if route + foreach($this->app->router()->getRoutes() as $route) { + if ($route->matchUrl($this->app->request()->url)) { + $match = $route; + break; + } + } + + // if pattern is not in the allowed, get HTTP_AUTHORIZATION and parse bearer-token + if (!in_array($match->pattern, $this->routes)) { + if (isset($_SERVER['HTTP_AUTHORIZATION']) || isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) { + + if (isset($_SERVER['HTTP_AUTHORIZATION'])) { + $header = $_SERVER['HTTP_AUTHORIZATION']; + } + + if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) { + $header = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; + } + + $token = trim(preg_replace('/^(?:\s+)?Bearer\s/', '', $header)); + $this->check($token); + } + } + + // if route not found, set $result to true + if (!$match) { + $this->result = true; + } + + return $this->result; + } +}