Module: TokenSecretAuth::ClassMethods

Defined in:
lib/token_secret_auth/base.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_by_credentials(token, secret = nil) ⇒ Object

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



57
58
59
60
61
62
63
# File 'lib/token_secret_auth/base.rb', line 57

def authenticate_by_credentials(token, secret=nil)
   = find_with_token(token)
  # note BCrypt's authenticate will return false or the object when matched
  if 
    .authenticate(secret)
  end
end

#decode_token(token) ⇒ Object



40
41
42
# File 'lib/token_secret_auth/base.rb', line 40

def decode_token(token)
  decoded = TokenSecretAuth.hash_id.decode(token).first
end

#find_with_token(token) ⇒ Object

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



46
47
48
49
50
51
52
53
# File 'lib/token_secret_auth/base.rb', line 46

def find_with_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_secretObject

create a new randomly generated secret



66
67
68
# File 'lib/token_secret_auth/base.rb', line 66

def generate_secret
  rand(36**secret_length).to_s(36)
end

#secret_lengthObject

the default length for a secret



71
72
73
# File 'lib/token_secret_auth/base.rb', line 71

def secret_length
  @secret_length ||= 32
end