Class: RightScale::AuthClient

Inherits:
Object
  • Object
show all
Defined in:
lib/right_agent/clients/auth_client.rb

Overview

Abstract base class for authorization client

Constant Summary collapse

PERMITTED_STATE_TRANSITIONS =
{
:pending      => [:pending, :authorized, :unauthorized, :failed, :closed],
:authorized   => [:authorized, :unauthorized, :expired, :failed, :closed],
:unauthorized => [:authorized, :unauthorized, :failed, :closed],
:expired      => [:authorized, :unauthorized, :expired, :failed, :closed],
:failed       => [:failed, :closed],
:closed      => [:closed] }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AuthClient

Initialize client Derived classes need to call reset_stats

Raises:

  • (NotImplementedError)


42
43
44
# File 'lib/right_agent/clients/auth_client.rb', line 42

def initialize(options = {})
  raise NotImplementedError, "#{self.class.name} is an abstract class"
end

Instance Attribute Details

#stateObject

State of authorization: :pending, :authorized, :unauthorized, :expired, :failed, :closed



30
31
32
# File 'lib/right_agent/clients/auth_client.rb', line 30

def state
  @state
end

Instance Method Details

#account_idInteger

Account if any to which agent using this client belongs

Returns:

  • (Integer)

    account ID

Raises:



81
82
83
84
# File 'lib/right_agent/clients/auth_client.rb', line 81

def 
  check_authorized
  @account_id
end

#api_urlString

URL for accessing RightApi including base path

Returns:

  • (String)

    URL including base path

Raises:



92
93
94
95
# File 'lib/right_agent/clients/auth_client.rb', line 92

def api_url
  check_authorized
  @api_url
end

#auth_headerHash

Authorization header to be added to HTTP request

Returns:

  • (Hash)

    authorization header

Raises:



71
72
73
74
# File 'lib/right_agent/clients/auth_client.rb', line 71

def auth_header
  check_authorized
  {"Authorization" => "Bearer #{@access_token}"}
end

#closeTrueClass

Take any actions necessary to quiesce client interaction in preparation for agent termination but allow any active requests to complete

Returns:

  • (TrueClass)

    always true



139
140
141
142
# File 'lib/right_agent/clients/auth_client.rb', line 139

def close
  self.state = :closed
  true
end

#communicated { ... } ⇒ TrueClass

Set callback for each successful communication excluding health checks Multiple callbacks are supported

Yields:

  • required block executed after successful communication

Returns:

  • (TrueClass)

    always true

Raises:

  • (ArgumentError)

    block missing



165
166
167
168
169
170
# File 'lib/right_agent/clients/auth_client.rb', line 165

def communicated(&callback)
  raise ArgumentError, "Block missing" unless callback
  @communicated_callbacks ||= []
  @communicated_callbacks << callback
  true
end

#expiredTrueClass

An HTTP request had an authorization expiration error Renew authorization

Returns:

  • (TrueClass)

    always true



119
120
121
122
123
124
# File 'lib/right_agent/clients/auth_client.rb', line 119

def expired
  Log.info("Renewing authorization for #{identity} because request failed due to expiration")
  self.state = :expired
  renew_authorization
  true
end

#headersHash

Headers to be added to HTTP request Include authorization header by default

Returns:

  • (Hash)

    headers to be added to request header

Raises:



60
61
62
63
# File 'lib/right_agent/clients/auth_client.rb', line 60

def headers
  check_authorized
  auth_header
end

#identityString

Identity of agent using this client

Returns:

  • (String)

    identity



49
50
51
# File 'lib/right_agent/clients/auth_client.rb', line 49

def identity
  @identity
end

#modeSymbol

RightNet communication mode

Returns:

  • (Symbol)

    :http or :amqp



111
112
113
# File 'lib/right_agent/clients/auth_client.rb', line 111

def mode
  @mode
end

#redirect(location) ⇒ TrueClass

An HTTP request received a redirect response

Parameters:

  • location (String)

    to which response indicated to redirect

Returns:

  • (TrueClass)

    always true



131
132
133
# File 'lib/right_agent/clients/auth_client.rb', line 131

def redirect(location)
  true
end

#router_urlString

URL for accessing RightNet router including base path

Returns:

  • (String)

    URL including base path

Raises:



103
104
105
106
# File 'lib/right_agent/clients/auth_client.rb', line 103

def router_url
  check_authorized
  @router_url
end

#stats(reset = false) ⇒ Hash

Current statistics for this client

Parameters:

  • reset (Boolean) (defaults to: false)

    the statistics after getting the current ones

Returns:

  • (Hash)

    current statistics

    Hash, NilClass

    “state” Activity stats or nil if none



178
179
180
181
182
183
# File 'lib/right_agent/clients/auth_client.rb', line 178

def stats(reset = false)
  stats = {}
  @stats.each { |k, v| stats[k] = v.all }
  reset_stats if reset
  stats
end

#status {|type, status| ... } ⇒ Symbol

Record callback to be notified of authorization status changes Multiple callbacks are supported

Yields:

  • (type, status)

    called when status changes (optional)

Yield Parameters:

  • type (Symbol)

    of client reporting status change: :auth

  • state (Symbol)

    of authorization

Returns:

  • (Symbol)

    current state



152
153
154
155
# File 'lib/right_agent/clients/auth_client.rb', line 152

def status(&callback)
  @status_callbacks = (@status_callbacks || []) << callback if callback
  state
end