Merge pull request #1352 from okriuchykhin/ok_SCI_2814

Rename user_id to sub in JWT [SCI-2814]
This commit is contained in:
Alex Kriuchykhin 2018-10-29 14:54:00 +01:00 committed by GitHub
commit 1083ce61fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 19 deletions

View file

@ -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

View file

@ -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

View file

@ -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