3
3
const electron = require ( 'electron' ) ;
4
4
const app = electron . app ;
5
5
6
- const restify = require ( 'restify' ) ;
7
- const url = require ( 'url' ) ;
8
- const crypto = require ( 'crypto' ) ;
9
- const AuthenticationContext = require ( 'adal-node' ) . AuthenticationContext ;
10
6
const AdalMainConfig = require ( "./main-config" ) ;
11
7
12
8
require ( 'electron-debug' ) ( { showDevTools : true } ) ;
@@ -20,13 +16,17 @@ let hash = "";
20
16
require ( 'electron-reload' ) ( __dirname + '/build' ) ;
21
17
22
18
function createWindow ( ) {
19
+ global . nodeVersion = process . versions . node ;
20
+ global . chromeVersion = process . versions . chrome ;
21
+ global . electronVersion = process . versions . electron ;
22
+
23
23
// Initialize the window to our specified dimensions
24
24
mainWindow = new BrowserWindow ( {
25
25
width : 1200 ,
26
26
height : 900 ,
27
27
autoHideMenuBar : true ,
28
28
webPreferences : {
29
- nodeIntegration : false
29
+ nodeIntegration : true
30
30
}
31
31
} ) ;
32
32
@@ -58,138 +58,3 @@ app.on('activate', function () {
58
58
}
59
59
} ) ;
60
60
61
- // ADAL Settings
62
- let authToken = '' ;
63
- let authorityUrl = AdalMainConfig . authorityHostUrl + '/' + AdalMainConfig . tenant ;
64
- let templateAuthzUrl = 'https://login.windows.net/' + AdalMainConfig . tenant + '/oauth2/authorize?response_type=code&client_id=<client_id>&redirect_uri=<redirect_uri>&state=<state>&resource=<resource>' ;
65
- let tenantID = '' ;
66
- let accessToken = '' ;
67
- let refreshToken = '' ;
68
- let user = {
69
- userID : '' ,
70
- lastName : '' ,
71
- firstName : '' ,
72
- fullName : ''
73
- }
74
-
75
- // Start Restify API Server
76
- let port = process . env . PORT || 3000 ;
77
- var server = restify . createServer ( { name : 'electron-backend' , version : '0.0.1' } ) ;
78
-
79
- server . use ( restify . queryParser ( ) ) ;
80
- server . use ( restify . bodyParser ( ) ) ;
81
-
82
- server . get ( '/info' , ( req , res , next ) => {
83
- res . send ( {
84
- nodeVersion : process . versions . node ,
85
- chromeVersion : process . versions . chrome ,
86
- electronVersion : process . versions . electron
87
- } ) ;
88
- } ) ;
89
-
90
- server . get ( '/auth' , ( req , res , next ) => {
91
- console . log ( "Authenticate attempt!" )
92
- clearStorage ( ) ;
93
-
94
- crypto . randomBytes ( 48 , function ( ex , buf ) {
95
- var token = buf . toString ( 'base64' ) . replace ( / \/ / g, '_' ) . replace ( / \+ / g, '-' ) ;
96
-
97
- this . authToken = token ;
98
- //res.cookie('authstate', token);
99
- var authorizationUrl = createAuthorizationUrl ( token ) ;
100
-
101
- console . log ( "Auth URL: " + authorizationUrl ) ;
102
- //res.redirect(authorizationUrl);
103
- mainWindow . loadURL ( authorizationUrl ) ;
104
- } ) ;
105
- } ) ;
106
-
107
-
108
- server . get ( '/auth/azureoauth/callback' , ( req , res , next ) => {
109
- // TODO: Need to investigate query state fix - low priority
110
- // if (this.authToken !== req.query.state) {
111
- // console.log('error: state does not match');
112
- // res.send('error: state does not match');
113
- // }
114
-
115
- clearStorage ( ) ;
116
-
117
- var authenticationContext = new AuthenticationContext ( authorityUrl ) ;
118
- authenticationContext . acquireTokenWithAuthorizationCode ( req . query . code , AdalMainConfig . redirectUri , AdalMainConfig . resource , AdalMainConfig . clientId , AdalMainConfig . clientSecret , function ( err , response ) {
119
- var message = '' ;
120
- if ( err ) {
121
- message = 'error: ' + err . message + '\n' ;
122
- logError ( message ) ;
123
- return ;
124
- }
125
-
126
- accessToken = response . accessToken ;
127
- refreshToken = response . refreshToken ;
128
- tenantID = response . tenantId ;
129
- user . userID = response . userId ;
130
- user . lastName = response . familyName ;
131
- user . firstName = response . firstName ;
132
- user . fullName = response . firstName + ' ' + response . familyName ;
133
-
134
- // console.log("User: " + JSON.stringify(user));
135
- // console.log("Access Token: " + response.accessToken);
136
- // console.log("Refresh Token:" + response.refreshToken);
137
-
138
- saveTokens ( ) ;
139
- saveUser ( ) ;
140
-
141
- mainWindow . loadURL ( 'file://' + __dirname + '/index.html' ) ;
142
-
143
- // Later, if the access token is expired it can be refreshed.
144
- authenticationContext . acquireTokenWithRefreshToken ( response . refreshToken , AdalMainConfig . clientId , AdalMainConfig . clientSecret , AdalMainConfig . resource , function ( refreshErr , refreshResponse ) {
145
- if ( refreshErr ) {
146
- message += 'refreshError: ' + refreshErr . message + '\n' ;
147
- logError ( message ) ;
148
- }
149
-
150
- accessToken = response . accessToken ;
151
- refreshToken = response . refreshToken ;
152
- tenantID = response . tenantId ;
153
- user . userID = response . userId ;
154
- user . lastName = response . familyName ;
155
- user . firstName = response . firstName ;
156
- user . fullName = response . firstName + ' ' + response . familyName ;
157
-
158
- saveTokens ( ) ;
159
- saveUser ( ) ;
160
- } ) ;
161
- } ) ;
162
- } ) ;
163
-
164
- server . listen ( port , ( ) => {
165
- console . log ( 'server running on port ' + port ) ;
166
- } ) ;
167
-
168
-
169
- function createAuthorizationUrl ( state ) {
170
- var authorizationUrl = templateAuthzUrl . replace ( '<client_id>' , AdalMainConfig . clientId ) ;
171
- authorizationUrl = authorizationUrl . replace ( '<redirect_uri>' , AdalMainConfig . redirectUri ) ;
172
- authorizationUrl = authorizationUrl . replace ( '<state>' , state ) ;
173
- authorizationUrl = authorizationUrl . replace ( '<resource>' , AdalMainConfig . resource ) ;
174
- return authorizationUrl ;
175
- }
176
-
177
- function logError ( message ) {
178
- let code = "localStorage.setItem('error', '" + message + "');" ;
179
- mainWindow . webContents . executeJavaScript ( code ) ;
180
- }
181
-
182
- function saveTokens ( ) {
183
- let code = "localStorage.setItem('accessToken', '" + accessToken + "');" ;
184
- mainWindow . webContents . executeJavaScript ( code ) ;
185
- }
186
-
187
- function saveUser ( ) {
188
- let code = "localStorage.setItem('user', '" + JSON . stringify ( user ) + "');" ;
189
- mainWindow . webContents . executeJavaScript ( code ) ;
190
- }
191
-
192
- function clearStorage ( ) {
193
- let code = "localStorage.clear()" ;
194
- mainWindow . webContents . executeJavaScript ( code ) ;
195
- }
0 commit comments