Skip to content
This repository was archived by the owner on Nov 18, 2021. It is now read-only.

Commit 60ceb82

Browse files
Cecil Du ToitCecil Du Toit
authored andcommitted
Added Electron remote call. Removed REST API.
1 parent f526c64 commit 60ceb82

File tree

6 files changed

+24
-168
lines changed

6 files changed

+24
-168
lines changed

index.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
<link rel="stylesheet" href="node_modules/material-design-lite/dist/material.blue-red.min.css" />
99
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
1010
<!--<link href="https://cdnjs.cloudflare.com/ajax/libs/material-design-iconic-font/2.2.0/css/material-design-iconic-font.min.css" rel="stylesheet">-->
11-
<!--<script>
12-
window.electron = require('electron');
13-
</script>-->
1411
</head>
1512

1613
<body>

main.js

Lines changed: 5 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
const electron = require('electron');
44
const app = electron.app;
55

6-
const restify = require('restify');
7-
const url = require('url');
8-
const crypto = require('crypto');
9-
const AuthenticationContext = require('adal-node').AuthenticationContext;
106
const AdalMainConfig = require("./main-config");
117

128
require('electron-debug')({ showDevTools: true });
@@ -20,13 +16,17 @@ let hash = "";
2016
require('electron-reload')(__dirname + '/build');
2117

2218
function createWindow() {
19+
global.nodeVersion = process.versions.node;
20+
global.chromeVersion = process.versions.chrome;
21+
global.electronVersion = process.versions.electron;
22+
2323
// Initialize the window to our specified dimensions
2424
mainWindow = new BrowserWindow({
2525
width: 1200,
2626
height: 900,
2727
autoHideMenuBar: true,
2828
webPreferences: {
29-
nodeIntegration: false
29+
nodeIntegration: true
3030
}
3131
});
3232

@@ -58,138 +58,3 @@ app.on('activate', function () {
5858
}
5959
});
6060

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-
}

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,12 @@
3535
"@angular/platform-browser": "2.0.0",
3636
"@angular/platform-browser-dynamic": "2.0.0",
3737
"@angular/router": "3.0.0",
38-
"adal-angular": "^1.0.12",
39-
"adal-node": "^0.1.22",
4038
"es6-promise": "^3.3.1",
4139
"es6-shim": "^0.35.1",
4240
"expose-loader": "^0.7.1",
4341
"material-design-lite": "^1.2.1",
4442
"modular-adal-angular": "^0.3.0",
4543
"reflect-metadata": "^0.1.8",
46-
"request": "^2.74.0",
47-
"restify": "^4.1.1",
4844
"rxjs": "5.0.0-beta.12",
4945
"systemjs": "^0.19.37",
5046
"zone.js": "^0.6.23",

src/app/app.component.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, AfterViewInit, OnInit } from "@angular/core";
2-
import { NgModule } from '@angular/core';
3-
import { BrowserModule } from '@angular/platform-browser';
2+
import { NgModule } from '@angular/core';
3+
import { BrowserModule } from '@angular/platform-browser';
44

55
import { ElectronService } from "../services/electron.service";
66
import { RuntimeInfoModel } from "../models/runtime-info.model";
@@ -11,23 +11,23 @@ declare var componentHandler: any;
1111
@Component({
1212
selector: "graph-app",
1313
templateUrl: "src/app/view-main.html",
14-
providers:[ElectronService]
14+
providers: [ElectronService]
1515
})
16-
export class AppComponent implements AfterViewInit, OnInit {
16+
export class AppComponent implements AfterViewInit, OnInit {
1717
userName: string = "";
1818
nodeVersion: string = "";
1919
chromeVersion: string = "";
2020
electronVersion: string = "";
2121

22-
constructor(private electronService: ElectronService) {
22+
constructor(private electronService: ElectronService) {
2323
}
2424

25-
ngOnInit(){
26-
this.electronService.GetInfo().then((info: RuntimeInfoModel) => {
27-
this.nodeVersion = info.nodeVersion;
28-
this.chromeVersion = info.chromeVersion;
29-
this.electronVersion = info.electronVersion;
30-
});
25+
ngOnInit() {
26+
let info: RuntimeInfoModel = this.electronService.GetInfo();
27+
28+
this.nodeVersion = info.nodeVersion;
29+
this.chromeVersion = info.chromeVersion;
30+
this.electronVersion = info.electronVersion;
3131
}
3232

3333
ngAfterViewInit() {

src/models/runtime-info.model.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export class RuntimeInfoModel{
2-
nodeVersion: string = "";
3-
chromeVersion: string = "";
4-
electronVersion: string = "";
2+
constructor(public nodeVersion: string, public chromeVersion: string, public electronVersion: string){
3+
}
54
}

src/services/electron.service.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Injectable } from "@angular/core";
22
import { Http, Headers, Response } from "@angular/http";
33
import 'rxjs/add/operator/toPromise';
4+
import { remote } from 'electron';
45

56
import { RuntimeInfoModel } from "../models/runtime-info.model";
67

@@ -13,13 +14,11 @@ export class ElectronService {
1314
this.http = http;
1415
}
1516

16-
public GetInfo(): Promise<RuntimeInfoModel> {
17-
return new Promise<RuntimeInfoModel>(resolve => {
18-
this.http.get("http://localhost:3000/info")
19-
.toPromise()
20-
.then(response => response.json() as RuntimeInfoModel)
21-
.catch(this.handleError)
22-
});
17+
public GetInfo(): RuntimeInfoModel {
18+
return new RuntimeInfoModel(
19+
remote.process.versions.node,
20+
remote.process.versions.chrome,
21+
remote.process.versions.electron);
2322
}
2423

2524
private handleError(error: any): Promise<any> {

0 commit comments

Comments
 (0)