Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mixer provider #314

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -16,3 +16,4 @@ config.json
coverage.*
lib-cov
complexity.md
yarn.lock
54 changes: 54 additions & 0 deletions examples/mixer.js
@@ -0,0 +1,54 @@
'use strict';

// Load modules

const Hapi = require('hapi');
const Hoek = require('hoek');
const Bell = require('../');


const server = new Hapi.Server();
server.connection({
port: 8000
});

server.register(Bell, (err) => {

Hoek.assert(!err, err);
server.auth.strategy('mixer', 'bell', {
provider: 'mixer',
password: 'cookie_encryption_password_secure',
isSecure: false,
/**
* you need to register your OAuth client here: https://mixer.com/lab/oauth
* for all scopes see https://dev.mixer.com/reference/oauth/index.html#oauth_scopes
*/
clientId: '',
clientSecret: ''
// Uncomment the below line for more scopes (check Mixer API documentation), "user:details:self" scope is set as default
// scope: ['user:details:self']
});

server.route({
method: ['GET', 'POST'],
path: '/bell/door',
config: {
auth: 'mixer',
handler: function (request, reply) {

if (!request.auth.isAuthenticated) {
return reply('Authentication failed due to: ' + request.auth.error.message);
}

reply('<pre>' + JSON.stringify(request.auth.credentials, null, 4) + '</pre>');

}
}
});

server.start((err) => {

Hoek.assert(!err, err);
console.log('Server started at:', server.info.uri);
});
});
3 changes: 2 additions & 1 deletion lib/providers/index.js
Expand Up @@ -35,5 +35,6 @@ exports = module.exports = {
twitter: require('./twitter'),
vk: require('./vk'),
wordpress: require('./wordpress'),
yahoo: require('./yahoo')
yahoo: require('./yahoo'),
mixer: require('./mixer')
};
32 changes: 32 additions & 0 deletions lib/providers/mixer.js
@@ -0,0 +1,32 @@
'use strict';

exports = module.exports = function (options) {

options = options || {};

const uri = options.uri || 'https://mixer.com';
const user = options.uri ? options.uri + '/api/v1/users/current' : 'https://mixer.com/api/v1/users/current';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably just be:

const user = uri + '/api/v1/users/current';


return {
protocol: 'oauth2',
auth: uri + '/oauth/authorize',
token: uri + '/api/v1/oauth/token',
scope: ['user:details:self'],
useParamsAuth: true,
headers: { 'User-Agent': 'hapi-bell-mixer' },
profile: function (credentials, params, get, callback) {

get(user, null, (profile) => {

credentials.profile = {
id: profile.id,
username: profile.username,
email: profile.email,
raw: profile
};

return callback();
});
}
};
};
164 changes: 164 additions & 0 deletions test/providers/mixer.js
@@ -0,0 +1,164 @@
'use strict';

// Load modules

const Bell = require('../../');
const Code = require('code');
const Hapi = require('hapi');
const Hoek = require('hoek');
const Lab = require('lab');
const Mock = require('../mock');

// Test shortcuts

const lab = exports.lab = Lab.script();
const describe = lab.describe;
const it = lab.it;
const expect = Code.expect;


describe('mixer', () => {

it('authenticates with moch', { parallel: false }, (done) => {

const mock = new Mock.V2();
mock.start((provider) => {

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

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

const custom = Bell.providers.mixer();
Hoek.merge(custom, provider);

const profile = {
id: '1234567890',
username: 'jerome724',
email: 'jerome724@example.com'
};

Mock.override('https://mixer.com/api/v1/users/current', profile);

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

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

reply(request.auth.credentials);
}
}
});

server.inject('/login', (res) => {

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

server.inject({ url: mockRes.headers.location, headers: { cookie } }, (response) => {

Mock.clear();
expect(response.result).to.equal({
provider: 'custom',
token: '456',
expiresIn: 3600,
refreshToken: undefined,
query: {},
profile: {
id: '1234567890',
username: 'jerome724',
email: 'jerome724@example.com',
raw: profile
}
});
mock.stop(done);
});
});
});
});
});
});

it('authenticates with moch and custom uri', { parallel: false }, (done) => {

const mock = new Mock.V2();
mock.start((provider) => {

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

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

const custom = Bell.providers.mixer({ uri: 'http://example.com' });
Hoek.merge(custom, provider);

const profile = {
id: '1234567890',
username: 'jerome724',
email: 'jerome724@example.com'
};

Mock.override('http://example.com/api/v1/users/current', profile);

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

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

reply(request.auth.credentials);
}
}
});

server.inject('/login', (res) => {

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

server.inject({ url: mockRes.headers.location, headers: { cookie } }, (response) => {

Mock.clear();
expect(response.result).to.equal({
provider: 'custom',
token: '456',
expiresIn: 3600,
refreshToken: undefined,
query: {},
profile: {
id: '1234567890',
username: 'jerome724',
email: 'jerome724@example.com',
raw: profile
}
});

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