Skip to content

Instantly share code, notes, and snippets.

@LEW21
Created August 1, 2014 22:48
Show Gist options
  • Save LEW21/39777b4fc9a831a58acc to your computer and use it in GitHub Desktop.
Save LEW21/39777b4fc9a831a58acc to your computer and use it in GitHub Desktop.
Gollum HTTP Basic Auth
require 'gollum/app'
class GollumAuth
User = Struct.new(:name, :email, :password_hash)
def users
if @_users
return @_users
end
all_users = YAML.load_file(File.expand_path('users.yml', File.dirname(__FILE__)))
@_users = all_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
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new(env)
session = env['rack.session'] = {}
auth = Rack::Auth::Basic::Request.new(env)
if auth.provided? && auth.basic? && auth.credentials && user = detected_user(auth.credentials)
session['gollum.author'] = {
:name => user.name,
:email => user.email
}
end
# Require login to modify anything.
if request.path =~ /^\/(edit|uploadFile|rename|edit|delete|create|revert|preview)\// and not session['gollum.author']
return [401, {'WWW-Authenticate' => %(Basic realm="Gollum Wiki")}, []]
end
@app.call(env)
end
end
use GollumAuth
gollum_path = File.expand_path(File.dirname(__FILE__)) + "/wikidata"
Precious::App.set(:gollum_path, gollum_path)
Precious::App.set(:default_markup, :markdown)
Precious::App.set(:wiki_options, {})
run Precious::App
Copy link

ghost commented Feb 1, 2016

Can you give the command to start Gollum which this middleware? How can i put options fror gollum?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment