Class: Rack::Webmoney

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/webmoney.rb

Overview

A Rack middleware that provides a more HTTPish API around Webmoney authentication

You trigger an Webmoney request similar to HTTP authentication. From your app, return a “401 Unauthorized” and a “WWW-Authenticate” header with the identifier you would like to validate.

On competition, the Webmoney response is automatically verified and assigned to env["rack.webmoney.response"].

Defined Under Namespace

Classes: Response, WmLib

Constant Summary collapse

HTTP_METHODS =

:stopdoc:

%w(GET HEAD PUT POST DELETE OPTIONS)
RESPONSE =
"rack.webmoney.response"
AUTHENTICATE_HEADER =
"WWW-Authenticate"
AUTHENTICATE_REGEXP =
/^Webmoney/
URL_FIELD_SELECTOR =
lambda { |field| field.to_s =~ %r{^https://} }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Webmoney

Initialize middleware with application

use Rack::Webmoney, :credentials => {:site_holder_wmid => 'your_site_holder_wmid',
                                     :site_rid => 'your_site_rid'},
                    :mode => Rails.env


149
150
151
152
153
154
155
156
157
158
# File 'lib/rack/webmoney.rb', line 149

def initialize(app, opts ={})
  @app = app
  @credentials = opts[:credentials]
  @mode = opts[:mode]
  @wm_instance = if defined?(Rails)
                   Rails.webmoney
                 else
                   WmLib.new(:wmid => @credentials[:site_holder_wmid])
                 end
end

Instance Attribute Details

#credentialsObject (readonly)

Returns the value of attribute credentials.



139
140
141
# File 'lib/rack/webmoney.rb', line 139

def credentials
  @credentials
end

#modeObject (readonly)

Returns the value of attribute mode.



139
140
141
# File 'lib/rack/webmoney.rb', line 139

def mode
  @mode
end

#wm_instanceObject (readonly)

Returns the value of attribute wm_instance.



139
140
141
# File 'lib/rack/webmoney.rb', line 139

def wm_instance
  @wm_instance
end

Class Method Details

.build_header(params = {}) ⇒ Object

Helper method for building the “WWW-Authenticate” header value.

Rack::Webmoney.build_header(:site_rid => "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
  #=> Webmoney site_rid="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"


89
90
91
92
93
94
95
96
97
# File 'lib/rack/webmoney.rb', line 89

def self.build_header(params = {})
  'Webmoney ' + params.map { |key, value|
    if value.is_a?(Array)
      "#{key}=\"#{value.join(',')}\""
    else
      "#{key}=\"#{value}\""
    end
  }.join(', ')
end

.parse_header(str) ⇒ Object

Helper method for parsing “WWW-Authenticate” header values into a hash.

Rack::Webmoney.parse_header("Webmoney site_rid="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
  #=> {:site_rid => "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}


104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rack/webmoney.rb', line 104

def self.parse_header(str)
  params = {}
  if str =~ AUTHENTICATE_REGEXP
    str = str.gsub(/#{AUTHENTICATE_REGEXP}\s+/, '')
    str.split(', ').each { |pair|
      key, *value = pair.split('=')
      value = value.join('=')
      value.gsub!(/^\"/, '').gsub!(/\"$/, "")
      value = value.split(',')
      params[key] = value.length > 1 ? value : value.first
    }
  end
  params
end

Instance Method Details

#call(env) ⇒ Object

Standard Rack call dispatch that accepts an env and returns a [status, header, body] response.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rack/webmoney.rb', line 162

def call(env)
  req = Rack::Request.new(env)
  if req.params["WmLogin_WMID"] || req.params["WmLogin_KeeperRetStr"]
    complete_authentication(env)
  end

  status, headers, body = @app.call(env)

  qs = headers[AUTHENTICATE_HEADER]
  if status.to_i == 401 && qs && qs.match(AUTHENTICATE_REGEXP)
    begin_authentication(env, qs)
  else
    [status, headers, body]
  end
end