Class: GrapeTokenAuth::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/grape_token_auth/token.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id = nil, token = nil, expiry = nil) ⇒ Token

Returns a new instance of Token.



6
7
8
9
10
# File 'lib/grape_token_auth/token.rb', line 6

def initialize(client_id = nil, token = nil, expiry = nil)
  @client_id = client_id || SecureRandom.urlsafe_base64(nil, false)
  @token = token || SecureRandom.urlsafe_base64(nil, false)
  @expiry = expiry || (Time.now + GrapeTokenAuth.token_lifespan).to_i
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



4
5
6
# File 'lib/grape_token_auth/token.rb', line 4

def client_id
  @client_id
end

#expiryObject (readonly)

Returns the value of attribute expiry.



4
5
6
# File 'lib/grape_token_auth/token.rb', line 4

def expiry
  @expiry
end

#tokenObject (readonly)

Returns the value of attribute token.



4
5
6
# File 'lib/grape_token_auth/token.rb', line 4

def token
  @token
end

Instance Method Details

#to_hObject



16
17
18
# File 'lib/grape_token_auth/token.rb', line 16

def to_h
  { expiry: expiry, token: to_password_hash, updated_at: Time.now }
end

#to_password_hashObject



20
21
22
# File 'lib/grape_token_auth/token.rb', line 20

def to_password_hash
  @password_hash ||= BCrypt::Password.create(@token)
end

#to_sObject



12
13
14
# File 'lib/grape_token_auth/token.rb', line 12

def to_s
  @token
end