Add domain index action (without pagination yet)

This commit is contained in:
Maciej Szlosarczyk 2018-07-30 15:09:48 +03:00
parent 13562aeb06
commit 10d42a0d74
No known key found for this signature in database
GPG key ID: 41D62D42D3B0D765
5 changed files with 155 additions and 4 deletions

View file

@ -0,0 +1,41 @@
class AuthTokenCreator
DEFAULT_VALIDITY = 2.hours
attr_reader :user
attr_reader :key
attr_reader :expires_at
def self.create_with_defaults(user)
new(user, Rails.application.config.secret_key_base, Time.now + DEFAULT_VALIDITY)
end
def initialize(user, key, expires_at)
@user = user
@key = key
@expires_at = expires_at.utc.strftime('%F %T %Z')
end
def hashable
{
user_ident: user.registrant_ident,
user_username: user.username,
expires_at: expires_at,
}.to_json
end
def encrypted_token
encryptor = OpenSSL::Cipher::AES.new(256, :CBC)
encryptor.encrypt
encryptor.key = key
encrypted_bytes = encryptor.update(hashable) + encryptor.final
Base64.urlsafe_encode64(encrypted_bytes)
end
def token_in_hash
{
access_token: encrypted_token,
expires_at: expires_at,
type: 'Bearer',
}
end
end