backend/internal/auth/client/client.go
2025-06-15 12:53:27 +02:00

134 lines
3 KiB
Go

/*
Copyright (C) 2025 hexlocation (hex@iwakura.rip) & grngxd (grng@iwakura.rip)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package client
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"stereo.cat/backend/internal/auth"
)
type Client struct {
RedirectUri string
ClientSecret string
ClientId string
}
const api = "https://discord.com/api/v10"
func New(redirectUri, clientId, clientSecret string) Client {
return Client{
RedirectUri: redirectUri,
ClientId: clientId,
ClientSecret: clientSecret,
}
}
func (c Client) GetUser(t auth.TokenResponse) (auth.User, error) {
user := auth.User{
Blacklisted: false,
CreatedAt: time.Now(),
}
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s", api, "users/@me"), nil)
if err != nil {
return user, nil
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+t.AccessToken)
resp, err := http.DefaultClient.Do(req)
if resp.StatusCode != http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return user, err
}
return user, errors.New(string(bodyBytes))
}
if err != nil {
return user, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&user)
if err != nil {
return user, err
}
return user, nil
}
func (c Client) ExchangeCode(code string) (auth.TokenResponse, error) {
var tokenResponse auth.TokenResponse
requestBody := url.Values{
"grant_type": {"authorization_code"},
"code": {code},
"redirect_uri": {c.RedirectUri},
}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/%s", api, "/oauth2/token"), strings.NewReader(requestBody.Encode()))
if err != nil {
return tokenResponse, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(c.ClientId, c.ClientSecret)
resp, err := http.DefaultClient.Do(req)
if resp.StatusCode != http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return tokenResponse, err
}
return tokenResponse, errors.New(string(bodyBytes))
}
if err != nil {
return tokenResponse, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&tokenResponse)
if err != nil {
return tokenResponse, err
}
return tokenResponse, nil
}