Class: Wazuh::Sawyer::Connection::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/wazuh/sawyer/token.rb

Class Method Summary collapse

Class Method Details

.jwt(endpoint, options, basic_user, basic_password) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/wazuh/sawyer/token.rb', line 5

def self.jwt(endpoint, options, basic_user, basic_password)
  if !@_token || (@_exp && @_exp -3 <= Time.now.to_i)
    options[:url] = endpoint
    options[:headers].merge!({'Authorization' => "Basic " + Base64.strict_encode64(basic_user + ':' + basic_password).strip})

    token = ::Faraday.new(options) {|f| f.response :json }.get('/security/user/authenticate').body['data']['token']
    @_exp = ::JWT.decode(token, nil, false).first['exp'].to_i
    ::JWT.decode(token, nil, false).first
    @_token = token
  end
  @_token
end