bixby-auth 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,88 @@
1
+ module ApiAuth
2
+
3
+ module RequestDrivers # :nodoc:
4
+
5
+ class RackRequest # :nodoc:
6
+
7
+ include ApiAuth::Helpers
8
+
9
+ def initialize(request)
10
+ @request = request
11
+ @headers = fetch_headers
12
+ true
13
+ end
14
+
15
+ def set_auth_header(header)
16
+ @request.env.merge!({ "Authorization" => header })
17
+ @headers = fetch_headers
18
+ @request
19
+ end
20
+
21
+ def calculated_md5
22
+ if @request.body
23
+ body = @request.body.read
24
+ @request.body.rewind
25
+ else
26
+ body = ''
27
+ end
28
+ md5_base64digest(body)
29
+ end
30
+
31
+ def populate_content_md5
32
+ if ['POST', 'PUT'].include?(@request.request_method)
33
+ @request.env["Content-MD5"] = calculated_md5
34
+ end
35
+ end
36
+
37
+ def md5_mismatch?
38
+ if ['POST', 'PUT'].include?(@request.request_method)
39
+ calculated_md5 != content_md5
40
+ else
41
+ false
42
+ end
43
+ end
44
+
45
+ def fetch_headers
46
+ capitalize_keys @request.env
47
+ end
48
+
49
+ def content_type
50
+ value = find_header(%w(CONTENT-TYPE CONTENT_TYPE HTTP_CONTENT_TYPE))
51
+ value.nil? ? "" : value
52
+ end
53
+
54
+ def content_md5
55
+ value = find_header(%w(CONTENT-MD5 CONTENT_MD5 HTTP-CONTENT-MD5 HTTP_CONTENT_MD5))
56
+ value.nil? ? "" : value
57
+ end
58
+
59
+ def request_uri
60
+ @request.url
61
+ end
62
+
63
+ def set_date
64
+ @request.env.merge!({ "DATE" => time_as_httpdate })
65
+ end
66
+
67
+ def timestamp
68
+ value = find_header(%w(DATE HTTP_DATE))
69
+ value.nil? ? "" : value
70
+ end
71
+
72
+ def authorization_header
73
+ find_header %w(Authorization AUTHORIZATION HTTP_AUTHORIZATION)
74
+ end
75
+
76
+ private
77
+
78
+ def find_header(keys)
79
+ keys.map {|key| @headers[key] }.compact.first
80
+ end
81
+
82
+ end
83
+
84
+ drivers["Rack::Request"] = RackRequest
85
+
86
+ end
87
+
88
+ end
@@ -0,0 +1,98 @@
1
+ # give access to RestClient @processed_headers
2
+ module RestClient;class Request;attr_accessor :processed_headers;end;end
3
+
4
+ module ApiAuth
5
+
6
+ module RequestDrivers # :nodoc:
7
+
8
+ class RestClientRequest # :nodoc:
9
+
10
+ include ApiAuth::Helpers
11
+
12
+ def initialize(request)
13
+ @request = request
14
+ @headers = fetch_headers
15
+ true
16
+ end
17
+
18
+ def set_auth_header(header)
19
+ @request.headers.merge!({ "Authorization" => header })
20
+ save_headers # enforce update of processed_headers based on last updated headers
21
+ @request
22
+ end
23
+
24
+ def calculated_md5
25
+ if @request.payload
26
+ body = @request.payload.read
27
+ @request.payload.instance_variable_get(:@stream).seek(0)
28
+ else
29
+ body = ''
30
+ end
31
+ md5_base64digest(body)
32
+ end
33
+
34
+ def populate_content_md5
35
+ if [:post, :put].include?(@request.method)
36
+ @request.headers["Content-MD5"] = calculated_md5
37
+ save_headers
38
+ end
39
+ end
40
+
41
+ def md5_mismatch?
42
+ if [:post, :put].include?(@request.method)
43
+ calculated_md5 != content_md5
44
+ else
45
+ false
46
+ end
47
+ end
48
+
49
+ def fetch_headers
50
+ capitalize_keys @request.processed_headers
51
+ end
52
+
53
+ def content_type
54
+ value = find_header(%w(CONTENT-TYPE CONTENT_TYPE HTTP_CONTENT_TYPE))
55
+ value.nil? ? "": value
56
+ end
57
+
58
+ def content_md5
59
+ value = find_header(%w(CONTENT-MD5 CONTENT_MD5))
60
+ value.nil? ? "" : value
61
+ end
62
+
63
+ def request_uri
64
+ @request.url
65
+ end
66
+
67
+ def set_date
68
+ @request.headers.merge!({ "DATE" => time_as_httpdate })
69
+ save_headers
70
+ end
71
+
72
+ def timestamp
73
+ value = find_header(%w(DATE HTTP_DATE))
74
+ value.nil? ? "" : value
75
+ end
76
+
77
+ def authorization_header
78
+ find_header %w(Authorization AUTHORIZATION HTTP_AUTHORIZATION)
79
+ end
80
+
81
+ private
82
+
83
+ def find_header(keys)
84
+ keys.map {|key| @headers[key] }.compact.first
85
+ end
86
+
87
+ def save_headers
88
+ @request.processed_headers = @request.make_headers(@request.headers)
89
+ @headers = fetch_headers
90
+ end
91
+
92
+ end
93
+
94
+ drivers["RestClient::Request"] = RestClientRequest
95
+
96
+ end
97
+
98
+ end
@@ -0,0 +1,21 @@
1
+
2
+
3
+ module ApiAuth
4
+ module RequestDrivers
5
+ class << self
6
+
7
+ def drivers
8
+ @drivers ||= {}
9
+ end
10
+
11
+ end
12
+ end
13
+ end
14
+
15
+ require 'api_auth/request_drivers/net_http'
16
+ require 'api_auth/request_drivers/curb'
17
+ require 'api_auth/request_drivers/rest_client'
18
+ require 'api_auth/request_drivers/action_controller'
19
+ require 'api_auth/request_drivers/action_dispatch'
20
+ require 'api_auth/request_drivers/rack'
21
+ require 'api_auth/request_drivers/httpi'
data/lib/api_auth.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+
4
+ require 'api_auth/errors'
5
+ require 'api_auth/helpers'
6
+
7
+ require 'api_auth/request_drivers'
8
+ require 'api_auth/headers'
9
+ require 'api_auth/base'
10
+ require 'api_auth/railtie'
data/lib/bixby-auth.rb ADDED
@@ -0,0 +1,3 @@
1
+
2
+ require "api_auth"
3
+ require "api_auth/request_drivers/bixby_request"