All Projects → pedroetb → Node Oauth2 Server Mongo Example

pedroetb / Node Oauth2 Server Mongo Example

Licence: mit
Working oauth2 server with mongodb storage and minimal configuration

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Node Oauth2 Server Mongo Example

Node Oauth2 Server Example
Working oauth2 server with minimal configuration
Stars: ✭ 115 (+51.32%)
Mutual labels:  oauth2, oauth, oauth2-server
Web Framework For Java
A seed project with spring boot for AngularJS, AngularJs Material, Thymeleaf, RESTful API, MySQL and admin panel based on AdminLTE.
Stars: ✭ 29 (-61.84%)
Mutual labels:  oauth2, oauth, oauth2-server
Flask Oauthlib
YOU SHOULD USE https://github.com/lepture/authlib
Stars: ✭ 1,429 (+1780.26%)
Mutual labels:  oauth2, oauth, oauth2-server
Hydra
OpenID Certified™ OpenID Connect and OAuth Provider written in Go - cloud native, security-first, open source API security for your infrastructure. SDKs for any language. Compatible with MITREid.
Stars: ✭ 11,884 (+15536.84%)
Mutual labels:  oauth2, oauth, oauth2-server
Authlib
The ultimate Python library in building OAuth, OpenID Connect clients and servers. JWS,JWE,JWK,JWA,JWT included.
Stars: ✭ 2,854 (+3655.26%)
Mutual labels:  oauth2, oauth, oauth2-server
Spruce
A social networking platform made using Node.js and MongoDB
Stars: ✭ 399 (+425%)
Mutual labels:  mongodb, oauth2, oauth
oauth2-server
A spec compliant, secure by default PHP OAuth 2.0 Server
Stars: ✭ 6,128 (+7963.16%)
Mutual labels:  oauth, oauth2, oauth2-server
Doorkeeper
Doorkeeper is an OAuth 2 provider for Ruby on Rails / Grape.
Stars: ✭ 4,917 (+6369.74%)
Mutual labels:  oauth2, oauth, oauth2-server
Fw Cloud Framework
基于springcloud全家桶开发分布式框架(支持oauth2认证授权、SSO登录、统一下单、微信公众号服务、Shardingdbc分库分表、常见服务监控、链路监控、异步日志、redis缓存等功能),实现基于Vue全家桶等前后端分离项目工程
Stars: ✭ 717 (+843.42%)
Mutual labels:  oauth2, oauth
Cpprestsdk
The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
Stars: ✭ 6,631 (+8625%)
Mutual labels:  oauth2, oauth
Play Silhouette
Silhouette is an authentication library for Play Framework applications that supports several authentication methods, including OAuth1, OAuth2, OpenID, CAS, 2FA, TOTP, Credentials, Basic Authentication or custom authentication schemes.
Stars: ✭ 826 (+986.84%)
Mutual labels:  oauth2, oauth
Mod auth openidc
OpenID Connect Relying Party implementation for Apache HTTP Server 2.x
Stars: ✭ 677 (+790.79%)
Mutual labels:  oauth2, oauth
Microservices Event Sourcing
Microservices Event Sourcing 是一个微服务架构的在线购物网站,使用Spring Boot、Spring Cloud、Spring Reactor、OAuth2、CQRS 构建,实现了基于Event Sourcing的最终一致性,提供了构建端到端微服务的最佳实践
Stars: ✭ 657 (+764.47%)
Mutual labels:  mongodb, oauth2
Pizzly
The simplest, fastest way to integrate your app with an OAuth API 😋
Stars: ✭ 796 (+947.37%)
Mutual labels:  oauth2, oauth
Rack Oauth2
OAuth 2.0 Server & Client Library. Both Bearer and MAC token type are supported.
Stars: ✭ 652 (+757.89%)
Mutual labels:  oauth2, oauth
Next Auth
Authentication for Next.js
Stars: ✭ 8,362 (+10902.63%)
Mutual labels:  oauth2, oauth
Oauth2 Shiro Jwt
use oauth2, shiro and spring specrity to make an ums system
Stars: ✭ 29 (-61.84%)
Mutual labels:  oauth2, oauth2-server
Ueberauth twitter
Twitter Strategy for Überauth
Stars: ✭ 31 (-59.21%)
Mutual labels:  oauth2, oauth
Fake Oauth2 Server
An OAuth2 server implementation to be used for testing
Stars: ✭ 34 (-55.26%)
Mutual labels:  oauth2, oauth2-server
Qq
[READ ONLY] Subtree split of the SocialiteProviders/QQ Provider (see SocialiteProviders/Providers)
Stars: ✭ 50 (-34.21%)
Mutual labels:  oauth2, oauth

node-oauth2-server with MongoDB example

This is a basic example of a OAuth2 server, using node-oauth2-server (version 3.0.1) with MongoDB storage and the minimum (only the required to work) model configuration.

If you want a simpler example without MongoDB storage, you should go to node-oauth2-server-example instead.

Setup

First, you should have MongoDB installed and running on your machine.

You also need to install nodejs and npm and then, simply run npm install and npm start. The server should now be running at http://localhost:3000.

Usage

You can use different grant types to get an access token. By now, password, client_credentials and refresh_token are available.

Checking example data

Firstly, you should create some entries in your MongoDB database.

You can call the loadExampleData function at model.js in order to create these entries automatically, and dump function to inspect the database content.

With password grant

You need to add a client. For example:

  • clientId: application
  • clientSecret: secret

And you have to add a user too. For example:

  • username: pedroetb
  • password: password

With client_credentials grant

You need to add a confidential client. For example:

  • clientId: confidentialApplication
  • clientSecret: topSecret

You don't need any user to use this grant type, but for security is only available to confidential clients.

With refresh_token grant

There is one client added to server and ready to work:

  • clientId: application
  • clientSecret: secret

You 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).

Obtaining a token

To obtain a token you should POST to http://localhost:3000/oauth/token.

With password grant

You need to include the client credentials in request headers and the user credentials and grant type in request body:

  • Headers
    • Authorization: "Basic " + clientId:clientSecret base64'd

      • (for example, to use application:secret, you should send Basic YXBwbGljYXRpb246c2VjcmV0)
    • Content-Type: application/x-www-form-urlencoded

  • Body
    • grant_type=password&username=pedroetb&password=password
      • (contains 3 parameters: grant_type, username and password)

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"

If all goes as planned, you should receive a response like this:

{
	"accessToken": "951d6f603c2ce322c5def00ce58952ed2d096a72",
	"accessTokenExpiresAt": "2018-11-18T16:18:25.852Z",
	"refreshToken": "67c8300ad53efa493c2278acf12d92bdb71832f9",
	"refreshTokenExpiresAt": "2018-12-02T15:18:25.852Z",
	"client": {
		"id": "application"
	},
	"user": {
		"id": "pedroetb"
	}
}

With client_credentials grant

You need to include the client credentials in request headers and the grant type in request body:

  • Headers
    • Authorization: "Basic " + clientId:clientSecret base64'd

      • (for example, to use confidentialApplication:topSecret, you should send Basic Y29uZmlkZW50aWFsQXBwbGljYXRpb246dG9wU2VjcmV0)
    • 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:

{
	"accessToken": "951d6f603c2ce322c5def00ce58952ed2d096a72",
	"accessTokenExpiresAt": "2018-11-18T16:18:25.852Z",
	"client": {
		"id": "confidentialApplication"
	},
	"user": {
		"id": "confidentialApplication"
	}
}

With refresh_token grant

When obtaining an access token using password grant, you get also a refresh token. With 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.

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. You need to include the client credentials in request headers and the refresh token and grant type in request body:

  • Headers
    • Authorization: "Basic " + clientId:clientSecret base64'd

      • (for example, to use application:secret, you should send Basic YXBwbGljYXRpb246c2VjcmV0)
    • Content-Type: application/x-www-form-urlencoded

  • Body
    • grant_type=refresh_token&refresh_token=67c8300ad53efa493c2278acf12d92bdb71832f9
      • (contains 2 parameters: grant_type and refresh_token)

For example, using curl:

curl http://localhost:3000/oauth/token \
	-d "grant_type=refresh_token" \
	-d "refresh_token=67c8300ad53efa493c2278acf12d92bdb71832f9" \
	-H "Authorization: Basic YXBwbGljYXRpb246c2VjcmV0" \
	-H "Content-Type: application/x-www-form-urlencoded"

If all goes as planned, you should receive a response like this:

{
	"accessToken": "17be4ee45b177651db3fd9d286042de75d48eb3b",
	"accessTokenExpiresAt": "2018-11-18T16:18:35.248Z",
	"refreshToken": "37eaff895c8fc9fc839c0098cf3fb01858097908",
	"refreshTokenExpiresAt": "2018-12-02T15:18:35.248Z",
	"client": {
		"id": "application"
	},
	"user": {
		"id": "pedroetb"
	}
}

Using the token

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 " + accessToken
      • (for example, Bearer 951d6f603c2ce322c5def00ce58952ed2d096a72)

For example, using curl:

curl http://localhost:3000 \
	-H "Authorization: Bearer 951d6f603c2ce322c5def00ce58952ed2d096a72"
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].