Sha256: 3304500b31b271932845f0b7afdc9cd37a5c1a6292d5aaf1c3efc44fbb5559ac

Contents?: true

Size: 1.29 KB

Versions: 8

Compression:

Stored size: 1.29 KB

Contents

module JFoundry
  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

      json_hashes = Base64.decode64(@auth_header.split(" ", 2).last)
      data_json = json_hashes.sub(JSON_HASH, "")[JSON_HASH]
      return {} unless data_json

      @token_data = MultiJson.load data_json, :symbolize_keys => true
    rescue MultiJson::DecodeError
      {}
    end

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

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

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
jfoundry-0.1.7 lib/jfoundry/auth_token.rb
jfoundry-0.1.6 lib/jfoundry/auth_token.rb
jfoundry-0.1.4 lib/jfoundry/auth_token.rb
jfoundry-0.1.3 lib/jfoundry/auth_token.rb
jfoundry-0.1.2 lib/jfoundry/auth_token.rb
jfoundry-0.1.1 lib/jfoundry/auth_token.rb
jfoundry-0.1.0.pre lib/jfoundry/auth_token.rb
jfoundry-0.1.0 lib/jfoundry/auth_token.rb