Add auth-token class

This commit is contained in:
Maciej Szlosarczyk 2018-07-19 10:31:31 +03:00
parent d67e777ea8
commit 1c6b838b2b
No known key found for this signature in database
GPG key ID: 41D62D42D3B0D765
5 changed files with 65 additions and 6 deletions

26
lib/auth_token.rb Normal file
View file

@ -0,0 +1,26 @@
class AuthToken
def initialize; end
def generate_token(user, secret = Rails.application.config.secret_key_base)
cipher = OpenSSL::Cipher::AES.new(256, :CBC)
expires_at = (Time.now.utc + 2.hours).strftime("%F %T %Z")
data = {
username: user.username,
expires_at: expires_at
}
hashable = data.to_json
cipher.encrypt
cipher.key = secret
encrypted = cipher.update(hashable) + cipher.final
base64_encoded = Base64.encode64(encrypted)
{
access_token: base64_encoded,
expires_at: expires_at,
type: "Bearer"
}
end
end