File: token_check.rb

package info (click to toggle)
ruby-gh 0.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,644 kB
  • sloc: ruby: 1,793; makefile: 4
file content (35 lines) | stat: -rw-r--r-- 921 bytes parent folder | download
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
# frozen_string_literal: true

require 'gh/error'
require 'base64'

module GH
  class TokenCheck < Wrapper
    attr_accessor :client_id, :client_secret, :token

    def setup(backend, options)
      @client_secret = options[:client_secret]
      @client_id = options[:client_id]
      @token = options[:token]
      @check_token = true
      super
    end

    def check_token
      return unless @check_token && client_id && client_secret && token

      @check_token = false

      auth_header = 'Basic %s' % Base64.strict_encode64("#{client_id}:#{client_secret}")
      http :post, path_for("/applications/#{client_id}/token"), body: "{\"access_token\": \"#{token}\"}",
                                                                'Authorization' => auth_header
    rescue GH::Error(response_status: 404) => e
      raise GH::TokenInvalid, e
    end

    def http(*)
      check_token
      super
    end
  end
end