From bc317ce196387d42c11533924eb83f6a25943f65 Mon Sep 17 00:00:00 2001 From: Emmanuel Chigbo Date: Thu, 9 Jun 2016 12:33:25 +0100 Subject: [PATCH] Pass the JWT token through the header instead of the body --- app/services/token_manager.rb | 3 +-- app/workers/notification_system_worker.rb | 4 +--- lib/zi_notification/client.rb | 3 ++- lib/zi_notification/connection.rb | 7 +++++-- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/app/services/token_manager.rb b/app/services/token_manager.rb index d9090f2..de68d1d 100644 --- a/app/services/token_manager.rb +++ b/app/services/token_manager.rb @@ -3,8 +3,7 @@ class TokenManager class << self def generate_token(user_id, exp = 24.hours.from_now, notify_object = nil) - payload = { user: user_id, exp: exp.to_i } - payload = { object: notify_object, exp: exp.to_i } if notify_object + payload = { user: user_id, exp: exp.to_i, payload: notify_object } issue_token(payload) end diff --git a/app/workers/notification_system_worker.rb b/app/workers/notification_system_worker.rb index b1ed7a2..86c4ef7 100644 --- a/app/workers/notification_system_worker.rb +++ b/app/workers/notification_system_worker.rb @@ -7,8 +7,6 @@ class NotificationSystemWorker def perform(klass, object_id) object = klass.constantize.find(object_id).object_for_notification - token = TokenManager.generate_token(nil, 5.minutes.from_now, object) - options = { object: object, json_token: token } - ZiNotification::Client.post(Endpoints[:new_resource], options) + ZiNotification::Client.post(Endpoints[:new_resource], object) end end diff --git a/lib/zi_notification/client.rb b/lib/zi_notification/client.rb index f1a2b79..c5f1057 100644 --- a/lib/zi_notification/client.rb +++ b/lib/zi_notification/client.rb @@ -9,7 +9,8 @@ class << self end def request(http_method, path, options) - ZiNotification::Connection.connection.send(http_method, path, options) + token = TokenManager.generate_token(nil, 5.minutes.from_now, options) + ZiNotification::Connection.connection(token).send(http_method, path) end end end diff --git a/lib/zi_notification/connection.rb b/lib/zi_notification/connection.rb index a8f1939..43ed5d5 100644 --- a/lib/zi_notification/connection.rb +++ b/lib/zi_notification/connection.rb @@ -5,10 +5,13 @@ def self.endpoint ENV['ZI_NOTIFICATION_URL'] end - def self.connection + def self.connection(token) # NOTE we need to also add the authorization once implemented on notifications options = { - headers: { 'Accept' => 'application/json; charset=utf-8' } + headers: { + 'Accept' => 'application/json; charset=utf-8', + 'Authorization' => "Token token=#{token}" + } } ::Faraday::Connection.new(endpoint, options) do |connection|