Skip to content

Instantly share code, notes, and snippets.

@raws
Created September 21, 2011 22:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save raws/1233539 to your computer and use it in GitHub Desktop.
Save raws/1233539 to your computer and use it in GitHub Desktop.
Authenticate against ExpressionEngine >= 2.2.2 member data in Ruby
#!/usr/bin/env RBENV_VERSION=1.9.3-preview1 ruby
require "digest"
require "sequel"
module ExpressionEngine
module Authentication
ALGORITHMS = {
32 => Digest::MD5,
40 => Digest::SHA1,
64 => Digest::SHA256,
128 => Digest::SHA512
}
def self.algorithm_for(string)
ALGORITHMS[string.bytesize]
end
end
def self.authenticate(username, password)
database = Sequel.connect("mysql://...")
member = database[:exp_members].first(:username => username)
return false unless member
algorithm = Authentication.algorithm_for(member[:password])
raise "Invalid password hash length (#{member[:password]} bytes)" unless algorithm
algorithm.hexdigest(member[:salt] + password) == member[:password]
end
end
print "Username: "
username = gets.chomp
print "Password: "
password = gets.chomp
if ExpressionEngine.authenticate(username, password)
puts "You're authenticated!"
else
puts "Authentication failed."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment