Skip to content

Commit d2bcc18

Browse files
authored
Merge pull request #22 from RadiusNetworks/scoped-path-updates
Initial Rails 5 API updates
2 parents fae3e75 + 5ed34ff commit d2bcc18

File tree

9 files changed

+129
-167
lines changed

9 files changed

+129
-167
lines changed

lib/kracken/controllers/json_api_compatible.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def munge_resource_root!
5050
end
5151

5252
def verify_scoped_resource(resource, options = {})
53-
name = "verify_scoped_#{resource}"
53+
name = "verify_scoped_#{resource}".to_sym
5454
relation = options.extract!(:as).fetch(:as, resource).to_s.pluralize
5555
scope = options.extract!(:scope).fetch(:scope, :current_user)
5656
resource_id = (resource_type == resource.to_sym) ? :id : "#{resource}_id"
@@ -74,14 +74,6 @@ def verify_required_params(options = {})
7474
end
7575
end
7676

77-
def self.included(base)
78-
base.instance_exec do
79-
extend Macros
80-
81-
before_action :munge_chained_param_ids!
82-
end
83-
end
84-
8577
module DataIntegrity
8678
# Scan each item in the data root and enforce it has an id set.
8779
def enforce_resource_ids!
@@ -102,6 +94,18 @@ def verify_required_params!
10294
"Single beacon object provided but multiple resources requested"
10395
end
10496
end
97+
98+
# Negotiate the mime type for the request format
99+
#
100+
# This will modify the request object setting the format.
101+
def negotiate_mime
102+
return if request.negotiate_mime(ALLOWED_MEDIA_TYPES)
103+
raise ::ActionController::UnknownFormat
104+
end
105+
106+
private
107+
108+
ALLOWED_MEDIA_TYPES = [Mime[:json]].freeze
105109
end
106110
include DataIntegrity
107111

@@ -125,6 +129,7 @@ def self.included(base)
125129
base.instance_exec do
126130
extend Macros
127131

132+
before_action :negotiate_mime
128133
before_action :munge_chained_param_ids!
129134
skip_before_action :verify_authenticity_token, raise: false
130135

lib/kracken/controllers/token_authenticatable.rb

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,33 @@ def request_http_token_authentication(realm = 'Application')
2929
end
3030
end
3131

32+
module_function
33+
34+
TOKEN_AUTH_CACHE_PREFIX = "auth/token/"
35+
36+
def cache_valid_auth(token, force: false, &generate_cache)
37+
cache_key = TOKEN_AUTH_CACHE_PREFIX + token
38+
val = Rails.cache.read(cache_key) unless force
39+
val ||= store_valid_auth(cache_key, &generate_cache)
40+
shallow_freeze(val)
41+
end
42+
43+
def clear_auth_cache
44+
Rails.cache.delete_matched TOKEN_AUTH_CACHE_PREFIX + "*"
45+
end
46+
47+
def shallow_freeze(val)
48+
# `nil` is frozen in Ruby 2.2 but not in Ruby 2.1
49+
return val if val.frozen? || val.nil?
50+
val.each { |_k, v| v.freeze }.freeze
51+
end
52+
53+
def store_valid_auth(cache_key)
54+
val = yield
55+
Rails.cache.write(cache_key, val, CACHE_TTL_OPTS) if val
56+
val
57+
end
58+
3259
private
3360

3461
CACHE_TTL_OPTS = {
@@ -51,19 +78,6 @@ def authenticate_user_with_token!
5178
}
5279
end
5380

54-
def cache_valid_auth(token, &generate_cache)
55-
cache_key = "auth/token/#{token}"
56-
val = Rails.cache.read(cache_key)
57-
val ||= store_valid_auth(cache_key, &generate_cache)
58-
shallow_freeze(val)
59-
end
60-
61-
def shallow_freeze(val)
62-
# `nil` is frozen in Ruby 2.2 but not in Ruby 2.1
63-
return val if val.frozen? || val.nil?
64-
val.each { |_k, v| v.freeze }.freeze
65-
end
66-
6781
def current_auth_info
6882
@_auth_info ||= {}
6983
end
@@ -96,12 +110,6 @@ def munge_header_auth_token!
96110
def realm
97111
self.class.realm
98112
end
99-
100-
def store_valid_auth(cache_key)
101-
val = yield
102-
Rails.cache.write(cache_key, val, CACHE_TTL_OPTS) if val
103-
val
104-
end
105113
end
106114

107115
end

lib/kracken/json_api.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require_relative 'json_api/exception_wrapper'
22
require_relative 'json_api/path'
33
require_relative 'json_api/public_exceptions'
4-
require_relative 'json_api/request'
54
require_relative 'json_api/routing_mapper'
65

76
module Kracken

lib/kracken/json_api/exception_wrapper.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,23 @@ def self.status_code_for_exception(class_name)
1717
end
1818
end
1919

20-
def is_details_exception?
21-
@@rescue_with_details_responses.has_key?(exception.class.name)
20+
# Temporary work around while we support versions of Rails before 4
21+
if Rails::VERSION::MAJOR < 5
22+
def is_details_exception?
23+
@@rescue_with_details_responses.has_key?(exception.class.name)
24+
end
25+
else
26+
attr_reader :raised_exception
27+
28+
def initialize(backtrace_cleaner, exception)
29+
super
30+
@raised_exception = exception
31+
end
32+
33+
def is_details_exception?
34+
@@rescue_with_details_responses.has_key?(raised_exception.class.name) ||
35+
@@rescue_with_details_responses.has_key?(exception.class.name)
36+
end
2237
end
2338
end
2439
end

lib/kracken/json_api/path.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
module Kracken
22
module JsonApi
33
class Path
4-
attr_reader :path_match
4+
attr_reader :basename
5+
attr_reader :pathname
56

67
def initialize(path)
7-
@path_match = Pathname(path).join('*').to_path
8+
@basename = ActionDispatch::Journey::Router::Utils.normalize_path(path)
9+
@pathname = @basename + "/"
810
end
911

1012
def matches?(request)
11-
request.supports_json_format? && request.path.fnmatch?(path_match)
13+
path_matches?(request.original_fullpath)
14+
end
15+
16+
private
17+
18+
def path_matches?(path)
19+
path == basename || path.start_with?(pathname)
1220
end
1321
end
1422
end

lib/kracken/json_api/public_exceptions.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def initialize(app)
3737
end
3838

3939
def call(env)
40-
if JsonApi.has_path?(JsonApi::Request.new(env))
40+
if JsonApi.has_path?(ActionDispatch::Request.new(env))
4141
capture_error(env)
4242
else
4343
@app.call(env)
@@ -59,11 +59,23 @@ def capture_error(env)
5959

6060
response
6161
rescue Exception => exception
62-
wrapper = ExceptionWrapper.new(env, exception)
62+
wrapper = exception_wrapper(env, exception)
6363
log_error(env, wrapper)
6464
render_json_error(wrapper)
6565
end
6666

67+
if Rails::VERSION::MAJOR < 5
68+
def exception_wrapper(env, exception)
69+
ExceptionWrapper.new(env, exception)
70+
end
71+
else
72+
def exception_wrapper(env, exception)
73+
request = ActionDispatch::Request.new(env)
74+
backtrace_cleaner = request.get_header('action_dispatch.backtrace_cleaner')
75+
ExceptionWrapper.new(backtrace_cleaner, exception)
76+
end
77+
end
78+
6779
if Rails.env.production?
6880
def additional_details(error)
6981
{}

lib/kracken/json_api/request.rb

Lines changed: 0 additions & 121 deletions
This file was deleted.

lib/kracken/rspec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ module Request
1414
def sign_in(user = nil)
1515
Kracken::SpecHelper.current_user = user
1616
end
17+
18+
def token_authorize(user, token:)
19+
Kracken::Controllers::TokenAuthenticatable::cache_valid_auth(token, force: true) do
20+
{ id: user.id, team_ids: user.team_ids }
21+
end
22+
end
1723
end
1824

1925
module Controller
@@ -43,8 +49,10 @@ def current_user
4349
end
4450
end
4551
module TokenAuthenticatable
52+
alias_method :__original_user__, :current_user
4653
def current_user
47-
Kracken::SpecHelper.current_user
54+
Kracken::SpecHelper.current_user or
55+
(current_user_id && __original_user__)
4856
end
4957

5058
alias_method :__original_auth__, :authenticate_user_with_token!
@@ -69,6 +77,10 @@ def authenticate_user_with_token!
6977
c.include Kracken::SpecHelper::Request, type: :kracken
7078
c.include Kracken::SpecHelper::Request, type: :request
7179

80+
c.before do
81+
Kracken::Controllers::TokenAuthenticatable.clear_auth_cache
82+
end
83+
7284
c.before(type: :kracken) do
7385
Kracken::SpecHelper.current_user = nil
7486
end

0 commit comments

Comments
 (0)