溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Node.JS, Mongoose和Jade怎樣搭建OAuth2服務(wù)器

發(fā)布時(shí)間:2021-11-17 14:19:24 來源:億速云 閱讀:148 作者:柒染 欄目:web開發(fā)

今天就跟大家聊聊有關(guān)Node.JS, Mongoose和Jade怎樣搭建OAuth2服務(wù)器,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

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

- Node.JS 的Express框架

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

- bcrypt,用于密碼加密

- superagent,用于測試

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

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

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

在代碼中的Models目錄下,我們可以清楚的看到Model的Schema定義。從這里,我們可以明白OAuth3的需要處理的主要數(shù)據(jù)結(jié)構(gòu),包括access_token, refresh_token, oauth_client.

var OAuthAccessTokensSchema = new Schema({    accessToken: { type: String, required: true, unique: true },    clientId: String,    userId: { type: String, required: true },    expires: Date  });   var OAuthRefreshTokensSchema = new Schema({    refreshToken: { type: String, required: true, unique: true },    clientId: String,    userId: { type: String, required: true },    expires: Date  });   var OAuthClientsSchema = new Schema({    clientId: String,    clientSecret: String,    redirectUri: String  });   var OAuthUsersSchema = new Schema({    email: { type: String, unique: true, required: true },    hashed_password: { type: String, required: true },    password_reset_token: { type: String, unique: true },    reset_token_expires: Date,    firstname: String,    lastname: String  });

通過運(yùn)行代碼中的seed.js,我們創(chuàng)建了一個user.

var app = require('./app');  var models = require('./models');   models.User.create({    email: 'alex@example.com',    hashed_password: '$2a$10$aZB36UooZpL.fAgbQVN/j.pfZVVvkHxEnj7vfkVSqwBOBZbB/IAAK' }, function() {    models.OAuthClientsModel.create({      clientId: 'papers3',      clientSecret: '123',      redirectUri: '/oauth/redirect'   }, function() {      process.exit();    });  });

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

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

模擬一個Oauth3的access token請求,運(yùn)行這份文件(node getToken.js)

var request = require('request');   //client_id  var t_client_id = 'papers3';  //client_secret  var t_client_secret = '123';  //clientCredentials  以client_id:client_secret形式組合并轉(zhuǎn)換成Base64-encoded  var clientCredentials = (t_client_id + ':'+t_client_secret).toString('base64');  //用戶名  var t_username = 'alex@example.com';  //密碼  var t_password = 'test';   console.log(clientCredentials);   //發(fā)送Post請求獲取Token  request.post({      url: 'http://' + clientCredentials + '@localhost:3000/oauth/token',    form: {      grant_type: 'password',      username: t_username,      password: t_password,      client_id: t_client_id,      client_secret: t_client_secret    },  }, function(err, res, body) {    console.log(body);    //獲得Token    var accessToken = JSON.parse(body).access_token;     request.get({      url: 'http://localhost:3000/secret',      headers: { Authorization: 'Bearer ' + accessToken }    }, function(err, res, body) {      console.log(body);     });  });

成功獲得access token.

oliverluan@localhost:~/Documents/EvWork/node_oauth3_example/oauth3-example$ node getToken.js  papers3:123  {    "token_type": "bearer",    "access_token": "620bb362f32857d5174802e06065305874953597",    "expires_in": 3600,    "refresh_token": "569be5f4cc1ea943021b3676eaa2a51825c2c257" }  Secret area

看完上述內(nèi)容,你們對Node.JS, Mongoose和Jade怎樣搭建OAuth2服務(wù)器有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI