Socket
Socket
Sign inDemoInstall

andyet-express-auth

Package Overview
Dependencies
25
Maintainers
4
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.12 to 0.1.0

.jshintignore

96

index.js
var _ = require('underscore'),
colors = require('colors'),
config = require('getconfig'),
crypto = require('crypto'),
request = require('request'),
querystring = require('querystring');
querystring = require('querystring'),
log = require('bucker').createLogger(config.bucker, module);
config.andyetAPIs = _.extend({
'accounts': 'https://apps.andyet.com',
'shippy': 'https://api.shippy.io',
'talky': 'https://api.talky.io'
}, config.andyetAPIs || {});
function AndYetMiddleware() {
var self = this;
this.showHelp = function (message) {
var output = [
"\n",
message.red,
"_____________________________________________________________",
"",
"var express = require('express'),",
" auth = require('andyet-express-auth'),",
" app = express();",
"",
"",
"app.use(express.cookieParser());",
"app.use(express.session({ secret: 'keyboard cat' }));",
"app.use(auth.middleware({",
" app: app",
" clientId: 'YOUR CLIENT ID',",
" clientSecret: 'YOUR CLIENT SECRET',",
" defaultRedirect: '/app'",
"});",
"",
"",
"// a route that requires being logged in with your &yet account",
"app.get('/my-secured-route', auth.secure(), function (req, res) {",
" // req.user is everything we know about the andyet user",
" // req.token is now the auth token",
" res.send(req.user)",
"});",
"_____________________________________________________________",
"",
""
].join('\n');
console.log(output);
};
this.middleware = function (app, opts) {
var self = this;
this.middleware = function (config) {
var self = this;
if (!config.app || !config.clientId || !config.clientSecret || !config.defaultRedirect) {
this.showHelp('You have to pass the app, clientId and clientSecret and a default redirect. For example:');
self.app = app;
if (!opts.defaultRedirect) {
log.warn('Missing defaultRedirect in andyetAuth settings, using "/"');
}
if (!opts.api) {
log.warn('Missing api in andyetAuth settings, using "shippy"');
}
// store our configs as properties
_.extend(this, {
loggedOutRedirect: '/'
}, config);
self.api = opts.api || 'shippy';
self.defaultRedirect = opts.defaultRedirect || '/';
self.loggedOutRedirect = opts.loggedOutRedirect || '/';
self.onRefreshToken = opts.onRefreshToken || function (user, token, cb) { cb(); };
// set our account and API urls
this.accountsUrl = config.accountsUrl || (config.local ? 'http://localhost:3001' : 'https://apps.andyet.com');
this.apiUrl = config.apiUrl || (config.local ? 'http://localhost:3000' : 'https://api.shippy.io');
this.onRefreshToken = config.onRefreshToken || function (user, token, cb) { cb(); };
// The login route. If we already have a token in the session we'll

@@ -76,5 +51,5 @@ // just continue through.

req.session.save(function () {
var url = self.accountsUrl + '/oauth/authorize?' + querystring.stringify({
var url = config.andyetAPIs.accounts + '/oauth/authorize?' + querystring.stringify({
response_type: 'code',
client_id: self.clientId,
client_id: config.andyetAuth.id,
state: req.session.oauthState

@@ -90,2 +65,3 @@ });

if (result.error) {
log.error('Failed to parse querystring: ' + result.error);
return response.redirect('/auth/andyet/failed');

@@ -95,2 +71,3 @@ }

if (result.state != req.session.oauthState) {
log.error('OAuth state values do not match: %s != %s', result.state, req.session.oauthState);
return response.redirect('/auth/andyet/failed');

@@ -100,3 +77,3 @@ }

request.post({
url: self.accountsUrl + '/oauth/access_token',
url: config.andyetAPIs.accounts + '/oauth/access_token',
strictSSL: true,

@@ -106,8 +83,8 @@ form: {

grant_type: 'authorization_code',
client_id: self.clientId,
client_secret: self.clientSecret
client_id: config.andyetAuth.id,
client_secret: config.andyetAuth.secret
}
}, function (err, res, body) {
if (res && res.statusCode === 200) {
token = JSON.parse(body);
var token = JSON.parse(body);
req.token = token;

@@ -128,2 +105,3 @@ var nextUrl = req.session.nextUrl || self.defaultRedirect || '/';

} else {
log.error('Error requesting access token: %s', err);
response.redirect('/auth/andyet/failed');

@@ -157,3 +135,3 @@ }

request.get({
url: self.apiUrl + '/me',
url: config.andyetAPIs[self.api] + '/me',
strictSSL: true,

@@ -169,2 +147,3 @@ headers: {

} else {
log.error('Error requesting user information: %s', err);
res.redirect('/auth/andyet/failed');

@@ -188,8 +167,8 @@ }

request.post({
url: self.accountsUrl + '/oauth/validate',
url: config.andyetAPIs.accounts + '/oauth/validate',
strictSSL: true,
form: {
access_token: cookieToken,
client_id: self.clientId,
client_secret: self.clientSecret
client_id: config.andyetAuth.id,
client_secret: config.andyetAauth.secret,
}

@@ -207,2 +186,3 @@ }, function (err, res2, body) {

}
log.error('Error validating cached token: %s', err);
res.redirect('/auth/andyet/failed');

@@ -209,0 +189,0 @@ });

{
"name": "andyet-express-auth",
"description": "Dead simple &yet auth middleware.",
"version": "0.0.12",
"version": "0.1.0",
"dependencies": {
"express": "3.x",
"colors": "0.6.0-1",
"bucker": "0.4.0",
"getconfig": "0.0.5",
"request": "2.21.0"
},
"devDependencies": {
"express": "3.x",
"precommit-hook": "0.3.4"
},
"main": "index.js"
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc