public
Fork of al3x/git-wiki
Description: A wiki engine that uses a Git repository as its data store.
Homepage: http://atonie.org/2008/02/git-wiki
Clone URL: git://github.com/jnewland/git-wiki.git
git-wiki / extensions.rb
100644 80 lines (65 sloc) 1.866 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def require_gem_with_feedback(gem)
  begin
    require gem
  rescue LoadError
    puts "You need to 'sudo gem install #{gem}' before we can proceed"
  end
end
 
class String
  def wiki_linked
    self.gsub!(/(?!<nowiki>)(?>\b((?:[A-Z]\w+){2,}))(?!<\/nowiki>)/) { |m| "<a href=\"/#{m}\">#{m}</a>" }
    self.gsub!(/\[(\w+){2,}\]/) { |m|
      m.gsub!(/(\[|\])/, '')
      "<a href=\"/#{m}\">#{m}</a>"
    }
    self.gsub!(/<\/?nowiki>/,'')
    self
  end
end
 
class Time
  def for_time_ago_in_words
    "#{(self.to_i * 1000)}"
  end
end
 
module HttpAuthentication
  module Basic
 
    def authenticate_or_request_with_http_basic(realm = "Application", &login_procedure)
      authenticate_with_http_basic(&login_procedure) || request_http_basic_authentication(realm)
    end
 
    def authenticate_with_http_basic(&login_procedure)
      authenticate(&login_procedure)
    end
 
    def request_http_basic_authentication(realm = "Application")
      authentication_request(realm)
    end
 
    private
 
      def authenticate(&login_procedure)
        if authorization
          login_procedure.call(*user_name_and_password)
        end
      end
 
      def user_name_and_password
        decode_credentials.split(/:/, 2)
      end
 
      def authorization
        request.env['HTTP_AUTHORIZATION'] ||
        request.env['X-HTTP_AUTHORIZATION'] ||
        request.env['X_HTTP_AUTHORIZATION'] ||
        request.env['REDIRECT_X_HTTP_AUTHORIZATION']
      end
 
      # Base64
      def decode_credentials
        (authorization.split.last || '').unpack("m").first
      end
 
      def authentication_request(realm)
        status(401)
        header("WWW-Authenticate" => %(Basic realm="#{realm.gsub(/"/, "")}"))
        throw :halt, "HTTP Basic: Access denied.\n"
      end
 
  end
end
 
module Sinatra
  class EventContext
    include HttpAuthentication::Basic
  end
end