-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.v
78 lines (71 loc) · 1.84 KB
/
main.v
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
module main
import vweb
import xbl
import json
import os
const (
port = 80
)
struct App {
vweb.Context
mut:
cnt int
pub mut:
login xbl.LoginData
}
// loads the config if exists; saves the config and stops the server for setup if not
fn (mut app App) load_config() {
app.login = xbl.LoginData{}
fpath := './login.conf'
if !os.is_file(fpath) {
arr := [
'client_id=$app.login.client_id',
'client_secret=$app.login.client_secret',
'redirect_uri=$app.login.redirect_uri',
'locale=$app.login.locale',
]
os.write_file(fpath, arr.join_lines())
panic('Configuration was not set up')
}
read := os.read_file(fpath) or { panic(err) }
rarr := read.split_into_lines()
assert rarr.len == 4
println(rarr)
mut m := map[string]string{}
for ln in rarr {
m[ln.all_before('=')] = ln.all_after('=')
}
assert m.len == 4
assert 'client_id' in m
assert 'client_secret' in m
assert 'redirect_uri' in m
assert 'locale' in m
app.login = xbl.LoginData{
client_id: m['client_id']
client_secret: m['client_secret']
redirect_uri: m['redirect_uri']
locale: m['locale']
}
}
fn main() {
mut app := App{}
app.load_config()
vweb.run_app<App>(mut app, port)
}
pub fn (mut app App) index() vweb.Result {
request_url := app.login.get_request_url()
app.redirect(request_url)
return $vweb.html()
}
pub fn (mut app App) signin() vweb.Result {
code := app.query['code']
access_token_info := app.login.get_access_token_info(code)
authentication_info := xbl.get_authentication_info(access_token_info['access_token'])
authorization_info := xbl.get_authorization_info(authentication_info['Token'])
$if debug {
println(json.encode(authorization_info))
}
// mut xuid := authorization_info.display_claims["xui"][0].xuid
gamertag := authorization_info.display_claims['xui'][0].gamertag // TODO figure out a way to make warning shut up
return $vweb.html()
}