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

今天我们来看一个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. var OAuthRefreshTokensSchema = new Schema({
  8.   refreshToken: { type: String, required: true, unique: true },
  9.   clientId: String,
  10.   userId: { type: String, required: true },
  11.   expires: Date
  12. });
  13. var OAuthClientsSchema = new Schema({
  14.   clientId: String,
  15.   clientSecret: String,
  16.   redirectUri: String
  17. });
  18. var OAuthUsersSchema = new Schema({
  19.   email: { type: String, unique: true, required: true },
  20.   hashed_password: { type: String, required: true },
  21.   password_reset_token: { type: String, unique: true },
  22.   reset_token_expires: Date,
  23.   firstname: String,
  24.   lastname: String
  25. });

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

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

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

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

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

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

成功获得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

网站栏目:Node.JS,Mongoose和Jade搭建OAuth2服务器
网址分享:http://www.hantingmc.com/qtweb/news2/344802.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联