-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessions.go
108 lines (94 loc) · 2.59 KB
/
sessions.go
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
package main
import (
"encoding/json"
"github.com/FreekingDean/AlmostHeaven/auth"
"log"
"net/http"
"time"
)
const (
fallout76ApplicationID = "2edd3c0e-db9d-4256-83bf-7fd064122948"
defaultSessionType = "basic"
defaultRefreshTime = 3600
defaultExpiryTime = 7200
)
var (
invalidLoginResponse = &PlatformResponse{Code: 14018, Message: "Invalid login credentials"}
)
func GetToken(w http.ResponseWriter, _ *http.Request) {
resp := &GenericPlatformResponse{
PlatformResponse: &PlatformResponse{
Code: 2000,
Message: "success",
Response: generateUUID(),
},
}
DefaultJSONEncoder(w, resp)
}
type LoginResponse struct {
ApplicationID string `json:"application_id"`
BUID string `json:"buid"` //Guessing this is Bethesda User ID
MasterAccountID string `json:"master_account_id"`
Username string `json:"username"`
ExternalAccount struct{} `json:"external_account"`
RefreshTime int64 `json:"refresh_time"`
TimeToRefresh int `json:"time_to_refresh"`
Expiration int64 `json:"exp"`
TimeToExpire int `json:"time_to_expire"`
SessionType string `json:"session_type"` //Should be "basic"
SessionToken string `json:"session_token"` //JWT
}
type LoginRequest struct {
ClientID string `json:"client_id"`
Language string `json:"language"`
Username string `json:"username"`
Password string `json:"password"`
}
func Login(w http.ResponseWriter, r *http.Request) {
var req LoginRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
log.Println(err)
}
user := auth.BasicAuth(req.Username, req.Password)
if user == nil {
resp := &GenericPlatformResponse{
PlatformResponse: invalidLoginResponse,
}
err = json.NewEncoder(w).Encode(resp)
if err != nil {
log.Println(err)
}
w.WriteHeader(401)
return
}
token, err := user.JWT().String()
if err != nil {
log.Println(err)
return
}
resp := &LoginResponse{
ApplicationID: fallout76ApplicationID,
BUID: user.ID,
MasterAccountID: user.ID,
Username: user.Username,
ExternalAccount: struct{}{},
RefreshTime: time.Now().Add(time.Second * defaultRefreshTime).Unix(),
TimeToRefresh: defaultRefreshTime,
Expiration: time.Now().Add(time.Second * defaultExpiryTime).Unix(),
TimeToExpire: defaultExpiryTime,
SessionType: defaultSessionType,
SessionToken: token,
}
fullResp := buildPlatformSuccess(resp)
DefaultJSONEncoder(w, fullResp)
}
func Logout(w http.ResponseWriter, r *http.Request) {
sresp := struct {
Success bool `json:"success"`
}{
Success: true,
}
resp := buildPlatformSuccess(sresp)
DefaultJSONEncoder(w, resp)
}