1
|
|
|
package oauth |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"bytes" |
5
|
|
|
"fmt" |
6
|
|
|
"io" |
7
|
|
|
"net/http" |
8
|
|
|
|
9
|
|
|
"github.com/memnix/memnix-rest/config" |
10
|
|
|
"github.com/memnix/memnix-rest/infrastructures" |
11
|
|
|
"github.com/rs/zerolog/log" |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
// GetGithubAccessToken gets the access token from Github using the code |
15
|
|
|
func GetGithubAccessToken(code string) (string, error) { |
16
|
|
|
// Set us the request body as JSON |
17
|
|
|
requestBodyMap := map[string]string{ |
18
|
|
|
"client_id": infrastructures.GetAppConfig().GithubConfig.ClientID, |
19
|
|
|
"client_secret": infrastructures.GetAppConfig().GithubConfig.ClientSecret, |
20
|
|
|
"code": code, |
21
|
|
|
} |
22
|
|
|
requestJSON, _ := config.JSONHelper.Marshal(requestBodyMap) |
23
|
|
|
|
24
|
|
|
// POST request to set URL |
25
|
|
|
req, reqerr := http.NewRequest( |
26
|
|
|
"POST", |
27
|
|
|
"https://github.com/login/oauth/access_token", |
28
|
|
|
bytes.NewBuffer(requestJSON), |
29
|
|
|
) |
30
|
|
|
if reqerr != nil { |
31
|
|
|
log.Info().Msg("Request failed") |
32
|
|
|
} |
33
|
|
|
req.Header.Set("Content-Type", "application/json") |
34
|
|
|
req.Header.Set("Accept", "application/json") |
35
|
|
|
|
36
|
|
|
// Get the response |
37
|
|
|
resp, resperr := http.DefaultClient.Do(req) |
38
|
|
|
if resperr != nil { |
39
|
|
|
log.Info().Msg("Response failed") |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// Response body converted to stringified JSON |
43
|
|
|
respbody, _ := io.ReadAll(resp.Body) |
44
|
|
|
|
45
|
|
|
// Represents the response received from Github |
46
|
|
|
type githubAccessTokenResponse struct { |
47
|
|
|
AccessToken string `json:"access_token"` |
48
|
|
|
TokenType string `json:"token_type"` |
49
|
|
|
Scope string `json:"scope"` |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Convert stringified JSON to a struct object of type githubAccessTokenResponse |
53
|
|
|
var ghresp githubAccessTokenResponse |
54
|
|
|
err := config.JSONHelper.Unmarshal(respbody, &ghresp) |
55
|
|
|
if err != nil { |
56
|
|
|
return "", err |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Return the access token (as the rest of the |
60
|
|
|
// details are relatively unnecessary for us) |
61
|
|
|
return ghresp.AccessToken, nil |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// GetGithubData gets the user data from Github using the access token |
65
|
|
|
func GetGithubData(accessToken string) (string, error) { |
66
|
|
|
// Get request to a set URL |
67
|
|
|
req, err := http.NewRequest( |
68
|
|
|
"GET", |
69
|
|
|
"https://api.github.com/user", |
70
|
|
|
nil, |
71
|
|
|
) |
72
|
|
|
if err != nil { |
73
|
|
|
log.Info().Msg("Request failed") |
74
|
|
|
return "", err |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Set the Authorization header before sending the request |
78
|
|
|
// Authorization: token XXXXXXXXXXXXXXXXXXXXXXXXXXX |
79
|
|
|
authorizationHeaderValue := fmt.Sprintf("token %s", accessToken) |
80
|
|
|
req.Header.Set("Authorization", authorizationHeaderValue) |
81
|
|
|
|
82
|
|
|
// Make the request |
83
|
|
|
resp, err := http.DefaultClient.Do(req) |
84
|
|
|
if err != nil { |
85
|
|
|
log.Info().Msg("Response failed") |
86
|
|
|
return "", err |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Read the response as a byte slice |
90
|
|
|
respbody, _ := io.ReadAll(resp.Body) |
91
|
|
|
|
92
|
|
|
// Convert byte slice to string and return |
93
|
|
|
return string(respbody), nil |
94
|
|
|
} |
95
|
|
|
|