-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathApp.js
189 lines (167 loc) · 4.43 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
180
181
182
183
184
185
186
187
188
189
// Polyfill
import "react-zlib-js";
import { useMemo, useEffect, useState, useCallback } from "react";
import { StatusBar } from "expo-status-bar";
import { StyleSheet, Image, Text, View, TextInput } from "react-native";
import bwipjs from "bwip-js";
import * as b64 from "base-64";
import { Buffer } from "buffer";
import { WebView } from "react-native-webview";
import * as OTPAuth from "otpauth";
import { useAsyncStorage } from "@react-native-async-storage/async-storage";
const TOTP_STEP = 15;
const totp = (secret, now) =>
new OTPAuth.TOTP({
secret,
period: TOTP_STEP,
}).generate({ timestamp: now });
const unixNow = () => Math.floor(Date.now());
function generateSignedToken(ticket, time) {
const parsedTicket = JSON.parse(Buffer.from(ticket, "base64").toString());
const bearerKey = parsedTicket.t;
const customerKey = Buffer.from(parsedTicket.ck, "hex");
const eventKey = Buffer.from(parsedTicket.ek, "hex");
const eventOTP = totp(eventKey, time);
const customerOTP = totp(customerKey, time);
const secs = Math.floor(time / 1000);
return [bearerKey, eventOTP, customerOTP, secs].join("::");
}
function useSignedToken(ticket) {
const [currentTime, setTime] = useState(0);
const [currentCounter, setCounter] = useState(0);
useEffect(() => {
const interval = setInterval(() => {
const newTime = unixNow();
const counter = Math.floor(newTime / (TOTP_STEP * 1000));
if (counter !== currentCounter) {
setCounter(counter);
setTime(newTime);
}
}, 500);
return () => clearInterval(interval);
}, [currentCounter]);
const token = useMemo(() => {
try {
return generateSignedToken(ticket, currentTime);
} catch (err) {
return "";
}
}, [ticket, currentTime]);
return token;
}
function Barcode({ text }) {
const html = useMemo(() => {
const uri =
"data:image/svg+xml;base64," +
b64.encode(
bwipjs
.toSVG({
bcid: "pdf417",
text,
})
.trim(),
);
return `<div><img src="${uri}"></div>`;
}, [text]);
return (
<View style={styles.barcode}>
<WebView style={styles.webview} source={{ html }} />
</View>
);
}
function Logo({ style }) {
return (
<Image style={style} source={require("./assets/flash-circuit-512.png")} />
);
}
export default function App() {
const [ticket, setTicket] = useState("");
const ticketStorage = useAsyncStorage("ticket");
useEffect(() => {
ticketStorage.getItem((err, t) => {
if (err) return console.error(err);
if (t) setTicket(t);
});
}, []);
const updateTicket = useCallback(
(newTicket) => {
setTicket(newTicket);
ticketStorage.setItem(newTicket).catch(console.error);
},
[ticketStorage],
);
const [ticketInputStyle, setTicketInputStyle] = useState(
styles.ticketInputBlurred,
);
const onFocusTicketInput = useCallback(() => {
setTicketInputStyle(styles.ticketInputFocused);
}, []);
const onBlurTicketInput = useCallback(() => {
setTicketInputStyle(styles.ticketInputBlurred);
}, []);
const token = useSignedToken(ticket);
return (
<View style={styles.container}>
<Logo style={styles.logo} />
<Text style={styles.title}>TicketGimp</Text>
<TextInput
placeholder="insert ticket here"
placeholderTextColor="grey"
value={ticket}
selectTextOnFocus
style={[styles.ticketInput, ticketInputStyle]}
onChangeText={updateTicket}
onFocus={onFocusTicketInput}
onBlur={onBlurTicketInput}
/>
{token ? <Barcode text={token} /> : <View style={{ flex: 1 }} />}
<StatusBar style="light" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 50,
backgroundColor: "#080c18",
alignItems: "center",
},
logo: {
position: "absolute",
width: 40,
height: 40,
left: 25,
top: 60,
},
ticketInput: {
padding: 5,
borderRadius: 5,
backgroundColor: "#222",
borderBottomWidth: 1,
width: "80%",
color: "white",
margin: 50,
},
ticketInputFocused: {
borderBottomWidth: 2,
borderBottomColor: "#d500f9",
},
ticketInputBlurred: {
borderBottomWidth: 1,
borderBottomColor: "#ddd",
},
title: {
flex: 1,
color: "white",
fontFamily: "monospace",
fontSize: 40,
},
barcode: {
flex: 3,
},
webview: {
width: 350,
flex: 0,
height: 120,
},
});