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

2.6k Views Asked by At

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 => "[email protected]",
        }
    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 => "[email protected]",
    }
end

I also tried:

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

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

There are 2 best solutions below

0
James On BEST ANSWER

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 => "%[email protected]" % settings.loggedInUser,
        }
    end
end
0
Björn Albers On

You can simply use the rubygem gollum-auth to add basic-authentication to Gollum 4 and 5:

https://github.com/bjoernalbers/gollum-auth

Just install it with gem install gollum-auth or Bundler and load it before gollum. Here is a sample rack config.ru to accomplish that (taken from the project's README):

#!/usr/bin/env ruby
require 'rubygems'
require 'gollum/auth' # Don't forget to load the gem!
require 'gollum/app'

# Define list of authorized users.
# Each user must have a username, password, name and email.
#
# Instead of a password you can also define a password_digest, which is the
# SHA-256 hash of a password.
#
# Example:
users = YAML.load %q{
---
- username: rick
  password: asdf754&1129-@lUZw
  name: Rick Sanchez
  email: [email protected]
- username: morty
  password_digest: 5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf5a9c173cacfc5
  name: Morty Smith
  email: [email protected]
}

# Allow unauthenticated users to read the wiki (disabled by default).
options = { allow_unauthenticated_readonly: true }

# Allow only authenticated users to change the wiki.
# (NOTE: This must be loaded *before* Precious::App!)
use Gollum::Auth, users, options

# That's it. The rest is for gollum only.
gollum_path = File.expand_path(File.dirname(__FILE__)) # CHANGE THIS TO POINT TO YOUR OWN WIKI REPO
wiki_options = {:universal_toc => false}
Precious::App.set(:gollum_path, gollum_path)
Precious::App.set(:wiki_options, wiki_options)
run Precious::App

Disclaimer: I'm the author of gollum-auth :-)