-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
179 lines (154 loc) · 5.48 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React, {Component} from 'react';
import Virtru from 'virtru-sdk';
import FileSaver from 'file-saver';
import history from './history';
import Log from './Log';
import Map from './Map';
import './Map.css';
class App extends Component {
constructor(props) {
super(props);
const email = process.env.REACT_APP_EMAIL;
if(!Virtru.Auth.isLoggedIn({email: email})) {
Virtru.Auth.loginWithGoogle({email: email, redirectUrl: "http://localhost:3000/"});
}
const client = new Virtru.Client({email});
this.state = {
policies: this.updatePolicies(history.policies),
log: this.updateLog(history.log),
client: client
};
}
addPolicy = (key, entry) => {
let copy = this.state.policies;
let updates = this.state.log;
const geo = this.ip2geo(entry.ip);
entry['loc'] = geo;
copy[key] = entry;
updates.push({
ip: entry.ip,
access: entry.access,
email: entry.users,
file: entry.file,
key: key
});
this.setState(prevState => ({
policies: this.updatePolicies(copy),
log: this.updateLog(updates)
}));
}
writeFile = () => {
const state = {};
state['policies'] = this.state.policies;
state['log'] = this.state.log;
const data = JSON.stringify(state, null, 4);
const blob = new Blob([data], {type: 'application/json'});
console.log(blob);
FileSaver.saveAs(blob, 'history.json');
}
flipVirtru = async(access, policyId) => {
console.log(policyId);
const policy = await this.state.client.fetchPolicy(policyId);
const users = this.state.policies[policyId]['users'];
const updatedPolicy = access === 'GRANT' ?
policy.builder().addUsersWithAccess(users).build() :
policy.builder().removeUsersWithAccess(users).build();
await this.state.client.updatePolicy(updatedPolicy);
console.log("SUCCESS");
}
flipAccess = (key) => {
let copy = this.state.policies;
let updates = this.state.log;
const flip = copy[key]['access'] === 'GRANT' ? 'REVOKE' : 'GRANT';
copy[key]['access'] = flip;
this.flipVirtru(flip, key);
updates.push({
ip: copy[key]['ip'],
access: flip,
email: copy[key]['users'],
file: copy[key]['file'],
loc: copy[key]['loc'],
key: key
});
this.setState(prevState => ({
policies: copy,
updates: updates
}))
}
ip2geo = (ip) => {
let entry = {};
if (process.env.REACT_APP_DEBUG === "true" || !process.env.REACT_APP_IPSTACK) {
fetch('https://ipapi.co/' + ip + '/json/')
.then(response => response.json())
.then(data => {
entry['lat'] = data.latitude;
entry['long'] = data.longitude;
entry['city'] = data.city;
entry['region'] = data.region;
entry['region code'] = data.region_code;
entry['country'] = data.country_name;
entry['country code'] = data.country;
});
} else {
fetch('http://api.ipstack.com/'+ ip +'?access_key=' +
process.env.REACT_APP_IPSTACK)
.then(response => response.json())
.then(data => {
entry['lat'] = data.latitude;
entry['long'] = data.longitude;
entry['city'] = data.city;
entry['region'] = data.region_name;
entry['region code'] = data.region_code;
entry['country'] = data.country_name;
entry['country code'] = data.country_code;
});
}
console.log(entry);
return entry;
}
updatePolicies = (policies) => {
for(var key in policies) {
if('ip' in policies[key] &&
!!policies[key]['ip'] &&
(!('loc' in policies[key]) || !policies[key]['loc']['lat'])) {
const ip = policies[key]['ip'];
const loc = this.ip2geo(ip);
policies[key]['loc'] = loc;
}
}
return policies;
}
updateLog = (log) => {
for(var idx in log) {
let entry = log[idx];
if('ip' in entry &&
!!entry['ip'] &&
!('loc' in entry)) {
const policies = history.policies;
if('loc' in policies[entry['key']] && !!policies[entry['key']]['loc']['lat']) {
entry['loc'] = policies[entry['key']]['loc'];
} else {
const ip = entry['ip'];
const loc = this.ip2geo(ip);
entry['loc'] = loc;
}
log[idx] = entry;
}
}
return log
}
render() {
console.log(this.state.policies);
return (
<div styles={{fontFamily: "Maven Pro"}}>
<Map flipAccess = {this.flipAccess}
addPolicy = {this.addPolicy}
writeFile = {this.writeFile}
ip2geo = {this.ip2geo}
data = {this.state.policies}
client = {this.state.client}/>
<Log data = {this.state.log} writeFile = {this.writeFile}/> </div>
);
}
}
export default App;