Class: Chronicle::ETL::OauthAuthorizer

Inherits:
Authorizer
  • Object
show all
Defined in:
lib/chronicle/etl/oauth_authorizer.rb

Overview

An authorization strategy that uses oauth2 (and omniauth under the hood)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Authorizer

find_by_provider, provider

Constructor Details

#initialize(port:, credentials: {}) ⇒ OauthAuthorizer

Create a new instance of OauthAuthorizer



42
43
44
45
46
# File 'lib/chronicle/etl/oauth_authorizer.rb', line 42

def initialize(port:, credentials: {})
  @port = port
  @credentials = credentials
  super
end

Class Attribute Details

.authorization_to_secret_mapObject (readonly)

Returns the value of attribute authorization_to_secret_map.



9
10
11
# File 'lib/chronicle/etl/oauth_authorizer.rb', line 9

def authorization_to_secret_map
  @authorization_to_secret_map
end

.client_idObject

Returns the value of attribute client_id.



10
11
12
# File 'lib/chronicle/etl/oauth_authorizer.rb', line 10

def client_id
  @client_id
end

.client_secretObject

Returns the value of attribute client_secret.



10
11
12
# File 'lib/chronicle/etl/oauth_authorizer.rb', line 10

def client_secret
  @client_secret
end

.provider_nameObject (readonly)

Returns the value of attribute provider_name.



9
10
11
# File 'lib/chronicle/etl/oauth_authorizer.rb', line 9

def provider_name
  @provider_name
end

.strategyObject (readonly)

Returns the value of attribute strategy.



9
10
11
# File 'lib/chronicle/etl/oauth_authorizer.rb', line 9

def strategy
  @strategy
end

Instance Attribute Details

#authorizationObject (readonly)

Returns the value of attribute authorization.



39
40
41
# File 'lib/chronicle/etl/oauth_authorizer.rb', line 39

def authorization
  @authorization
end

Class Method Details

.allObject

Returns all subclasses of OauthAuthorizer (Used by AuthorizationServer to build omniauth providers)



34
35
36
# File 'lib/chronicle/etl/oauth_authorizer.rb', line 34

def all
  ObjectSpace.each_object(::Class).select { |klass| klass < self }
end

.omniauth_strategy(strategy) ⇒ Object

Macro for specifying which omniauth strategy to use



13
14
15
# File 'lib/chronicle/etl/oauth_authorizer.rb', line 13

def omniauth_strategy(strategy)
  @strategy = strategy
end

.optionsObject

Macro for specifying options to pass to omniauth



28
29
30
# File 'lib/chronicle/etl/oauth_authorizer.rb', line 28

def options
  @options ||= {}
end

.pluck_secrets(map) ⇒ Object

Macro for specifying hash of returned authorization to secrets hash



23
24
25
# File 'lib/chronicle/etl/oauth_authorizer.rb', line 23

def pluck_secrets(map)
  @authorization_to_secret_map = map
end

.scope(value) ⇒ Object

Macro for specifying which omniauth scopes to request



18
19
20
# File 'lib/chronicle/etl/oauth_authorizer.rb', line 18

def scope(value)
  options[:scope] = value
end

Instance Method Details

#authorize!Object

Start up an authorization server and handle the oauth flow



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chronicle/etl/oauth_authorizer.rb', line 49

def authorize!
  associate_oauth_credentials
  @server = load_server
  spinner = TTY::Spinner.new(':spinner :title', format: :dots_2)
  spinner.auto_spin
  spinner.update(title: "Starting temporary authorization server on port #{@port}"'')

  server_thread = start_authorization_server(port: @port)
  start_oauth_flow

  spinner.update(title: 'Waiting for authorization to complete in your browser')
  sleep 0.1 while authorization_pending?(server_thread)

  @server.quit!
  server_thread.join
  spinner.success("(#{'successful'.green})")

  # TODO: properly handle failed authorizations
  raise Chronicle::ETL::AuthorizationError unless @server.latest_authorization

  @authorization = @server.latest_authorization

  extract_secrets(authorization: @authorization, pluck_values: self.class.authorization_to_secret_map)
end