Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API to be used with playground #146

Merged
merged 6 commits into from Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -519,6 +519,13 @@ Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protrac

The workspace includes a playground application that can be used to test out features of the SDK. Run this using `ng serve playground` and browse to http://localhost:4200.

#### Running an express server

An express server can be started by running `npm run server:api`, which can be used to make testing Http Interceptors easier.
The express server exposes a single endpoint at `http://localhost:3001/api/external` that needs to be called with an `Authorization` header containing a token for the corresponding `domain` and `audience`, configurable in [`api-server.js`](api-server.js).

The playground application is preconfigured to call the above endpoint when clicking the `Call external API` button.

## Further help

To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).
Expand Down
45 changes: 45 additions & 0 deletions api-server.js
@@ -0,0 +1,45 @@
const express = require('express');
const cors = require('cors');
const jwt = require('express-jwt');
const jwksRsa = require('jwks-rsa');

const app = express();

const authConfig = {
domain: 'brucke.auth0.com',
audience: 'http://localhost/',
appUri: 'http://localhost:4200',
};

if (!authConfig.domain || !authConfig.audience) {
throw 'Please make sure that auth_config.json is in place and populated';
}

app.use(
cors({
origin: authConfig.appUri,
})
);

const checkJwt = jwt({
secret: jwksRsa.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `https://${authConfig.domain}/.well-known/jwks.json`,
}),

audience: authConfig.audience,
issuer: `https://${authConfig.domain}/`,
algorithms: ['RS256'],
});

app.get('/api/external', checkJwt, (req, res) => {
res.send({
msg: 'Your access token was successfully validated!',
});
});

const port = process.env.API_SERVER_PORT || 3001;

app.listen(port, () => console.log(`Api started on port ${port}`));