<|start_filename|>node_modules/oauth20-provider/lib/error/invalidGrant.js<|end_filename|>
var
    util = require('util'),
    oauth2 = require('./oauth2.js');

var invalidGrant = function (msg) {
    invalidGrant.super_.call(this, 'invalid_grant', msg, 400, this.constructor);
};
util.inherits(invalidGrant, oauth2);
invalidGrant.prototype.name = 'OAuth2InvalidGrant';
invalidGrant.prototype.logLevel = 'info';

module.exports = invalidGrant;

<|start_filename|>node_modules/oauth20-provider/test/server/model/rethinkdb/oauth2/code.js<|end_filename|>
var crypto = require('crypto'),
    RethinkDb = require('rethinkdb'),
    connection = require('./../connection.js');

var TABLE = 'authorization_code';

module.exports.create = function(userId, clientId, scope, ttl, cb) {
    var code = crypto.randomBytes(32).toString('hex');
    var obj = {code: code, userId: userId, clientId: clientId, scope: scope, ttl: new Date().getTime() + ttl * 1000};

    connection.acquire(function(err, conn) {
        if (err) cb(err);
        else RethinkDb.table(TABLE).insert(obj, {}).run(conn, function(err) {
            cb(err, code);
        });
    });
};

module.exports.fetchByCode = function(code, cb) {
    connection.acquire(function(err, conn) {
        if (err) cb(err);
        else {
            RethinkDb.table(TABLE).filter({ code: code }).limit(1).run(conn, function(err, cursor) {
                if (err) cb(err);
                else cursor.next(cb);
            });
        }
    });
};

module.exports.getUserId = function(code) {
    return code.userId;
};

module.exports.getClientId = function(code) {
    return code.clientId;
};

module.exports.getScope = function(code) {
    return code.scope;
};

module.exports.checkTtl = function(code) {
    return (code.ttl > new Date().getTime());
};

module.exports.removeByCode = function(code, cb) {
    connection.acquire(function(err, conn) {
        if (err) cb(err);
        else RethinkDb.table(TABLE).filter({ code: code }).delete().run(conn, cb);
    });
};

<|start_filename|>node_modules/oauth20-provider/lib/error/unauthorizedClient.js<|end_filename|>
var
    util = require('util'),
    oauth2 = require('./oauth2.js');

var unauthorizedClient = function (msg) {
    unauthorizedClient.super_.call(this, 'unauthorized_client', msg, 400, this.constructor);
};
util.inherits(unauthorizedClient, oauth2);
unauthorizedClient.prototype.name = 'OAuth2UnauthorizedClient';
unauthorizedClient.prototype.logLevel = 'info';

module.exports = unauthorizedClient;

<|start_filename|>node_modules/oauth20-provider/lib/error/accessDenied.js<|end_filename|>
var
    util = require('util'),
    oauth2 = require('./oauth2.js');

var accessDenied = function (msg) {
    accessDenied.super_.call(this, 'access_denied', msg, 403, this.constructor);
};
util.inherits(accessDenied, oauth2);
accessDenied.prototype.name = 'OAuth2AccessDenied';
accessDenied.prototype.logLevel = 'info';

module.exports = accessDenied;

<|start_filename|>node_modules/oauth20-provider/lib/error/unsupportedResponseType.js<|end_filename|>
var
    util = require('util'),
    oauth2 = require('./oauth2.js');

var unsupportedResponseType = function (msg) {
    unsupportedResponseType.super_.call(this, 'unsupported_response_type', msg, 400, this.constructor);
};
util.inherits(unsupportedResponseType, oauth2);
unsupportedResponseType.prototype.name = 'OAuth2UnsupportedResponseType';
unsupportedResponseType.prototype.logLevel = 'info';

module.exports = unsupportedResponseType;

<|start_filename|>node_modules/oauth20-provider/test/server/model/redis/redis.js<|end_filename|>
var
    redis = require('redis');

module.exports = redis.createClient();

// No need to wait data load
require('./data.js').initialize();


<|start_filename|>node_modules/oauth20-provider/lib/middleware/index.js<|end_filename|>
module.exports = {
    bearer: require('./bearer.js')
};

<|start_filename|>node_modules/oauth20-provider/test/clientCredentials.js<|end_filename|>
var
    request = require('supertest'),
    data = require('./server/model/data.js'),
    app = require('./server/app.js');

describe('Client Credentials Grant Type ',function() {

    var
        accessToken;

    it('POST /token with grant_type="client_credentials" expect token', function(done) {
        request(app)
            .post('/token')
            .set('Authorization', 'Basic ' + new Buffer(data.clients[0].id + ':' + data.clients[0].secret, 'ascii').toString('base64'))
            .send({grant_type: 'client_credentials'})
            .expect(200, /access_token/)
            .end(function(err, res) {
                if (err) return done(err);
                accessToken = res.body.access_token;
                done();
            });
    });

    it('POST /secure expect forbidden', function(done) {
        request(app)
            .get('/secure')
            .set('Authorization', 'Bearer ' + accessToken)
            .expect(403, done);
    });

    it('POST /client expect authorized', function(done) {
        request(app)
            .get('/client')
            .set('Authorization', 'Bearer ' + accessToken)
            .expect(200, done);
    });

});

<|start_filename|>node_modules/oauth20-provider/lib/controller/authorization/index.js<|end_filename|>
module.exports = {
    code:       require('./code'),
    implicit:   require('./implicit')
};


<|start_filename|>node_modules/oauth20-provider/lib/error/index.js<|end_filename|>
module.exports = {
    accessDenied:               require('./accessDenied.js'),
    forbidden:                  require('./forbidden.js'),
    invalidClient:              require('./invalidClient.js'),
    invalidGrant:               require('./invalidGrant.js'),
    invalidRequest:             require('./invalidRequest.js'),
    invalidScope:               require('./invalidScope.js'),
    oauth2:                     require('./oauth2.js'),
    serverError:                require('./serverError.js'),
    unauthorizedClient:         require('./unauthorizedClient.js'),
    unsupportedGrantType:       require('./unsupportedGrantType.js'),
    unsupportedResponseType:    require('./unsupportedResponseType.js')
};

<|start_filename|>node_modules/oauth20-provider/lib/error/forbidden.js<|end_filename|>
var
    util = require('util'),
    oauth2 = require('./oauth2.js');

// @todo: check standards (and other libraries) for error in case of wrong access_token
var forbidden = function (msg) {
    forbidden.super_.call(this, 'forbidden', msg, 403, this.constructor);
};
util.inherits(forbidden, oauth2);
forbidden.prototype.name = 'OAuth2Forbidden';
forbidden.prototype.logLevel = 'warn';

module.exports = forbidden;

<|start_filename|>node_modules/oauth20-provider/test/server/model/rethinkdb/connection.js<|end_filename|>
var config = require('./config.js'),
    RethinkDb = require('rethinkdb');

// Using only one active connection for test purposes
var _connection;

module.exports.acquire = function(cb) {
    if (_connection) return cb(null, _connection);

    RethinkDb.connect(config, function(err, connection) {
        if (err) cb(err);
        else {
            _connection = connection;
            cb(null, _connection);
        }
    });
};

module.exports.close = function(cb) {
    if (!_connection) return cb();

    _connection.close(cb);
};

<|start_filename|>node_modules/oauth20-provider/test/server/model/redis/oauth2/user.js<|end_filename|>
var
    util = require('util'),
    redis = require('./../redis.js');

var KEY = {
    USER         : 'user:id:%s',
    USER_USERNAME: 'user:username:%s'
};

module.exports.KEY = KEY;

module.exports.getId = function(user) {
    return user.id;
};

var fetchById = function(id, cb) {
    redis.get(util.format(KEY.USER, id), function(err, stringified) {
        if (err) cb(err);
        else if (!stringified) cb();
        else {
            try {
                var obj = JSON.parse(stringified);
                cb(null, obj);
            } catch (e) {
                cb();
            }
        }
    });
};

module.exports.fetchById = fetchById;

module.exports.fetchByUsername = function(username, cb) {
    redis.get(util.format(KEY.USER_USERNAME, username), function(err, userId) {
        if (err) cb(err);
        else if (!userId) cb();
        else {
            fetchById(userId, cb);
        }
    });
};

module.exports.checkPassword = function(user, password, cb) {
    (user.password == password) ? cb(null, true) : cb(null, false);
};

module.exports.fetchFromRequest = function(req) {
    return req.session.user;
};

<|start_filename|>node_modules/oauth20-provider/test/server/model/memory/oauth2/code.js<|end_filename|>
var crypto = require('crypto'),
    codes = require('./../../data.js').codes;

module.exports.create = function(userId, clientId, scope, ttl, cb) {
    var code = crypto.randomBytes(32).toString('hex');
    var obj = {code: code, userId: userId, clientId: clientId, scope: scope, ttl: new Date().getTime() + ttl * 1000};
    codes.push(obj);
    cb(null, code);
};

module.exports.fetchByCode = function(code, cb) {
    for (var i in codes) {
        if (codes[i].code == code) return cb(null, codes[i]);
    }
    cb();
};

module.exports.getUserId = function(code) {
    return code.userId;
};

module.exports.getClientId = function(code) {
    return code.clientId;
};

module.exports.getScope = function(code) {
    return code.scope;
};

module.exports.checkTtl = function(code) {
    return (code.ttl > new Date().getTime());
};

module.exports.removeByCode = function(code, cb) {
    for (var i in codes) {
        if (codes[i].code == code) {
            codes.splice(i, 1);
            break;
        }
    }
    cb();
};

<|start_filename|>node_modules/oauth20-provider/test/server/model/rethinkdb/oauth2/user.js<|end_filename|>
var RethinkDb = require('rethinkdb'),
    connection = require('./../connection.js');

var TABLE = 'user';

module.exports.getId = function(user) {
    return user['id'];
};

module.exports.fetchById = function(id, cb) {
    connection.acquire(function(err, conn) {
        if (err) cb(err);
        else RethinkDb.table(TABLE).get(id).run(conn, cb);
    });
};

module.exports.fetchByUsername = function(username, cb) {
    connection.acquire(function(err, conn) {
        if (err) cb(err);
        else {
            RethinkDb.table(TABLE).filter({ username: username }).limit(1).run(conn, function(err, cursor) {
                if (err) cb(err);
                else {
                    cursor.toArray(function(err, users) {
                        if (err) cb(err);
                        else cb(err, users && users.length ? users[0] : null);
                    });
                }
            });
        }
    });
};

module.exports.checkPassword = function(user, password, cb) {
    (user.password == password) ? cb(null, true) : cb(null, false);
};

module.exports.fetchFromRequest = function(req) {
    return req.session.user;
};

<|start_filename|>node_modules/oauth20-provider/lib/error/invalidClient.js<|end_filename|>
var
    util = require('util'),
    oauth2 = require('./oauth2.js');

var invalidClient = function (msg) {
    invalidClient.super_.call(this, 'invalid_client', msg, 401, this.constructor);
};
util.inherits(invalidClient, oauth2);
invalidClient.prototype.name = 'OAuth2InvalidClient';
invalidClient.prototype.logLevel = 'info';

module.exports = invalidClient;

<|start_filename|>node_modules/oauth20-provider/lib/error/serverError.js<|end_filename|>
var
    util = require('util'),
    oauth2 = require('./oauth2.js');

var serverError = function (msg) {
    serverError.super_.call(this, 'server_error', msg, 500, this.constructor);
};
util.inherits(serverError, oauth2);
serverError.prototype.name = 'OAuth2ServerError';
serverError.prototype.logLevel = 'error';

module.exports = serverError;

<|start_filename|>node_modules/oauth20-provider/lib/error/unsupportedGrantType.js<|end_filename|>
var
    util = require('util'),
    oauth2 = require('./oauth2.js');

var unsupportedGrantType = function (msg) {
    unsupportedGrantType.super_.call(this, 'unsupported_grant_type', msg, 400, this.constructor);
};
util.inherits(unsupportedGrantType, oauth2);
unsupportedGrantType.prototype.name = 'OAuth2UnsupportedGrantType';
unsupportedGrantType.prototype.logLevel = 'info';

module.exports = unsupportedGrantType;

<|start_filename|>node_modules/oauth20-provider/lib/controller/index.js<|end_filename|>
module.exports = {
    authorization:  require('./authorization.js'),
    token:          require('./token.js')
};

<|start_filename|>node_modules/oauth20-provider/test/server/model/rethinkdb/index.js<|end_filename|>
module.exports = {
    oauth2: require('./oauth2')
};

<|start_filename|>node_modules/oauth20-provider/lib/model/index.js<|end_filename|>
module.exports = {
    accessToken:    require('./accessToken.js'),
    client:         require('./client.js'),
    code:           require('./code.js'),
    refreshToken:   require('./refreshToken.js'),
    user:           require('./user.js')
};
// @todo: find and remove unnecessary methods in all the models

<|start_filename|>node_modules/oauth20-provider/test/server/model/redis/index.js<|end_filename|>
module.exports = {
    oauth2:  require('./oauth2')
};

<|start_filename|>node_modules/oauth20-provider/lib/error/oauth2.js<|end_filename|>
var
    util = require('util');

var oauth2 = function (code, msg, status, constructor) {
    Error.call(this);
    Error.captureStackTrace(this, constructor || this.constructor);

    this.code = code;
    this.message = msg;
    this.status = status;
};
util.inherits(oauth2, Error);
oauth2.prototype.name = 'OAuth2AbstractError';
oauth2.prototype.logLevel = 'error';

module.exports = oauth2;

<|start_filename|>node_modules/oauth20-provider/test/server/model/redis/data.js<|end_filename|>
var async = require('async'),
    util = require('util'),
    redis = require('./redis.js'),
    data = require('./../data.js');

module.exports.initialize = function() {
    async.parallel([
        // Insert user
        function(cb) {
            var model = require('./oauth2/user.js');
            async.eachSeries(data.users, function(user, cb) {
                redis.set(util.format(model.KEY.USER, user.id), JSON.stringify(user), function(err) {
                    if (err) return cb(err);
                    redis.set(util.format(model.KEY.USER_USERNAME, user.username), user.id, cb)
                })
            }, cb);
        },
        // Insert client
        function(cb) {
            var model = require('./oauth2/client.js');
            async.eachSeries(data.clients, function(client, cb) {
                redis.set(util.format(model.KEY.CLIENT, client.id), JSON.stringify(client), cb);
            }, cb)
        }
    ], function(err) {
        if (err) throw new Error('Unable to fill redis with test data');
    });
};


<|start_filename|>node_modules/oauth20-provider/lib/error/invalidRequest.js<|end_filename|>
var
    util = require('util'),
    oauth2 = require('./oauth2.js');

var invalidRequest = function (msg) {
    invalidRequest.super_.call(this, 'invalid_request', msg, 400, this.constructor);
};
util.inherits(invalidRequest, oauth2);
invalidRequest.prototype.name = 'OAuth2InvalidRequest';
invalidRequest.prototype.logLevel = 'info';

module.exports = invalidRequest;

<|start_filename|>node_modules/oauth20-provider/lib/controller/authorization/decision.js<|end_filename|>
var error = require('./../../error');

/**
 * Decision controller
 * Used for: "authorization_code"
 * Page is used to ask user whether user agree or not to allow client to access his information with current scope
 * It should return a POST form with "decision" parameter:
 * 0 - if user does not allow client to obtain access
 * 1 - if user allows
 * For basic example look into ./test/server/oauth20.js
 *
 * @param req Request object
 * @param res Response object
 * @param client Client object
 * @param scope Scope asked
 * @param user User object
 */
module.exports = function(req, res, client, scope, user) {
    throw new error.serverError('Decision page is not implemented');
};

<|start_filename|>node_modules/oauth20-provider/test/server/model/rethinkdb/data.js<|end_filename|>
var async = require('async'),
    RethinkDb = require('rethinkdb'),
    connection = require('./connection.js'),
    config = require('./config.js'),
    data = require('./../data.js');

module.exports.initialize = function(cb) {

    var conn,
        tables;

    async.series([
        // Connection
        function(cb) {
            connection.acquire(function(err, c) {
                if (err) cb(err);
                else {
                    conn = c;
                    cb();
                }
            });
        },
        // Create DB
        function(cb) {
            RethinkDb.dbList().run(conn, function(err, dbList) {
                if (err)
                    cb(err);
                else if (dbList.indexOf(config.db) != -1)
                    cb();
                else
                    RethinkDb.dbCreate(config.db).run(conn, cb);
            });
        },
        // Get tables
        function(cb) {
            RethinkDb.db(config.db).tableList().run(conn, function(err, data) {
                if (err) cb(err);
                else {
                    tables = data;
                    cb();
                }
            });
        },
        // Create missing tables
        function(cb) {
            async.eachSeries([ 'access_token', 'refresh_token', 'authorization_code', 'client', 'user' ], function(t, cb) {
                if (tables.indexOf(t) != -1) return cb();

                RethinkDb.db(config.db).tableCreate(t, {}).run(conn, cb);
            }, cb);
        },
        // Replace users data
        function(cb) {
            async.eachSeries(data.users, function(obj, cb) {
                RethinkDb.table('user').get(obj.id).replace(obj).run(conn, cb);
            }, cb);
        },
        // Replace clients data
        function(cb) {
            async.eachSeries(data.clients, function(obj, cb) {
                RethinkDb.table('client').get(obj.id).replace(obj).run(conn, cb);
            }, cb);
        }
    ], cb);

};

if (require.main == module) {
    module.exports.initialize(function(err) {
        if (err) console.error(err);
        else {
            console.log('Data initialized');
            console.log('Closing connection to RethinkDB');
            connection.close(function(err) {
                if (err) console.error(err);
                else console.log('Finished');
            });
        }
    });
}

<|start_filename|>node_modules/oauth20-provider/lib/error/invalidScope.js<|end_filename|>
var
    util = require('util'),
    oauth2 = require('./oauth2.js');

var invalidScope = function (msg) {
    invalidScope.super_.call(this, 'invalid_scope', msg, 400, this.constructor);
};
util.inherits(invalidScope, oauth2);
invalidScope.prototype.name = 'OAuth2InvalidScope';
invalidScope.prototype.logLevel = 'info';

module.exports = invalidScope;