(code string, c *gin.Context)
| 78 | } |
| 79 | |
| 80 | func getLinuxdoUserInfoByCode(code string, c *gin.Context) (*LinuxdoUser, error) { |
| 81 | if code == "" { |
| 82 | return nil, errors.New("invalid code") |
| 83 | } |
| 84 | |
| 85 | // Get access token using Basic auth |
| 86 | tokenEndpoint := "https://connect.linux.do/oauth2/token" |
| 87 | credentials := common.LinuxDOClientId + ":" + common.LinuxDOClientSecret |
| 88 | basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(credentials)) |
| 89 | |
| 90 | // Get redirect URI from request |
| 91 | scheme := "http" |
| 92 | if c.Request.TLS != nil { |
| 93 | scheme = "https" |
| 94 | } |
| 95 | redirectURI := fmt.Sprintf("%s://%s/api/oauth/linuxdo", scheme, c.Request.Host) |
| 96 | |
| 97 | data := url.Values{} |
| 98 | data.Set("grant_type", "authorization_code") |
| 99 | data.Set("code", code) |
| 100 | data.Set("redirect_uri", redirectURI) |
| 101 | |
| 102 | req, err := http.NewRequest("POST", tokenEndpoint, strings.NewReader(data.Encode())) |
| 103 | if err != nil { |
| 104 | return nil, err |
| 105 | } |
| 106 | |
| 107 | req.Header.Set("Authorization", basicAuth) |
| 108 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 109 | req.Header.Set("Accept", "application/json") |
| 110 | |
| 111 | client := http.Client{Timeout: 5 * time.Second} |
| 112 | res, err := client.Do(req) |
| 113 | if err != nil { |
| 114 | return nil, errors.New("failed to connect to Linux DO server") |
| 115 | } |
| 116 | defer res.Body.Close() |
| 117 | |
| 118 | var tokenRes struct { |
| 119 | AccessToken string `json:"access_token"` |
| 120 | Message string `json:"message"` |
| 121 | } |
| 122 | if err := json.NewDecoder(res.Body).Decode(&tokenRes); err != nil { |
| 123 | return nil, err |
| 124 | } |
| 125 | |
| 126 | if tokenRes.AccessToken == "" { |
| 127 | return nil, fmt.Errorf("failed to get access token: %s", tokenRes.Message) |
| 128 | } |
| 129 | |
| 130 | // Get user info |
| 131 | userEndpoint := "https://connect.linux.do/api/user" |
| 132 | req, err = http.NewRequest("GET", userEndpoint, nil) |
| 133 | if err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | req.Header.Set("Authorization", "Bearer "+tokenRes.AccessToken) |
| 137 | req.Header.Set("Accept", "application/json") |
no outgoing calls
no test coverage detected