Skip to content

Instantly share code, notes, and snippets.

@xjia1
Created December 2, 2017 07:14
Show Gist options
  • Save xjia1/d477156990e45ffe2528960fd51bf2fc to your computer and use it in GitHub Desktop.
Save xjia1/d477156990e45ffe2528960fd51bf2fc to your computer and use it in GitHub Desktop.
# Authenticate users against a htpasswd file.
#
# This source file should be put under the app/models/ directory.
#
# Add
# gem 'htauth'
# to the Gemfile.local file and run
# bundle install --without development test
# again.
#
# Execute
# INSERT INTO auth_sources (type, name, base_dn, onthefly_register)
# VALUES ('AuthSourceHtpasswd', 'htpasswd', '/etc/nginx/.htpasswd', 1)
# in the redmine database.
#
class AuthSourceHtpasswd < AuthSource
validates_presence_of :base_dn
def authenticate(login, password)
return nil if login.blank? || password.blank?
logger.info "Authenticating '#{login}' against '#{self.base_dn}'"
HTAuth::PasswdFile.open(self.base_dn) do |pf|
unless pf.has_entry?(login)
raise AuthSourceException.new("'#{login}' not found")
end
unless pf.fetch(login).authenticated?(password)
raise AuthSourceException.new("wrong password")
end
return {
:firstname => nil,
:lastname => nil,
:mail => nil,
:auth_source_id => self.id
}
end
end
def auth_method_name
"htpasswd"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment