Mercurial > public > pacobot
comparison auth/auth.go @ 8:e9df3bb010f4
fix issues
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Thu, 13 Mar 2025 17:41:42 +0000 |
parents | cmd/auth.go@a8aab75f68c9 |
children | aaf85ae1f942 |
comparison
equal
deleted
inserted
replaced
7:a8aab75f68c9 | 8:e9df3bb010f4 |
---|---|
1 package auth | |
2 | |
3 import ( | |
4 "encoding/json" | |
5 "log" | |
6 "net/http" | |
7 "net/url" | |
8 ) | |
9 | |
10 // TODO: Change unmarshall to JSON DECODE | |
11 | |
12 func GetAuthUrl() string { | |
13 config := ReadConfig() | |
14 | |
15 baseUrl := &url.URL{ | |
16 Scheme: "https", | |
17 Host: "id.twitch.tv", | |
18 Path: "/oauth2/authorize", | |
19 } | |
20 | |
21 params := url.Values{} | |
22 params.Add("client_id", config.ClientId) | |
23 params.Add("force_verify", "true") | |
24 params.Add("redirect_uri", "http://localhost:8080/twitch-auth-code-callback") | |
25 params.Add("response_type", "code") | |
26 params.Add("scope", "channel:bot user:read:chat") | |
27 params.Add("state", "c3ab8aa609ea11e793ae92361f002671") | |
28 | |
29 baseUrl.RawQuery = params.Encode() | |
30 | |
31 return baseUrl.String() | |
32 } | |
33 | |
34 func GetAuthToken(authCode string) AuthRes { | |
35 config := ReadConfig() | |
36 | |
37 baseUrl := &url.URL{ | |
38 Scheme: "https", | |
39 Host: "id.twitch.tv", | |
40 Path: "/oauth2/token", | |
41 } | |
42 | |
43 formData := url.Values{} | |
44 formData.Add("client_id", config.ClientId) | |
45 formData.Add("client_secret", config.ClientSecret) | |
46 formData.Add("code", authCode) | |
47 formData.Add("grant_type", "authorization_code") | |
48 formData.Add("redirect_uri", "http://localhost:8080/twitch-auth-code-callback") | |
49 | |
50 res, err := http.PostForm(baseUrl.String(), formData) | |
51 | |
52 if err != nil { | |
53 log.Fatal(err) | |
54 } | |
55 | |
56 defer res.Body.Close() | |
57 | |
58 if res.StatusCode != 200 { | |
59 log.Fatal("GetAuthToken") | |
60 } | |
61 | |
62 var authRes AuthRes | |
63 | |
64 err = json.NewDecoder(res.Body).Decode(&authRes) | |
65 | |
66 if err != nil { | |
67 log.Fatal(err) | |
68 } | |
69 | |
70 return authRes | |
71 } | |
72 | |
73 func IsAuthTokenValid(authToken string) bool { | |
74 baseUrl := &url.URL{ | |
75 Scheme: "https", | |
76 Host: "id.twitch.tv", | |
77 Path: "oauth2/validate", | |
78 } | |
79 | |
80 req, err := http.NewRequest("GET", baseUrl.String(), nil) | |
81 | |
82 if err != nil { | |
83 log.Fatal(err) | |
84 } | |
85 | |
86 req.Header.Set("Authorization", "OAuth "+authToken) | |
87 | |
88 client := &http.Client{} | |
89 resp, err := client.Do(req) | |
90 | |
91 if err != nil { | |
92 log.Fatal(err) | |
93 } | |
94 | |
95 defer resp.Body.Close() | |
96 | |
97 return resp.StatusCode == 200 | |
98 } | |
99 | |
100 func RevokeAuthToken(authToken string) { | |
101 config := ReadConfig() | |
102 | |
103 baseUrl := &url.URL{ | |
104 Scheme: "https", | |
105 Host: "id.twitch.tv", | |
106 Path: "oauth2/revoke", | |
107 } | |
108 | |
109 data := url.Values{} | |
110 data.Add("client_id", config.ClientId) | |
111 data.Add("token", authToken) | |
112 | |
113 res, err := http.PostForm(baseUrl.String(), data) | |
114 | |
115 if err != nil { | |
116 log.Fatal(err) | |
117 } | |
118 | |
119 defer res.Body.Close() | |
120 } | |
121 | |
122 func RefreshAuthToken(authToken, refreshToken string) AuthRes { | |
123 config := ReadConfig() | |
124 | |
125 baseUrl := &url.URL{ | |
126 Scheme: "https", | |
127 Host: "id.twitch.tv", | |
128 Path: "oauth2/token", | |
129 } | |
130 | |
131 data := url.Values{} | |
132 data.Add("grant_type", "refresh_token") | |
133 data.Add("refresh_token", refreshToken) | |
134 data.Add("client_id", config.ClientId) | |
135 data.Add("client_secret", config.ClientSecret) | |
136 | |
137 res, err := http.PostForm(baseUrl.String(), data) | |
138 | |
139 if err != nil { | |
140 log.Fatal(err) | |
141 } | |
142 | |
143 defer res.Body.Close() | |
144 | |
145 var authRes AuthRes | |
146 | |
147 err = json.NewDecoder(res.Body).Decode(&authRes) | |
148 | |
149 if err != nil { | |
150 log.Fatal(err) | |
151 } | |
152 | |
153 return authRes | |
154 } | |
155 | |
156 func GetUser(userName, authToken string) UserRes { | |
157 config := ReadConfig() | |
158 | |
159 baseUrl := &url.URL{ | |
160 Scheme: "https", | |
161 Host: "api.twitch.tv", | |
162 Path: "helix/users", | |
163 } | |
164 | |
165 params := url.Values{} | |
166 params.Add("login", userName) | |
167 | |
168 req, err := http.NewRequest("GET", baseUrl.String(), nil) | |
169 | |
170 if err != nil { | |
171 log.Fatal(err) | |
172 } | |
173 | |
174 req.Header.Set("Client-ID", config.ClientId) | |
175 req.Header.Set("Authorization", "Bearer "+authToken) | |
176 | |
177 client := &http.Client{} | |
178 res, err := client.Do(req) | |
179 | |
180 if err != nil { | |
181 log.Fatal(err) | |
182 } | |
183 | |
184 defer res.Body.Close() | |
185 | |
186 var userRes UserRes | |
187 | |
188 err = json.NewDecoder(res.Body).Decode(&userRes) | |
189 | |
190 if err != nil { | |
191 log.Fatal(err) | |
192 } | |
193 | |
194 return userRes | |
195 } |