mirror of
https://github.com/internetee/registry.git
synced 2025-06-10 06:34:46 +02:00
Create AuthTokenCreator and AuthTokenDecryptor classes
This commit is contained in:
parent
dad57ba528
commit
dc8230dcc2
8 changed files with 221 additions and 33 deletions
43
lib/auth_token/auth_token_decryptor.rb
Normal file
43
lib/auth_token/auth_token_decryptor.rb
Normal file
|
@ -0,0 +1,43 @@
|
|||
class AuthTokenDecryptor
|
||||
attr_reader :decrypted_data
|
||||
attr_reader :token
|
||||
attr_reader :key
|
||||
attr_reader :user
|
||||
|
||||
def self.create_with_defaults(token)
|
||||
self.new(token, Rails.application.config.secret_key_base)
|
||||
end
|
||||
|
||||
def initialize(token, key)
|
||||
@token = token
|
||||
@key = key
|
||||
end
|
||||
|
||||
def decrypt_token
|
||||
decipher = OpenSSL::Cipher::AES.new(256, :CBC)
|
||||
decipher.decrypt
|
||||
decipher.key = key
|
||||
|
||||
base64_decoded = Base64.decode64(token)
|
||||
plain = decipher.update(base64_decoded) + decipher.final
|
||||
|
||||
@decrypted_data = JSON.parse(plain, symbolize_names: true)
|
||||
rescue OpenSSL::Cipher::CipherError
|
||||
false
|
||||
end
|
||||
|
||||
def valid?
|
||||
decrypted_data && valid_user? && still_valid?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid_user?
|
||||
@user = RegistrantUser.find_by(registrant_ident: decrypted_data[:user_ident])
|
||||
@user&.username == decrypted_data[:user_username]
|
||||
end
|
||||
|
||||
def still_valid?
|
||||
decrypted_data[:expires_at] > Time.now
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue