@sassoftware/viya-serverjs 0.6.1-1 → 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.
@@ -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,14 @@
3
3
  * SPDX-License-Identifier: Apache-2.0
4
4
  */
5
5
 
6
- import setCookies from './setCookies';
6
+ import setCookies from '../plugins/setCookies';
7
7
  let debug = require('debug')('logon');
8
8
  async function logon (req, h, options) {
9
9
  debugger;
10
- debug('.................................................in logon');
11
-
12
-
13
10
  let r = await setCookies(req, h, options);
14
11
  debug(r.redirect);
15
12
  debug('in logon after setcookie', r.redirect);
16
13
  return h.redirect(r.redirect);
17
14
  }
18
15
 
19
-
20
-
21
-
22
-
23
-
24
16
  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 = {
@@ -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,22 +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
- authDefault =
41
- options.serverMode === "app"
42
- ? false
43
- : {
44
- strategies: ["token", "session"],
45
- mode: "required",
46
- };
47
36
 
48
- authLogon = {
49
- mode: "required",
50
- strategy: "sas",
51
- };
52
- }
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
+
53
48
  let getAppb = getApp.bind(
54
49
  null,
55
50
  options // process.env.USETOKEN === "YES" ? options : null
@@ -91,31 +86,12 @@ module.exports = function setDefaultRoutes(server, options) {
91
86
  path: `${appName}`,
92
87
 
93
88
  options: {
94
- 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,
95
91
  handler: getAppb,
96
92
  },
97
93
  },
98
94
 
99
- {
100
- method: ["GET"],
101
- path: `${appName}/api`,
102
- options: {
103
- auth: authDefault,
104
- handler: async (req, h) => {
105
- return h.redirect(`${appName}/documentation`);
106
- },
107
- },
108
- },
109
- {
110
- method: ["GET"],
111
- path: `/develop`,
112
- options: {
113
- auth: false,
114
- cors: true,
115
- handler: reactDev,
116
- },
117
- },
118
-
119
95
  {
120
96
  method: ["GET"],
121
97
  path: `${appName}/callback`,
@@ -145,15 +121,9 @@ module.exports = function setDefaultRoutes(server, options) {
145
121
  method: ["GET"],
146
122
  path: `${appName}/appenv`,
147
123
  options: {
148
- auth: /*authDefault*/ false,
149
- handler: async (req, h) => {
124
+ auth: authDefault,
125
+ handler: async (req, h) => {
150
126
  let allAppEnv = options.allAppEnv;
151
- if (options.userInfo != null) {
152
- let uappenv = options.userInfo("APPENV", options);
153
- if (uappenv != null) {
154
- allAppEnv.APPENV = { ...allAppEnv.APPENV, ...uappenv };
155
- }
156
- }
157
127
  allAppEnv.credentials = options.credentials;
158
128
 
159
129
  let s =
@@ -171,15 +141,9 @@ module.exports = function setDefaultRoutes(server, options) {
171
141
  method: ["GET"],
172
142
  path: `/appenv`,
173
143
  options: {
174
- auth: /*authDefault*/ false,
144
+ auth: authDefault,
175
145
  handler: async (req, h) => {
176
146
  let allAppEnv = options.allAppEnv;
177
- if (options.userInfo != null) {
178
- let uappenv = options.userInfo("APPENV", options);
179
- if (uappenv != null) {
180
- allAppEnv.APPENV = { ...allAppEnv.APPENV, ...uappenv };
181
- }
182
- }
183
147
  allAppEnv.credentials = options.credentials;
184
148
 
185
149
  let s =
@@ -189,27 +153,15 @@ module.exports = function setDefaultRoutes(server, options) {
189
153
  debug(options.allAppEnv);
190
154
 
191
155
  }
192
- debug(s)
156
+
193
157
  return s;
194
158
  },
195
159
  },
196
160
  },
197
- /*
198
- {
199
- method: ["GET"],
200
- path: `${appName}/{param*}`,
201
-
202
- options: {
203
- auth: authDefault,
204
- handler: getApp2,
205
- },
206
- },
207
- */
208
161
 
209
162
  {
210
163
  method: ["GET"],
211
- path: `/{param*}`,
212
-
164
+ path: `/assets/{param*}`,
213
165
  options: {
214
166
  auth: authDefault,
215
167
  handler: getApp2,
@@ -236,7 +188,9 @@ module.exports = function setDefaultRoutes(server, options) {
236
188
  let pr = {
237
189
  method: ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
238
190
  path: `${appName}/proxy/{param*}`,
191
+
239
192
  options: {
193
+ auth: authDefault,
240
194
  handler: {
241
195
  proxy: {
242
196
  mapUri: proxyMapUri,
@@ -249,11 +203,8 @@ module.exports = function setDefaultRoutes(server, options) {
249
203
  debug(pr);
250
204
  defaultTable.push(pr);
251
205
 
252
- let routeTables = uTable !== null ? defaultTable.concat(uTable) : defaultTable;
206
+ let routeTables0= uTable !== null ? defaultTable.concat(uTable) : defaultTable;
207
+ let routeTables = setupUserRoutes(routeTables0, options);
253
208
 
254
- routeTables.forEach((r) => {
255
- r.options.pre = [{ method: setContext, assign: 'context' }];
256
- console.log, ('Setting pre for route', r.path, r.options.pre);
257
- });
258
209
  server.route(routeTables);
259
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,20 +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);
31
+ // register cookie and bell
32
+ await server.register(require('@hapi/cookie'));
33
+ await server.register(require('@hapi/bell'));
36
34
 
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
- // console.log(server.registerations);
45
- }
35
+ await appCookie(server, options);
36
+ await SASAuth(server, options);
37
+
38
+ // setup default routes now that we have auth strategies
46
39
  setDefaultRoutes(server, options);
47
40
  return true;
48
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;