Sha256: 1d66ba4fa8d7ecfe8eb50bd4a6f5c625bf30decaac511a0879691b175d7aa817

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

require 'rack'
require 'active_model'
require 'gollum/auth/version'
require 'gollum/auth/request'
require 'gollum/auth/user'

module Gollum
  module Auth
    def self.new(*args)
      App.new(*args)
    end

    class App
      def initialize(app, users, opts = { })
        @app = app
        users.each { |args| User.new(args).save! }
        @opts = { allow_guests: false }.merge(opts)
      end

      def call(env)
        request = Request.new(env)
        if request.needs_authentication?(@opts[:allow_guests])
          auth = Rack::Auth::Basic::Request.new(env)
          if auth.provided? && auth.basic? && valid?(auth.credentials)
            user = User.find(auth.credentials.first)
            request.store_author_in_session(user)
          else
            return [
              401,
              { 'Content-Type' => 'text/plain', 'WWW-Authenticate' => 'Basic realm="Gollum Wiki"' },
              [ 'Not authorized' ]
            ]
          end
        end
        @app.call(env)
      end

      private

      def valid?(credentials)
        username, password = credentials
        current_user = User.find(username)
        current_user && current_user.valid_password?(password)
      end

      def users
        User.all
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gollum-auth-0.5.0 lib/gollum/auth.rb
gollum-auth-0.4.0 lib/gollum/auth.rb