-
Notifications
You must be signed in to change notification settings - Fork 128
/
github.go
109 lines (90 loc) · 2.33 KB
/
github.go
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
package oauth
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
)
// NewGithubValidator creates new github validator
func NewGithubValidator(client *http.Client, clientID, clientSecret string, scopes []string) *GithubValidator {
return &GithubValidator{
client: client,
clientID: clientID,
clientSecret: clientSecret,
scopes: scopes,
}
}
// GithubValidator is github oauth validator
type GithubValidator struct {
client *http.Client
clientID string
clientSecret string
scopes []string
}
// Validate validates oauth token
func (v GithubValidator) Validate(accessToken string) error {
uri := fmt.Sprintf("https://api.github.com/applications/%s/token", v.clientID)
data := GithubValidatorRequest{
AccessToken: accessToken,
}
body, err := json.Marshal(data)
if err != nil {
return err
}
parsedURI, err := url.Parse(uri)
if err != nil {
return err
}
parsedURI.User = url.UserPassword(v.clientID, v.clientSecret)
req, err := http.NewRequest(http.MethodPost, parsedURI.String(), bytes.NewBuffer(body))
if err != nil {
return err
}
resp, err := v.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return fmt.Errorf("status: %s", resp.Status)
}
result, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
var token GithubValidatorResponse
if err = json.Unmarshal(result, &token); err != nil {
return err
}
if token.ExpiresAt != nil && token.ExpiresAt.Before(time.Now()) {
return fmt.Errorf("token expired at %v", token.ExpiresAt)
}
scopeMap := make(map[string]struct{}, len(token.Scopes))
for _, scope := range token.Scopes {
scopeMap[scope] = struct{}{}
}
for _, scope := range v.scopes {
if _, ok := scopeMap[scope]; !ok {
return fmt.Errorf("token doesn't contain scope %s", scope)
}
}
return nil
}
// GetEndpoint returns endpoint
func (v GithubValidator) GetEndpoint() oauth2.Endpoint {
return github.Endpoint
}
// GithubValidatorRequest contains github validation request
type GithubValidatorRequest struct {
AccessToken string `json:"access_token"`
}
// GithubValidatorResponse contains github validation response
type GithubValidatorResponse struct {
ExpiresAt *time.Time `json:"expires_at"`
Scopes []string `json:"scopes"`
}