diff --git a/package.json b/package.json index 0f5aa4e18c..553f2374ca 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,9 @@ "commander": "2.9.0", "deepcopy": "0.6.3", "express": "4.14.0", + "firebase-admin": "^4.0.3", "intersect": "1.0.1", + "jsonwebtoken": "7.2.0", "lodash": "4.17.2", "lru-cache": "4.0.2", "mime": "1.3.4", diff --git a/src/authDataManager/firebase.js b/src/authDataManager/firebase.js new file mode 100644 index 0000000000..117302898f --- /dev/null +++ b/src/authDataManager/firebase.js @@ -0,0 +1,53 @@ +// Firebase authentication provider +var https = require('https'); +var jwt = require("jsonwebtoken"); + +// Returns a promise that fulfills iff this user id is valid. +function validateAuthData(authData) { + return request().then(function (response) { + var publicKey = response[Object.keys(response)[0]] + try { + var decodedToken = jwt.verify(authData.access_token, publicKey); + console.log(authData) + + if (decodedToken == null || decodedToken.id == null || decodedToken.id == "") { + throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Google auth is invalid for this user.'); + } + resolve(decodedToken); + } catch (error) { + throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, "Token validation error"); + } + + throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Google auth is invalid for this user.'); + + }); +} + +// A promisey wrapper for api requests +function request() { + return new Promise(function (resolve, reject) { + var googlePublicKeyUrl = "https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com"; + https.get(googlePublicKeyUrl, function (res) { + var data = ''; + res.on('data', function (chunk) { + data += chunk; + }); + res.on('end', function () { + data = JSON.parse(data); + resolve(data); + }); + }).on('error', function () { + reject('Failed to validate this access token with Google.'); + }); + }); +} + +// Returns a promise that fulfills if this app id is valid. +function validateAppId() { + return Promise.resolve(); +} + +module.exports = { + validateAppId, + validateAuthData +}; \ No newline at end of file diff --git a/src/authDataManager/index.js b/src/authDataManager/index.js index 5fe0f32b60..58bccc321f 100755 --- a/src/authDataManager/index.js +++ b/src/authDataManager/index.js @@ -13,6 +13,7 @@ let vkontakte = require("./vkontakte"); let qq = require("./qq"); let wechat = require("./wechat"); let weibo = require("./weibo"); +let firebase = require("./firebase"); let anonymous = { validateAuthData: () => { @@ -39,7 +40,8 @@ let providers = { vkontakte, qq, wechat, - weibo + weibo, + firebase } module.exports = function(oauthOptions = {}, enableAnonymousUsers = true) {