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

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

Instance Attribute Summary

Attributes inherited from Core::FilteredService

#filters

Attributes inherited from Core::Service

#host

Instance Method Summary collapse

Methods inherited from Core::FilteredService

#call, #with_filter

Methods inherited from Core::Service

#call, #generate_uri

Constructor Details

#initialize(host = Azure.config.acs_host, issuer = Azure.config.sb_issuer, access_key = Azure.config.sb_access_key) ⇒ WrapService

Returns a new instance of WrapService.



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

def initialize(host=Azure.config.acs_host, issuer=Azure.config.sb_issuer, access_key=Azure.config.sb_access_key)
  super(host)
  @issuer = issuer
  @access_key = access_key
end

Instance Method Details

#get_access_token(resource_uri) ⇒ Object

Gets a WRAP access token with specified parameters.

Returns access token (String)



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

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



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

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



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

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.



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

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