Class: CFoundry::AuthToken

Inherits:
Object
  • Object
show all
Defined in:
lib/cfoundry/auth_token.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_header, refresh_token = nil) ⇒ AuthToken

Returns a new instance of AuthToken.



19
20
21
22
# File 'lib/cfoundry/auth_token.rb', line 19

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

Instance Attribute Details

#auth_headerObject

Returns the value of attribute auth_header.



24
25
26
# File 'lib/cfoundry/auth_token.rb', line 24

def auth_header
  @auth_header
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



25
26
27
# File 'lib/cfoundry/auth_token.rb', line 25

def refresh_token
  @refresh_token
end

Class Method Details

.from_hash(hash) ⇒ Object



11
12
13
14
15
16
# File 'lib/cfoundry/auth_token.rb', line 11

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

.from_uaa_token_info(token_info) ⇒ Object



4
5
6
7
8
9
# File 'lib/cfoundry/auth_token.rb', line 4

def from_uaa_token_info(token_info)
  new(
    token_info.auth_header,
    token_info.info[:refresh_token]
  )
end

Instance Method Details

#expirationObject



55
56
57
# File 'lib/cfoundry/auth_token.rb', line 55

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

#expires_soon?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/cfoundry/auth_token.rb', line 59

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

#to_hashObject



27
28
29
30
31
32
# File 'lib/cfoundry/auth_token.rb', line 27

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

#token_dataObject

TODO: rename to #data



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cfoundry/auth_token.rb', line 37

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