File: oauth_applications.rb

package info (click to toggle)
ruby-octokit 10.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,092 kB
  • sloc: ruby: 13,339; sh: 99; makefile: 7; javascript: 3
file content (116 lines) | stat: -rw-r--r-- 4,365 bytes parent folder | download | duplicates (2)
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
# 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[: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[: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 access_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_token('deadbeef1234567890deadbeef987654321')
      def delete_app_token(access_token, options = {})
        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 access_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[: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