comparison cmd/auth.go @ 7:a8aab75f68c9

cmd/auth.go: refactor code
author Dennis C. M. <dennis@denniscm.com>
date Wed, 12 Mar 2025 14:45:00 +0000
parents 4deabe76bd7f
children
comparison
equal deleted inserted replaced
6:4deabe76bd7f 7:a8aab75f68c9
1 package cmd 1 package cmd
2 2
3 import ( 3 import (
4 "bytes"
5 "encoding/json" 4 "encoding/json"
6 "io"
7 "log" 5 "log"
8 "net/http" 6 "net/http"
9 "net/url" 7 "net/url"
10 ) 8 )
11 9
73 71
74 if err != nil { 72 if err != nil {
75 log.Fatal(err) 73 log.Fatal(err)
76 } 74 }
77 75
78 log.Println(authRes.Scope)
79
80 return authRes 76 return authRes
81 } 77 }
82 78
83 func IsAuthTokenValid(authToken string) bool { 79 func IsAuthTokenValid(authToken string) bool {
84 endpoint := "https://id.twitch.tv/oauth2/validate" 80 baseUrl := &url.URL{
85 req, err := http.NewRequest("GET", endpoint, nil) 81 Scheme: "https",
86 82 Host: "id.twitch.tv",
87 if err != nil { 83 Path: "oauth2/validate",
88 log.Fatalf("Error creating request: %v\n", err) 84 }
85
86 req, err := http.NewRequest("GET", baseUrl.String(), nil)
87
88 if err != nil {
89 log.Fatal(err)
89 } 90 }
90 91
91 req.Header.Set("Authorization", "OAuth "+authToken) 92 req.Header.Set("Authorization", "OAuth "+authToken)
92 93
93 client := &http.Client{} 94 client := &http.Client{}
94 resp, err := client.Do(req) 95 resp, err := client.Do(req)
95 96
96 if err != nil { 97 if err != nil {
97 log.Fatalf("Error sending request: %v\n", err) 98 log.Fatal(err)
98 } 99 }
99 100
100 defer resp.Body.Close() 101 defer resp.Body.Close()
101 102
102 return resp.StatusCode == 200 103 return resp.StatusCode == 200
103 } 104 }
104 105
105 func RevokeAuthToken(authToken string) { 106 func RevokeAuthToken(authToken string) {
106 config := readConfig() 107 config := readConfig()
107 108
109 baseUrl := &url.URL{
110 Scheme: "https",
111 Host: "id.twitch.tv",
112 Path: "oauth2/revoke",
113 }
114
108 data := url.Values{} 115 data := url.Values{}
109 data.Set("client_id", config.ClientId) 116 data.Add("client_id", config.ClientId)
110 data.Set("token", authToken) 117 data.Add("token", authToken)
111 118
112 endpoint := "https://id.twitch.tv/oauth2/revoke" 119 res, err := http.PostForm(baseUrl.String(), data)
113 req, err := http.NewRequest("POST", endpoint, bytes.NewBufferString(data.Encode())) 120
114 121 if err != nil {
115 if err != nil { 122 log.Fatal(err)
116 log.Fatalf("Error creating request: %v\n", err) 123 }
117 } 124
118 125 defer res.Body.Close()
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 } 126 }
130 127
131 func RefreshAuthToken(authToken, refreshToken string) AuthRes { 128 func RefreshAuthToken(authToken, refreshToken string) AuthRes {
132 config := readConfig() 129 config := readConfig()
133 130
131 baseUrl := &url.URL{
132 Scheme: "https",
133 Host: "id.twitch.tv",
134 Path: "oauth2/token",
135 }
136
134 data := url.Values{} 137 data := url.Values{}
135 data.Set("grant_type", "refresh_token") 138 data.Add("grant_type", "refresh_token")
136 data.Set("refresh_token", refreshToken) 139 data.Add("refresh_token", refreshToken)
137 data.Set("client_id", config.ClientId) 140 data.Add("client_id", config.ClientId)
138 data.Set("client_secret", config.ClientSecret) 141 data.Add("client_secret", config.ClientSecret)
139 142
140 endpoint := "https://id.twitch.tv/oauth2/token" 143 res, err := http.PostForm(baseUrl.String(), data)
141 req, err := http.NewRequest("POST", endpoint, bytes.NewBufferString(data.Encode())) 144
142 145 if err != nil {
143 if err != nil { 146 log.Fatal(err)
144 log.Fatalf("Error creating request: %v\n", err) 147 }
145 } 148
146 149 defer res.Body.Close()
147 req.Header.Set("Content-Type", "application/x-www-form-urlencoded") 150
148 151 var authRes AuthRes
149 client := &http.Client{} 152
150 resp, err := client.Do(req) 153 err = json.NewDecoder(res.Body).Decode(&authRes)
151 154
152 if err != nil { 155 if err != nil {
153 log.Fatalf("Error sending request: %v\n", err) 156 log.Fatal(err)
154 } 157 }
155 158
156 defer resp.Body.Close() 159 return authRes
157 160 }
158 body, err := io.ReadAll(resp.Body) 161
159 162 type UserRes struct {
160 if err != nil { 163 Data []struct {
161 log.Fatalf("Error reading response body: %v", err) 164 Id string `json:"id"`
162 } 165 } `json:"data"`
163 166 }
164 var authResponse AuthRes 167
165 168 func GetUser(userName, authToken string) UserRes {
166 if err := json.Unmarshal(body, &authResponse); err != nil { 169 config := readConfig()
167 log.Fatalf("Error parsing JSON: %v", err) 170
168 } 171 baseUrl := &url.URL{
169 172 Scheme: "https",
170 return authResponse 173 Host: "api.twitch.tv",
171 } 174 Path: "helix/users",
172 175 }
173 // TODO: Return broadcaste user id 176
174 func GetBroadcasterUserId(userName, authToken string) { 177 params := url.Values{}
175 config := readConfig() 178 params.Add("login", userName)
176 179
177 endpoint := "https://api.twitch.tv/helix/users?login=" + userName 180 req, err := http.NewRequest("GET", baseUrl.String(), nil)
178 req, err := http.NewRequest("GET", endpoint, nil) 181
179 182 if err != nil {
180 if err != nil { 183 log.Fatal(err)
181 log.Fatalf("Error creating request: %v\n", err)
182 } 184 }
183 185
184 req.Header.Set("Client-ID", config.ClientId) 186 req.Header.Set("Client-ID", config.ClientId)
185 req.Header.Set("Authorization", "Bearer "+authToken) 187 req.Header.Set("Authorization", "Bearer "+authToken)
186 188
187 client := &http.Client{} 189 client := &http.Client{}
188 resp, err := client.Do(req) 190 res, err := client.Do(req)
189 191
190 if err != nil { 192 if err != nil {
191 log.Fatalf("Error sending request: %v\n", err) 193 log.Fatal(err)
192 } 194 }
193 195
194 defer resp.Body.Close() 196 defer res.Body.Close()
195 197
196 body, err := io.ReadAll(resp.Body) 198 var userRes UserRes
197 199
198 if err != nil { 200 err = json.NewDecoder(res.Body).Decode(&userRes)
199 log.Fatalf("Error reading response body: %v", err) 201
200 } 202 if err != nil {
201 203 log.Fatal(err)
202 log.Println(string(body)) 204 }
203 } 205
206 return userRes
207 }