Sha256: f09c3c1ed972e0e72bd1a321bd5ac297b5b5558898ca03571d221bb58cb4d023

Contents?: true

Size: 1.76 KB

Versions: 34

Compression:

Stored size: 1.76 KB

Contents

require 'yao/client'

module Yao
  class TokenV3
    def self.issue(cli, auth_info)
      t = new(auth_info)
      t.refresh(cli)
      t
    end

    def initialize(auth_info, token_data=nil)
      @auth_info = auth_info

      @endpoints = {}
    end
    attr_accessor :auth_info, :token, :issued_at, :expire_at, :endpoints
    alias expires expire_at
    alias to_s token

    def register(response)
      @token = response.headers["X-Subject-Token"]

      token_data = response.body["token"]
      @issued_at = Time.parse(token_data["issued_at"]).localtime
      @expire_at = Time.parse(token_data["expires_at"]).localtime
      Yao.current_tenant_id token_data["project"]["id"]
    end

    def expired?
      return true unless self.expire_at
      Time.now >= self.expire_at
    end

    def refresh(cli)
      @endpoints.clear

      res = cli.post("#{Yao.config.auth_url}/auth/tokens") do |req|
        req.body = auth_info.to_json
        req.headers['Content-Type'] = 'application/json'
      end

      register(res)
      register_endpoints(res.body["token"]["catalog"])
      self
    end

    def register_endpoints(_endpoints)
      return unless _endpoints

      _endpoints.each do |endpoint_data|
        type = endpoint_data["type"]
        region_name = Yao.config.region_name ? Yao.config.region_name : 'RegionOne'
        endpoints = endpoint_data["endpoints"].select { |ep| ep.has_value?(region_name) }
        urls = {}
        endpoints.each do |ep|
          name = "#{ep["interface"]}_url".to_sym
          urls[name] = ep["url"]
        end
        @endpoints[type] = urls
      end

      Yao.default_client.register_endpoints(@endpoints, token: self)
    end
  end

  def self.current_tenant_id(id=nil)
    if id
      @__tenant_id = id
    end
    @__tenant_id
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
yao-0.21.0 lib/yao/tokenv3.rb
yao-0.20.0 lib/yao/tokenv3.rb
yao-0.19.0 lib/yao/tokenv3.rb
yao-0.18.0 lib/yao/tokenv3.rb
yao-0.17.0 lib/yao/tokenv3.rb
yao-0.16.0 lib/yao/tokenv3.rb
yao-0.15.0 lib/yao/tokenv3.rb
yao-0.14.0 lib/yao/tokenv3.rb
yao-0.13.4 lib/yao/tokenv3.rb
yao-0.13.3 lib/yao/tokenv3.rb
yao-0.13.2 lib/yao/tokenv3.rb
yao-0.13.1 lib/yao/tokenv3.rb
yao-0.13.0 lib/yao/tokenv3.rb
yao-0.12.0 lib/yao/tokenv3.rb
yao-0.11.3 lib/yao/tokenv3.rb
yao-0.11.2 lib/yao/tokenv3.rb
yao-0.11.1 lib/yao/tokenv3.rb
yao-0.11.0 lib/yao/tokenv3.rb
yao-0.10.1 lib/yao/tokenv3.rb
yao-0.10.0 lib/yao/tokenv3.rb