connect-concierge
Advanced tools
Comparing version 0.1.6 to 0.2.0
@@ -7,3 +7,3 @@ | ||
(function() { | ||
var ConnectConcierge, dust, fs, request, utils, _, _isAdmin; | ||
var ConnectConcierge, dust, fs, request, urlencode, utils, _, _isAdmin; | ||
@@ -20,2 +20,4 @@ fs = require('fs'); | ||
urlencode = require('urlencode'); | ||
_isAdmin = utils.isAdmin; | ||
@@ -27,9 +29,6 @@ | ||
ConnectConcierge.prototype.getMiddlewares = function(options) { | ||
return [this.getEnvironment(options), this.resolveTags(options), this.resolveTags(options), this.resolveTemplate(options), this.renderTemplate(options)]; | ||
return [this.getEnvironment(options), this.authenticationHandler(options), this.resolveTags(options), this.licenseManagerCall(options), this.resolveTemplate(options), this.renderTemplate(options)]; | ||
}; | ||
ConnectConcierge.prototype.getEnvironment = function(options) { | ||
if (options == null) { | ||
options = {}; | ||
} | ||
ConnectConcierge.prototype.getEnvironment = function() { | ||
return function(req, res, next) { | ||
@@ -172,2 +171,59 @@ var env, envRegex, match; | ||
ConnectConcierge.prototype.authenticationHandler = function() { | ||
return function(req, res, next) { | ||
var VtexIdclientAutCookie, forbidden, loginUriRegexp, match, redirectUrl, returnUrl, unauthorized, urlEncodedAuthCookie, vtexIdCookieRegexp, vtexIdEndpoint; | ||
loginUriRegexp = /admin\/Site\/Login.aspx/; | ||
if (loginUriRegexp.test(req.url) || req.url.indexOf('/admin/Content') === 0 || req.url.indexOf('/admin/Scripts') === 0 || req.url.indexOf('/meta/whoami') === 0) { | ||
return next(); | ||
} | ||
returnUrl = urlencode(req.url); | ||
redirectUrl = "/admin/Site/Login.aspx?ReturnUrl=" + returnUrl; | ||
vtexIdCookieRegexp = /.*VtexIdclientAutCookie=([^;]*)/; | ||
match = vtexIdCookieRegexp.exec(req.headers.cookie); | ||
VtexIdclientAutCookie = match[1]; | ||
if (VtexIdclientAutCookie == null) { | ||
console.log('redirecting to ', redirectUrl); | ||
res.statusCode = 302; | ||
res.setHeader('Location', redirectUrl); | ||
return res.end(); | ||
} | ||
forbidden = function(body) { | ||
console.warn("Forbidden access for auth cookie", VtexIdclientAutCookie, '\n', body); | ||
return res.status(403).render('error', { | ||
error: "Forbidden. Sorry, you can't touch this." | ||
}); | ||
}; | ||
unauthorized = function(body) { | ||
console.warn("Unauthorized access for auth cookie", VtexIdclientAutCookie, '\n', body); | ||
return res.status(401).render('error', { | ||
error: "Unauthorized. Sorry, could not authenticate you." | ||
}); | ||
}; | ||
vtexIdEndpoint = "https://vtexid.vtex.com.br/api/vtexid/pub/authenticated/user?authToken="; | ||
urlEncodedAuthCookie = encodeURIComponent(VtexIdclientAutCookie); | ||
return request.get(vtexIdEndpoint + urlEncodedAuthCookie, function(err, response, body) { | ||
var e; | ||
try { | ||
body = JSON.parse(body); | ||
if (body == null) { | ||
res.writeHead(302, { | ||
'Location': redirectUrl | ||
}); | ||
return res.end; | ||
} | ||
if (body.user == null) { | ||
return unauthorized(body); | ||
} | ||
if (body.user.indexOf("@vtex.com.br") === -1) { | ||
return forbidden(body); | ||
} | ||
return next(); | ||
} catch (_error) { | ||
e = _error; | ||
return next(e); | ||
} | ||
}); | ||
}; | ||
}; | ||
return ConnectConcierge; | ||
@@ -174,0 +230,0 @@ |
{ | ||
"name": "connect-concierge", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"description": "Connect middlewares for VTEX backoffice app Concierge", | ||
"main": "libs/connect-concierge.js", | ||
"main": "main.js", | ||
"scripts": { | ||
@@ -30,4 +30,7 @@ "test": "mocha --compilers coffee:coffee-script/register --reporter spec test/**" | ||
"underscore": "~1.7.0", | ||
"request": "~2.48.0" | ||
"request": "~2.48.0", | ||
"coffee-script": "~1.8.0", | ||
"urlencode": "~0.2.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26332
17
222
5
+ Addedcoffee-script@~1.8.0
+ Addedurlencode@~0.2.0
+ Addedaddress@2.0.3(transitive)
+ Addedcoffee-script@1.8.0(transitive)
+ Addediconv-lite@0.2.11(transitive)
+ Addedmkdirp@0.3.5(transitive)
+ Addedurlencode@0.2.0(transitive)
+ Addedutility@0.1.13(transitive)