Skip to content

Commit

Permalink
Merge pull request #1218 from octokit/tarebyte/applications-api-oops
Browse files Browse the repository at this point in the history
Fix incorrect URLs for the Authorizations client
  • Loading branch information
tarebyte committed Mar 25, 2020
2 parents c5037e4 + 7299bb5 commit 6175187
Show file tree
Hide file tree
Showing 19 changed files with 331 additions and 242 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Metrics/BlockLength:
Exclude:
spec/**/*_spec.rb
43 changes: 20 additions & 23 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'jruby-openssl', :platforms => :jruby
gem 'rake', '> 11.0.1', '< 12.0'
gem 'jruby-openssl', platforms: :jruby
gem 'rake', '~> 13.0', '>= 13.0.1'

group :development do
gem 'awesome_print', :require => 'ap'
gem 'guard-rspec', '~> 4.5'
gem 'hirb-unicode'
gem 'pry'
gem 'redcarpet'
gem 'wirb'
gem 'wirble'
gem 'awesome_print', require: 'ap'
gem 'yard'
end

group :test do
gem 'coveralls', :require => false
gem 'json', '~> 1.7', :platforms => [:jruby]
gem 'jwt', '~> 1.5', '>= 1.5.6'
gem 'multi_json', '~> 1.11.0'
gem 'mime-types', '< 2.0.0'
gem 'netrc', '~> 0.7.7'
gem 'rb-fsevent', '~> 0.9'
gem 'rspec', '~> 3.0.0'
gem 'simplecov', :require => false
gem 'vcr', '~> 4.0'
gem 'webmock', '~> 3.4', '>= 3.4.2'
gem 'coveralls', require: false
gem 'json', '~> 1.7', platforms: [:jruby]
gem 'jwt', '~> 2.2', '>= 2.2.1'
gem 'mime-types', '~> 3.3', '>= 3.3.1'
gem 'multi_json', '~> 1.14', '>= 1.14.1'
gem 'netrc', '~> 0.11.0'
gem 'rb-fsevent', '~> 0.10.3'
gem 'rspec', '~> 3.9'
gem 'simplecov', require: false
gem 'vcr', '~> 5.1'
gem 'webmock', '~> 3.8', '>= 3.8.2'
end

platforms :rbx do
gem 'psych'
gem 'rubysl', '~> 2.0'
group :test, :development do
gem 'pry-byebug'
gem 'redcarpet'
gem 'rubocop'
end

gemspec
2 changes: 2 additions & 0 deletions lib/octokit/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
require 'octokit/client/marketplace'
require 'octokit/client/milestones'
require 'octokit/client/notifications'
require 'octokit/client/oauth_applications'
require 'octokit/client/objects'
require 'octokit/client/organizations'
require 'octokit/client/pages'
Expand Down Expand Up @@ -97,6 +98,7 @@ class Client
include Octokit::Client::Marketplace
include Octokit::Client::Milestones
include Octokit::Client::Notifications
include Octokit::Client::OauthApplications
include Octokit::Client::Objects
include Octokit::Client::Organizations
include Octokit::Client::Pages
Expand Down
73 changes: 1 addition & 72 deletions lib/octokit/client/authorizations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,77 +140,6 @@ def scopes(token = @access_token, options = {})
sort
end

# Check if a token is valid.
#
# Applications can check if a token is valid without rate limits.
#
# @param token [String] 40 character GitHub OAuth access token
#
# @return [Sawyer::Resource] A single authorization for the authenticated user
# @see https://developer.github.com/v3/oauth_authorizations/#check-an-authorization
# @example
# client = Octokit::Client.new(:client_id => 'abcdefg12345', :client_secret => 'secret')
# client.check_application_authorization('deadbeef1234567890deadbeef987654321')
def check_application_authorization(token, options = {})
opts = ensure_api_media_type(:applications_api, options.dup)
opts[:access_token] = token
key = opts.delete(:client_id) || client_id
secret = opts.delete(:client_secret) || client_secret

as_app(key, secret) do |app_client|
app_client.post "applications/#{client_id}/tokens", opts
end
end

# Reset a token
#
# Applications can reset a token without requiring a user to re-authorize.
#
# @param token [String] 40 character GitHub OAuth access token
#
# @return [Sawyer::Resource] A single authorization for the authenticated user
# @see https://developer.github.com/v3/oauth_authorizations/#reset-an-authorization
# @example
# client = Octokit::Client.new(:client_id => 'abcdefg12345', :client_secret => 'secret')
# client.reset_application_authorization('deadbeef1234567890deadbeef987654321')
def reset_application_authorization(token, options = {})
opts = ensure_api_media_type(:applications_api, options.dup)
opts[:access_token] = token
key = opts.delete(:client_id) || client_id
secret = opts.delete(:client_secret) || client_secret

as_app(key, secret) do |app_client|
app_client.patch "applications/#{client_id}/tokens", opts
end
end

# Revoke a token
#
# Applications can revoke (delete) a token
#
# @param token [String] 40 character GitHub OAuth access token
#
# @return [Boolean] Result
# @see https://developer.github.com/v3/oauth_authorizations/#revoke-an-authorization-for-an-application
# @example
# client = Octokit::Client.new(:client_id => 'abcdefg12345', :client_secret => 'secret')
# client.revoke_application_authorization('deadbeef1234567890deadbeef987654321')
def revoke_application_authorization(token, options = {})
opts = ensure_api_media_type(:applications_api, options.dup)
opts[:access_token] = token
key = opts.delete(:client_id) || client_id
secret = opts.delete(:client_secret) || client_secret

as_app(key, secret) do |app_client|
app_client.delete "applications/#{client_id}/tokens", opts

app_client.last_response.status == 204
end
rescue Octokit::NotFound
false
end
alias :delete_application_authorization :revoke_application_authorization

# Revoke all tokens for an app
#
# Applications can revoke all of their tokens in a single request
Expand All @@ -236,7 +165,7 @@ def revoke_all_application_authorizations(options = {})
def authorize_url(app_id = client_id, options = {})
opts = options.dup
if app_id.to_s.empty?
raise Octokit::ApplicationCredentialsRequired.new "client_id required"
raise Octokit::ApplicationCredentialsRequired, "client_id required"
end
authorize_url = opts.delete(:endpoint) || Octokit.web_endpoint
authorize_url << "login/oauth/authorize?client_id=#{app_id}"
Expand Down
122 changes: 122 additions & 0 deletions lib/octokit/client/oauth_applications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# frozen_string_literal: true

module Octokit
class Client

# Methods for the OauthApplications API
#
# @see https://developer.github.com/v3/apps/oauth_applications
module OauthApplications

# Check if a token is valid.
#
# Applications can check if a token is valid without rate limits.
#
# @param access_token [String] 40 character GitHub OAuth access token
#
# @return [Sawyer::Resource] A single authorization for the authenticated user
# @see https://developer.github.com/v3/apps/oauth_applications/#check-a-token
#
# @example
# client = Octokit::Client.new(:client_id => 'abcdefg12345', :client_secret => 'secret')
# client.check_token('deadbeef1234567890deadbeef987654321')
def check_token(access_token, options = {})
options = ensure_api_media_type(:applications_api, options.dup)
options[:access_token] = access_token

key = options.delete(:client_id) || client_id
secret = options.delete(:client_secret) || client_secret

as_app(key, secret) do |app_client|
app_client.post "applications/#{client_id}/token", options
end
end
alias check_application_authorization check_token

# Reset a token
#
# Applications can reset a token without requiring a user to re-authorize.
#
# @param access_token [String] 40 character GitHub OAuth access token
#
# @return [Sawyer::Resource] A single authorization for the authenticated user
# @see https://developer.github.com/v3/apps/oauth_applications/#reset-a-token
#
# @example
# client = Octokit::Client.new(:client_id => 'abcdefg12345', :client_secret => 'secret')
# client.reset_token('deadbeef1234567890deadbeef987654321')
def reset_token(access_token, options = {})
options = ensure_api_media_type(:applications_api, options.dup)
options[:access_token] = access_token

key = options.delete(:client_id) || client_id
secret = options.delete(:client_secret) || client_secret

as_app(key, secret) do |app_client|
app_client.patch "applications/#{client_id}/token", options
end
end
alias reset_application_authorization reset_token

# Delete an app token
#
# Applications can revoke (delete) a token
#
# @param token [String] 40 character GitHub OAuth access token
#
# @return [Boolean] Result
# @see https://developer.github.com/v3/apps/oauth_applications/#delete-an-app-token
#
# @example
# client = Octokit::Client.new(:client_id => 'abcdefg12345', :client_secret => 'secret')
# client.delete_token('deadbeef1234567890deadbeef987654321')
def delete_app_token(access_token, options = {})
options = ensure_api_media_type(:applications_api, options.dup)
options[:access_token] = access_token

key = options.delete(:client_id) || client_id
secret = options.delete(:client_secret) || client_secret

begin
as_app(key, secret) do |app_client|
app_client.delete "applications/#{client_id}/token", options
app_client.last_response.status == 204
end
rescue Octokit::NotFound
false
end
end
alias delete_application_authorization delete_app_token
alias revoke_application_authorization delete_app_token

# Delete an app authorization
#
# OAuth application owners can revoke a grant for their OAuth application and a specific user.
#
# @param accces_token [String] 40 character GitHub OAuth access token
#
# @return [Boolean] Result
# @see https://developer.github.com/v3/apps/oauth_applications/#delete-an-app-token
#
# @example
# client = Octokit::Client.new(:client_id => 'abcdefg12345', :client_secret => 'secret')
# client.delete_app_authorization('deadbeef1234567890deadbeef987654321')
def delete_app_authorization(access_token, options = {})
options = ensure_api_media_type(:applications_api, options.dup)
options[:access_token] = access_token

key = options.delete(:client_id) || client_id
secret = options.delete(:client_secret) || client_secret

begin
as_app(key, secret) do |app_client|
app_client.delete "applications/#{client_id}/grant", options
app_client.last_response.status == 204
end
rescue Octokit::NotFound
false
end
end
end
end
end

This file was deleted.

Loading

0 comments on commit 6175187

Please sign in to comment.