<commit_before>/**
 * RenRen OAuth2 Lib
 * @author <NAME>
 */

// Local Require
var util = require('util'),
	OAuth2 = require('../support/oauth').OAuth2,
	ClientBase = require('./clientBase'),
	URL= require('url');

// export renrenClient
var renrenClient = module.exports = function renrenClient(appinfo, user) {
	ClientBase.call(this);
	this.name = 'renren';
	this.secret = appinfo.secret;
	
	// check token
	var	now = new Date();
	if(user){
		this.refresh = user.refresh;
		
		if( !user.expire || user.expire > now){
			this.token = user.token;
			this.expire = user.expire;
			return;
		}
	}
	// Create OAuth2 Object based on config
	this.oa2 = new OAuth2(
		appinfo.key, appinfo.secret,
		this.api.oauth_site, this.api.oauth_authorize, this.api.oauth_token /* oauth urls */
	);
	
};
util.inherits(renrenClient, ClientBase);

// Set API
renrenClient.prototype.api = require('./apiconfig').RenRenAPI;

/**
 * Authorization Part
 * @param {Request} req express request object
 * @param {Response} res express response object
 * @param {Function} callback Callback Function
 * @param {Object} context 'this' object of callback
 */
renrenClient.prototype.authorize = function (req, res, callback, context) {
	var self = this;
	// set access token callback
	var access_cbfunc = function(error, access_token, refresh_token, result) {
		//Finally call back
		if (error) return callback.call(context, error);
		
		// set expire date
		if(result['expires_in']){
			var expire_at = new Date();
			expire_at.setTime(expire_at.getTime()+result['expires_in']*1000);
		}
		
		// remove temp
		delete req.session.redirectURI;
		
		// store the access token in the session
		self.token = access_token;
		self.expire = expire_at;
		self.refresh = refresh_token;
		
		var user = { 
			platform: self.name, 
			token: access_token, 
			refresh: refresh_token, 
			expire: expire_at.getTime() };
		
		callback.call(context, null, user);
	};
	
	// if has token, directly return
	if(this.token){
		return callback.call(context, null, {
				platform: this.name, 
				token: this.token, 
				refresh: this.refresh, 
				expire: this.expire
			});
	}
	
	// if has refresh token
	if(this.refresh){
		this.oa2.refreshAccessToken(this.refresh,{
			grant_type: 'refresh_token',
		}, access_cbfunc );
	}else{ // start new round
		var redirectURI;
		
		// This is get access token round
		if(req.param("code")){
			redirectURI = req.session.redirectURI;
			this.oa2.getOAuthAccessToken(
				req.param("code"),
				{
					redirect_uri: redirectURI,
					grant_type: 'authorization_code',
				}, access_cbfunc);
		}else{ // This is authorize round
			// set redirect_uri
			redirectURI = req.session.redirectURI = "http://"+req.header('Host') + req.url;
			
			var authorizeUrl = this.oa2.getAuthorizeUrl({
					response_type: 'code',
					redirect_uri: redirectURI,
				});
			
			res.redirect(authorizeUrl);<commit_msg>fix renren Client bug. enable refresh token method<commit_after>/**
 * RenRen OAuth2 Lib
 * @author <NAME>
 */

// Local Require
var util = require('util'),
	OAuth2 = require('../support/oauth').OAuth2,
	ClientBase = require('./clientBase'),
	URL= require('url');

// export renrenClient
var renrenClient = module.exports = function renrenClient(appinfo, user) {
	ClientBase.call(this);
	this.name = 'renren';
	this.secret = appinfo.secret;
	
	// check token
	var	now = new Date();
	if(user){
		this.refresh = user.refresh;
		
		if( !user.expire || user.expire > now){
			this.token = user.token;
			this.expire = user.expire;
			return;
		}
	}
	// Create OAuth2 Object based on config
	this.oa2 = new OAuth2(
		appinfo.key, appinfo.secret,
		this.api.oauth_site, this.api.oauth_authorize, this.api.oauth_token /* oauth urls */
	);
	
};
util.inherits(renrenClient, ClientBase);

// Set API
renrenClient.prototype.api = require('./apiconfig').RenRenAPI;

/**
 * Authorization Part
 * @param {Request} req express request object
 * @param {Response} res express response object
 * @param {Function} callback Callback Function
 * @param {Object} context 'this' object of callback
 */
renrenClient.prototype.authorize = function (req, res, callback, context) {
	var self = this;
	// set access token callback
	var access_cbfunc = function(error, access_token, refresh_token, result) {
		//Finally call back
		if (error) return callback.call(context, error);
		
		// set expire date
		if(result['expires_in']){
			var expire_at = Date.now() + result['expires_in']*1000 ;
			var r_expire_at = Date.now() + result['expires_in']*1000*2 ;
		}
		
		// remove temp
		req.session.redirectURI && delete req.session.redirectURI;
		
		// store the access token in the session
		self.token = access_token;
		self.expire = expire_at;
		self.refresh = refresh_token;
		
		var user = { 
			platform: self.name, 
			token: access_token, 
			refresh: refresh_token, 
			expire: expire_at, 
			r_expire: r_expire_at };
		
		callback.call(context, null, user);
	};
	
	// if has token, directly return
	if(this.token){
		return callback.call(context, null, {
				platform: this.name, 
				token: this.token, 
				refresh: this.refresh, 
				expire: this.expire, 
				r_expire: this.r_expire
			});
	}
	
	// if has refresh token
	if(this.refresh && this.r_expire && Date.now() < this.r_expire){
		this.oa2.refreshAccessToken(this.refresh,{
			grant_type: 'refresh_token',
		}, access_cbfunc );
	}else{ // start new round
		var redirectURI;
		
		// This is get access token round
		if(req.param("code")){
			if(req.session.redirectURI){
				redirectURI = req.session.redirectURI;
			}else{
				var i = req.url.indexOf("?");
				redirectURI = "http://"+req.header('Host') + (i>=0 ? req.url.substring(0,i) : req.url);
			}
			this.oa2.getOAuthAccessToken(
				req.param("code"),
				{
					redirect_uri: redirectURI,
					grant_type: 'authorization_code',
				}, access_cbfunc);
		}else{ // This is authorize round
			// set redirect_uri
			redirectURI = req.session.redirectURI = "http://"+req.header('Host') + req.url;
			
			var authorizeUrl = this.oa2.getAuthorizeUrl({
					response_type: 'code',
					redirect_uri: redirectURI,
				});
			
			res.redirect(authorizeUrl);
{'fraction_non_alphanumeric': 0.13811074918566776, 'fraction_numerical': 0.005374592833876222, 'mean_word_length': 3.4532269760696157, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5353067', 'n_tokens_mistral': 2365, 'n_tokens_neox': 2043, 'n_words': 485}