Sha256: d5c772dfead5729c07504d119513634c9366146774f1f3da0353e39da5e3c65e

Contents?: true

Size: 844 Bytes

Versions: 1

Compression:

Stored size: 844 Bytes

Contents

require "jwt"

module VNCPostAPI
  class UserLogin < ::ActiveResource::Base
    self.include_root_in_json = false
    self.include_format_in_path = false
    self.prefix = "/User/Login"
    self.element_name = ""

    class << self
      def bearer_token(username: VNCPostAPI.config.username, password: VNCPostAPI.config.password)
        cache_key = "VNCPostAPI/bearer_token"
        cached_token = VNCPostAPI.cache.read(cache_key)
        return cached_token if cached_token

        token = create(Username: username, Password: password).token
        VNCPostAPI.cache.write(
          cache_key,
          token,
          expires_in: expires_in(token) - 15 # clear cache earlier
        )

        token
      end

      private

      def expires_in(jwt)
        Time.at(JWT.decode(jwt, nil, false)[0]["exp"])
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vncpost_api-0.3.0 lib/vncpost_api/resources/user_login.rb