3

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)
James
  • 361
  • 3
  • 11
  • The title is about Rails, but your question has nothing about Rails. `RubyonRails != Ruby` – Roman Kiselenko Nov 19 '16 at 08:37
  • Your question is unclear. In the title you ask about global variables, in the text you ask about local variables. Which is it? – Jörg W Mittag Nov 19 '16 at 11:12
  • Also, like almost all programming languages, Ruby doesn't allow libraries to change core language semantics such as variable scope, so it is highly unlikely that your question is in any way related to [tag:rack] or [tag:gollum-wiki]. – Jörg W Mittag Nov 19 '16 at 11:13
  • @JörgWMittag Hi, I've updated the question, is it clear what I'm trying to achieve now? – James Nov 19 '16 at 18:38

2 Answers2

5

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
James
  • 361
  • 3
  • 11
2

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: rick@example.com
- username: morty
  password_digest: 5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf5a9c173cacfc5
  name: Morty Smith
  email: morty@example.com
}

# 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 :-)

Björn Albers
  • 323
  • 4
  • 7