@sassoftware/viya-serverjs 0.6.1-2 → 0.6.1-5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,12 +4,12 @@
4
4
  */
5
5
  'use strict';
6
6
 
7
- import setCookies from './setCookies';
7
+ import setCookies from '../plugins/setCookies';
8
8
  let debug = require('debug')('codeauth');
9
9
  async function codeAuth (req, h, options) {
10
- debug('in codeauth');
10
+ debug('calling setCookies in codeAuth');
11
11
  await setCookies(req, h, options);
12
- debug(options);
12
+
13
13
  let indexHTML = process.env.APPENTRY == null ? 'index.html' : process.env.APPENTRY;
14
14
  if (process.env.REDIRECT != null) {
15
15
  debug('using REDIRECT env variable', process.env.REDIRECT);
@@ -30,7 +30,6 @@ async function getApp (options, req, h) {
30
30
 
31
31
  debug('calling codeauth');
32
32
  let r = codeAuth(req, h, options);
33
- debug(options.userCache);
34
33
  return r;
35
34
 
36
35
  } else {
@@ -3,22 +3,17 @@
3
3
  * SPDX-License-Identifier: Apache-2.0
4
4
  */
5
5
 
6
- import setCookies from './setCookies';
6
+ // import setCookies from './setCookies';
7
+ import codeAuth from './codeAuth.js';
7
8
  let debug = require('debug')('logon');
8
9
  async function logon (req, h, options) {
9
10
  debugger;
10
- debug('.................................................in logon');
11
-
12
-
13
- let r = await setCookies(req, h, options);
11
+
12
+ debug('calling codeauth');
13
+ let r = codeAuth(req, h, options);
14
+ // r = await setCookies(req, h, options);
14
15
  debug(r.redirect);
15
- debug('in logon after setcookie', r.redirect);
16
16
  return h.redirect(r.redirect);
17
17
  }
18
18
 
19
-
20
-
21
-
22
-
23
-
24
19
  export default logon;
@@ -2,6 +2,8 @@ let debug = require('debug')('proxyMapUri');
2
2
  async function proxyMapUri (req) {
3
3
  let credentials = req.auth.credentials;
4
4
  debug('------------------------------------------');
5
+ debug('serverstate', req.server.state );
6
+ debug('state', req.state);
5
7
  if (credentials != null) {
6
8
  let sid = credentials.sid;
7
9
  debug('sid=', sid);
package/src/iService.js CHANGED
@@ -112,10 +112,10 @@ function iService (userRouteTable, useDefault, asset, allAppEnv, serverMode, use
112
112
  let hapiServer = Hapi.server(sConfig);
113
113
 
114
114
  /*
115
- const cache = hapiServer.cache({ segment: 'sessions', expiresIn: 3 * 24 * 60 * 60 * 1000 });
115
+ const cache = hapiServer.cache({ segment: 'sid', expiresIn: 3 * 24 * 60 * 60 * 1000 });
116
116
  hapiServer.app.cache = cache;
117
- */
118
-
117
+ */
118
+
119
119
  let nodeCacheOptions = {
120
120
  stdTTL : 24*60*60*1000,
121
121
  checkPeriod : 3600,
@@ -125,6 +125,7 @@ function iService (userRouteTable, useDefault, asset, allAppEnv, serverMode, use
125
125
  };
126
126
  let storeCache = new NodeCache(nodeCacheOptions);
127
127
  hapiServer.app.cache = storeCache;
128
+
128
129
 
129
130
  // common plugins
130
131
  let visionOptions = {
@@ -187,7 +188,7 @@ function iService (userRouteTable, useDefault, asset, allAppEnv, serverMode, use
187
188
  });
188
189
  }
189
190
  }
190
- console.log('Plugin', process.env.PLUGIN);
191
+
191
192
 
192
193
  //
193
194
  // Start server
@@ -16,64 +16,69 @@
16
16
  *
17
17
  */
18
18
 
19
-
20
- let bell = require('@hapi/bell');
21
- let uuid = require('uuid');
19
+ let uuid = require('uuid');
22
20
  let debug = require('debug')('sasauth');
23
21
 
24
- exports.plugin = {
25
- name : 'SASauth',
26
- version : '1.0.0',
27
- register: iSASauth
28
- };
22
+ async function SASauth(server, options) {
23
+ debug('in iSASauth');
24
+ debug('options', options);
29
25
 
30
- async function iSASauth (server, options) {
31
- debug('in iSASauth');
32
- debug('options', options);
33
- let bellAuthOptions;
34
- let provider;
35
- // test for k8s deployment
36
- let host = options.host + '/SASLogon';
37
-
26
+ let provider;
27
+ // test for k8s deployment
28
+ let host = options.host + '/SASLogon';
29
+ if (options.ns != null) {
30
+ host = `https://sas-logon-app.${options.ns}.svc.cluster.local`;
31
+ } else if (options.nsHost != null) {
32
+ host = options.nsHost;
33
+ }
34
+ // ...
35
+ debug(host);
36
+ provider = {
37
+ name: 'sas',
38
+ protocol: 'oauth2',
39
+ useParamsAuth: false,
40
+ auth: host + '/oauth/authorize',
41
+ token: host + '/oauth/token',
38
42
 
39
- if (options.ns != null) {
40
- host = `https://sas-logon-app.${options.ns}.svc.cluster.local`;
41
- } else if (options.nsHost != null) {
42
- host = options.nsHost;
43
- }
44
- // ...
45
- debug(host);
46
- provider = {
47
- name : 'sas',
48
- protocol : 'oauth2',
49
- useParamsAuth: false,
50
- auth : host + '/oauth/authorize',
51
- token : host + '/oauth/token',
43
+ profileMethod: 'get',
52
44
 
53
- profileMethod: 'get',
54
-
55
- profile: async function (credentials, params, get) {
56
-
57
- server.log('SASAuth profile', credentials);
58
- debug('credentials', credentials);
45
+ profile: async function (credentials, params, get) {
46
+ server.log('SASAuth profile', credentials);
47
+ debug('credentials', credentials);
48
+ debug('params', params);
49
+ credentials.profile = {
50
+ provider: 'sas',
51
+ id: 'sasuser',
52
+ displayName: 'SAS User',
53
+ email: 'sasuser@sas.com',
54
+ raw: {
55
+ id: 'sasuser',
56
+ displayName: 'SAS User',
57
+ emails: [
58
+ {
59
+ value: 'sasuser@sas.com'
60
+ }
61
+ ]
59
62
  }
60
-
61
-
62
- };
63
-
64
- bellAuthOptions = {
65
- provider : provider,
66
- password : uuid.v4(),
67
- clientId : options.clientId,
68
- clientSecret: options.clientSecret,
69
- // isSameSite : options.isSameSite,
70
- isSecure : options.isSecure
71
- };
72
- // console.log('SASAuth options', bellAuthOptions);
73
- debug('belloptions', bellAuthOptions);
74
- server.log('SASAuth',bellAuthOptions);
75
- await server.register(bell);
76
- server.auth.strategy('sas', 'bell', bellAuthOptions);
77
-
63
+ };
78
64
  }
79
-
65
+
66
+
67
+ };
68
+
69
+ let bellAuthOptions = {
70
+ provider: provider,
71
+ password: uuid.v4(),
72
+ clientId: options.clientId,
73
+ clientSecret: options.clientSecret,
74
+ isSameSite : options.isSameSite,
75
+ isSecure: options.isSecure
76
+ };
77
+
78
+ debug('belloptions', bellAuthOptions);
79
+
80
+ server.auth.strategy('sas', 'bell', bellAuthOptions);
81
+
82
+ }
83
+
84
+ export default SASauth;
@@ -1,49 +1,35 @@
1
1
 
2
2
  let uuid = require('uuid');
3
- let debug = require('debug')('cookie');
3
+ let debug = require('debug')('appcookie');
4
4
 
5
- module.exports = async function appCookie (server, options){
5
+ async function appCookie(server, options) {
6
6
 
7
- await server.register(require('@hapi/cookie'));
8
-
9
- debug('in appCookie');
10
- debug(options.redirectTo);
11
- let cookieOptions = {
12
- cookie: {
13
- name : 'cookie',
14
- password : uuid.v4(),
15
- isSecure : options.isSecure,
16
- isSameSite: options.isSameSite
17
- },
18
- redirectTo : options.redirectTo,
19
- appendNext : {name: 'next'},
20
- validate : async (req, session) => {
21
- debug('Cookie validateFunc', `path - ${req.path}`);
22
-
23
- if (session == null) {
24
- console.log('session is null');
25
- return {isValid: false};
26
- }
27
- let credentials = null;
28
- let sid;
29
- if (Array.isArray(session) === true && session.length > 0) {
30
- sid = session[0].sid;
31
- } else {
32
- sid = session.sid;
33
- }
34
- if (sid != null) {
35
- credentials = await req.server.app.cache.get(sid);
36
- }
37
-
38
- if (credentials == null) {
39
- return {isValid: false};
40
- }
41
- debug('Cookie validateFunc', sid);
42
- return {isValid: true, credentials: credentials};
43
- }
44
- };
45
- // console.log('cookie options', cookieOptions);
46
- debug('Cookie Options',cookieOptions);
47
- server.auth.strategy('session', 'cookie', cookieOptions);
7
+ debug('in appCookie');
8
+ debug(options.redirectTo);
9
+ let cookieOptions = {
10
+ cookie: {
11
+ name: 'session',
12
+ password: uuid.v4(),
13
+ isSecure: options.isSecure,
14
+ isSameSite: options.isSameSite
15
+ },
16
+ redirectTo: options.redirectTo,
17
+ appendNext: { name: 'next' },
18
+ validate: async (req, session) => {
19
+ debug('validating cookie session', session);
20
+ if (!session) {
21
+ return { isValid: false };
22
+ }
23
+ return {
24
+ isValid: true,
25
+ credentials: session // becomes request.auth.credentials
26
+ };
27
+ }
28
+ };
29
+ debug('session cookie options', cookieOptions);
30
+
31
+ server.auth.strategy('session', 'cookie', cookieOptions);
32
+ server.auth.default('session');
48
33
 
49
34
  };
35
+ export default appCookie;
@@ -16,20 +16,31 @@
16
16
  *
17
17
  */
18
18
 
19
- let debug = require('debug')('context');
20
- async function setContext (req,h){
21
- let credentials = req.auth.credentials
22
- debug(credentials);
23
- let context = {
24
- path : req.path,
25
- params : req.params,
26
- query : req.query,
19
+
20
+ let debug = require('debug')('setcontext');
21
+ async function setContext(req, h) {
22
+ let credentials = req.auth.credentials;
23
+ let cachedCredentials = null;
24
+ debug('Set Context Credentials', req.path, credentials);
25
+ try {
26
+ cachedCredentials = await req.server.app.cache.get('session');
27
+ debug('Cached Credentials', cachedCredentials);
28
+ } catch (e) {
29
+ debug('No cached credentials');
30
+ }
31
+
32
+ let fcredentials = credentials || cachedCredentials;
33
+
34
+ let context = {
35
+ path: req.path,
36
+ params: req.params,
37
+ query: req.query,
27
38
  payload: req.payload,
28
- queryOrig: (credentials != null) ? credentials.query : {},
29
- token : (credentials != null) ? `bearer ${credentials.token}` : null,
30
- credentials: credentials,
31
- host : process.env.VIYA_SERVER
32
- };
39
+ queryOrig: (fcredentials != null) ? fcredentials.query : {},
40
+ credentials: {...fcredentials, host: process.env.VIYA_SERVER},
41
+ credType: (credentials != null) ? 'auth' : 'cached',
42
+ host: process.env.VIYA_SERVER
43
+ };
33
44
  return context;
34
45
  }
35
46
  export default setContext;
@@ -2,43 +2,51 @@
2
2
  * Copyright © 2019, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
3
3
  * SPDX-License-Identifier: Apache-2.0
4
4
  */
5
- let uuid = require('uuid');
5
+ let uuid = require('uuid');
6
6
  let debug = require('debug')('setcookies');
7
7
 
8
- async function setCookies (req, h, options) {
8
+ async function setCookies(req, h, options) {
9
9
  let credentials = req.auth.credentials;
10
-
11
- debug('setcookie', credentials);
12
-
10
+
11
+ // protect against failed logon
13
12
  if (credentials != null && req.auth.error != null) {
14
13
  debug('setcookie credentials', credentials);
15
14
  debug('setcookie error', req.auth.error);
16
15
  debug('logon failed');
17
16
  return { status: false, error: req.auth.error };
18
17
  }
19
-
20
- // create a cookie(sid) and save credentials in cache
18
+
19
+ debug('credentials in setcookie', credentials);
20
+
21
+ // use cookieAuth to set cookies
22
+
23
+ let cookieInfo = {
24
+ name: 'session',
25
+ accessToken: credentials.token,
26
+ refreshToken: credentials.refreshToken,
27
+ expiresIn: credentials.expiresIn,
28
+ provider: credentials.provider,
29
+ }
30
+ debug('------------set cookie-------------\n', cookieInfo);
31
+ await req.server.app.cache.set('session', cookieInfo,0);
32
+
33
+ req.cookieAuth.set(cookieInfo);
34
+
35
+ // set sid
36
+
21
37
  const sid = uuid.v4();
22
38
  credentials.sid = sid;
23
39
  if (options != null) {
24
40
  options.allAppEnv.LOGONPAYLOAD.token = credentials.token;
25
41
  options.allAppEnv.LOGONPAYLOAD.tokenType = 'bearer';
26
- options.userCache = {...credentials};
27
42
  debug(options.allAppEnv.LOGONPAYLOAD);
28
43
  }
29
- debug('userCache', options.userCache);
30
-
31
- await req.server.app.cache.set(sid, credentials, 0);
32
- // Can we get away without setting cookie for this session?
33
- // Need to also modify keepAlive
34
- if (process.env.COOKIES !== 'NO') {
35
- debugger;
36
- req.cookieAuth.set({ sid });
37
- };
44
+
45
+
38
46
  debug('credentials query', credentials.query);
39
47
  let redirect = (credentials.query != null && credentials.query.next != null) ? credentials.query.next : null;
40
48
  debug('setcookie-redirect', redirect);
41
- return { status: true, error: null , redirect: redirect};
49
+ return { status: true, error: null, redirect: redirect };
42
50
  }
43
51
 
44
52
  export default setCookies;
@@ -54,23 +62,23 @@ async function getCredentials (req) {
54
62
  };
55
63
 
56
64
  let payload = {
57
- url : `${process.env.VIYA_SERVER}/SASLogon/oauth/token`,
58
- method: 'POST',
65
+ url : `${process.env.VIYA_SERVER}/SASLogon/oauth/token`,
66
+ method: 'POST',
59
67
 
60
- headers: {
61
- // 'Authorization': 'Basic ' + Buffer.from(`${process.env.CLIENTID}:${process.env.CLIENTSECRET}`).toString('base64'),
62
- 'Accept' : 'application/json',
63
- 'Content-Type': 'application/x-www-form-urlencoded'
64
- },
68
+ headers: {
69
+ // 'Authorization': 'Basic ' + Buffer.from(`${process.env.CLIENTID}:${process.env.CLIENTSECRET}`).toString('base64'),
70
+ 'Accept' : 'application/json',
71
+ 'Content-Type': 'application/x-www-form-urlencoded'
72
+ },
65
73
  data: qs.stringify({
66
74
  client_id : `${process.env.CLIENTID}`,
67
75
  client_secret: `${process.env.CLIENTSECRET}`,
68
76
  redirect_uri : `${location}`,
69
77
 
70
- 'grant_type': 'authorization_code',
71
- code : req.query.code
72
- })
73
- };
78
+ 'grant_type': 'authorization_code',
79
+ code : req.query.code
80
+ })
81
+ };
74
82
  try {
75
83
  let r = await axios(payload);
76
84
  return r.data;
@@ -26,35 +26,23 @@ import {
26
26
  logout,
27
27
  logon,
28
28
  setupUserRoutes,
29
- reactDev,
30
29
  proxyMapUri,
31
30
  } from "../handlers";
32
31
  let debug = require("debug")("routes");
33
- import setContext from "./setContext.js";
32
+ //import setContext from "./setContext.js";
34
33
  module.exports = function setDefaultRoutes(server, options) {
35
34
  debug("setDefaultRoutes");
36
35
  let appName = "/" + options.appName;
37
- let authDefault = false;
38
- let authLogon = false;
39
- if (options.authFlow === "server") {
40
- /*
41
- authDefault =
42
- options.serverMode === "app"
43
- ? false
44
- : {
45
- strategies: ["token", "session"],
46
- mode: "required",
47
- };
48
- */
49
- authDefault = {
50
- strategy: "session",
51
- mode: "try",
52
- };
53
- authLogon = {
54
- mode: "required",
55
- strategy: "sas",
56
- };
57
- }
36
+
37
+ let authDefault = {
38
+ strategy: "session",
39
+ mode: "try",
40
+ };
41
+ let authLogon = {
42
+ strategy: "sas",
43
+ mode: "required"
44
+ };
45
+
58
46
  let getAppb = getApp.bind(
59
47
  null,
60
48
  options // process.env.USETOKEN === "YES" ? options : null
@@ -66,10 +54,6 @@ module.exports = function setDefaultRoutes(server, options) {
66
54
  options.authLogon = authLogon;
67
55
 
68
56
  debug(options.userRouteTable);
69
- let uTable =
70
- options.userRouteTable !== null
71
- ? setupUserRoutes(options.userRouteTable, options)
72
- : null;
73
57
 
74
58
  let defaultTable = [
75
59
  {
@@ -96,7 +80,8 @@ module.exports = function setDefaultRoutes(server, options) {
96
80
  path: `${appName}`,
97
81
 
98
82
  options: {
99
- auth: (process.env.USELOGON === 'YES') ? null : options.serverMode === "app" ? authLogon : authDefault,
83
+ // auth: (process.env.USELOGON === 'YES') ? null : options.serverMode === "app" ? authLogon : authDefault,
84
+ auth: authLogon,
100
85
  handler: getAppb,
101
86
  },
102
87
  },
@@ -130,23 +115,14 @@ module.exports = function setDefaultRoutes(server, options) {
130
115
  method: ["GET"],
131
116
  path: `${appName}/appenv`,
132
117
  options: {
133
- auth: /*authDefault*/ false,
118
+ auth: authDefault,
134
119
  handler: async (req, h) => {
135
120
  let allAppEnv = options.allAppEnv;
136
- if (options.userInfo != null) {
137
- let uappenv = options.userInfo("APPENV", options);
138
- if (uappenv != null) {
139
- allAppEnv.APPENV = { ...allAppEnv.APPENV, ...uappenv };
140
- }
141
- }
142
121
  allAppEnv.credentials = options.credentials;
143
122
 
144
123
  let s =
145
124
  `let LOGONPAYLOAD = ${JSON.stringify(allAppEnv.LOGONPAYLOAD)};` +
146
125
  `let APPENV = ${JSON.stringify(allAppEnv.APPENV)};`;
147
- if (process.env.SHOWENV != null) {
148
- console.log(s);
149
- }
150
126
  debug(s);
151
127
  return s;
152
128
  },
@@ -156,45 +132,22 @@ module.exports = function setDefaultRoutes(server, options) {
156
132
  method: ["GET"],
157
133
  path: `/appenv`,
158
134
  options: {
159
- auth: /*authDefault*/ false,
135
+ auth: authDefault,
160
136
  handler: async (req, h) => {
161
137
  let allAppEnv = options.allAppEnv;
162
- if (options.userInfo != null) {
163
- let uappenv = options.userInfo("APPENV", options);
164
- if (uappenv != null) {
165
- allAppEnv.APPENV = { ...allAppEnv.APPENV, ...uappenv };
166
- }
167
- }
168
138
  allAppEnv.credentials = options.credentials;
169
139
 
170
140
  let s =
171
141
  `let LOGONPAYLOAD = ${JSON.stringify(allAppEnv.LOGONPAYLOAD)};` +
172
142
  `let APPENV = ${JSON.stringify(allAppEnv.APPENV)};`;
173
- if (process.env.SHOWENV != null) {
174
- debug(options.allAppEnv);
175
-
176
- }
177
- debug(s)
178
143
  return s;
179
144
  },
180
145
  },
181
146
  },
182
- /*
183
- {
184
- method: ["GET"],
185
- path: `${appName}/{param*}`,
186
-
187
- options: {
188
- auth: authDefault,
189
- handler: getApp2,
190
- },
191
- },
192
- */
193
147
 
194
148
  {
195
149
  method: ["GET"],
196
- path: `/{param*}`,
197
-
150
+ path: `/assets/{param*}`,
198
151
  options: {
199
152
  auth: authDefault,
200
153
  handler: getApp2,
@@ -216,29 +169,25 @@ module.exports = function setDefaultRoutes(server, options) {
216
169
  handler: keepAlive2,
217
170
  },
218
171
  },
219
- ];
220
-
221
- let pr = {
222
- method: ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
223
- path: `${appName}/proxy/{param*}`,
224
- options: {
225
- handler: {
226
- proxy: {
227
- mapUri: proxyMapUri,
228
- xforward: true,
229
- passThrough: true,
172
+ {
173
+ method: ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
174
+ path: `${appName}/proxy/{param*}`,
175
+ options: {
176
+ auth: authDefault,
177
+ handler: {
178
+ proxy: {
179
+ mapUri: proxyMapUri,
180
+ xforward: true,
181
+ passThrough: true,
182
+ },
230
183
  },
231
184
  },
232
- },
233
- };
234
- debug(pr);
235
- defaultTable.push(pr);
185
+ }
186
+ ];
236
187
 
237
- let routeTables = uTable !== null ? defaultTable.concat(uTable) : defaultTable;
188
+ let uTable = options.userRouteTable();
189
+ let routeTables0 = options.userRouteTable !== null ? defaultTable.concat(uTable) : defaultTable;
190
+ let routeTables = setupUserRoutes(routeTables0, options);
238
191
 
239
- routeTables.forEach((r) => {
240
- r.options.pre = [{ method: setContext, assign: 'context' }];
241
- console.log, ('Setting pre for route', r.path, r.options.pre);
242
- });
243
192
  server.route(routeTables);
244
193
  };