1
|
|
|
package oauth |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"bytes" |
5
|
|
|
"context" |
6
|
|
|
"fmt" |
7
|
|
|
"io" |
8
|
|
|
"net/http" |
9
|
|
|
|
10
|
|
|
"github.com/memnix/memnix-rest/config" |
11
|
|
|
"github.com/memnix/memnix-rest/infrastructures" |
12
|
|
|
"github.com/pkg/errors" |
13
|
|
|
"github.com/rs/zerolog/log" |
14
|
|
|
) |
15
|
|
|
|
16
|
|
|
// GetDiscordAccessToken gets the access token from Discord |
17
|
|
|
func GetDiscordAccessToken(code string) (string, error) { |
18
|
|
|
reqBody := bytes.NewBuffer([]byte(fmt.Sprintf( |
19
|
|
|
"client_id=%s&client_secret=%s&grant_type=authorization_code&redirect_uri=%s&code=%s&scope=identify,email", |
20
|
|
|
infrastructures.AppConfig.DiscordConfig.ClientID, |
21
|
|
|
infrastructures.AppConfig.DiscordConfig.ClientSecret, |
22
|
|
|
config.GetCurrentURL()+"/v2/security/discord_callback", |
23
|
|
|
code, |
24
|
|
|
))) |
25
|
|
|
|
26
|
|
|
// POST request to set URL |
27
|
|
|
req, reqerr := http.NewRequestWithContext(context.Background(), |
28
|
|
|
http.MethodPost, |
29
|
|
|
"https://discord.com/api/oauth2/token", |
30
|
|
|
reqBody, |
31
|
|
|
) |
32
|
|
|
if reqerr != nil || req == nil || req.Body == nil || req.Header == nil { |
33
|
|
|
log.Debug().Err(reqerr).Msg("discord.go: GetDiscordAccessToken: Request failed (reqerr != nil || req == nil || req.Body == nil || req.Header == nil)") |
34
|
|
|
return "", errors.Wrap(reqerr, "get discord access token request failed") |
35
|
|
|
} |
36
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
37
|
|
|
req.Header.Set("Accept", "application/json") |
38
|
|
|
|
39
|
|
|
// Get the response |
40
|
|
|
resp, resperr := http.DefaultClient.Do(req) |
41
|
|
|
if resperr != nil || resp == nil || resp.Body == nil { |
42
|
|
|
log.Debug().Err(resperr).Msg("discord.go: GetDiscordAccessToken: Response failed (resperr != nil || resp == nil || resp.Body == nil)") |
43
|
|
|
return "", errors.Wrap(resperr, "get discord access token response failed") |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Response body converted to stringified JSON |
47
|
|
|
respbody, _ := io.ReadAll(resp.Body) |
48
|
|
|
|
49
|
|
|
// Represents the response received from Github |
50
|
|
|
type discordAccessTokenResponse struct { |
51
|
|
|
AccessToken string `json:"access_token"` |
52
|
|
|
TokenType string `json:"token_type"` |
53
|
|
|
Scope string `json:"scope"` |
54
|
|
|
Expires int `json:"expires_in"` |
55
|
|
|
RefreshToken string `json:"refresh_token"` |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Convert stringified JSON to a struct object of type githubAccessTokenResponse |
59
|
|
|
var ghresp discordAccessTokenResponse |
60
|
|
|
err := config.JSONHelper.Unmarshal(respbody, &ghresp) |
61
|
|
|
if err != nil { |
62
|
|
|
return "", err |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Return the access token (as the rest of the |
66
|
|
|
// details are relatively unnecessary for us) |
67
|
|
|
return ghresp.AccessToken, nil |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// GetDiscordData gets the user data from Discord |
71
|
|
|
func GetDiscordData(accessToken string) (string, error) { |
72
|
|
|
req, reqerr := http.NewRequestWithContext(context.Background(), |
73
|
|
|
http.MethodGet, |
74
|
|
|
"https://discord.com/api/users/@me", |
75
|
|
|
nil, |
76
|
|
|
) |
77
|
|
|
|
78
|
|
|
if reqerr != nil || req == nil || req.Body == nil || req.Header == nil { |
79
|
|
|
log.Debug().Err(reqerr).Msg("discord.go: GetDiscordData: Request failed (reqerr != nil || req == nil || req.Body == nil || req.Header == nil)") |
80
|
|
|
return "", errors.Wrap(reqerr, "get discord data request failed") |
81
|
|
|
} |
82
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", accessToken)) |
83
|
|
|
|
84
|
|
|
// Get the response |
85
|
|
|
resp, resperr := http.DefaultClient.Do(req) |
86
|
|
|
if resperr != nil || resp == nil || resp.Body == nil { |
87
|
|
|
log.Debug().Err(resperr).Msg("discord.go: GetDiscordData: Response failed (resperr != nil || resp == nil || resp.Body == nil)") |
88
|
|
|
return "", errors.Wrap(resperr, "get discord data response failed") |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Response body converted to stringified JSON |
92
|
|
|
respbody, _ := io.ReadAll(resp.Body) |
93
|
|
|
|
94
|
|
|
return string(respbody), nil |
95
|
|
|
} |
96
|
|
|
|