Class: RightHook::Authenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/right_hook/authenticator.rb

Overview

The authenticator provides an interface to retrieving or creating GitHub authorizations.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_client) ⇒ Authenticator

:nodoc:



22
23
24
# File 'lib/right_hook/authenticator.rb', line 22

def initialize(_client)
  @_client = _client
end

Class Method Details

.build(username, password) ⇒ Object

Build a client with a username and an explicit password.



8
9
10
# File 'lib/right_hook/authenticator.rb', line 8

def build(username, password)
  new(Octokit::Client.new(login: username, password: password))
end

.interactive_build(username) ⇒ Object

Prompt the user for their password (without displaying the entered keys). This approach is offered for convenience to make it easier to not store passwords on disk.



14
15
16
17
18
# File 'lib/right_hook/authenticator.rb', line 14

def interactive_build(username)
  require 'io/console'
  puts "What is the password for #{username}? (Your typing will be hidden.)"
  new(Octokit::Client.new(login: username, password: $stdin.noecho(&:gets).chomp))
end

Instance Method Details

#create_authorization(note) ⇒ Object

Create a new GitHub authorization with the given note. If one already exists with that note, it will not create a duplicate.



28
29
30
# File 'lib/right_hook/authenticator.rb', line 28

def create_authorization(note)
  _client.create_authorization(scopes: %w(repo), note: note).token
end

#find_or_create_authorization_by_note(note) ⇒ Object

If there is already an authorization by this note, use it; otherwise create it



38
39
40
41
42
43
44
45
# File 'lib/right_hook/authenticator.rb', line 38

def find_or_create_authorization_by_note(note)
  found_auth = list_authorizations.find {|auth| auth.note == note}
  if found_auth
    found_auth.token
  else
    create_authorization(note)
  end
end

#list_authorizationsObject

Returns an array of all of the authorizations for the authenticated account.



33
34
35
# File 'lib/right_hook/authenticator.rb', line 33

def list_authorizations
  _client.authorizations
end