Class: Rack::Jive::SignedRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/jive/signed_request.rb,
lib/rack/jive/signed_request/version.rb

Constant Summary collapse

VERSION =
"0.1.5"

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}, &block) ⇒ SignedRequest

Returns a new instance of SignedRequest.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rack/jive/signed_request.rb', line 10

def initialize(app, opts={}, &block)
	@app = app

	if block_given?
		if block.arity == 1
			block.call(self)
		else
			instance_eval(&block)
		end
	end
end

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rack/jive/signed_request.rb', line 22

def call(env)
	request = Request.new(env)

	# Only bother authenticating if the request is identifying itself as signed
	if env["HTTP_X_SHINDIG_AUTHTYPE"] === "signed" || env["HTTP_AUTHORIZATION"].to_s.match(/^JiveEXTN/)
		auth_header_params = ::CGI.parse env["HTTP_AUTHORIZATION"].gsub(/^JiveEXTN\s/,'')

		begin
			secret = @secret.call(auth_header_params)
			if ::Jive::SignedRequest.authenticate(env["HTTP_AUTHORIZATION"], secret)
				env["jive.user_id"] = env["HTTP_X_JIVE_USER_ID"]
				env["jive.email"] = env["HTTP_X_JIVE_USER_EMAIL"]
				env["jive.external"] = (env["HTTP_X_JIVE_USER_EXTERNAL"] === "true")
				env["jive.tenant_id"] = auth_header_params["tenant_id"].first
				env["jive.client_id"] = auth_header_params["client_id"].first
			else
				env["jive.errors.signed_request"] = "Could not authenticate"
			end
		rescue ArgumentError => $e
			env["jive.errors.signed_request"] = $e.message
		end
	end

	@app.call(env)
end

#secret(&block) ⇒ Object



48
49
50
# File 'lib/rack/jive/signed_request.rb', line 48

def secret(&block)
	@secret = block
end