From 04ff5870af7cf1127356dd44a9428b7ee8c863a1 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Sun, 28 Oct 2018 14:34:51 +0100 Subject: [PATCH] Rename user_id to sub in JWT [SCI-2814] --- app/controllers/api/api_controller.rb | 10 +--------- app/services/api/core_jwt.rb | 8 +------- spec/support/api_helper.rb | 6 +++--- 3 files changed, 5 insertions(+), 19 deletions(-) diff --git a/app/controllers/api/api_controller.rb b/app/controllers/api/api_controller.rb index e1c5d76a3..3292e4fd8 100644 --- a/app/controllers/api/api_controller.rb +++ b/app/controllers/api/api_controller.rb @@ -75,18 +75,10 @@ module Api raise JWT::InvalidPayload, 'Default: Wrong ISS in the token' end payload = CoreJwt.decode(token) - @current_user = User.find_by_id(payload['user_id']) + @current_user = User.find_by_id(payload['sub']) unless current_user raise JWT::InvalidPayload, 'Default: User mapping not found' end - - # Implement sliding sessions, i.e send new token in case of successful - # authorization and when tokens TTL reached specific value (to avoid token - # generation on each request) - if CoreJwt.refresh_needed?(payload) - new_token = CoreJwt.encode(user_id: current_user.id) - response.headers['X-Access-Token'] = new_token - end end def auth_params diff --git a/app/services/api/core_jwt.rb b/app/services/api/core_jwt.rb index cf872df9b..10c39850a 100644 --- a/app/services/api/core_jwt.rb +++ b/app/services/api/core_jwt.rb @@ -25,16 +25,10 @@ module Api )[:iss].to_s end - def self.refresh_needed?(payload) - time_left = payload[:exp].to_i - Time.now.to_i - return true if time_left < (Api.configuration.core_api_token_ttl.to_i / 2) - false - end - # Method used by Doorkeeper for custom tokens def self.generate(options = {}) encode( - { user_id: options[:resource_owner_id] }, + { sub: options[:resource_owner_id] }, options[:expires_in].seconds.from_now.to_i ) end diff --git a/spec/support/api_helper.rb b/spec/support/api_helper.rb index 5fc55cf5c..ff815a060 100644 --- a/spec/support/api_helper.rb +++ b/spec/support/api_helper.rb @@ -1,14 +1,14 @@ module ApiHelper def generate_token(user_id) - Api::CoreJwt.encode(user_id: user_id) + Api::CoreJwt.encode(sub: user_id) end def generate_expired_token(user_id) - Api::CoreJwt.encode({ user_id: user_id }, (Time.now.to_i - 300)) + Api::CoreJwt.encode({ sub: user_id }, (Time.now.to_i - 300)) end def decode_token(token) - Api::CoreJwt.decode(token)['user_id'].to_i + Api::CoreJwt.decode(token)['sub'].to_i end def json