added logout

This commit is contained in:
2026-01-24 15:23:28 +11:00
parent c780050f8e
commit 9497c17c30
14 changed files with 327 additions and 191 deletions

View File

@@ -11,6 +11,7 @@ import (
"github.com/pkg/errors"
)
// Token represents a response from the Discord OAuth API after a successful authorization request
type Token struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
@@ -22,46 +23,32 @@ type Token struct {
const oauthurl string = "https://discord.com/oauth2/authorize"
const apiurl string = "https://discord.com/api/v10"
func GetOAuthLink(cfg *Config, state, trustedHost string) (string, error) {
if cfg == nil {
return "", errors.New("cfg cannot be nil")
}
// GetOAuthLink generates a new Discord OAuth2 link for user authentication
func (api *APIClient) GetOAuthLink(state string) (string, error) {
if state == "" {
return "", errors.New("state cannot be empty")
}
if trustedHost == "" {
return "", errors.New("trustedHost cannot be empty")
}
values := url.Values{}
values.Add("response_type", "code")
values.Add("client_id", cfg.ClientID)
values.Add("scope", cfg.OAuthScopes)
values.Add("client_id", api.cfg.ClientID)
values.Add("scope", api.cfg.OAuthScopes)
values.Add("state", state)
values.Add("redirect_uri", fmt.Sprintf("%s/%s", trustedHost, cfg.RedirectPath))
values.Add("redirect_uri", fmt.Sprintf("%s/%s", api.trustedHost, api.cfg.RedirectPath))
values.Add("prompt", "none")
return fmt.Sprintf("%s?%s", oauthurl, values.Encode()), nil
}
func AuthorizeWithCode(cfg *Config, code, trustedHost string, apiClient *APIClient) (*Token, error) {
// AuthorizeWithCode uses a users authorization token generated by OAuth2 to get a token for
// making requests to the API on behalf of the user
func (api *APIClient) AuthorizeWithCode(code string) (*Token, error) {
if code == "" {
return nil, errors.New("code cannot be empty")
}
if cfg == nil {
return nil, errors.New("config cannot be nil")
}
if trustedHost == "" {
return nil, errors.New("trustedHost cannot be empty")
}
if apiClient == nil {
return nil, errors.New("apiClient cannot be nil")
}
// Prepare form data
data := url.Values{}
data.Set("grant_type", "authorization_code")
data.Set("code", code)
data.Set("redirect_uri", fmt.Sprintf("%s/%s", trustedHost, cfg.RedirectPath))
// Create request
data.Set("redirect_uri", fmt.Sprintf("%s/%s", api.trustedHost, api.cfg.RedirectPath))
req, err := http.NewRequest(
"POST",
apiurl+"/oauth2/token",
@@ -70,27 +57,21 @@ func AuthorizeWithCode(cfg *Config, code, trustedHost string, apiClient *APIClie
if err != nil {
return nil, errors.Wrap(err, "failed to create request")
}
// Set headers
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// Set basic auth (client_id and client_secret)
req.SetBasicAuth(cfg.ClientID, cfg.ClientSecret)
// Execute request with rate limit handling
resp, err := apiClient.Do(req)
req.SetBasicAuth(api.cfg.ClientID, api.cfg.ClientSecret)
resp, err := api.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to execute request")
}
defer resp.Body.Close()
// Read response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read response body")
}
// Check status code
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("discord API returned status %d: %s", resp.StatusCode, string(body))
}
// Parse JSON response
var tokenResp Token
if err := json.Unmarshal(body, &tokenResp); err != nil {
return nil, errors.Wrap(err, "failed to parse token response")
@@ -98,21 +79,14 @@ func AuthorizeWithCode(cfg *Config, code, trustedHost string, apiClient *APIClie
return &tokenResp, nil
}
func RefreshToken(cfg *Config, token *Token, apiClient *APIClient) (*Token, error) {
// RefreshToken uses the refresh token to generate a new token pair
func (api *APIClient) RefreshToken(token *Token) (*Token, error) {
if token == nil {
return nil, errors.New("token cannot be nil")
}
if cfg == nil {
return nil, errors.New("config cannot be nil")
}
if apiClient == nil {
return nil, errors.New("apiClient cannot be nil")
}
// Prepare form data
data := url.Values{}
data.Set("grant_type", "refresh_token")
data.Set("refresh_token", token.RefreshToken)
// Create request
req, err := http.NewRequest(
"POST",
apiurl+"/oauth2/token",
@@ -121,27 +95,21 @@ func RefreshToken(cfg *Config, token *Token, apiClient *APIClient) (*Token, erro
if err != nil {
return nil, errors.Wrap(err, "failed to create request")
}
// Set headers
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// Set basic auth (client_id and client_secret)
req.SetBasicAuth(cfg.ClientID, cfg.ClientSecret)
// Execute request with rate limit handling
resp, err := apiClient.Do(req)
req.SetBasicAuth(api.cfg.ClientID, api.cfg.ClientSecret)
resp, err := api.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to execute request")
}
defer resp.Body.Close()
// Read response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read response body")
}
// Check status code
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("discord API returned status %d: %s", resp.StatusCode, string(body))
}
// Parse JSON response
var tokenResp Token
if err := json.Unmarshal(body, &tokenResp); err != nil {
return nil, errors.Wrap(err, "failed to parse token response")
@@ -149,21 +117,14 @@ func RefreshToken(cfg *Config, token *Token, apiClient *APIClient) (*Token, erro
return &tokenResp, nil
}
func RevokeToken(cfg *Config, token *Token, apiClient *APIClient) error {
// RevokeToken sends a request to the Discord API to revoke the token pair
func (api *APIClient) RevokeToken(token *Token) error {
if token == nil {
return errors.New("token cannot be nil")
}
if cfg == nil {
return errors.New("config cannot be nil")
}
if apiClient == nil {
return errors.New("apiClient cannot be nil")
}
// Prepare form data
data := url.Values{}
data.Set("token", token.AccessToken)
data.Set("token_type_hint", "access_token")
// Create request
req, err := http.NewRequest(
"POST",
apiurl+"/oauth2/token/revoke",
@@ -172,18 +133,14 @@ func RevokeToken(cfg *Config, token *Token, apiClient *APIClient) error {
if err != nil {
return errors.Wrap(err, "failed to create request")
}
// Set headers
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// Set basic auth (client_id and client_secret)
req.SetBasicAuth(cfg.ClientID, cfg.ClientSecret)
// Execute request with rate limit handling
resp, err := apiClient.Do(req)
req.SetBasicAuth(api.cfg.ClientID, api.cfg.ClientSecret)
resp, err := api.Do(req)
if err != nil {
return errors.Wrap(err, "failed to execute request")
}
defer resp.Body.Close()
// Check status code
if resp.StatusCode != http.StatusOK {
return errors.Errorf("discord API returned status %d", resp.StatusCode)
}