Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Express In-Memory Example

DO NOT USE THIS EXAMPLE IN PRODUCTION

How it works

The Model in model.js implements the minimal required model specifications. It does only store the credentials in memory, which means that any active session dumped when the express app is restarted.

The express app is using the model when creating a new OAuthServer in index.js.

Run the example

This example uses the Password grant, which is very simple to understand.

  1. run the app
node index.js
  1. Open a new terminal and get a token from the /token endpoint
$ curl -d "grant_type=password&username=jdoe&password=foobarbaz"  -u "doeclient:foobarbaz" "http://localhost:3000/token"

It should return a json response, something like this

{
  "access_token": "abe2eb52cf61d62b28ac503befc1b49405858f265aee0daf0e0f51faf656b03d",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "bc771863a272a89d6efcdd6ba35ff387e0143f4d1c7db527f722221a290b6adc"
}
  1. access the /secret route with the token
$ curl -i -H "Authorization: Bearer abe2eb52cf61d62b28ac503befc1b49405858f265aee0daf0e0f51faf656b03d" http://localhost:3000/secret

It should return a response like this

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 11
ETag: W/"b-0OPwxEnRaKDD5DIcpNepVAAxO4E"
Date: Fri, 26 Jul 2024 08:10:23 GMT
Connection: keep-alive
Keep-Alive: timeout=5

Secret area

Dump

You can also dump the contents of the memory store (for debugging) like so:

memorystore.dump();