{"id":18487123,"url":"https://github.com/pedroetb/node-oauth2-server-example","last_synced_at":"2025-04-10T02:19:55.078Z","repository":{"id":48170637,"uuid":"58336271","full_name":"pedroetb/node-oauth2-server-example","owner":"pedroetb","description":"Working oauth2 server with minimal configuration","archived":false,"fork":false,"pushed_at":"2022-12-10T16:33:49.000Z","size":50,"stargazers_count":159,"open_issues_count":3,"forks_count":66,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-02T10:37:49.900Z","etag":null,"topics":["client-credentials","grant","nodejs","oauth","oauth2","oauth2-server","password","refresh-token"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pedroetb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-08T23:37:59.000Z","updated_at":"2025-02-14T10:38:56.000Z","dependencies_parsed_at":"2023-01-26T04:00:17.459Z","dependency_job_id":null,"html_url":"https://github.com/pedroetb/node-oauth2-server-example","commit_stats":{"total_commits":23,"total_committers":2,"mean_commits":11.5,"dds":0.04347826086956519,"last_synced_commit":"37ce8a0fb22e59774e24b3443140412930b97465"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedroetb%2Fnode-oauth2-server-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedroetb%2Fnode-oauth2-server-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedroetb%2Fnode-oauth2-server-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pedroetb%2Fnode-oauth2-server-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pedroetb","download_url":"https://codeload.github.com/pedroetb/node-oauth2-server-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248142932,"owners_count":21054674,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["client-credentials","grant","nodejs","oauth","oauth2","oauth2-server","password","refresh-token"],"created_at":"2024-11-06T12:50:14.625Z","updated_at":"2025-04-10T02:19:55.054Z","avatar_url":"https://github.com/pedroetb.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# node-oauth2-server example\n\nThis is a basic example of a OAuth2 server, using [node-oauth2-server](https://github.com/oauthjs/node-oauth2-server) (version 3.0.1) with the minimum (only the required to work) model configuration.\n\nIf you want an example with a better data management system, you should go to [node-oauth2-server-mongo-example](https://github.com/pedroetb/node-oauth2-server-mongo-example) instead.\n\n## Setup\n\nInstall **nodejs** and **npm** and then, simply run `npm install` and `npm start`. The server should now be running at `http://localhost:3000`.\n\n## Usage\n\nYou can use different grant types to get an access token. By now, `password`, `client_credentials` and `refresh_token` are available.\n\n### Checking example data\n\n#### With *password* grant\n\nThere is one client added to server and ready to work:\n\n* **clientId**: `application`\n* **clientSecret**: `secret`\n\nAnd there is also one existing user:\n\n* **username**: `pedroetb`\n* **password**: `password`\n\n#### With *client_credentials* grant\n\nThere is one confidential client added to server and ready to work:\n\n* **clientId**: `confidentialApplication`\n* **clientSecret**: `topSecret`\n\nYou don't need any user to use this grant type, but for security is only available to confidential clients.\n\n#### With *refresh_token* grant\n\nThere is one client added to server and ready to work:\n\n* **clientId**: `application`\n* **clientSecret**: `secret`\n\nYou don't need any user to use this grant type, it was already provided when original token was obtained (by *password* grant type, for example).\n\n### Obtaining a token\n\nTo obtain a token you should POST to `http://localhost:3000/oauth/token`.\n\n#### With *password* grant\n\nYou need to include the client credentials in request headers and the user credentials and grant type in request body:\n\n* **Headers**\n\t* **Authorization**: `\"Basic \" + clientId:clientSecret base64'd`\n\t\t* (for example, to use `application:secret`, you should send `Basic YXBwbGljYXRpb246c2VjcmV0`)\n\n\t* **Content-Type**: `application/x-www-form-urlencoded`\n* **Body**\n\t* `grant_type=password\u0026username=pedroetb\u0026password=password`\n\t\t* (contains 3 parameters: `grant_type`, `username` and `password`)\n\nFor example, using `curl`:\n```\ncurl http://localhost:3000/oauth/token \\\n\t-d \"grant_type=password\" \\\n\t-d \"username=pedroetb\" \\\n\t-d \"password=password\" \\\n\t-H \"Authorization: Basic YXBwbGljYXRpb246c2VjcmV0\" \\\n\t-H \"Content-Type: application/x-www-form-urlencoded\"\n```\n\nIf all goes as planned, you should receive a response like this:\n\n```\n{\n\t\"accessToken\": \"951d6f603c2ce322c5def00ce58952ed2d096a72\",\n\t\"accessTokenExpiresAt\": \"2018-11-18T16:18:25.852Z\",\n\t\"refreshToken\": \"67c8300ad53efa493c2278acf12d92bdb71832f9\",\n\t\"refreshTokenExpiresAt\": \"2018-12-02T15:18:25.852Z\",\n\t\"client\": {\n\t\t\"id\": \"application\"\n\t},\n\t\"user\": {\n\t\t\"id\": \"pedroetb\"\n\t}\n}\n```\n\n#### With *client_credentials* grant\n\nYou need to include the client credentials in request headers and the grant type in request body:\n\n* **Headers**\n\t* **Authorization**: `\"Basic \" + clientId:clientSecret base64'd`\n\t\t* (for example, to use `confidentialApplication:topSecret`, you should send `Basic Y29uZmlkZW50aWFsQXBwbGljYXRpb246dG9wU2VjcmV0`)\n\n\t* **Content-Type**: `application/x-www-form-urlencoded`\n* **Body**\n\t* `grant_type=client_credentials`\n\nFor example, using `curl`:\n```\ncurl http://localhost:3000/oauth/token \\\n\t-d \"grant_type=client_credentials\" \\\n\t-H \"Authorization: Basic Y29uZmlkZW50aWFsQXBwbGljYXRpb246dG9wU2VjcmV0\" \\\n\t-H \"Content-Type: application/x-www-form-urlencoded\"\n```\n\nIf all goes as planned, you should receive a response like this:\n\n```\n{\n\t\"accessToken\": \"951d6f603c2ce322c5def00ce58952ed2d096a72\",\n\t\"accessTokenExpiresAt\": \"2018-11-18T16:18:25.852Z\",\n\t\"client\": {\n\t\t\"id\": \"confidentialApplication\"\n\t},\n\t\"user\": {\n\t\t\"id\": \"confidentialApplication\"\n\t}\n}\n```\n\n#### With *refresh_token* grant\n\nWhen obtaining an access token using *password* grant, you get also a refresh token.\nWith this token you can get a new access token, using only that value (username and password are not needed), while it has not been expired.\n\n\u003e Remember that, if you refresh a token while it was still valid, the old access and refresh tokens get revoked, and only the new access and refresh tokens are valid to be used.\n\nYou need to include the client credentials in request headers and the refresh token and grant type in request body:\n\n* **Headers**\n\t* **Authorization**: `\"Basic \" + clientId:clientSecret base64'd`\n\t\t* (for example, to use `application:secret`, you should send `Basic YXBwbGljYXRpb246c2VjcmV0`)\n\n\t* **Content-Type**: `application/x-www-form-urlencoded`\n* **Body**\n\t* `grant_type=refresh_token\u0026refresh_token=67c8300ad53efa493c2278acf12d92bdb71832f9`\n\t\t* (contains 2 parameters: `grant_type` and `refresh_token`)\n\nFor example, using `curl`:\n```\ncurl http://localhost:3000/oauth/token \\\n\t-d \"grant_type=refresh_token\" \\\n\t-d \"refresh_token=67c8300ad53efa493c2278acf12d92bdb71832f9\" \\\n\t-H \"Authorization: Basic YXBwbGljYXRpb246c2VjcmV0\" \\\n\t-H \"Content-Type: application/x-www-form-urlencoded\"\n```\n\nIf all goes as planned, you should receive a response like this:\n\n```\n{\n\t\"accessToken\": \"17be4ee45b177651db3fd9d286042de75d48eb3b\",\n\t\"accessTokenExpiresAt\": \"2018-11-18T16:18:35.248Z\",\n\t\"refreshToken\": \"37eaff895c8fc9fc839c0098cf3fb01858097908\",\n\t\"refreshTokenExpiresAt\": \"2018-12-02T15:18:35.248Z\",\n\t\"client\": {\n\t\t\"id\": \"application\"\n\t},\n\t\"user\": {\n\t\t\"id\": \"pedroetb\"\n\t}\n}\n```\n\n### Using the token\n\nNow, 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:\n\n* **Headers**\n\t* **Authorization**: `\"Bearer \" + accessToken`\n\t\t* (for example, `Bearer 951d6f603c2ce322c5def00ce58952ed2d096a72`)\n\nFor example, using `curl`:\n```\ncurl http://localhost:3000 \\\n\t-H \"Authorization: Bearer 951d6f603c2ce322c5def00ce58952ed2d096a72\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpedroetb%2Fnode-oauth2-server-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpedroetb%2Fnode-oauth2-server-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpedroetb%2Fnode-oauth2-server-example/lists"}