Skip to content

Commit 419f07c

Browse files
author
Clark Bynum
authored
Update and remove OAuth endpoints (#1461)
Fixes #1331.
1 parent dce4b6b commit 419f07c

File tree

4 files changed

+65
-547
lines changed

4 files changed

+65
-547
lines changed

github/authorizations.go

Lines changed: 37 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -134,137 +134,6 @@ func (a AuthorizationUpdateRequest) String() string {
134134
return Stringify(a)
135135
}
136136

137-
// List the authorizations for the authenticated user.
138-
//
139-
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#list-your-authorizations
140-
func (s *AuthorizationsService) List(ctx context.Context, opts *ListOptions) ([]*Authorization, *Response, error) {
141-
u := "authorizations"
142-
u, err := addOptions(u, opts)
143-
if err != nil {
144-
return nil, nil, err
145-
}
146-
147-
req, err := s.client.NewRequest("GET", u, nil)
148-
if err != nil {
149-
return nil, nil, err
150-
}
151-
152-
var auths []*Authorization
153-
resp, err := s.client.Do(ctx, req, &auths)
154-
if err != nil {
155-
return nil, resp, err
156-
}
157-
return auths, resp, nil
158-
}
159-
160-
// Get a single authorization.
161-
//
162-
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#get-a-single-authorization
163-
func (s *AuthorizationsService) Get(ctx context.Context, id int64) (*Authorization, *Response, error) {
164-
u := fmt.Sprintf("authorizations/%d", id)
165-
166-
req, err := s.client.NewRequest("GET", u, nil)
167-
if err != nil {
168-
return nil, nil, err
169-
}
170-
171-
a := new(Authorization)
172-
resp, err := s.client.Do(ctx, req, a)
173-
if err != nil {
174-
return nil, resp, err
175-
}
176-
return a, resp, nil
177-
}
178-
179-
// Create a new authorization for the specified OAuth application.
180-
//
181-
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#create-a-new-authorization
182-
func (s *AuthorizationsService) Create(ctx context.Context, auth *AuthorizationRequest) (*Authorization, *Response, error) {
183-
u := "authorizations"
184-
185-
req, err := s.client.NewRequest("POST", u, auth)
186-
if err != nil {
187-
return nil, nil, err
188-
}
189-
190-
a := new(Authorization)
191-
resp, err := s.client.Do(ctx, req, a)
192-
if err != nil {
193-
return nil, resp, err
194-
}
195-
return a, resp, nil
196-
}
197-
198-
// GetOrCreateForApp creates a new authorization for the specified OAuth
199-
// application, only if an authorization for that application doesn’t already
200-
// exist for the user.
201-
//
202-
// If a new token is created, the HTTP status code will be "201 Created", and
203-
// the returned Authorization.Token field will be populated. If an existing
204-
// token is returned, the status code will be "200 OK" and the
205-
// Authorization.Token field will be empty.
206-
//
207-
// clientID is the OAuth Client ID with which to create the token.
208-
//
209-
// GitHub API docs:
210-
// https://developer.github.com/v3/oauth_authorizations/#get-or-create-an-authorization-for-a-specific-app
211-
// https://developer.github.com/v3/oauth_authorizations/#get-or-create-an-authorization-for-a-specific-app-and-fingerprint
212-
func (s *AuthorizationsService) GetOrCreateForApp(ctx context.Context, clientID string, auth *AuthorizationRequest) (*Authorization, *Response, error) {
213-
var u string
214-
if auth.Fingerprint == nil || *auth.Fingerprint == "" {
215-
u = fmt.Sprintf("authorizations/clients/%v", clientID)
216-
} else {
217-
u = fmt.Sprintf("authorizations/clients/%v/%v", clientID, *auth.Fingerprint)
218-
}
219-
220-
req, err := s.client.NewRequest("PUT", u, auth)
221-
if err != nil {
222-
return nil, nil, err
223-
}
224-
225-
a := new(Authorization)
226-
resp, err := s.client.Do(ctx, req, a)
227-
if err != nil {
228-
return nil, resp, err
229-
}
230-
231-
return a, resp, nil
232-
}
233-
234-
// Edit a single authorization.
235-
//
236-
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#update-an-existing-authorization
237-
func (s *AuthorizationsService) Edit(ctx context.Context, id int64, auth *AuthorizationUpdateRequest) (*Authorization, *Response, error) {
238-
u := fmt.Sprintf("authorizations/%d", id)
239-
240-
req, err := s.client.NewRequest("PATCH", u, auth)
241-
if err != nil {
242-
return nil, nil, err
243-
}
244-
245-
a := new(Authorization)
246-
resp, err := s.client.Do(ctx, req, a)
247-
if err != nil {
248-
return nil, resp, err
249-
}
250-
251-
return a, resp, nil
252-
}
253-
254-
// Delete a single authorization.
255-
//
256-
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#delete-an-authorization
257-
func (s *AuthorizationsService) Delete(ctx context.Context, id int64) (*Response, error) {
258-
u := fmt.Sprintf("authorizations/%d", id)
259-
260-
req, err := s.client.NewRequest("DELETE", u, nil)
261-
if err != nil {
262-
return nil, err
263-
}
264-
265-
return s.client.Do(ctx, req, nil)
266-
}
267-
268137
// Check if an OAuth token is valid for a specific app.
269138
//
270139
// Note that this operation requires the use of BasicAuth, but where the
@@ -273,14 +142,19 @@ func (s *AuthorizationsService) Delete(ctx context.Context, id int64) (*Response
273142
//
274143
// The returned Authorization.User field will be populated.
275144
//
276-
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#check-an-authorization
277-
func (s *AuthorizationsService) Check(ctx context.Context, clientID string, token string) (*Authorization, *Response, error) {
278-
u := fmt.Sprintf("applications/%v/tokens/%v", clientID, token)
145+
// GitHub API docs: https://developer.github.com/v3/apps/oauth_applications/#check-a-token
146+
func (s *AuthorizationsService) Check(ctx context.Context, clientID, accessToken string) (*Authorization, *Response, error) {
147+
u := fmt.Sprintf("applications/%v/token", clientID)
148+
149+
reqBody := &struct {
150+
AccessToken string `json:"access_token"`
151+
}{AccessToken: accessToken}
279152

280-
req, err := s.client.NewRequest("GET", u, nil)
153+
req, err := s.client.NewRequest("POST", u, reqBody)
281154
if err != nil {
282155
return nil, nil, err
283156
}
157+
req.Header.Set("Accept", mediaTypeOAuthAppPreview)
284158

285159
a := new(Authorization)
286160
resp, err := s.client.Do(ctx, req, a)
@@ -301,14 +175,19 @@ func (s *AuthorizationsService) Check(ctx context.Context, clientID string, toke
301175
//
302176
// The returned Authorization.User field will be populated.
303177
//
304-
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#reset-an-authorization
305-
func (s *AuthorizationsService) Reset(ctx context.Context, clientID string, token string) (*Authorization, *Response, error) {
306-
u := fmt.Sprintf("applications/%v/tokens/%v", clientID, token)
178+
// GitHub API docs: https://developer.github.com/v3/apps/oauth_applications/#reset-a-token
179+
func (s *AuthorizationsService) Reset(ctx context.Context, clientID, accessToken string) (*Authorization, *Response, error) {
180+
u := fmt.Sprintf("applications/%v/token", clientID)
307181

308-
req, err := s.client.NewRequest("POST", u, nil)
182+
reqBody := &struct {
183+
AccessToken string `json:"access_token"`
184+
}{AccessToken: accessToken}
185+
186+
req, err := s.client.NewRequest("PATCH", u, reqBody)
309187
if err != nil {
310188
return nil, nil, err
311189
}
190+
req.Header.Set("Accept", mediaTypeOAuthAppPreview)
312191

313192
a := new(Authorization)
314193
resp, err := s.client.Do(ctx, req, a)
@@ -325,74 +204,40 @@ func (s *AuthorizationsService) Reset(ctx context.Context, clientID string, toke
325204
// username is the OAuth application clientID, and the password is its
326205
// clientSecret. Invalid tokens will return a 404 Not Found.
327206
//
328-
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#revoke-an-authorization-for-an-application
329-
func (s *AuthorizationsService) Revoke(ctx context.Context, clientID string, token string) (*Response, error) {
330-
u := fmt.Sprintf("applications/%v/tokens/%v", clientID, token)
207+
// GitHub API docs: https://developer.github.com/v3/apps/oauth_applications/#delete-an-app-token
208+
func (s *AuthorizationsService) Revoke(ctx context.Context, clientID, accessToken string) (*Response, error) {
209+
u := fmt.Sprintf("applications/%v/token", clientID)
331210

332-
req, err := s.client.NewRequest("DELETE", u, nil)
211+
reqBody := &struct {
212+
AccessToken string `json:"access_token"`
213+
}{AccessToken: accessToken}
214+
215+
req, err := s.client.NewRequest("DELETE", u, reqBody)
333216
if err != nil {
334217
return nil, err
335218
}
219+
req.Header.Set("Accept", mediaTypeOAuthAppPreview)
336220

337221
return s.client.Do(ctx, req, nil)
338222
}
339223

340-
// ListGrants lists the set of OAuth applications that have been granted
341-
// access to a user's account. This will return one entry for each application
342-
// that has been granted access to the account, regardless of the number of
343-
// tokens an application has generated for the user.
344-
//
345-
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#list-your-grants
346-
func (s *AuthorizationsService) ListGrants(ctx context.Context, opts *ListOptions) ([]*Grant, *Response, error) {
347-
u, err := addOptions("applications/grants", opts)
348-
if err != nil {
349-
return nil, nil, err
350-
}
351-
352-
req, err := s.client.NewRequest("GET", u, nil)
353-
if err != nil {
354-
return nil, nil, err
355-
}
356-
357-
grants := []*Grant{}
358-
resp, err := s.client.Do(ctx, req, &grants)
359-
if err != nil {
360-
return nil, resp, err
361-
}
362-
363-
return grants, resp, nil
364-
}
365-
366-
// GetGrant gets a single OAuth application grant.
367-
//
368-
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#get-a-single-grant
369-
func (s *AuthorizationsService) GetGrant(ctx context.Context, id int64) (*Grant, *Response, error) {
370-
u := fmt.Sprintf("applications/grants/%d", id)
371-
req, err := s.client.NewRequest("GET", u, nil)
372-
if err != nil {
373-
return nil, nil, err
374-
}
375-
376-
grant := new(Grant)
377-
resp, err := s.client.Do(ctx, req, grant)
378-
if err != nil {
379-
return nil, resp, err
380-
}
381-
382-
return grant, resp, nil
383-
}
384-
385224
// DeleteGrant deletes an OAuth application grant. Deleting an application's
386225
// grant will also delete all OAuth tokens associated with the application for
387226
// the user.
388227
//
389-
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#delete-a-grant
390-
func (s *AuthorizationsService) DeleteGrant(ctx context.Context, id int64) (*Response, error) {
391-
u := fmt.Sprintf("applications/grants/%d", id)
392-
req, err := s.client.NewRequest("DELETE", u, nil)
228+
// GitHub API docs: https://developer.github.com/v3/apps/oauth_applications/#delete-an-app-authorization
229+
func (s *AuthorizationsService) DeleteGrant(ctx context.Context, clientID, accessToken string) (*Response, error) {
230+
u := fmt.Sprintf("applications/%v/grant", clientID)
231+
232+
reqBody := &struct {
233+
AccessToken string `json:"access_token"`
234+
}{AccessToken: accessToken}
235+
236+
req, err := s.client.NewRequest("DELETE", u, reqBody)
393237
if err != nil {
394238
return nil, err
395239
}
240+
req.Header.Set("Accept", mediaTypeOAuthAppPreview)
396241

397242
return s.client.Do(ctx, req, nil)
398243
}

0 commit comments

Comments
 (0)