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:



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

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
# File 'lib/right_hook/authenticator.rb', line 14

def interactive_build(username)
  require 'io/console'
  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.



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

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



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

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.



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

def list_authorizations
  _client.authorizations
end