Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Mail.ru OAuth module for everyauth
var oauthModule = require('./oauth2')
, url = require('url')
, crypto = require('crypto');
var mailru = module.exports =
oauthModule.submodule('mailru')
.configurable({
display: 'set to "mobile" if you want users to see a mobile optimized version of the auth page',
scope: 'specify types of access: See http://api.mail.ru/docs/guides/restapi/#permissions'
})
.oauthHost('https://connect.mail.ru')
.apiHost('http://www.appsmail.ru/platform/api')
.entryPath('/auth/mailru')
.callbackPath('/auth/mailru/callback')
.authQueryParam('display', function () {
return this._display && this.display();
})
.authQueryParam('response_type', 'code')
.authQueryParam('scope', function () {
return this._scope && this.scope();
})
.accessTokenParam('grant_type', 'authorization_code')
.accessTokenHttpMethod('post')
.postAccessTokenParamsVia('data')
.accessTokenPath('/oauth/token')
.fetchOAuthUser( function (accessToken, http) {
var promise = this.Promise(),
extradata = http.extra;
if(typeof accessToken === 'undefined'){
if(typeof extradata.access_token === 'undefined') {
var strextra = JSON.stringify(extradata).replace(/^\{"(.*)":""\}$/,'"$1"'),
extra = eval('(' + JSON.parse(strextra) + ')');
accessToken = extra.access_token;
} else {
accessToken = extra.access_token;
}
}
var query = {
app_id: this.appId(),
method: 'users.getInfo',
secure:1,
session_key: accessToken
},
data = '',
fields = [];
for ( key in query ) {
data += key + '=' + query[key];
}
data += this.appSecret();
query.sig = crypto.createHash('md5').update(data).digest("hex");
for ( key in query ) {
fields.push(key + '=' + query[key]);
}
this.oauth.get(this.apiHost() + '?' + fields.join('&'), accessToken, function (err, data) {
if (err) {
if (err.error) {
promise.fail(err.error.error_msg)
}
else {
promise.fail(err.error_message);
}
}
var data = JSON.parse(data);
if (data.error) {
promise.fail(data.error.error_msg)
}
else {
var oauthUser = data[0];
promise.fulfill(oauthUser);
}
});
return promise;
})
.authCallbackDidErr(function(req) {
var parsedUrl = url.parse(req.url, true);
return parsedUrl.query && !!parsedUrl.query.error;
})
.convertErr( function (data) {
return new Error(JSON.parse(data).error.error_msg);
});
// First, register an app on mail.ru
everyauth.mailru
.appId('YOUR CONSUMER KEY HERE')
.appSecret('YOUR CONSUMER SECRET HERE')
.scope('messages')
.entryPath('/auth/mailru')
.callbackPath('/auth/mailru/callback')
.findOrCreateUser( function (session, accessToken, accessTokenExtra, mailruUser) {
// find or create user logic goes here
// Return a user or Promise that promises a user
// Promises are created via
// var promise = this.Promise();
// return promise;
})
.redirectPath('/');

Sannis commented Apr 12, 2012

Why not in everyauth repo? ;)

Owner

biggora commented Apr 21, 2012

in everyauth repo added pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment