Git Product home page Git Product logo

nsg-module-oauth's Introduction

Coldbox Module to allow OAuth Support

Currently this module only supports OAuth v1. I'm working on modularizing OAuth v2 and hopefully will release it soon.

Setup & Installation

###Here is an example handler that will inject the module service and handle login to twitter

component {

	property name="oauthService" inject="oauthV1Service@oauth";

	function index(event,rc,prc){
		var data = getSetting('twitter')['oauth'];

		if(!structKeyExists(session,'twitterOAuth')){
			session['twitterOAuth'] = structNew();
		}

		if( event.getValue('id','doNothing') == 'activateUser' ){
			var results = duplicate(session['twitterOAuth']);

			oauthService.init();
			oauthService.setRequestMethod('GET');
			oauthService.setConsumerKey(data['key']);
			oauthService.setConsumerSecret(data['secret']);
			oauthService.setAccessToken(session['twitterOAuth']['oauth_token']);
			oauthService.setAccessTokenSecret(session['twitterOAuth']['oauth_token_secret']);
			oauthService.setRequestURL('https://api.twitter.com/1.1/users/show.json');
			oauthService.addParam(name="user_id",value=results['referenceID']);
			var data = deserializeJSON(oauthService.send()['fileContent']);

			announceInterception( state='socialLoginSuccess', interceptData=results );

			location(url="/",addToken=false);

		}else if( event.valueExists('oauth_token') ){
			session['twitterOAuth']['oauth_token'] = event.getValue('oauth_token');
			session['twitterOAuth']['oauth_verifier'] = event.getValue('oauth_verifier');

			oauthService.init();
			oauthService.setConsumerKey(data['key']);
			oauthService.setConsumerSecret(data['secret']);
			oauthService.setRequestURL('https://api.twitter.com/oauth/access_token');
			oauthService.setRequestMethod('POST');
			oauthService.addParam(name="oauth_token",value=session['twitterOAuth']['oauth_token']);
			oauthService.addParam(name="oauth_verifier",value=session['twitterOAuth']['oauth_verifier']);

			var results = oauthService.send();

			var results = oauthService.send();
			if( results['status_code'] == 200 ){
				var myFields = listToArray(results['fileContent'],'&');

				for(var i=1;i<=arrayLen(myFields);i++){
					session['twitterOAuth'][listFirst(myFields[i],'=')] = listLast(myFields[i],'=');
				}
				setNextEvent('twitter/oauth/activateUser')
			}else{
				throw('Unknown OAuth Error');
			}

		}else{

			oauthService.init();
			oauthService.setConsumerKey(data['key']);
			oauthService.setConsumerSecret(data['secret']);
			oauthService.setRequestURL('https://api.twitter.com/oauth/request_token');
			oauthService.setRequestMethod('POST');

			oauthService.addParam(name="oauth_callback",value="#( cgi.server_port == 443 ? 'https' : 'http' )#://#cgi.http_host#/#event.getCurrentModule()#/oauth/");

			var results = oauthService.send();

			if( results['status_code'] == 200 ){
				var myFields = listToArray(results['fileContent'],'&');

				for(var i=1;i<=arrayLen(myFields);i++){
					session['twitterOAuth'][listFirst(myFields[i],'=')] = listLast(myFields[i],'=');
				}

				location(url="https://api.twitter.com/oauth/authorize?oauth_token=#session['twitterOAuth']['oauth_token']#",addToken=false);
			}else{
				throw('Unknown OAuth Error');
			}
		}
	}

}

###Changelog

v1.0.0 - current release Upcoming changes will include support for oauth V2 and additional modules for twitter, facebook, linkedin, pinterest, google, evernote, & trello to provide complete social login support.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.