module TokAccess::TokAuthenticable

Public Instance Methods

create_tok() click to toggle source

Create a new tok with a new token and a new device_token that can be access using the methods get_token and get_device_token.

# File lib/tok_access/tok_authenticable.rb, line 68
def create_tok
  generate_access_toks
  return self
end
generate_access_toks(device_token = nil) click to toggle source
# File lib/tok_access/tok_authenticable.rb, line 94
def generate_access_toks(device_token = nil)
  if !device_token
    self.toks.order(updated_at: :asc).first.destroy if self.toks.count == TokAccess.config.tokens_limit
    tok = self.toks.create
  else
    tok = self.toks.find_by(device_token: device_token)
    if !tok
      self.toks.order(updated_at: :asc).first.destroy if self.toks.count >= TokAccess.config.tokens_limit
      tok = self.toks.create
    end
    tok.regenerate_token
    tok.regenerate_device_token
  end
  if tok
    @refreshed = true
    set_token tok.token
    set_device_token tok.device_token
  end
end
get_device_token() click to toggle source

Return nil if you haven't authenticated the object using the method tok_auth or the method tok_auth failed the authentication process. Otherwise, return the device_token attribute of one of the associated object toks.

# File lib/tok_access/tok_authenticable.rb, line 23
def get_device_token
  @_device_token
end
get_token() click to toggle source

Return nil if you haven't authenticated the object using the method tok_auth or the method tok_auth failed the authentication process. Otherwise, return the token attribute of one of the associated object toks.

# File lib/tok_access/tok_authenticable.rb, line 15
def get_token
  @_token
end
provide_access(tok) click to toggle source

If the object is associated to the tok given, the method will return the object and the methods get_tok and get_device_tok will return the tokens. Otherwise return nil

# File lib/tok_access/tok_authenticable.rb, line 56
def provide_access(tok)
  if self.toks.find_by(id: tok.id)
    refresh tok
    set_token tok.token
    set_device_token tok.device_token
    return self
  end
  nil
end
refresh(tok) click to toggle source
# File lib/tok_access/tok_authenticable.rb, line 75
def refresh(tok)
  if 15.minutes.ago >= tok.updated_at
    tok.regenerate_token
    @refreshed = true
  end
end
refreshed?() click to toggle source

Return nil if you haven't use the method TokAccess.validate_access. After the TokAccess.validate_access method is called, if the access was validated successfuly, this method will return true if the token was regenerated, otherwise return nil

# File lib/tok_access/tok_authenticable.rb, line 31
def refreshed?
  @refreshed
end
set_device_token(token) click to toggle source
# File lib/tok_access/tok_authenticable.rb, line 86
def set_device_token(token)
  @_device_token = token
end
set_token(token) click to toggle source
# File lib/tok_access/tok_authenticable.rb, line 82
def set_token(token)
  @_token = token
end
setup_access_tok() click to toggle source
# File lib/tok_access/tok_authenticable.rb, line 90
def setup_access_tok
  self.toks.build()
end
tok_auth(password, device_token = nil ) click to toggle source

Authenticate the object with the given password . If the authentication process is successful return the object and the methods get_tok and get_device_tok will return the tokens Return nil if the authentication process fails You can pass as a parameter a device_token. If the object has a tok that match the device_token given, will regenerate the tok. If you do not pass the device_token parameter, the method will create a new tok associated with the object. If the object.toks.size is equal to the toks_limit, the method will destroy one of the toks and create a new one

# File lib/tok_access/tok_authenticable.rb, line 45
def tok_auth(password, device_token = nil )
  if self.authenticate(password)
    generate_access_toks(device_token)
    return self
  end
  nil
end