Class: Legion::Extensions::Github::Helpers::BrowserAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/github/helpers/browser_auth.rb

Constant Summary collapse

DEFAULT_SCOPES =
'repo admin:org admin:repo_hook read:user'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret: nil, scopes: DEFAULT_SCOPES, auth: nil) ⇒ BrowserAuth

Returns a new instance of BrowserAuth.



17
18
19
20
21
22
# File 'lib/legion/extensions/github/helpers/browser_auth.rb', line 17

def initialize(client_id:, client_secret: nil, scopes: DEFAULT_SCOPES, auth: nil, **)
  @client_id = client_id
  @client_secret = client_secret
  @scopes = scopes
  @auth = auth || Object.new.extend(OAuth::Runners::Auth)
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



15
16
17
# File 'lib/legion/extensions/github/helpers/browser_auth.rb', line 15

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



15
16
17
# File 'lib/legion/extensions/github/helpers/browser_auth.rb', line 15

def client_secret
  @client_secret
end

#scopesObject (readonly)

Returns the value of attribute scopes.



15
16
17
# File 'lib/legion/extensions/github/helpers/browser_auth.rb', line 15

def scopes
  @scopes
end

Instance Method Details

#authenticateObject



24
25
26
27
28
29
30
# File 'lib/legion/extensions/github/helpers/browser_auth.rb', line 24

def authenticate
  if gui_available?
    authenticate_browser
  else
    authenticate_device_code
  end
end

#gui_available?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
# File 'lib/legion/extensions/github/helpers/browser_auth.rb', line 32

def gui_available?
  os = host_os
  return true if /darwin|mswin|mingw/.match?(os)

  !ENV['DISPLAY'].nil? || !ENV['WAYLAND_DISPLAY'].nil?
end

#open_browser(url) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/legion/extensions/github/helpers/browser_auth.rb', line 39

def open_browser(url)
  cmd = case host_os
        when /darwin/      then 'open'
        when /linux/       then 'xdg-open'
        when /mswin|mingw/ then 'start'
        end
  return false unless cmd

  system(cmd, url)
end