Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,6 @@ Create a PUT request for the API endpoint `/api/candy` , with below requestBody
</details>





2 changes: 2 additions & 0 deletions api-server/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
AUDIENCE=http://localhost:3010
ISSUER_BASE_URL=https://dev-zzqedh33f717ukmd.us.auth0.com
14 changes: 13 additions & 1 deletion api-server/app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
const express = require('express');
const app = express();
const { auth, requiredScopes } = require('express-oauth2-jwt-bearer');
require('dotenv').config();
const port = 3000; // You can change the port as needed

app.use(express.json());

if (!process.env.ISSUER_BASE_URL || !process.env.AUDIENCE) {
throw 'Make sure you have ISSUER_BASE_URL, and AUDIENCE in your .env file';
}

const checkJwt = auth();

// Sample candy data
const candyData = [
{
Expand Down Expand Up @@ -84,7 +92,11 @@ app.put('/api/candy/', (req, res) => {
res.json(candyData[candyIndex]);
});


app.get('/api/private/candy', checkJwt, function(req, res) {
res.json({
message: 'Hello from a private endpoint! You need to be authenticated to hit the candy store'
});
});


app.listen(port, () => {
Expand Down
47 changes: 47 additions & 0 deletions api-server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.2"
"jsonwebtoken": "^9.0.2",
"express-oauth2-jwt-bearer": "^1.6.0",
"dotenv": "^10.0.0"
}
}
12 changes: 12 additions & 0 deletions candy-api-tests/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { defineConfig, devices } from '@playwright/test';

// Define your secrets
export const config = {
// Other configurations...
secrets: {
client_id: 'XKDpzYJvVgsF7N6AgiGr0joOgEPQ06CR',
client_secret: 'bA4b3p8a72kiAS8SU4KJLZqEYEI2Kbo0PaHATS0VATLEV4aEITKhbeS16RUHVNw1',
// Add more secrets as needed
},
};


/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
Expand Down Expand Up @@ -30,6 +41,7 @@ export default defineConfig({
trace: 'on-first-retry',
},


/* Configure projects for major browsers */
projects: [
{
Expand Down
28 changes: 28 additions & 0 deletions candy-api-tests/tests/api-candy-auth0-tests.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect, test } from '@playwright/test'
import { config } from '../playwright.config';

test('get private endpoint', async ( {request})=> {

const tokenResponse = await request.post('https://dev-zzqedh33f717ukmd.us.auth0.com/oauth/token', {
data: {
"client_id": config.secrets.client_id,
"client_secret": config.secrets.client_secret,
"audience": "http://localhost:3010",
"grant_type": "client_credentials"
}
});

expect(tokenResponse.status()).toBe(200);

const tokenResponseAsJson = await tokenResponse.json();


const response = await request.get('http://localhost:3000/api/private/candy', {
headers: {
'Authorization': `Bearer ${tokenResponseAsJson.access_token}`,
}
});
const responseAsJson = await response.json();

expect(response.status()).toBe(200);
})