Created
December 2, 2017 07:14
-
-
Save xjia1/d477156990e45ffe2528960fd51bf2fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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