Class: Captive::GithubDeviceFlow

Inherits:
Object
  • Object
show all
Defined in:
lib/captive/github_device_flow.rb

Constant Summary collapse

CLIENT_ID =
"Iv23liHr9n6eK4XhwtnP"
RETRIABLE_ERRORS =
%w[authorization_pending slow_down].freeze
DeviceFlowError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(http: HttpClient.new) ⇒ GithubDeviceFlow

Returns a new instance of GithubDeviceFlow.



9
10
11
# File 'lib/captive/github_device_flow.rb', line 9

def initialize(http: HttpClient.new)
  @http = http
end

Instance Method Details

#poll_token(device_code:, interval:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/captive/github_device_flow.rb', line 16

def poll_token(device_code:, interval:)
  loop do
    sleep(interval)
    data = @http.post("https://github.com/login/oauth/access_token",
                      client_id: CLIENT_ID, device_code: device_code,
                      grant_type: "urn:ietf:params:oauth:grant-type:device_code")
    return data["access_token"] if data["access_token"]
    raise DeviceFlowError, data["error"] unless RETRIABLE_ERRORS.include?(data["error"])
  end
end

#request_device_code ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/captive/github_device_flow.rb', line 27

def request_device_code
  data = @http.post("https://github.com/login/device/code", client_id: CLIENT_ID)
  raise DeviceFlowError, "Erreur GitHub : #{data.inspect}" unless data["user_code"]

  {
    device_code:      data["device_code"],
    user_code:        data["user_code"],
    verification_uri: data["verification_uri"],
    interval:         data["interval"],
  }
end