This is a basic example of a OAuth2 server, using node-oauth2-server with the minimum (only the required to work) model configuration.
If you want an example with a better data management system, you should go to node-oauth2-server-mongo-example instead.
Install nodejs and npm and then, simply run npm install
and npm start
. The server should now be running at http://localhost:3000
.
You can use different grant types to get an access token. By now, password
and client_credentials
are available.
There is one client added to server and ready to work:
- clientId:
application
- secret:
secret
And there is also one existing user:
- username:
pedroetb
- password:
password
There is one confidential client added to server and ready to work:
- clientId:
confidentialApplication
- secret:
topSecret
You don't need any user to use this grant type, but for security is only available to confidential clients.
To obtain a token you should POST to http://localhost:3000/oauth/token
.
You need to include the client credentials in request headers and the user credentials and grant type in request body:
- Headers
-
Authorization:
"Basic " + clientId:secret base64'd
- (for example, to use
application:secret
, you should sendBasic YXBwbGljYXRpb246c2VjcmV0
)
- (for example, to use
-
Content-Type:
application/x-www-form-urlencoded
-
- Body
grant_type=password&username=pedroetb&password=password
- (contains 3 parameters:
grant_type
,username
andpassword
)
- (contains 3 parameters:
For example, using curl
:
curl http://localhost:3000/oauth/token \
-d "grant_type=password" \
-d "username=pedroetb" \
-d "password=password" \
-H "Authorization: Basic YXBwbGljYXRpb246c2VjcmV0" \
-H "Content-Type: application/x-www-form-urlencoded"
You need to include the client credentials in request headers and the grant type in request body:
- Headers
-
Authorization:
"Basic " + clientId:secret base64'd
- (for example, to use
confidentialApplication:topSecret
, you should sendBasic Y29uZmlkZW50aWFsQXBwbGljYXRpb246dG9wU2VjcmV0
)
- (for example, to use
-
Content-Type:
application/x-www-form-urlencoded
-
- Body
grant_type=client_credentials
For example, using curl
:
curl http://localhost:3000/oauth/token \
-d "grant_type=client_credentials" \
-H "Authorization: Basic Y29uZmlkZW50aWFsQXBwbGljYXRpb246dG9wU2VjcmV0" \
-H "Content-Type: application/x-www-form-urlencoded"
If all goes as planned, you should receive a response like this:
{
"token_type": "bearer",
"access_token": "72ab415822b56cf0f9f93f07fe978d9aae859325",
"expires_in": 3600
}
Now, you can use your brand-new token to access restricted areas. For example, you can GET to http://localhost:3000/
including your token at headers:
- Headers
- Authorization:
"Bearer " + access_token
- (for example,
Bearer 72ab415822b56cf0f9f93f07fe978d9aae859325
)
- (for example,
- Authorization:
For example, using curl
:
curl http://localhost:3000 \
-H "Authorization: Bearer 72ab415822b56cf0f9f93f07fe978d9aae859325"