UNPKG

4.94 kBJavaScriptView Raw
1/*!
2 * Simple Node Mobile Portal
3 * Copyright(c) 2012 Faisal Kottarathil
4 * MIT Licensed
5 */
6/**
7 * Oauth middleware for `simpleportal.Server`
8 *
9 * @property oauth
10 * @for simpleportal
11 * @type {oauth}
12 * @static
13 */
14
15/**
16 * Oauth middleware for `simpleportal.Server`
17 *
18 * @class oauth
19 * @module middleware
20 * @static
21 */
22var oauth = module.exports = {};
23
24var util = require('./util');
25
26var SimplePortalOAuth = require('./wrapper/oauth').SimplePortalOAuth;
27var logger = require("./logger");
28
29var options ={};
30
31var defaultSimplePortalOAuth;
32
33/**
34 * To get the default oauth object
35 *
36 * @method getDefautlSimplePortalOAuth
37 * @return defaultSimplePortalOAuth
38 */
39oauth.getDefautlSimplePortalOAuth = function(){
40 return defaultSimplePortalOAuth;
41}
42
43/**
44 * To get a Simpleportal oauth object using the configuration defined in the app configuration
45 *
46 * @method getSimplePortalOAuth
47 * @return {SimplePortalOAuth} object defined with the provider key in the configuration
48 */
49oauth.getSimplePortalOAuth = function(oauthproviderkey){
50 var instance = this;
51
52 if(instance.oauthproviders&&instance.oauthproviders[oauthproviderkey])
53 return new SimplePortalOAuth(oauthproviderkey, instance.oauthproviders[oauthproviderkey]);
54 else
55 return null;
56}
57
58/**
59 * To send the response back according to the message from remote server
60 * @method sendResponse
61 *
62 * @param {} response http response
63 * @param {} error error if occured during the remote server connection
64 * @param {} results
65 * @private
66 */
67function sendResponse(response, error, results){
68 if(error){
69 response.send(200, {"Content-Type": "text/plain"}, JSON.stringify(error));
70 } else if(results.redirectUrl){
71 response.send(302, { 'Location': results.redirectUrl, "Content-Type": "text/plain", 'Cache-Control':'no-cache, no-store, max-age=0, must-revalidate'});
72 } else if(results.statusCode && results.statusCode == 401){
73 response.send(200, {"Content-Type": "text/plain"}, results.data);
74 } else if(results.status){
75 response.send(200, {"Content-Type": "text/plain"}, results.status);
76 } else{
77 response.send(200, {"Content-Type": "text/plain"}, results);
78 }
79}
80
81/**
82 * To initialize the router required for the `simpleportal.Server`
83 *
84 * @method initRouter
85 * @param {} router router object where the oauth handlers will be registered
86 * @param {callback} callback The callback to excecute when complete
87 */
88oauth.initRouter = function(router, callback){
89 logger.getInstance().info('Simple Portal - oauthloader : initRouter', 'Initializing oauth routers');
90
91 router.dispatch.addUrlHandlers({
92 '/oauth': {
93 '/login': function (request, response, next) {
94 if(oauth.getDefautlSimplePortalOAuth())
95 defaultSimplePortalOAuth.login(request, function(error, results){
96 sendResponse(response, error, results);
97 });
98 },
99 '/logout': function (request, response, next) {
100 if(oauth.getDefautlSimplePortalOAuth())
101 defaultSimplePortalOAuth.logout(request, function(){
102 var actionurl = request.headers['referer'];
103 if(!actionurl)
104 actionurl = '/'
105 response.send(302, {
106 'Location': actionurl,"Content-Type": "text/plain", 'Cache-Control':'no-cache, no-store, max-age=0, must-revalidate'
107 });
108 });
109 },
110 '/callback': function (request, response, next) {
111 if(oauth.getDefautlSimplePortalOAuth())
112 defaultSimplePortalOAuth.callback(request, function(error, results){
113 sendResponse(response, error, results);
114 });
115 },
116 '/status': function (request, response, next) {
117 if(oauth.getDefautlSimplePortalOAuth())
118 defaultSimplePortalOAuth.status(request, function(error, results){
119 sendResponse(response, error, results);
120 });
121 }
122 }
123 });
124
125 if(callback)
126 callback();
127}
128
129/**
130 * To initialize the oauth middleware
131 *
132 * @method init
133 * @param {} configuration Configuration for the middleware
134 * @param {callback} callback The callback to excecute when complete
135 */
136oauth.init = function(configuration, callback){
137 logger.getInstance().info('Simple Portal -oauth', 'Initializing Oauth service!!!');
138 var instance = this;
139 instance.oauthproviders={};
140
141 if(configuration && configuration.oauth){
142 options = configuration.oauth;
143
144 for(provider in options){
145 var localPort = configuration.port;
146
147 if(configuration.hidePort)
148 localPort = '80';
149
150 options[provider].localHost = util.constructUrl({host:configuration.host, port:localPort, secure:configuration.secure});
151 }
152
153 instance.oauthproviders[provider] = options[provider];
154
155 defaultSimplePortalOAuth = new SimplePortalOAuth(configuration.oauth.use, options[configuration.oauth.use]);
156 } else
157 logger.getInstance().info('Simple Portal -oauth', 'Oauth configuration is not done properly!!!');
158
159 if(callback)
160 callback();
161}