如何配置 Gollum-wiki 以允许基本身份验证并使用 git 提交的用户名

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

我正在尝试为 gollum 配置基本的 http 身份验证,但我希望登录的用户名用于 git 提交。

我已经修改了 config.ru 以便基本身份验证有效,现在我只需要弄清楚如何实现与此等效的方法:

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

然后我可以删除 "John Smith" 字符串。

顺便说一句 - 请原谅这个愚蠢的问题,我以前从未接触过 Ruby,现在已经晚了。

#!/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

所以我可以看到会话只存在于 Precious Class 命名空间中,所以我不能直接从我的身份验证方法设置它:

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

我也试过:

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)

这是一个解决方案,它允许您定义一系列用户,启用基本的 http 身份验证并使用登录的用户名进行适当的提交。

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

您可以简单地使用 ruby​​gem gollum-auth 为 Gollum 4 和 5 添加基本身份验证:

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

只需使用 gem install gollum-auth 或 Bundler 安装它并在 gollum 之前加载它。 这是一个示例机架 config.ru 来实现这一点(取自项目的自述文件):

#!/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

免责声明:我是 gollum-auth 的作者 :-)