6
|
1 package cmd
|
|
2
|
|
3 import (
|
|
4 "bytes"
|
|
5 "encoding/json"
|
|
6 "io"
|
|
7 "log"
|
|
8 "net/http"
|
|
9 "net/url"
|
|
10 )
|
|
11
|
|
12 // TODO: Change unmarshall to JSON DECODE
|
|
13
|
|
14 func GetAuthUrl() string {
|
|
15 config := readConfig()
|
|
16
|
|
17 baseUrl := &url.URL{
|
|
18 Scheme: "https",
|
|
19 Host: "id.twitch.tv",
|
|
20 Path: "/oauth2/authorize",
|
|
21 }
|
|
22
|
|
23 params := url.Values{}
|
|
24 params.Add("client_id", config.ClientId)
|
|
25 params.Add("force_verify", "true")
|
|
26 params.Add("redirect_uri", "http://localhost:8080/twitch-auth-code-callback")
|
|
27 params.Add("response_type", "code")
|
|
28 params.Add("scope", "channel:bot user:read:chat")
|
|
29 params.Add("state", "c3ab8aa609ea11e793ae92361f002671")
|
|
30
|
|
31 baseUrl.RawQuery = params.Encode()
|
|
32
|
|
33 return baseUrl.String()
|
|
34 }
|
|
35
|
|
36 type AuthRes struct {
|
|
37 AccessToken string `json:"access_token"`
|
|
38 RefreshToken string `json:"refresh_token"`
|
|
39 Scope []string `json:"scope"`
|
|
40 }
|
|
41
|
|
42 func GetAuthToken(authCode string) AuthRes {
|
|
43 config := readConfig()
|
|
44
|
|
45 baseUrl := &url.URL{
|
|
46 Scheme: "https",
|
|
47 Host: "id.twitch.tv",
|
|
48 Path: "/oauth2/token",
|
|
49 }
|
|
50
|
|
51 formData := url.Values{}
|
|
52 formData.Add("client_id", config.ClientId)
|
|
53 formData.Add("client_secret", config.ClientSecret)
|
|
54 formData.Add("code", authCode)
|
|
55 formData.Add("grant_type", "authorization_code")
|
|
56 formData.Add("redirect_uri", "http://localhost:8080/twitch-auth-code-callback")
|
|
57
|
|
58 res, err := http.PostForm(baseUrl.String(), formData)
|
|
59
|
|
60 if err != nil {
|
|
61 log.Fatal(err)
|
|
62 }
|
|
63
|
|
64 defer res.Body.Close()
|
|
65
|
|
66 if res.StatusCode != 200 {
|
|
67 log.Fatal("GetAuthToken")
|
|
68 }
|
|
69
|
|
70 var authRes AuthRes
|
|
71
|
|
72 err = json.NewDecoder(res.Body).Decode(&authRes)
|
|
73
|
|
74 if err != nil {
|
|
75 log.Fatal(err)
|
|
76 }
|
|
77
|
|
78 log.Println(authRes.Scope)
|
|
79
|
|
80 return authRes
|
|
81 }
|
|
82
|
|
83 func IsAuthTokenValid(authToken string) bool {
|
|
84 endpoint := "https://id.twitch.tv/oauth2/validate"
|
|
85 req, err := http.NewRequest("GET", endpoint, nil)
|
|
86
|
|
87 if err != nil {
|
|
88 log.Fatalf("Error creating request: %v\n", err)
|
|
89 }
|
|
90
|
|
91 req.Header.Set("Authorization", "OAuth "+authToken)
|
|
92
|
|
93 client := &http.Client{}
|
|
94 resp, err := client.Do(req)
|
|
95
|
|
96 if err != nil {
|
|
97 log.Fatalf("Error sending request: %v\n", err)
|
|
98 }
|
|
99
|
|
100 defer resp.Body.Close()
|
|
101
|
|
102 return resp.StatusCode == 200
|
|
103 }
|
|
104
|
|
105 func RevokeAuthToken(authToken string) {
|
|
106 config := readConfig()
|
|
107
|
|
108 data := url.Values{}
|
|
109 data.Set("client_id", config.ClientId)
|
|
110 data.Set("token", authToken)
|
|
111
|
|
112 endpoint := "https://id.twitch.tv/oauth2/revoke"
|
|
113 req, err := http.NewRequest("POST", endpoint, bytes.NewBufferString(data.Encode()))
|
|
114
|
|
115 if err != nil {
|
|
116 log.Fatalf("Error creating request: %v\n", err)
|
|
117 }
|
|
118
|
|
119 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
120
|
|
121 client := &http.Client{}
|
|
122 resp, err := client.Do(req)
|
|
123
|
|
124 if err != nil {
|
|
125 log.Fatalf("Error sending request: %v\n", err)
|
|
126 }
|
|
127
|
|
128 defer resp.Body.Close()
|
|
129 }
|
|
130
|
|
131 func RefreshAuthToken(authToken, refreshToken string) AuthRes {
|
|
132 config := readConfig()
|
|
133
|
|
134 data := url.Values{}
|
|
135 data.Set("grant_type", "refresh_token")
|
|
136 data.Set("refresh_token", refreshToken)
|
|
137 data.Set("client_id", config.ClientId)
|
|
138 data.Set("client_secret", config.ClientSecret)
|
|
139
|
|
140 endpoint := "https://id.twitch.tv/oauth2/token"
|
|
141 req, err := http.NewRequest("POST", endpoint, bytes.NewBufferString(data.Encode()))
|
|
142
|
|
143 if err != nil {
|
|
144 log.Fatalf("Error creating request: %v\n", err)
|
|
145 }
|
|
146
|
|
147 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
148
|
|
149 client := &http.Client{}
|
|
150 resp, err := client.Do(req)
|
|
151
|
|
152 if err != nil {
|
|
153 log.Fatalf("Error sending request: %v\n", err)
|
|
154 }
|
|
155
|
|
156 defer resp.Body.Close()
|
|
157
|
|
158 body, err := io.ReadAll(resp.Body)
|
|
159
|
|
160 if err != nil {
|
|
161 log.Fatalf("Error reading response body: %v", err)
|
|
162 }
|
|
163
|
|
164 var authResponse AuthRes
|
|
165
|
|
166 if err := json.Unmarshal(body, &authResponse); err != nil {
|
|
167 log.Fatalf("Error parsing JSON: %v", err)
|
|
168 }
|
|
169
|
|
170 return authResponse
|
|
171 }
|
|
172
|
|
173 // TODO: Return broadcaste user id
|
|
174 func GetBroadcasterUserId(userName, authToken string) {
|
|
175 config := readConfig()
|
|
176
|
|
177 endpoint := "https://api.twitch.tv/helix/users?login=" + userName
|
|
178 req, err := http.NewRequest("GET", endpoint, nil)
|
|
179
|
|
180 if err != nil {
|
|
181 log.Fatalf("Error creating request: %v\n", err)
|
|
182 }
|
|
183
|
|
184 req.Header.Set("Client-ID", config.ClientId)
|
|
185 req.Header.Set("Authorization", "Bearer "+authToken)
|
|
186
|
|
187 client := &http.Client{}
|
|
188 resp, err := client.Do(req)
|
|
189
|
|
190 if err != nil {
|
|
191 log.Fatalf("Error sending request: %v\n", err)
|
|
192 }
|
|
193
|
|
194 defer resp.Body.Close()
|
|
195
|
|
196 body, err := io.ReadAll(resp.Body)
|
|
197
|
|
198 if err != nil {
|
|
199 log.Fatalf("Error reading response body: %v", err)
|
|
200 }
|
|
201
|
|
202 log.Println(string(body))
|
|
203 }
|