Sha256: 2bec541f4f2abafb3c9ff390a98c193e9c8e56d66676bf213ec83faf71192863

Contents?: true

Size: 1.23 KB

Versions: 9

Compression:

Stored size: 1.23 KB

Contents

module CFoundry
  class AuthToken
    class << self
      def from_uaa_token_info(token_info)
        new(
          token_info.auth_header,
          token_info.info[:refresh_token]
        )
      end

      def from_hash(hash)
        new(
          hash[:token],
          hash[:refresh_token]
        )
      end
    end

    def initialize(auth_header, refresh_token = nil)
      @auth_header = auth_header
      @refresh_token = refresh_token
    end

    attr_accessor :auth_header
    attr_reader :refresh_token

    def to_hash
      {
        :token => auth_header,
        :refresh_token => @refresh_token
      }
    end

    JSON_HASH = /\{.*?\}/.freeze

    # TODO: rename to #data
    def token_data
      return @token_data if @token_data
      return {} unless @auth_header

      data_json = @auth_header.split(" ", 2).last
      return {} unless data_json

      @token_data = JWT.decode(data_json, nil, false)[0].symbolize_keys
    rescue
      {}
    end

    def auth_header=(auth_header)
      @token_data = nil
      @auth_header = auth_header
    end

    def expiration
      token_data[:exp].nil? ? Time.at(0) : Time.at(token_data[:exp])
    end

    def expires_soon?
      (expiration.to_i - Time.now.to_i) < 60
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
new_cfoundry-4.9.4 lib/cfoundry/auth_token.rb
new_cfoundry-4.9.3 lib/cfoundry/auth_token.rb
ncfoundry-4.9.4 lib/cfoundry/auth_token.rb
ncfoundry-4.9.3 lib/cfoundry/auth_token.rb
ncfoundry-4.9.2 lib/cfoundry/auth_token.rb
new_cfoundry-4.9.2 lib/cfoundry/auth_token.rb
new_cfoundry-4.9.1 lib/cfoundry/auth_token.rb
new_cfoundry-4.9.0 lib/cfoundry/auth_token.rb
new_cfoundry-4.8.3 lib/cfoundry/auth_token.rb