-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth.go
More file actions
114 lines (97 loc) · 2.67 KB
/
Copy pathoauth.go
File metadata and controls
114 lines (97 loc) · 2.67 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package gitcode
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
const (
DefaultOAuthBaseURL = "https://gitcode.com"
)
type OAuthClient struct {
clientID string
clientSecret string
redirectURI string
baseURL string
httpClient *http.Client
}
func NewOAuthClient(clientID, clientSecret, redirectURI string) *OAuthClient {
return &OAuthClient{
clientID: clientID,
clientSecret: clientSecret,
redirectURI: redirectURI,
baseURL: DefaultOAuthBaseURL,
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
}
}
func (o *OAuthClient) SetBaseURL(baseURL string) {
o.baseURL = strings.TrimRight(baseURL, "/")
}
func (o *OAuthClient) AuthorizeURL(scope, state string) string {
params := url.Values{}
params.Set("client_id", o.clientID)
params.Set("redirect_uri", o.redirectURI)
params.Set("response_type", "code")
if scope != "" {
params.Set("scope", scope)
}
if state != "" {
params.Set("state", state)
}
return fmt.Sprintf("%s/oauth/authorize?%s", o.baseURL, params.Encode())
}
type OAuthToken struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Scope string `json:"scope"`
CreatedAt time.Time `json:"created_at"`
}
func (o *OAuthClient) ExchangeToken(ctx context.Context, code string) (*OAuthToken, error) {
data := url.Values{}
data.Set("grant_type", "authorization_code")
data.Set("code", code)
data.Set("client_id", o.clientID)
data.Set("client_secret", o.clientSecret)
return o.doTokenRequest(ctx, data)
}
func (o *OAuthClient) RefreshToken(ctx context.Context, refreshToken string) (*OAuthToken, error) {
data := url.Values{}
data.Set("grant_type", "refresh_token")
data.Set("refresh_token", refreshToken)
return o.doTokenRequest(ctx, data)
}
func (o *OAuthClient) doTokenRequest(ctx context.Context, data url.Values) (*OAuthToken, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, o.baseURL+"/oauth/token", strings.NewReader(data.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := o.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("OAuth token request failed with %d: %s", resp.StatusCode, string(body))
}
var token OAuthToken
if err := json.Unmarshal(body, &token); err != nil {
return nil, err
}
return &token, nil
}
func NewClientFromOAuthToken(token *OAuthToken) *Client {
c := NewClient(token.AccessToken)
return c
}