rubyrackgollum-wiki

How to configure Gollum-wiki to allow basic authentication and to use the username for the git commit


I'm trying to configure basic http authentication for gollum, but I want the logged in username to be used for the git commit.

I've modified config.ru so that basic authentication works, now I just need to figure out how I can achieve the equivalent of this:

session['gollum.author'] => "%s" % loggedIn

Then I can remove the "John Smith" string.

BTW - Forgive the daft question, I've never touched Ruby before and its late.

#!/usr/bin/env ruby
#--------------------------------------------------------------------
# - example custom rack for the Gollum wiki engine
# - file should be placed in wiki root
# - RACK_APP environment variable should be set to the filename
# - entrypoint.sh script will run this app using:
#   $ rackup $RACK_APP -p 4567
#--------------------------------------------------------------------
require 'rubygems'
require 'gollum/app'

gollum_path = File.expand_path(File.dirname(__FILE__))
wiki_options = {
    :live_preview => false,
    :allow_editing => true,
    :allow_uploads => true,
    :universal_toc => false,
}

users = {'user' => 'password'}
loggedIn = "anonymous"

use Rack::Auth::Basic, 'realm' do |username, password|
    users.key?(username) && users[username] == password
    loggedIn = username
end

Precious::App.set(:gollum_path, gollum_path)
Precious::App.set(:default_markup, :markdown)
Precious::App.set(:wiki_options, wiki_options)
run Precious::App

#set author
class Precious::App
    before do
        session['gollum.author'] = {
            :name => "%s" % "john smith",   # => "%s" % loggedIn
            :email => "jsmith@example.com",
        }
    end
end

So I can see that session only exists inside the Precious Class namespace, so I can't set it directly from my authentication method:

use Rack::Auth::Basic, 'realm' do |username, password|
    users.key?(username) && users[username] == password
    session['gollum.author'] = {
        :name => "%s" % "john smith",   # => "%s" % username
        :email => "jsmith@example.com",
    }
end

I also tried:

use Rack::Auth::Basic, 'realm' do |username, password|
    users.key?(username) && users[username] == password
    loggedIn = {
        :name => "%s" % username,
        :email => "jsmith@example.com",
    }
end

Precious::App.set(:session['gollum.author'], loggedIn)

Solution

  • Here is a solution, it allows you to define a series of users, enables basic http authentication and uses the logged in username for the fit commits.

    require 'rubygems'
    require 'gollum/app'
    
    gollum_path = File.expand_path(File.dirname(__FILE__))
    wiki_options = {
        :live_preview => false,
        :allow_editing => true,
        :allow_uploads => true,
        :universal_toc => false,
    }
    
    users = {'user' => 'password',
             'user2' => 'password2'}
    
    use Rack::Auth::Basic, 'realm' do |username, password|
        if users.key?(username) && users[username] == password
            Precious::App.set(:loggedInUser, username)
        end
    end
    
    Precious::App.set(:gollum_path, gollum_path)
    Precious::App.set(:default_markup, :markdown)
    Precious::App.set(:wiki_options, wiki_options)
    run Precious::App
    
    #set author
    class Precious::App
        before do
            session['gollum.author'] = {
                :name => "%s" % settings.loggedInUser,
                :email => "%s@example.com" % settings.loggedInUser,
            }
        end
    end