summaryrefslogtreecommitdiff
path: root/src/products/app
diff options
context:
space:
mode:
Diffstat (limited to 'src/products/app')
-rw-r--r--src/products/app/Http/Controllers/ProductsController.php32
-rw-r--r--src/products/app/Http/Middleware/Auth0Middleware.php51
-rw-r--r--src/products/app/Product.php14
3 files changed, 97 insertions, 0 deletions
diff --git a/src/products/app/Http/Controllers/ProductsController.php b/src/products/app/Http/Controllers/ProductsController.php
new file mode 100644
index 0000000..7d55460
--- /dev/null
+++ b/src/products/app/Http/Controllers/ProductsController.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Product;
+use Illuminate\Http\Request;
+
+class ProductsController extends Controller
+{
+ public function get_all()
+ {
+ return response()->json(Product::all());
+ }
+
+ public function create(Request $request)
+ {
+ $this->validate($request, [
+ 'name' => 'required|unique:products'
+ ]);
+
+ $product = Product::create($request->all());
+
+ return response()->json($product, 201);
+ }
+
+ public function delete($id)
+ {
+ Product::FindOrFail($id)->delete();
+
+ return response('Deleted successfully', 200);
+ }
+}
diff --git a/src/products/app/Http/Middleware/Auth0Middleware.php b/src/products/app/Http/Middleware/Auth0Middleware.php
new file mode 100644
index 0000000..d4b5ebc
--- /dev/null
+++ b/src/products/app/Http/Middleware/Auth0Middleware.php
@@ -0,0 +1,51 @@
+<?php
+
+namespace App\Http\Middleware;
+
+use Closure;
+use Auth0\SDK\Exception\InvalidTokenException;
+use Auth0\SDK\Helpers\JWKFetcher;
+use Auth0\SDK\Helpers\Tokens\AsymmetricVerifier;
+use Auth0\SDK\Helpers\Tokens\TokenVerifier;
+
+class Auth0Middleware
+{
+ public function handle($request, Closure $next, $scopeRequired = null)
+ {
+ $token = $request->bearerToken();
+
+ if (!$token)
+ return response()->json('No token provided', 401);
+
+ $decodedToken = $this->validateAndDecode($token);
+
+ if ($scopeRequired && !$this->tokenHasScope($decodedToken, $scopeRequired))
+ return response()->json(['message' => 'Missing permission'], 403);
+
+ return $next($request);
+ }
+
+ public function validateAndDecode($token)
+ {
+ try {
+ $jwksUri = env('AUTH0_DOMAIN') . '.well-known/jwks.json';
+ $jwksFetcher = new JWKFetcher(null, ['base_uri' => $jwksUri]);
+ $signatureVerifier = new AsymmetricVerifier($jwksFetcher);
+ $tokenVerifier = new TokenVerifier(env('AUTH0_DOMAIN'), env('AUTH0_AUD'), $signatureVerifier);
+
+ return $tokenVerifier->verify($token);
+ } catch (InvalidTokenException $e) {
+ throw $e;
+ }
+ }
+
+ protected function tokenHasScope($token, $scopeRequired)
+ {
+ if (empty($token['scope']))
+ return false;
+
+ $tokenScopes = explode(' ', $token['scope']);
+
+ return in_array($scopeRequired, $tokenScopes);
+ }
+}
diff --git a/src/products/app/Product.php b/src/products/app/Product.php
new file mode 100644
index 0000000..1223533
--- /dev/null
+++ b/src/products/app/Product.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Product extends Model
+{
+ protected $fillable = [
+ 'name'
+ ];
+
+ protected $hidden = [];
+}