Skip to content

Commit 3c9ff72

Browse files
committed
hotfix : Fix JWT Filter config for CI - set filter module, add autoload path
1 parent 48dd8b4 commit 3c9ff72

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed
Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
11
require_relative "../code/failure"
22
require_relative "../utils/base_response"
33

4-
class JwtAuthFilter
5-
def initialize(app)
6-
@app = app
7-
end
4+
module Filter
5+
class JwtAuthFilter
6+
def initialize(app)
7+
@app = app
8+
end
89

9-
def call(env)
10-
request = Rack::Request.new(env)
11-
token = request.get_header("HTTP_AUTHORIZATION")&.split(" ")&.last
10+
def call(env)
11+
request = Rack::Request.new(env)
12+
token = request.get_header("HTTP_AUTHORIZATION")&.split(" ")&.last
1213

13-
unless request.url.include? "/api-docs"
14-
if token.nil?
15-
return [
16-
Failure::NO_TOKEN_IN_HEADER.status_code,
17-
{ "Content-Type" => "application/json" },
18-
[ BaseResponse.of_failure(Failure::NO_TOKEN_IN_HEADER).to_json ]
19-
]
20-
else
21-
begin
22-
validate_jwt(env, token)
23-
rescue JWT::ExpiredSignature => e
14+
unless request.url.include? "/api-docs"
15+
if token.nil?
2416
return [
25-
Failure::INVALID_TOKEN_IN_HEADER.status_code,
17+
Failure::NO_TOKEN_IN_HEADER.status_code,
2618
{ "Content-Type" => "application/json" },
27-
[ BaseResponse.of_failure(Failure::INVALID_TOKEN_IN_HEADER).to_json ]
19+
[ BaseResponse.of_failure(Failure::NO_TOKEN_IN_HEADER).to_json ]
2820
]
21+
else
22+
begin
23+
validate_jwt(env, token)
24+
rescue JWT::ExpiredSignature => e
25+
return [
26+
Failure::INVALID_TOKEN_IN_HEADER.status_code,
27+
{ "Content-Type" => "application/json" },
28+
[ BaseResponse.of_failure(Failure::INVALID_TOKEN_IN_HEADER).to_json ]
29+
]
30+
end
2931
end
3032
end
33+
@app.call(env)
3134
end
32-
@app.call(env)
33-
end
3435

35-
private
36-
def validate_jwt(env, token)
37-
payload, = JWT.decode(token, ENV["JWT_SECRET"], true, { algorithm: "HS256" })
38-
env["user_id"] = payload["id"]
39-
env["user_role"] = payload["role"]
36+
private
37+
def validate_jwt(env, token)
38+
payload, = JWT.decode(token, ENV["JWT_SECRET"], true, { algorithm: "HS256" })
39+
env["user_id"] = payload["id"]
40+
env["user_role"] = payload["role"]
41+
end
4042
end
4143
end

config/application.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Application < Rails::Application
1616
# Common ones are `templates`, `generators`, or `middleware`, for example.
1717
config.autoload_lib(ignore: %w[assets tasks])
1818
config.autoload_paths += %W[#{config.root}/app/controllers/code]
19+
config.autoload_paths += %W[#{config.root}/app/controllers/filter]
1920

2021
# Configuration for the application, engines, and railties goes here.
2122
#

config/environments/development.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require "active_support/core_ext/integer/time"
22

33
Rails.application.configure do
4-
require_relative "../../app/controllers/filter/jwt_auth_filter"
54
config.middleware.use JwtAuthFilter
65

76
config.enable_reloading = true

0 commit comments

Comments
 (0)