-
Notifications
You must be signed in to change notification settings - Fork 1
/
eve.html
163 lines (118 loc) · 5.69 KB
/
eve.html
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
<html>
<head>
</head>
<body>
<script>
const clientID = '91a7412560544d04bf83845207a9c56a';
const callbackURI = 'https://ryanmathewson.github.io/eve.html';
const scopes = 'esi-ui.open_window.v1 esi-ui.write_waypoint.v1';
async function StartAuthFlow() {
const tokenBytes = new Uint32Array(32);
self.crypto.getRandomValues(tokenBytes);
var code_verifier = urlsafe_b64encode(tokenBytes);
var hash = await sha256(code_verifier);
var code_challenge = urlsafe_b64encode(hash);
var state = {
code_verifier: code_verifier,
queryString: window.location.search
};
var authorizationURL = "https://login.eveonline.com/v2/oauth/authorize?response_type=code&redirect_uri=" + encodeURIComponent(callbackURI) + "&client_id=" + clientID + "&scope=" + encodeURIComponent(scopes) + "&code_challenge=" + code_challenge + "&code_challenge_method=S256&state=" + encodeURIComponent(JSON.stringify(state));
window.location.href = authorizationURL;
}
async function FinishAuthFlow(code, stateJSON) {
var state = JSON.parse(stateJSON);
codeVerifier = state.code_verifier;
var details = {
'grant_type': 'authorization_code',
'code': code,
'client_id': clientID,
"code_verifier": codeVerifier
};
var formBody = [];
for (var property in details) {
var encodedKey = encodeURIComponent(property);
var encodedValue = encodeURIComponent(details[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
const response = await fetch('https://login.eveonline.com/v2/oauth/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: formBody
});
console.log(response);
if (!response.ok) {
var reason = await response.text();
console.log("Auth Failed: " + reason);
return;
}
const jsonData = await response.json();
const accessToken = jsonData.access_token;
document.cookie = "access_token=" + accessToken + "; max-age=1200; secure";
document.location.href = document.location.href.replace(document.location.search, state.queryString);
}
function urlsafe_b64encode(bytes) {
// Convert from bytes to base64 and apply replacements to mimic the python functions
return btoa(String.fromCharCode.apply(null, new Uint8Array(bytes))).replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '')
}
async function sha256(message) {
// encode as UTF-8
const msgBuffer = new TextEncoder().encode(message);
console.log(msgBuffer);
// hash the message
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
console.log(hashBuffer);
return Array.from(new Uint8Array(hashBuffer));
}
async function OpenMarketWindow(accessToken, itemTypeID) {
await fetch('https://esi.evetech.net/latest/ui/openwindow/marketdetails/?datasource=tranquility&token=' + accessToken + '&type_id=' + itemTypeID, {
method: 'POST'
});
}
async function NavigateToWaypoint(accessToken, destination_id, add_to_begining, clear_other_waypoints){
await fetch('https://esi.evetech.net/latest/ui/autopilot/waypoint/?datasource=tranquility&token=' + accessToken + '&destination_id=' + destination_id + '&add_to_beginning=' + add_to_begining + "&clear_other_waypoints=" + clear_other_waypoints, {
method: 'POST'
});
}
function getCookie(cookieName) {
let cookie = {};
document.cookie.split(';').forEach(function (el) {
let [key, value] = el.split('=');
cookie[key.trim()] = value;
})
return cookie[cookieName];
}
function clearCookie(){
document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); });
}
async function Main() {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const code = urlParams.get('code');
const stateJSON = urlParams.get('state');
if (code != null && stateJSON != null) {
await FinishAuthFlow(code, stateJSON);
return;
}
const accessToken = getCookie('access_token');
if (accessToken == null) {
await StartAuthFlow();
return;
}
const marketItemID = urlParams.get('marketItemID');
if (marketItemID != null) {
OpenMarketWindow(accessToken, marketItemID);
}
const destination_id = urlParams.get('destination_id');
const add_to_begining = urlParams.get('add_to_begining');
const clear_other_waypoints = urlParams.get('clear_other_waypoints');
if(destination_id != null && add_to_begining != null && clear_other_waypoints != null){
NavigateToWaypoint(accessToken, destination_id, add_to_begining, clear_other_waypoints);
}
}
Main();
</script>
</body>
</html>