mirror of
https://github.com/internetee/registry.git
synced 2025-07-22 02:35:57 +02:00
Add auth-token class
This commit is contained in:
parent
d67e777ea8
commit
1c6b838b2b
5 changed files with 65 additions and 6 deletions
26
lib/auth_token.rb
Normal file
26
lib/auth_token.rb
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue