Class: Yao::Token
- Inherits:
-
Object
- Object
- Yao::Token
- Defined in:
- lib/yao/token.rb
Instance Attribute Summary collapse
-
#auth_info ⇒ Object
Returns the value of attribute auth_info.
-
#endpoints ⇒ Object
Returns the value of attribute endpoints.
-
#expire_at ⇒ Object
(also: #expires)
Returns the value of attribute expire_at.
-
#issued_at ⇒ Object
Returns the value of attribute issued_at.
-
#token ⇒ Object
(also: #to_s)
Returns the value of attribute token.
Class Method Summary collapse
Instance Method Summary collapse
- #expired? ⇒ Boolean
-
#initialize(auth_info, token_data = nil) ⇒ Token
constructor
A new instance of Token.
- #refresh(cli) ⇒ Object
- #register(token_data) ⇒ Object
- #register_endpoints(_endpoints) ⇒ Object
Constructor Details
#initialize(auth_info, token_data = nil) ⇒ Token
Returns a new instance of Token.
12 13 14 15 16 |
# File 'lib/yao/token.rb', line 12 def initialize(auth_info, token_data=nil) @auth_info = auth_info @endpoints = {} end |
Instance Attribute Details
#auth_info ⇒ Object
Returns the value of attribute auth_info.
17 18 19 |
# File 'lib/yao/token.rb', line 17 def auth_info @auth_info end |
#endpoints ⇒ Object
Returns the value of attribute endpoints.
17 18 19 |
# File 'lib/yao/token.rb', line 17 def endpoints @endpoints end |
#expire_at ⇒ Object Also known as: expires
Returns the value of attribute expire_at.
17 18 19 |
# File 'lib/yao/token.rb', line 17 def expire_at @expire_at end |
#issued_at ⇒ Object
Returns the value of attribute issued_at.
17 18 19 |
# File 'lib/yao/token.rb', line 17 def issued_at @issued_at end |
#token ⇒ Object Also known as: to_s
Returns the value of attribute token.
17 18 19 |
# File 'lib/yao/token.rb', line 17 def token @token end |
Class Method Details
.issue(cli, auth_info) ⇒ Object
6 7 8 9 10 |
# File 'lib/yao/token.rb', line 6 def self.issue(cli, auth_info) t = new(auth_info) t.refresh(cli) t end |
Instance Method Details
#expired? ⇒ Boolean
28 29 30 31 |
# File 'lib/yao/token.rb', line 28 def expired? return true unless self.expire_at Time.now >= self.expire_at end |
#refresh(cli) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/yao/token.rb', line 33 def refresh(cli) @endpoints.clear res = cli.post("#{Yao.config.auth_url}/tokens") do |req| req.body = auth_info.to_json req.headers['Content-Type'] = 'application/json' end body = res.body["access"] register(body["token"]) register_endpoints(body["serviceCatalog"]) self end |
#register(token_data) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/yao/token.rb', line 21 def register(token_data) @token = token_data["id"] @issued_at = Time.parse(token_data["issued_at"]).localtime @expire_at = Time.parse(token_data["expires"]).localtime Yao.current_tenant_id token_data["tenant"]["id"] end |
#register_endpoints(_endpoints) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/yao/token.rb', line 47 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' endpoint = endpoint_data["endpoints"].find { |ep| ep.has_value?(region_name) } urls = {} if endpoint urls[:public_url] = endpoint["publicURL"] if endpoint["publicURL"] urls[:admin_url] = endpoint["adminURL"] if endpoint["adminURL"] end @endpoints[type] = urls end Yao.default_client.register_endpoints(@endpoints, token: self) end |