-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord.go
96 lines (79 loc) · 2.44 KB
/
discord.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package oauth
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
type DiscordResponse struct {
ID string `json:"id"`
Username string `json:"username"`
AvatarHash string `json:"avatar"`
GlobalName string `json:"global_name"`
}
type DiscordOauth struct{}
func (o *DiscordOauth) LoginURL(cfg *OAuthConfig) string {
return fmt.Sprintf("https://discord.com/api/oauth2/authorize?client_id=%s&redirect_uri=%s&response_type=code&scope=identify", cfg.ClientID, url.QueryEscape(cfg.CallbackURL))
}
func (o *DiscordOauth) RequestAccessToken(code string, cfg *OAuthConfig) (string, error) {
q := url.Values{}
q.Add("client_id", cfg.ClientID)
q.Add("client_secret", cfg.Secret)
q.Add("redirect_uri", cfg.CallbackURL)
q.Add("code", code)
q.Add("grant_type", "authorization_code")
q.Add("scope", "identify connections")
buf := bytes.NewBufferString(q.Encode())
req, err := http.NewRequest("POST", "https://discord.com/api/oauth2/token", buf)
if err != nil {
return "", err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
if resp.StatusCode != 200 {
return "", fmt.Errorf("invalid status code in token-response: %d", resp.StatusCode)
}
defer resp.Body.Close()
tokenData := AccessTokenResponse{}
err = json.NewDecoder(resp.Body).Decode(&tokenData)
if err != nil {
return "", err
}
return tokenData.AccessToken, nil
}
func (o *DiscordOauth) RequestUserInfo(access_token string, cfg *OAuthConfig) (*OauthUserInfo, error) {
req, err := http.NewRequest("GET", "https://discord.com/api/users/@me", nil)
if err != nil {
return nil, nil
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Bearer "+access_token)
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("invalid status code in response: %d", resp.StatusCode)
}
userData := DiscordResponse{}
err = json.NewDecoder(resp.Body).Decode(&userData)
if err != nil {
return nil, err
}
info := OauthUserInfo{
Provider: ProviderTypeDiscord,
Name: userData.Username,
ExternalID: userData.ID,
DisplayName: userData.GlobalName,
AvatarURL: fmt.Sprintf("https://cdn.discordapp.com/avatars/%s/%s.png", userData.ID, userData.AvatarHash),
}
return &info, nil
}