Class: Azure::ServiceBus::Auth::WrapService

Inherits:
Core::FilteredService
  • Object
show all
Defined in:
lib/azure/service_bus/auth/wrap_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(host = nil, issuer = nil, access_key = nil, options = {}) ⇒ WrapService

Returns a new instance of WrapService.



23
24
25
26
27
28
# File 'lib/azure/service_bus/auth/wrap_service.rb', line 23

def initialize(host=nil, issuer=nil, access_key=nil, options={})
  host = host || (options[:client] || Azure).acs_host
  super(host, options)
  @issuer = issuer || client.sb_issuer
  @access_key = access_key || client.sb_access_key
end

Instance Method Details

#get_access_token(resource_uri) ⇒ Object

Gets a WRAP access token with specified parameters.

Returns access token (String)



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/azure/service_bus/auth/wrap_service.rb', line 33

def get_access_token(resource_uri)
    uri = wrap_uri

    body = get_wrap_acs_body(resource_uri)

    headers = {
      "Content-Type" => "application/x-www-form-urlencoded",
      "Content-Length" => "0"
    }

    response = call(:post, uri, body, headers)
    parse_token(response.body)
end

#get_wrap_acs_body(resource_uri) ⇒ Object

Generate the wrap ACS body for the given uri as a String

resource_uri - The resource URI

Returns a url-encoded String



52
53
54
55
56
57
58
59
60
61
# File 'lib/azure/service_bus/auth/wrap_service.rb', line 52

def get_wrap_acs_body(resource_uri)
  non_ssl_uri = resource_uri.dup
  non_ssl_uri.scheme = 'http'
  params = {
    :wrap_scope => non_ssl_uri.to_s,
    :wrap_name => @issuer,
    :wrap_password => @access_key
  }
  ::URI.encode_www_form(params)
end

#parse_token(body) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/azure/service_bus/auth/wrap_service.rb', line 76

def parse_token(body)
  begin
    decoded = URI.decode_www_form(body.strip)
    token = decoded.assoc("wrap_access_token").last

    expires_in = decoded.assoc("wrap_access_token_expires_in").last.to_i
    return { :token => token, :expiration => Time.now.to_i + expires_in / 2 }
  rescue => e
    raise "Cannot get the access token from returned string: %s" % body
  end
end

#wrap_uri(path = "WRAPv0.9", query = {}) ⇒ Object

Generate the URI for the ACS Wrap service

path - String. Path for the uri (optional, Default=“WRAPv0.9”) query - Hash. Query parameters for the uri (optional)

Returns a URI.



69
70
71
72
73
74
# File 'lib/azure/service_bus/auth/wrap_service.rb', line 69

def wrap_uri(path="WRAPv0.9", query={})
  query = query || {}
  uri = URI.parse(File.join(host, path))
  uri.query = URI.encode_www_form(query) unless query.empty?
  uri
end