bixby-auth 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/Gemfile +28 -0
- data/Gemfile.lock +245 -0
- data/LICENSE.txt +20 -0
- data/README.md +19 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/bixby-auth.gemspec +129 -0
- data/lib/api-auth.rb +2 -0
- data/lib/api_auth/base.rb +105 -0
- data/lib/api_auth/errors.rb +12 -0
- data/lib/api_auth/headers.rb +82 -0
- data/lib/api_auth/helpers.rb +39 -0
- data/lib/api_auth/railtie.rb +129 -0
- data/lib/api_auth/request_drivers/action_controller.rb +84 -0
- data/lib/api_auth/request_drivers/action_dispatch.rb +17 -0
- data/lib/api_auth/request_drivers/bixby_request.rb +65 -0
- data/lib/api_auth/request_drivers/curb.rb +72 -0
- data/lib/api_auth/request_drivers/httpi.rb +82 -0
- data/lib/api_auth/request_drivers/net_http.rb +98 -0
- data/lib/api_auth/request_drivers/rack.rb +88 -0
- data/lib/api_auth/request_drivers/rest_client.rb +98 -0
- data/lib/api_auth/request_drivers.rb +21 -0
- data/lib/api_auth.rb +10 -0
- data/lib/bixby-auth.rb +3 -0
- data/spec/api_auth_spec.rb +660 -0
- data/spec/application_helper.rb +2 -0
- data/spec/fixtures/upload.png +0 -0
- data/spec/headers_spec.rb +356 -0
- data/spec/helpers_spec.rb +14 -0
- data/spec/railtie_spec.rb +134 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/test_helper.rb +2 -0
- data/test/helper.rb +35 -0
- data/test/test_bixby-auth.rb +7 -0
- metadata +346 -0
@@ -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
data/lib/bixby-auth.rb
ADDED