大部分的人包含我在內,在開發oauth相關工具時,使用Github用的很高興, 寫出了一段code,確定oauth可以運作,就很高興的把code放上github了


如以下範例程式

var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
 passport.use(new GoogleStrategy({
    clientID: GOOGLE_CLIENT_ID,
    clientSecret: GOOGLE_CLIENT_SECRET,
    callbackURL: "http://127.0.0.1:3000/auth/google/callback"
  },
  function(accessToken, refreshToken, profile, done) {
    User.findOrCreate({ googleId: profile.id }, function (err, user) {
      return done(err, user);
    });
  }
));

但,clientId以及ClientSecret也一起上去了。 Github是open的空間,很難保證之後是否會有有心人士,把你註冊的clientId/clientSecret資訊拿來用。 如果client有用到的是需要付費的api,就慘了!


在放上github前,先做些保護吧


以node.js來說,讓程式執行外部參數有幾種方式

1. process.env variables 
2. hand-loading an arbitrarily formatted file using io streams 
3. loading another javascript or json file with require().

各有優缺點,但3比較適於佈署。所以,建立一個google-oauth.js

var googlecreds = {"web":{
 "auth_uri":"https://accounts.google.com/o/oauth2/auth",
 "client_secret":"THIS_IS_A_SECRET",
 "token_uri":"https://accounts.google.com/o/oauth2/token",
 "client_email":"APP_ID@developer.gserviceaccount.com",
 "client_x509_cert_url":"https://www.googleapis.com/robot/v1/metadata/x509/APP\_ID@developer.gserviceaccount.com",
 "client_id":"APP_ID.apps.googleusercontent.com",
 "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs"}}

module.exports = googlecreds.web;

然後再主要程式把它require進來

var GoogleOauth = require('./google-oauth.js');

passport.use(new GoogleStrategy({
    clientID: GoogleOauth.client_id,
    clientSecret: GoogleOauth.client_secret,
    callbackURL: "http://127.0.0.1:3000/auth/google/callback"
  },

當然,在git的.gitignore裡面要把google-oauth.js放進去ignore掉

這樣client的資訊就不致於外流了。

arrow
arrow
    文章標籤
    node.js
    全站熱搜

    Perry Wu 發表在 痞客邦 留言(0) 人氣()