Skip to content

Instantly share code, notes, and snippets.

@gpolitis
Forked from dstrelau/app.rb
Created January 28, 2012 00:22
Show Gist options
  • Save gpolitis/1691759 to your computer and use it in GitHub Desktop.
Save gpolitis/1691759 to your computer and use it in GitHub Desktop.
Gollum protected by HTTP Basic
require 'gollum/frontend/app'
require 'digest/sha1'
class App < Precious::App
User = Struct.new(:name, :email, :password_hash)
before { authenticate! }
helpers do
def authenticate!
@_auth ||= Rack::Auth::Basic::Request.new(request.env)
if @_auth.provided?
end
if @_auth.provided? && @_auth.basic? && @_auth.credentials &&
@user = detected_user(@_auth.credentials)
return @user
else
response['WWW-Authenticate'] = %(Basic realm="Gollum Wiki")
throw(:halt, [401, "Not authorized\n"])
end
end
def users
@_users ||= settings.authorized_users.map {|u| User.new(*u) }
end
def detected_user(credentials)
users.detect do |u|
[u.email, u.password_hash] ==
[credentials[0], Digest::SHA1.hexdigest(credentials[1])]
end
end
end
def commit_message
{
:message => params[:message],
:name => @user.name,
:email => @user.email
}
end
end
__DIR__ = File.expand_path(File.dirname(__FILE__))
$: << __DIR__
require 'app'
require 'yaml'
App.set(:gollum_path, __DIR__)
App.set(:authorized_users, YAML.load_file(File.expand_path('users.yml', __DIR__)))
App.set(:wiki_options, {})
run App
---
- - User Name
- user@name.co
- a94a8fe5ccb19ba61c4c0873d391e987982fbbd3 #test
- - Another User
- another@user.co
- a94a8fe5ccb19ba61c4c0873d391e987982fbbd3 #test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment