Passed
Push — main ( aafc79...d8fe53 )
by Yume
01:26 queued 13s
created

oauth.GetDiscordAccessToken   B

Complexity

Conditions 6

Size

Total Lines 56
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 35
nop 1
dl 0
loc 56
rs 8.1066
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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