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

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.
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 = {
@@ -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;// use this once cookies are working properly
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,
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,52 @@
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
+ console.log('is it there', req.cookieAuth != null);
32
+ await req.server.app.cache.set('session', cookieInfo,0);
33
+
34
+ req.cookieAuth.set(cookieInfo);
35
+
36
+ // set sid
37
+
21
38
  const sid = uuid.v4();
22
39
  credentials.sid = sid;
23
40
  if (options != null) {
24
41
  options.allAppEnv.LOGONPAYLOAD.token = credentials.token;
25
42
  options.allAppEnv.LOGONPAYLOAD.tokenType = 'bearer';
26
- options.userCache = {...credentials};
27
43
  debug(options.allAppEnv.LOGONPAYLOAD);
28
44
  }
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
- };
45
+
46
+
38
47
  debug('credentials query', credentials.query);
39
48
  let redirect = (credentials.query != null && credentials.query.next != null) ? credentials.query.next : null;
40
49
  debug('setcookie-redirect', redirect);
41
- return { status: true, error: null , redirect: redirect};
50
+ return { status: true, error: null, redirect: redirect };
42
51
  }
43
52
 
44
53
  export default setCookies;
@@ -54,23 +63,23 @@ async function getCredentials (req) {
54
63
  };
55
64
 
56
65
  let payload = {
57
- url : `${process.env.VIYA_SERVER}/SASLogon/oauth/token`,
58
- method: 'POST',
66
+ url : `${process.env.VIYA_SERVER}/SASLogon/oauth/token`,
67
+ method: 'POST',
59
68
 
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
- },
69
+ headers: {
70
+ // 'Authorization': 'Basic ' + Buffer.from(`${process.env.CLIENTID}:${process.env.CLIENTSECRET}`).toString('base64'),
71
+ 'Accept' : 'application/json',
72
+ 'Content-Type': 'application/x-www-form-urlencoded'
73
+ },
65
74
  data: qs.stringify({
66
75
  client_id : `${process.env.CLIENTID}`,
67
76
  client_secret: `${process.env.CLIENTSECRET}`,
68
77
  redirect_uri : `${location}`,
69
78
 
70
- 'grant_type': 'authorization_code',
71
- code : req.query.code
72
- })
73
- };
79
+ 'grant_type': 'authorization_code',
80
+ code : req.query.code
81
+ })
82
+ };
74
83
  try {
75
84
  let r = await axios(payload);
76
85
  return r.data;
@@ -26,7 +26,6 @@ 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");
@@ -34,27 +33,18 @@ 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
+
46
+ console.log("Auth Flow", options.authFlow);
47
+
58
48
  let getAppb = getApp.bind(
59
49
  null,
60
50
  options // process.env.USETOKEN === "YES" ? options : null
@@ -96,7 +86,8 @@ module.exports = function setDefaultRoutes(server, options) {
96
86
  path: `${appName}`,
97
87
 
98
88
  options: {
99
- auth: (process.env.USELOGON === 'YES') ? null : options.serverMode === "app" ? authLogon : authDefault,
89
+ // auth: (process.env.USELOGON === 'YES') ? null : options.serverMode === "app" ? authLogon : authDefault,
90
+ auth: authLogon,
100
91
  handler: getAppb,
101
92
  },
102
93
  },
@@ -130,15 +121,9 @@ module.exports = function setDefaultRoutes(server, options) {
130
121
  method: ["GET"],
131
122
  path: `${appName}/appenv`,
132
123
  options: {
133
- auth: /*authDefault*/ false,
134
- handler: async (req, h) => {
124
+ auth: authDefault,
125
+ handler: async (req, h) => {
135
126
  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
127
  allAppEnv.credentials = options.credentials;
143
128
 
144
129
  let s =
@@ -156,15 +141,9 @@ module.exports = function setDefaultRoutes(server, options) {
156
141
  method: ["GET"],
157
142
  path: `/appenv`,
158
143
  options: {
159
- auth: /*authDefault*/ false,
144
+ auth: authDefault,
160
145
  handler: async (req, h) => {
161
146
  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
147
  allAppEnv.credentials = options.credentials;
169
148
 
170
149
  let s =
@@ -174,27 +153,15 @@ module.exports = function setDefaultRoutes(server, options) {
174
153
  debug(options.allAppEnv);
175
154
 
176
155
  }
177
- debug(s)
156
+
178
157
  return s;
179
158
  },
180
159
  },
181
160
  },
182
- /*
183
- {
184
- method: ["GET"],
185
- path: `${appName}/{param*}`,
186
-
187
- options: {
188
- auth: authDefault,
189
- handler: getApp2,
190
- },
191
- },
192
- */
193
161
 
194
162
  {
195
163
  method: ["GET"],
196
- path: `/{param*}`,
197
-
164
+ path: `/assets/{param*}`,
198
165
  options: {
199
166
  auth: authDefault,
200
167
  handler: getApp2,
@@ -221,7 +188,9 @@ module.exports = function setDefaultRoutes(server, options) {
221
188
  let pr = {
222
189
  method: ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
223
190
  path: `${appName}/proxy/{param*}`,
191
+
224
192
  options: {
193
+ auth: authDefault,
225
194
  handler: {
226
195
  proxy: {
227
196
  mapUri: proxyMapUri,
@@ -234,11 +203,8 @@ module.exports = function setDefaultRoutes(server, options) {
234
203
  debug(pr);
235
204
  defaultTable.push(pr);
236
205
 
237
- let routeTables = uTable !== null ? defaultTable.concat(uTable) : defaultTable;
206
+ let routeTables0= uTable !== null ? defaultTable.concat(uTable) : defaultTable;
207
+ let routeTables = setupUserRoutes(routeTables0, options);
238
208
 
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
209
  server.route(routeTables);
244
210
  };
@@ -16,11 +16,10 @@
16
16
  *
17
17
  */
18
18
 
19
- let SASauth = require('./SASauth');
20
- let appCookie = require('./appCookie');
21
- let token = require('./token');
19
+ import SASAuth from './SASauth.js';
20
+ import appCookie from './appCookie.js';
22
21
  let setDefaultRoutes = require('./setDefaultRoutes');
23
- let log = require('debug')('auth');
22
+ let debug = require('debug')('auth');
24
23
 
25
24
 
26
25
  /** Notes:
@@ -29,21 +28,14 @@ let log = require('debug')('auth');
29
28
  */
30
29
  async function setupAuth (server, options){
31
30
 
32
- if (options.authFlow === 'server') {
33
- await server.register({plugin: SASauth, options: options});
34
- // await server.register({plugin: appCookie, options: options});
35
- await appCookie(server,options);
36
- /*
37
- let def = 'session';
38
- if (options.serverMode === 'api') {
39
- await server.register({ plugin: token });
40
- def = 'token';
41
- }
42
- log('***********************Default auth', def);
43
- server.auth.default(def);
44
- */
45
- // console.log(server.registerations);
46
- }
31
+ // register cookie and bell
32
+ await server.register(require('@hapi/cookie'));
33
+ await server.register(require('@hapi/bell'));
34
+
35
+ await appCookie(server, options);
36
+ await SASAuth(server, options);
37
+
38
+ // setup default routes now that we have auth strategies
47
39
  setDefaultRoutes(server, options);
48
40
  return true;
49
41
  };
@@ -40,7 +40,7 @@ function setupUserRoutes (u, options) {
40
40
  } else if (rx.options.auth === 'logon') {
41
41
  rx.options.auth = options.authLogon;
42
42
  }
43
-
43
+ console.log('route', rx.method, rx.path, rx.options.auth, rx.options.pre);
44
44
  return rx;
45
45
  });
46
46
  return routes;
package/src/readCerts.js CHANGED
@@ -1,33 +1,33 @@
1
- /**
2
- * Copyright © 2025, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
5
- import fs from 'fs';
6
- function getCerts(tlsdir) {
7
-
8
- if (tlsdir == null || tlsdir === 'NONE') {
9
- return null;
10
- }
11
-
12
- console.log(`[Note] Reading certs from directory: ` + tlsdir);
13
- if (fs.existsSync(tlsdir) === false) {
14
- console.error("[Warning] Specified cert dir does not exist: " + tlsdir);
15
- return null;
16
- }
17
-
18
- let listOfFiles = fs.readdirSync(tlsdir);
19
- console.log("[Note] TLS/SSL files found: " + listOfFiles);
20
- let options = {};
21
- for(let i=0; i < listOfFiles.length; i++) {
22
- let fname = listOfFiles[i];
23
- let name = tlsdir + '/' + listOfFiles[i];
24
- let key = fname.split('.')[0];
25
- console.log('Reading TLS file: ' + name + ' as key: ' + key);
26
- options[key] = fs.readFileSync(name, { encoding: 'utf8' });
27
- }
28
- console.log('cert files', Object.keys(options));
29
-
30
- return options;
31
-
32
- }
1
+ /**
2
+ * Copyright © 2025, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import fs from 'fs';
6
+ function getCerts(tlsdir) {
7
+
8
+ if (tlsdir == null || tlsdir === 'NONE') {
9
+ return null;
10
+ }
11
+
12
+ console.log(`[Note] Reading certs from directory: ` + tlsdir);
13
+ if (fs.existsSync(tlsdir) === false) {
14
+ console.error("[Warning] Specified cert dir does not exist: " + tlsdir);
15
+ return null;
16
+ }
17
+
18
+ let listOfFiles = fs.readdirSync(tlsdir);
19
+ console.log("[Note] TLS/SSL files found: " + listOfFiles);
20
+ let options = {};
21
+ for(let i=0; i < listOfFiles.length; i++) {
22
+ let fname = listOfFiles[i];
23
+ let name = tlsdir + '/' + listOfFiles[i];
24
+ let key = fname.split('.')[0];
25
+ console.log('Reading TLS file: ' + name + ' as key: ' + key);
26
+ options[key] = fs.readFileSync(name, { encoding: 'utf8' });
27
+ }
28
+ console.log('cert files', Object.keys(options));
29
+
30
+ return options;
31
+
32
+ }
33
33
  export default getCerts;