Skip to content

Commit

Permalink
add reddit as oauth provider and enable basic http authentication hea…
Browse files Browse the repository at this point in the history
…der as required for reddit oauth
  • Loading branch information
Charles Lawrence committed Apr 29, 2015
1 parent 746120a commit 7b54667
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ exports.v2 = function (settings) {
var requestOptions = {
payload: Querystring.stringify(query),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
// Authorization: 'Basic ' + + (new Buffer(settings.clientId + ':' + settings.clientSecret, 'utf8')).toString('base64')
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + (new Buffer(settings.clientId + ':' + settings.clientSecret, 'utf8')).toString('base64')
}
};

Expand Down
3 changes: 2 additions & 1 deletion lib/providers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ exports = module.exports = {
vk: require('./vk'),
yahoo: require('./yahoo'),
arcgisonline: require('./arcgisonline'),
phabricator: require('./phabricator')
phabricator: require('./phabricator'),
reddit: require('./reddit')
};
29 changes: 29 additions & 0 deletions lib/providers/reddit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Load modules


// Declare internals

var internals = {};


exports = module.exports = function (options) {
return {
protocol: 'oauth2',
auth: 'https://www.reddit.com/api/v1/authorize',
token: 'https://www.reddit.com/api/v1/access_token',
scope: ['identity'], // minimal scope to request
scopeSeparator: ' ',
headers: {'User-Agent': 'hapi-bell-reddit'},
profile: function (credentials, params, get, callback) {
var queryOptions = {
access_token: credentials.token,
client_id: this.clientId
};

get('https://oauth.reddit.com/api/v1/me', queryOptions, function (profile) {
credentials.profile = profile;
return callback();
});
}
};
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"github",
"foursquare",
"vk",
"arcgisonline"
"arcgisonline",
"reddit"
],
"engines": {
"node": ">=0.10.33"
Expand Down
79 changes: 79 additions & 0 deletions test/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,85 @@ var expect = Code.expect;

describe('Bell', function () {

describe('#reddit', function () {

it('authenticates with mock', { parallel: false }, function (done) {

var mock = new Mock.V2();
mock.start(function (provider) {

var server = new Hapi.Server();
server.connection({ host: 'localhost', port: 80 });
server.register(Bell, function (err) {

expect(err).to.not.exist();

var custom = Bell.providers.reddit();
Hoek.merge(custom, provider);

var profile = {
name: 'john',
created: 0,
created_utc: 0,
hide_from_robots: true,
gold_creddits: 0,
link_karma: 0,
comment_karma: 0,
over_18: true,
is_gold: false,
is_mod: true,
gold_expiration: null,
has_verified_email: true,
id: 'abcde',
inbox_count: 0
};

Mock.override('https://oauth.reddit.com/api/v1/me', profile);

server.auth.strategy('custom', 'bell', {
password: 'password',
isSecure: false,
clientId: 'reddit',
clientSecret: 'secret',
provider: custom
});

server.route({
method: '*',
path: '/login',
config: {
auth: 'custom',
handler: function (request, reply) {
reply(request.auth.credentials);
}
}
});

server.inject('/login', function (res) {

var cookie = res.headers['set-cookie'][0].split(';')[0] + ';';
mock.server.inject(res.headers.location, function (res) {

server.inject({ url: res.headers.location, headers: { cookie: cookie } }, function (res) {
expect(res.result).to.deep.equal({
provider: 'custom',
token: '456',
expiresIn: 3600,
refreshToken: undefined,
query: {},
profile: profile
});

Mock.clear();
mock.stop(done);
});
});
});
});
});
});
});

describe('#facebook', function () {

it('authenticates with mock', { parallel: false }, function (done) {
Expand Down

0 comments on commit 7b54667

Please sign in to comment.