-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlink.go
More file actions
156 lines (132 loc) · 3.29 KB
/
Copy pathlink.go
File metadata and controls
156 lines (132 loc) · 3.29 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"slices"
"strconv"
"github.com/WiiLink24/AccountManager/middleware"
"github.com/WiiLink24/nwc24"
"github.com/gin-gonic/gin"
)
func updateUserRequest(uid any, payload map[string]any) error {
url := fmt.Sprintf("https://sso.riiconnect24.net/api/v3/core/users/%s/", uid)
data, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("failed to marshal payload")
}
client := &http.Client{}
req, err := http.NewRequest("PATCH", url, bytes.NewReader(data))
if err != nil {
return fmt.Errorf("failed to create request")
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", config.OIDCConfig.ServiceAccountToken))
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to send request")
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to update user")
}
return nil
}
func linkRedirect(c *gin.Context) {
c.Redirect(http.StatusFound, "https://sso.riiconnect24.net/device")
}
func link(c *gin.Context) {
wiiNumber := c.PostForm("wii_num")
wwfcCert := c.PostForm("cert")
serialNumber := c.PostForm("serno")
// Verify cert first
ngId, err := verifySignature("WIILINK_ACCOUNT_LINKER", wwfcCert)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"error": err.Error(),
})
return
}
// Now that the certificate is verified, validate the Wii number.
intWiiNumber, err := strconv.Atoi(wiiNumber)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"error": err.Error(),
})
return
}
wiiNoObj := nwc24.LoadWiiNumber(uint64(intWiiNumber))
if !wiiNoObj.CheckWiiNumber() {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"error": "invalid wii number",
})
return
}
if wiiNoObj.GetHollywoodID() != ngId {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"error": "device id associated with the Wii Number does not match the Wii.",
})
return
}
_wiis, ok := c.Get("wiis")
if !ok {
c.JSON(http.StatusOK, gin.H{
"success": false,
"error": "failed to get wiis",
})
return
}
wiis := _wiis.([]middleware.Wii)
wiiIdx := slices.IndexFunc(wiis, func(w middleware.Wii) bool {
return w.WiiNumber == wiiNumber
})
if wiiIdx != -1 {
// Wii is already linked, update object, presumably with serial number.
wiis[wiiIdx].SerialNumber = serialNumber
} else {
// Add new object
wii := middleware.Wii{
WiiNumber: wiiNumber,
HollywoodID: int(ngId),
DominosLinked: false,
JustEatLinked: false,
SerialNumber: serialNumber,
}
wiis = append(wiis, wii)
}
uid, ok := c.Get("uid")
if !ok {
c.JSON(http.StatusOK, gin.H{
"success": false,
"error": "failed to get uid",
})
}
publicProfile, ok := c.Get("public_profile")
if !ok {
c.JSON(http.StatusOK, gin.H{
"success": false,
"error": "failed to get public_profile",
})
}
payload := map[string]any{
"attributes": map[string]any{
"public_profile": publicProfile,
"wiis": wiis,
},
}
err = updateUserRequest(uid, payload)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"error": err.Error(),
})
}
c.JSON(http.StatusOK, gin.H{
"success": true,
})
}