Class: Manabu::Connection::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/manabu/connection/auth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, host, port, **options) ⇒ Auth

Returns a new instance of Auth.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/manabu/connection/auth.rb', line 8

def initialize(username, password, host, port, **options)
  @username = username
  @host = host
  @port = port
  @transactor = Transactor.new(host, port,
                               options.fetch(:force_secure_connection, true),
                               options.fetch(:transport_type, :msgpack),
                               options)
  @connection = false
  _authenticate(username, password)

  ObjectSpace.define_finalizer(self, -> { @connection = false })
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



6
7
8
# File 'lib/manabu/connection/auth.rb', line 6

def connection
  @connection
end

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/manabu/connection/auth.rb', line 6

def host
  @host
end

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/manabu/connection/auth.rb', line 6

def port
  @port
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



6
7
8
# File 'lib/manabu/connection/auth.rb', line 6

def refresh_token
  @refresh_token
end

#tokenObject

Returns the value of attribute token.



6
7
8
# File 'lib/manabu/connection/auth.rb', line 6

def token
  @token
end

#transactorObject

Returns the value of attribute transactor.



6
7
8
# File 'lib/manabu/connection/auth.rb', line 6

def transactor
  @transactor
end

#usernameObject

Returns the value of attribute username.



6
7
8
# File 'lib/manabu/connection/auth.rb', line 6

def username
  @username
end

Instance Method Details

#_authenticate(username, password) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/manabu/connection/auth.rb', line 34

def _authenticate(username, password)
  response = @transactor.post("authenticate", username: username, password: password)
  @connection = true

  @token = response[:tokens][:auth_token]
  @transactor.authorization = @token

  _refresh(response[:tokens])
end

#_refresh(tokens) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/manabu/connection/auth.rb', line 44

def _refresh(tokens)
  @refresh_token = tokens[:refresh_token]
  thread = Thread.new do
    loop do
      break unless @connection
      sleep(120)
      refresh_response = transactor.post("authenticate/refresh",
                                         refresh_token: @refresh_token
                                        )
      @transactor.authorization = refresh_response[:tokens][:auth_token]
      @refresh_token = refresh_response[:tokens][:refresh_token]
    end
  end
end

#full_hostObject



30
31
32
# File 'lib/manabu/connection/auth.rb', line 30

def full_host
  @transactor.full_host
end

#stopObject



26
27
28
# File 'lib/manabu/connection/auth.rb', line 26

def stop()

end

#success?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/manabu/connection/auth.rb', line 22

def success?()
  @connection
end