forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
234 lines (185 loc) · 5.17 KB
/
client.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package github
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
)
var (
// DefaultURL is the default location for the GitHub API.
DefaultURL = "https://api.github.com"
)
var (
// errTwoFactor is returned when two factor authentication is required
// to create an authorization for the user.
errTwoFactor = errors.New("github: two factor required")
// errUnauthorized is returned if the request to create an authorization
// results in a 401.
errUnauthorized = errors.New("github: unauthorized")
// errNoToken is returned if there was no access token in the github
// response.
errNoToken = errors.New("github: no token in response")
)
const (
// HeaderTwoFactor is the HTTP header that github users for two factor
// authentication.
//
// In a request, the value of this header should be the two factor auth
// code. In a response, the presence of this header indicates that two
// factor authentication is required for the user. See
// http://goo.gl/h7al6K for more information.
HeaderTwoFactor = "X-GitHub-OTP"
)
// CreateAuthorizationOpts is a set of options used when creating a GitHub
// authorization.
type CreateAuthorizationOpts struct {
Scopes []string
ClientID string
ClientSecret string
Username string
Password string
TwoFactor string
}
// Authorization represents a GitHub Authorization. See http://goo.gl/bs9I3o for
// more information.
type Authorization struct {
Token string `json:"token"`
}
type User struct {
Login string `json:"login"`
}
// Client is a github client.
type Client struct {
// The github api url. The zero value is https://api.github.com.
URL string
client *http.Client
}
// CreateAuthorization creates a new GitHub authorization (or returns the
// existing authorization if present) for the GitHub OAuth application. See
// http://goo.gl/bs9I3o.
func (c *Client) CreateAuthorization(opts CreateAuthorizationOpts) (*Authorization, error) {
f := struct {
Scopes []string `json:"scopes"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
}{
Scopes: opts.Scopes,
ClientID: opts.ClientID,
ClientSecret: opts.ClientSecret,
}
req, err := c.NewRequest("POST", "/authorizations", f)
if err != nil {
return nil, err
}
req.SetBasicAuth(opts.Username, opts.Password)
// If a two factor auth code is present, set the `X-GitHub-OTP` header
// value. See http://goo.gl/Lumn6s.
if opts.TwoFactor != "" {
req.Header.Set(HeaderTwoFactor, opts.TwoFactor)
}
var a Authorization
resp, err := c.Do(req, &a)
if err != nil {
return nil, err
}
// When the `X-GitHub-OTP` header is present in the response, it means
// a two factor auth code needs to be provided.
if resp.Header.Get(HeaderTwoFactor) != "" {
return nil, errTwoFactor
}
if resp.StatusCode == 401 {
return nil, errUnauthorized
}
if err := checkResponse(resp); err != nil {
return nil, err
}
if a.Token == "" {
return nil, errNoToken
}
return &a, nil
}
// GetUser makes an authenticated request to /user and returns the User.
func (c *Client) GetUser(token string) (*User, error) {
req, err := c.NewRequest("GET", "/user", nil)
if err != nil {
return nil, err
}
tokenAuth(req, token)
var u User
if _, err := c.Do(req, &u); err != nil {
return nil, err
}
return &u, nil
}
// IsMember returns true of the authenticated user is a member of the
// organization.
func (c *Client) IsMember(organization, token string) (bool, error) {
req, err := c.NewRequest("HEAD", fmt.Sprintf("/user/memberships/orgs/%s", organization), nil)
if err != nil {
return false, err
}
tokenAuth(req, token)
resp, err := c.Do(req, nil)
if err != nil {
return false, err
}
if err := checkResponse(resp); err != nil {
return false, nil
}
return true, nil
}
func (c *Client) NewRequest(method, path string, v interface{}) (*http.Request, error) {
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(v); err != nil {
return nil, err
}
url := c.URL
if url == "" {
url = DefaultURL
}
req, err := http.NewRequest(method, fmt.Sprintf("%s%s", url, path), buf)
if err != nil {
return req, err
}
req.Header.Set("Accept", "application/vnd.github.v3+json")
return req, nil
}
func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
client := c.client
if client == nil {
client = http.DefaultClient
}
resp, err := client.Do(req)
if err != nil {
return resp, err
}
if v != nil && resp.StatusCode/100 == 2 {
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(v); err != nil {
return resp, err
}
}
return resp, nil
}
// tokenAuth sets the Authorization header in a request to use an OAuth token as
// the means of authentication. See http://goo.gl/kFTlnA.
func tokenAuth(req *http.Request, token string) {
req.SetBasicAuth(token, "x-oauth-basic")
}
type errorResponse struct {
Message string `json:"message"`
}
func (e *errorResponse) Error() string {
return fmt.Sprintf("github: %s", e.Message)
}
func checkResponse(resp *http.Response) error {
if resp.StatusCode/100 != 2 {
var errResp errorResponse
if err := json.NewDecoder(resp.Body).Decode(&errResp); err != nil {
return err
}
return &errResp
}
return nil
}