lib/yao/auth.rb in yao-0.0.2.rc3 vs lib/yao/auth.rb in yao-0.0.2
- old
+ new
@@ -1,74 +1,38 @@
require 'yao'
require 'json'
require 'time'
-module Yao::Auth
+require 'yao/token'
+
+module Yao
%i(tenant_name username password).each do |name|
- Yao.config.param name, nil do |_|
- Yao::Auth.try_new
- end
+ Yao.config.param name, nil
end
- class << self
- def try_new
- if Yao.config.tenant_name && Yao.config.username && Yao.config.password && Yao.default_client
- Yao::Auth.new
+ module Auth
+ class << self
+ def try_new
+ if Yao.config.tenant_name && Yao.config.username && Yao.config.password && Yao.default_client
+ Yao::Auth.new
+ end
end
- end
- def new(
- tenant_name: Yao.config.tenant_name,
- username: Yao.config.username,
- password: Yao.config.password
- )
- authinfo = {
- auth: {
- passwordCredentials: {
- username: username, password: password
+ def new(
+ tenant_name: Yao.config.tenant_name,
+ username: Yao.config.username,
+ password: Yao.config.password
+ )
+ auth_info = {
+ auth: {
+ passwordCredentials: {
+ username: username, password: password
+ }
}
}
- }
- authinfo[:auth][:tenantName] = tenant_name if tenant_name
+ auth_info[:auth][:tenantName] = tenant_name if tenant_name
- reply = Yao.default_client.default.post('/v2.0/tokens') do |req|
- req.body = authinfo.to_json
- req.headers['Content-Type'] = 'application/json'
+ return Token.issue(Yao.default_client.default, auth_info)
end
-
- body = reply.body["access"]
-
- token = Token.new(body["token"])
- token.register_endpoints(body["serviceCatalog"])
- return token
- end
- end
-
- class Token
- def initialize(token_data)
- @token = token_data["id"]
- @issued_at = Time.parse token_data["issued_at"]
- @expire_at = Time.parse token_data["expires"]
-
- @endpoints = {}
- end
- attr_accessor :token, :issued_at, :expire_at, :endpoints
- alias expires expire_at
-
- def register_endpoints(_endpoints)
- return unless _endpoints
-
- _endpoints.each do |endpoint_data|
- key = endpoint_data["type"]
- value = if d = endpoint_data["endpoints"].first
- d["publicURL"]
- else
- nil
- end
- if value
- @endpoints[key] = value
- end
- end
- Yao.default_client.register_endpoints(@endpoints, token: @token)
end
end
end