日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Node.JS,Mongoose和Jade搭建OAuth2服務(wù)器

今天我們來看一個(gè)Node.JS的實(shí)際應(yīng)用。這是國外的Paper應(yīng)用開發(fā)者所搭建的OAuth2服務(wù)器,使用的主要技術(shù)包括:

- Node.JS 的Express框架

- Mongoose工具集,Mongodb的一個(gè)流行庫,方便建立模型。

- bcrypt,用于密碼加密

- superagent,用于測試

Papers是一項(xiàng)論文數(shù)據(jù)庫移動(dòng)應(yīng)用,有iOS和Android版本。寫論文的同學(xué)們會很需要,關(guān)于它的介紹,請看開發(fā)者的官方博客。http://blog.papersapp.com/oauth-server-in-node-js/

雖然Papers并不是開源的,但是作者已經(jīng)把寫好的node-oauth2-server模塊以及Papers的認(rèn)證過程一起打包放在了GitHub上,我們可以下載研究:

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

在代碼中的Models目錄下,我們可以清楚的看到Model的Schema定義。從這里,我們可以明白OAuth2的需要處理的主要數(shù)據(jù)結(jié)構(gòu),包括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. });

通過運(yùn)行代碼中的seed.js,我們創(chuàng)建了一個(gè)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. });

這樣我們就可以開始體驗(yàn)這個(gè)Node.JS的OAuth2服務(wù)器了。先讓Mongo運(yùn)行起來,負(fù)責(zé)后臺數(shù)據(jù)庫, 比如"mongod -dbpath ./", 之后運(yùn)行"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

模擬一個(gè)Oauth2的access token請求,運(yùn)行這份文件(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形式組合并轉(zhuǎn)換成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. //發(fā)送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

當(dāng)前名稱:Node.JS,Mongoose和Jade搭建OAuth2服務(wù)器
本文路徑:http://www.dlmjj.cn/article/cdjsoje.html