UNPKG

4.74 kBJavaScriptView Raw
1/*
2
3 ----------------------------------------------------------------------------
4 | qewd-ripple: QEWD-based Middle Tier for Ripple OSI |
5 | |
6 | Copyright (c) 2016-17 Ripple Foundation Community Interest Company |
7 | All rights reserved. |
8 | |
9 | http://rippleosi.org |
10 | Email: code.custodian@rippleosi.org |
11 | |
12 | Author: Rob Tweed, M/Gateway Developments Ltd |
13 | |
14 | Licensed under the Apache License, Version 2.0 (the "License"); |
15 | you may not use this file except in compliance with the License. |
16 | You may obtain a copy of the License at |
17 | |
18 | http://www.apache.org/licenses/LICENSE-2.0 |
19 | |
20 | Unless required by applicable law or agreed to in writing, software |
21 | distributed under the License is distributed on an "AS IS" BASIS, |
22 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
23 | See the License for the specific language governing permissions and |
24 | limitations under the License. |
25 ----------------------------------------------------------------------------
26
27 10 March 2017
28
29*/
30
31var createSession = require('../sessions/create');
32var authenticate = require('../sessions/authenticate');
33
34/*
35function authenticate(messageObj) {
36 var cookie = messageObj.headers.cookie;
37 if (!cookie) return {error: true};
38
39 var pieces = cookie.split(';');
40 var token;
41 pieces.forEach(function(piece) {
42 if (piece.indexOf('JSESSIONID') !== -1) {
43 token = piece.split('JSESSIONID=')[1];
44 }
45 });
46
47 // this logic needs to change / be adapted once proper identity management added
48
49
50 if (!token) {error: true};
51 console.log('token = ' + token);
52 var status = this.sessions.authenticate(token);
53 //console.log('status: ' + JSON.stringify(status));
54 return status;
55}
56*/
57
58
59function initialise(messageObj, finished) {
60
61 var status = authenticate.call(this, messageObj);
62 var session;
63 var mode = this.userDefined.rippleMode; // "secure" or "demo"
64
65
66 if (status.error) {
67 // no session yet established for client
68 // or previous session expired
69
70 // if secure mode, signal the browser to redirect to Auth0
71 // a new QEWD session will be created by the redirect URL
72
73 if (mode === 'secure') {
74 var auth0 = this.userDefined.auth0;
75 var connections = auth0.connections || ['Username-Password-Authentication', 'google-oauth2', 'twitter'];
76 var config = {
77 domain: auth0.domain,
78 clientID: auth0.clientID,
79 callbackURL: auth0.callbackURL,
80 responseType: 'code'
81 };
82 finished({
83 redirectTo: 'auth0',
84 config: config,
85 connections: connections
86 });
87 return;
88 }
89
90 // OK we're in demo mode...
91
92 // create a new session
93
94 console.log('*** /user/initialise.js - creating new session');
95 session = createSession.call(this);
96 session.authenticated = true;
97
98 // create simulated user in QEWD Session
99
100 var user = {
101 sub: '28AD8576-1948-4C84-8B5E-55FB7EE027CE',
102 given_name: 'Bob',
103 family_name: 'Smith',
104 email: 'bob.smith@gmail.com',
105 scope: {
106 homeView: 'chart',
107 autoAdvancedSearch: !1,
108 setting2: !0,
109 setting3: !0
110 },
111 tenant_id: 'Ripple',
112 tenant_name: 'Ripple Demonstrator',
113 role: 'idcr'
114 };
115
116 session.data.$(['auth0', '_json']).setDocument(user);
117
118 // browser will store new token as cookie and then fetch user
119
120 //console.log('initialise finished');
121 //console.log('token = ' + session.token);
122
123 finished({
124 token: session.token,
125 mode: mode
126 });
127
128 return;
129 }
130
131 // the JSESSIONID cookie was for an active QEWD Session
132 // which will contain the user information, so just tell
133 // the browser to carry on and fetch the user info
134
135
136 finished({
137 ok: true,
138 mode: mode
139 });
140
141 return;
142}
143
144module.exports = initialise;