1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var oauth = module.exports = {};
|
23 |
|
24 | var util = require('./util');
|
25 |
|
26 | var SimplePortalOAuth = require('./wrapper/oauth').SimplePortalOAuth;
|
27 | var logger = require("./logger");
|
28 |
|
29 | var options ={};
|
30 |
|
31 | var defaultSimplePortalOAuth;
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | oauth.getDefautlSimplePortalOAuth = function(){
|
40 | return defaultSimplePortalOAuth;
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | oauth.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 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | function 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 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | oauth.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 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | oauth.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 | }
|