module TokenSecretAuth::ClassMethods

Public Instance Methods

authenticate_by_credentials(token, secret=nil) click to toggle source

.authenticate_by_credentials finds correct instance by its token and then authenticates the password for that instance

# File lib/token_secret_auth/base.rb, line 57
def authenticate_by_credentials(token, secret=nil)
  account = find_by_token(token)
  # note BCrypt's authenticate will return false or the object when matched
  if account
    account.authenticate(secret)
  end
end
decode_token(token) click to toggle source
# File lib/token_secret_auth/base.rb, line 40
def decode_token(token)
  decoded = TokenSecretAuth.hash_id.decode(token).first
end
find_by_token(token) click to toggle source

.find_by_token Use on model files to find a particular instance based on the token (hashed ID)

# File lib/token_secret_auth/base.rb, line 46
def find_by_token(token)
  begin
    find(decode_token(token))
  rescue Hashids::InputError
    # controller should handle not found when we can't decode bad token
    return find(nil)
  end
end
generate_secret() click to toggle source

create a new randomly generated secret

# File lib/token_secret_auth/base.rb, line 66
def generate_secret
  rand(36**secret_length).to_s(36)
end
secret_length() click to toggle source

the default length for a secret

# File lib/token_secret_auth/base.rb, line 71
def secret_length
  @secret_length ||= 32
end