Node.JS, Mongoose和Jade搭建OAuth2服务器

开发 前端
Papers是一项论文数据库移动应用,有iOS和Android版本。写论文的同学们会很需要,关于它的介绍,请看开发者的官方博客。http://blog.papersapp.com/oauth-server-in-node-js/

今天我们来看一个Node.JS的实际应用。这是国外的Paper应用开发者所搭建的OAuth2服务器,使用的主要技术包括:

- Node.JS 的Express框架

- Mongoose工具集,Mongodb的一个流行库,方便建立模型。

- bcrypt,用于密码加密

- superagent,用于测试

Papers是一项论文数据库移动应用,有iOS和Android版本。写论文的同学们会很需要,关于它的介绍,请看开发者的官方博客。http://blog.papersapp.com/oauth-server-in-node-js/

虽然Papers并不是开源的,但是作者已经把写好的node-oauth2-server模块以及Papers的认证过程一起打包放在了GitHub上,我们可以下载研究:

https://github.com/mekentosj/oauth2-example

在代码中的Models目录下,我们可以清楚的看到Model的Schema定义。从这里,我们可以明白OAuth2的需要处理的主要数据结构,包括access_token, refresh_token, oauth_client.

  1. var OAuthAccessTokensSchema = new Schema({  
  2.   accessToken: { type: String, required: true, unique: true },  
  3.   clientId: String,  
  4.   userId: { type: String, required: true },  
  5.   expires: Date  
  6. });  
  7.  
  8. var OAuthRefreshTokensSchema = new Schema({  
  9.   refreshToken: { type: String, required: true, unique: true },  
  10.   clientId: String,  
  11.   userId: { type: String, required: true },  
  12.   expires: Date  
  13. });  
  14.  
  15. var OAuthClientsSchema = new Schema({  
  16.   clientId: String,  
  17.   clientSecret: String,  
  18.   redirectUri: String  
  19. });  
  20.  
  21. var OAuthUsersSchema = new Schema({  
  22.   email: { type: String, unique: true, required: true },  
  23.   hashed_password: { type: String, required: true },  
  24.   password_reset_token: { type: String, unique: true },  
  25.   reset_token_expires: Date,  
  26.   firstname: String,  
  27.   lastname: String  
  28. }); 

通过运行代码中的seed.js,我们创建了一个user.

  1. var app = require('./app');  
  2. var models = require('./models');  
  3.  
  4. models.User.create({  
  5.   email: 'alex@example.com',  
  6.   hashed_password: '$2a$10$aZB36UooZpL.fAgbQVN/j.pfZVVvkHxEnj7vfkVSqwBOBZbB/IAAK' 
  7. }, function() {  
  8.   models.OAuthClientsModel.create({  
  9.     clientId: 'papers3',  
  10.     clientSecret: '123',  
  11.     redirectUri: '/oauth/redirect' 
  12.   }, function() {  
  13.     process.exit();  
  14.   });  
  15. }); 

这样我们就可以开始体验这个Node.JS的OAuth2服务器了。先让Mongo运行起来,负责后台数据库, 比如"mongod -dbpath ./", 之后运行"npm start".

  1. oliverluan@localhost:~/Documents/EvWork/node_oauth2_example/oauth2-example$ npm start  
  2.  
  3. > oauth2-experiment@0.0.1 start /Users/oliverluan/Documents/EvWork/node_oauth2_example/oauth2-example  
  4. > ./node_modules/.bin/nodemon server.js  
  5.  
  6. 14 Apr 07:02:43 - [nodemon] v1.0.17  
  7. 14 Apr 07:02:43 - [nodemon] to restart at any time, enter `rs`  
  8. 14 Apr 07:02:43 - [nodemon] watching: *.*  
  9. 14 Apr 07:02:43 - [nodemon] starting `node server.js`  
  10. connect.multipart() will be removed in connect 3.0  
  11. visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives  
  12. connect.limit() will be removed in connect 3.0  
  13. Express server listening on port: 3000  
  14. POST /oauth/token 200 102ms - 175b  
  15. GET /secret 200 2ms - 11b 

模拟一个Oauth2的access token请求,运行这份文件(node getToken.js)

  1. var request = require('request');  
  2.  
  3. //client_id  
  4. var t_client_id = 'papers3';  
  5. //client_secret  
  6. var t_client_secret = '123';  
  7. //clientCredentials  以client_id:client_secret形式组合并转换成Base64-encoded  
  8. var clientCredentials = (t_client_id + ':'+t_client_secret).toString('base64');  
  9. //用户名  
  10. var t_username = 'alex@example.com';  
  11. //密码  
  12. var t_password = 'test';  
  13.  
  14. console.log(clientCredentials);  
  15.  
  16. //发送Post请求获取Token  
  17. request.post({    
  18.   url: 'http://' + clientCredentials + '@localhost:3000/oauth/token',  
  19.   form: {  
  20.     grant_type: 'password',  
  21.     username: t_username,  
  22.     password: t_password,  
  23.     client_id: t_client_id,  
  24.     client_secret: t_client_secret  
  25.   },  
  26. }, function(err, res, body) {  
  27.   console.log(body);  
  28.   //获得Token  
  29.   var accessToken = JSON.parse(body).access_token;  
  30.  
  31.   request.get({  
  32.     url: 'http://localhost:3000/secret',  
  33.     headers: { Authorization: 'Bearer ' + accessToken }  
  34.   }, function(err, res, body) {  
  35.     console.log(body);  
  36.    });  
  37. });  

成功获得access token.

  1. oliverluan@localhost:~/Documents/EvWork/node_oauth2_example/oauth2-example$ node getToken.js  
  2. papers3:123  
  3. {  
  4.   "token_type""bearer",  
  5.   "access_token""620bb362f32857d5174802e06065305874953597",  
  6.   "expires_in": 3600,  
  7.   "refresh_token""569be5f4cc1ea943021b3676eaa2a51825c2c257" 
  8. }  
  9. Secret area 

原文链接:http://blog.csdn.net/u011581005/article/details/23650917

责任编辑:林师授 来源: neokidd的博客
相关推荐

2020-10-12 08:06:28

HTTP 服务器证书

2019-02-15 10:49:37

Node.jsweb服务器

2021-08-29 23:33:44

OAuth2服务器Keycloak

2021-11-15 13:58:00

服务器配置授权

2022-02-15 07:35:12

服务器KeycloakOAuth2

2013-05-02 14:13:44

Android开发OAuth2服务认证

2020-03-17 13:24:04

微服务架构数据

2011-06-17 10:29:04

Nodejavascript

2021-09-02 10:49:25

Node.jsPHP服务器开发

2022-06-05 13:52:32

Node.jsDNS 的原理DNS 服务器

2011-07-26 11:07:08

JavaScript

2021-02-04 09:18:20

服务器认证自定义

2022-05-13 15:15:18

服务器OAuth2控制台

2011-09-08 10:21:50

Node.js

2011-10-19 14:38:46

Node.js

2020-10-29 16:00:03

Node.jsweb前端

2015-03-10 10:59:18

Node.js开发指南基础介绍

2022-08-28 16:30:34

Node.jsDocker指令

2013-11-01 09:34:56

Node.js技术

2019-03-15 09:34:18

静态资源服务器命令前端
点赞
收藏

51CTO技术栈公众号