This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SDK-3990] Add migration guide and EOL notice (#46)
- Loading branch information
1 parent
e2155ed
commit 596fecd
Showing
2 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
# Migrating to express-oauth2-jwt-bearer | ||
|
||
Our recommended middleware for protecting Express APIs is now [express-oauth2-jwt-bearer](https://github.com/auth0/express-oauth2-jwt-bearer). This guide demonstrates how to migrate from `express-oauth2-bearer` to `express-oauth2-jwt-bearer`. | ||
|
||
## Basic setup | ||
|
||
### Before | ||
|
||
```bash | ||
# .env | ||
|
||
ISSUER_BASE_URL=https://YOUR_DOMAIN | ||
ALLOWED_AUDIENCES=https://api.yourapplication.com | ||
``` | ||
|
||
```javascript | ||
const { auth } = require('express-oauth2-bearer'); | ||
|
||
app.use(auth()); | ||
``` | ||
|
||
Or in your application code: | ||
|
||
```javascript | ||
const { auth } = require('express-oauth2-bearer'); | ||
|
||
app.use(auth({ | ||
issuerBaseURL: 'https://tenant.auth0.com', | ||
allowedAudiences: 'https://api.yourapplication.com' | ||
})); | ||
``` | ||
|
||
### After | ||
|
||
```bash | ||
# .env | ||
|
||
ISSUER_BASE_URL=https://YOUR_ISSUER_DOMAIN | ||
AUDIENCE=https://my-api.com | ||
``` | ||
|
||
```javascript | ||
const { auth } = require('express-oauth2-jwt-bearer'); | ||
|
||
app.use(auth()); | ||
``` | ||
|
||
Or in your application code: | ||
|
||
```javascript | ||
const { auth } = require('express-oauth2-jwt-bearer'); | ||
|
||
app.use(auth({ | ||
issuerBaseURL: 'https://YOUR_ISSUER_DOMAIN', | ||
audience: 'https://my-api.com' | ||
})); | ||
``` | ||
|
||
## JWTs signed with symmetric algorithms (eg HS256) | ||
|
||
### Before | ||
|
||
```javascript | ||
const { auth } = require('express-oauth2-bearer'); | ||
|
||
app.use(auth({ | ||
issuerBaseURL: 'https://tenant.auth0.com', | ||
allowedAudiences: 'https://api.yourapplication.com', | ||
clientSecret: 'YOUR SECRET' | ||
})); | ||
``` | ||
|
||
### After | ||
|
||
```javascript | ||
const { auth } = require('express-oauth2-jwt-bearer'); | ||
|
||
app.use(auth({ | ||
issuer: 'https://YOUR_ISSUER_DOMAIN', | ||
audience: 'https://my-api.com', | ||
secret: 'YOUR SECRET', | ||
tokenSigningAlg: 'HS256' | ||
})); | ||
``` | ||
|
||
## Require scopes | ||
|
||
### Before | ||
|
||
```javascript | ||
const { auth, requiredScopes } = require('express-oauth2-bearer'); | ||
|
||
app.use(auth()); | ||
|
||
app.get('/products', | ||
requiredScopes('read:products'), | ||
(req, res) => { | ||
res.sendStatus(200); | ||
}); | ||
``` | ||
|
||
### After (same API) | ||
|
||
```javascript | ||
const { auth, requiredScopes } = require('express-oauth2-jwt-bearer'); | ||
|
||
app.use(auth()); | ||
|
||
app.get('/products', | ||
requiredScopes('read:products'), | ||
(req, res) => { | ||
res.sendStatus(200); | ||
}); | ||
``` | ||
|
||
## Inspect token | ||
|
||
### Before | ||
|
||
```javascript | ||
const { auth } = require('express-oauth2-bearer'); | ||
|
||
app.use(auth()); | ||
|
||
app.get('/products', | ||
(req, res) => { | ||
const auth = req.auth; | ||
auth.claims; // The decoded JWT payload. | ||
auth.token; // The raw JWT token. | ||
res.sendStatus(200); | ||
}); | ||
``` | ||
|
||
### After | ||
|
||
```javascript | ||
const { auth } = require('express-oauth2-jwt-bearer'); | ||
|
||
app.use(auth()); | ||
|
||
app.get('/products', | ||
(req, res) => { | ||
const auth = req.auth; | ||
auth.header; // The decoded JWT header. | ||
auth.payload; // The decoded JWT payload. | ||
auth.token; // The raw JWT token. | ||
res.sendStatus(200); | ||
}); | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters