From 753bf8499ba5d8815db8d08a1dcf69caa20660cf Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Sat, 1 Apr 2023 12:00:33 -0600 Subject: Mejora estructura del proyecto --- app/services/token_generation_service.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 app/services/token_generation_service.rb (limited to 'app/services/token_generation_service.rb') diff --git a/app/services/token_generation_service.rb b/app/services/token_generation_service.rb new file mode 100644 index 0000000..3af1e3f --- /dev/null +++ b/app/services/token_generation_service.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +# The service responsible for JWT generation. +class TokenGenerationService + def initialize(params) + @email = params[:email] + @role = params[:role] + @user_account = UserAccount.find_by(email: @email) + end + + def call(refresh_token_expiration) + @user_account.update_attribute(:session_key, SecureRandom.hex(16)) + { token: generate_token(DateTime.current + 30.minutes), refresh: generate_token(refresh_token_expiration.to_i) } + end + + private + + def generate_token(expiration) + JWT.encode(payload.merge({ exp: expiration.to_i }), ENV['HMAC_SECRET_KEY'], 'HS512') + end + + def payload + { data: @email, aud: @role, jti: @user_account.session_key } + end +end -- cgit v1.2.3