tdiary 4.1.1 → 4.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/ChangeLog +42 -0
  4. data/Gemfile +2 -11
  5. data/Gemfile.lock +64 -61
  6. data/README.md +4 -2
  7. data/doc/HOWTO-authenticate-in-rack.md +96 -8
  8. data/doc/HOWTO-testing-tDiary.md +1 -1
  9. data/doc/INSTALL-paas.md +19 -54
  10. data/doc/README.en.md +1 -1
  11. data/doc/README.md +1 -1
  12. data/js/comment_ajax.js +1 -1
  13. data/lib/tdiary/application/configuration.rb +5 -9
  14. data/lib/tdiary/application.rb +59 -36
  15. data/lib/tdiary/cli.rb +9 -3
  16. data/lib/tdiary/core_ext.rb +1 -1
  17. data/lib/tdiary/diary_container.rb +55 -0
  18. data/lib/tdiary/environment.rb +3 -0
  19. data/lib/tdiary/plugin/00default.rb +4 -3
  20. data/lib/tdiary/plugin/05referer.rb +9 -7
  21. data/lib/tdiary/plugin.rb +13 -2
  22. data/lib/tdiary/rack/auth/basic.rb +1 -1
  23. data/lib/tdiary/rack/auth/omniauth/authorization.rb +64 -0
  24. data/lib/tdiary/rack/auth/omniauth.rb +78 -46
  25. data/lib/tdiary/rack/auth.rb +20 -0
  26. data/lib/tdiary/rack/session.rb +35 -0
  27. data/lib/tdiary/rack.rb +2 -5
  28. data/lib/tdiary/style.rb +2 -1
  29. data/lib/tdiary/tasks/assets.rake +1 -1
  30. data/lib/tdiary/tasks/release.rake +18 -7
  31. data/lib/tdiary/version.rb +1 -1
  32. data/lib/tdiary.rb +1 -0
  33. data/misc/paas/heroku/Gemfile.local +8 -0
  34. data/misc/paas/heroku/app.json +19 -0
  35. data/misc/paas/heroku/config.ru +6 -0
  36. data/misc/paas/heroku/tasks/mongodb.rake +12 -0
  37. data/misc/paas/heroku/tdiary.conf +14 -14
  38. data/misc/plugin/amazon.rb +9 -1
  39. data/misc/plugin/category.rb +1 -19
  40. data/misc/plugin/category_autocomplete.rb +2 -2
  41. data/misc/plugin/comment_ajax.rb +2 -0
  42. data/misc/plugin/comment_emoji_autocomplete.rb +2 -2
  43. data/misc/plugin/comment_mail-smtp.rb +23 -8
  44. data/misc/plugin/makerss.rb +5 -1
  45. data/misc/plugin/recent_comment3.rb +1 -5
  46. data/misc/plugin/recent_list.rb +1 -5
  47. data/misc/plugin/theme_online.rb +8 -2
  48. data/spec/acceptance/save_conf_plugin_spec.rb +2 -2
  49. data/spec/core/application_spec.rb +65 -0
  50. data/spec/core/diary_container_spec.rb +71 -0
  51. data/spec/core/plugin_spec.rb +13 -0
  52. data/spec/fixtures/tdiary.conf.gem +2 -2
  53. data/spec/fixtures/tdiary.conf.rack +2 -2
  54. data/spec/fixtures/tdiary.conf.secure +2 -2
  55. data/spec/fixtures/tdiary.conf.webrick +2 -2
  56. data/spec/spec_helper.rb +5 -4
  57. metadata +15 -6
  58. data/lib/tdiary/application/extensions/omniauth.rb +0 -22
  59. data/misc/paas/heroku/Gemfile +0 -20
  60. data/misc/paas/heroku/Gemfile.lock +0 -74
@@ -7,7 +7,7 @@ require 'tdiary/rack'
7
7
  # FIXME too dirty hack :-<
8
8
  class CGI
9
9
  def env_table_rack
10
- $RACK_ENV
10
+ $RACK_ENV || ENV
11
11
  end
12
12
 
13
13
  alias :env_table_orig :env_table
@@ -27,53 +27,76 @@ module TDiary
27
27
  end
28
28
 
29
29
  def initialize( base_dir = '/' )
30
- @app = ::Rack::Builder.app {
30
+ @app = ::Rack::Builder.app do
31
31
  map base_dir do
32
- # call extensions setup before the core setup (fixed #442)
33
- Application.config.builder_procs.reverse.each do |builder_proc|
34
- instance_eval &builder_proc
32
+ map Application.config.path[:index] do
33
+ use TDiary::Rack::HtmlAnchor
34
+ use TDiary::Rack::Static, "public"
35
+ use TDiary::Rack::ValidRequestPath
36
+ run TDiary::Dispatcher.index
35
37
  end
38
+
39
+ map Application.config.path[:update] do
40
+ use TDiary::Rack::Auth
41
+ run TDiary::Dispatcher.update
42
+ end
43
+
44
+ map Application.config.path[:assets] do
45
+ environment = Sprockets::Environment.new
46
+ TDiary::Application.config.assets_paths.each {|assets_path|
47
+ environment.append_path assets_path
48
+ }
49
+
50
+ if Application.config.assets_precompile
51
+ require 'tdiary/rack/assets/precompile'
52
+ use TDiary::Rack::Assets::Precompile, environment
53
+ end
54
+
55
+ run environment
56
+ end
36
57
  end
37
- }
58
+ end
59
+ run_plugin_startup_procs
38
60
  end
39
61
 
40
62
  def call( env )
41
- @app.call( env )
42
- end
43
- end
44
-
45
- Application.configure do
46
- config.builder do
47
- map Application.config.path[:index] do
48
- use TDiary::Rack::HtmlAnchor
49
- use TDiary::Rack::Static, "public"
50
- use TDiary::Rack::ValidRequestPath
51
- run TDiary::Dispatcher.index
63
+ begin
64
+ @app.call( env )
65
+ rescue Exception => e
66
+ body = ["#{e.class}: #{e}\n"]
67
+ body << e.backtrace.join("\n")
68
+ [500, {'Content-Type' => 'text/plain'}, body]
52
69
  end
70
+ end
53
71
 
54
- map Application.config.path[:update] do
55
- instance_eval &Application.config.authenticate_proc
56
- run TDiary::Dispatcher.update
57
- end
72
+ private
73
+ def run_plugin_startup_procs
74
+ # avoid offline mode at CGI.new
75
+ ARGV.replace([""])
76
+ cgi = RackCGI.new
58
77
 
59
- map Application.config.path[:assets] do
60
- environment = Sprockets::Environment.new
61
- TDiary::Extensions::constants.map {|extension|
62
- TDiary::Extensions::const_get( extension ).assets_path
63
- }.flatten.uniq.each {|assets_path|
64
- environment.append_path assets_path
65
- }
78
+ request = TDiary::Request.new(ENV, cgi)
79
+ conf = TDiary::Configuration.new(cgi, request)
80
+ tdiary = TDiary::TDiaryBase.new(cgi, '', conf)
81
+ io = conf.io_class.new(tdiary)
66
82
 
67
- if Application.config.assets_precompile
68
- require 'tdiary/rack/assets/precompile'
69
- use TDiary::Rack::Assets::Precompile, environment
70
- end
83
+ plugin = TDiary::Plugin.new(
84
+ 'conf' => conf,
85
+ 'mode' => 'startup',
86
+ 'diaries' => tdiary.diaries,
87
+ 'cgi' => cgi,
88
+ 'years' => nil,
89
+ 'cache_path' => io.cache_path,
90
+ 'date' => Time.now,
91
+ 'comment' => nil,
92
+ 'last_modified' => Time.now, # FIXME
93
+ 'logger' => TDiary.logger,
94
+ # 'debug' => true
95
+ )
71
96
 
72
- run environment
73
- end
97
+ # run startup plugin
98
+ plugin.__send__(:startup_proc, self)
74
99
  end
75
-
76
- config.authenticate TDiary::Rack::Auth::Basic, '.htpasswd'
77
100
  end
78
101
  end
79
102
 
data/lib/tdiary/cli.rb CHANGED
@@ -58,7 +58,7 @@ module TDiary
58
58
 
59
59
  desc "assets_copy", "copy assets files"
60
60
  def assets_copy
61
- require 'tdiary/environment'
61
+ require 'tdiary'
62
62
  assets_path = File.join(TDiary.server_root, 'public/assets')
63
63
  TDiary::Application.config.assets_paths.each do |path|
64
64
  Dir.glob(File.join(path, '*')).each do |entity|
@@ -96,7 +96,10 @@ module TDiary
96
96
  "bind to the IP"
97
97
  method_option "port", aliases: "p", type: :numeric, default: 19292, banner:
98
98
  "use PORT"
99
+ method_option "log", aliases: "l", type: :string, banner:
100
+ "File to redirect output"
99
101
  def server
102
+ require 'tdiary'
100
103
  require 'tdiary/environment'
101
104
 
102
105
  if options[:cgi]
@@ -105,12 +108,13 @@ module TDiary
105
108
  :bind => options[:bind],
106
109
  :port => options[:port],
107
110
  :logger => $stderr,
108
- :access_log => $stderr,
111
+ :access_log => options[:log] ? File.open(options[:log], 'a') : $stderr
109
112
  }
110
113
  TDiary::Server.run( opts )
111
114
  elsif
112
115
  # --rack option
113
116
  # Rack::Server reads ARGV as :config, so delete it
117
+ require 'webrick'
114
118
  ARGV.shift
115
119
  opts = {
116
120
  :environment => ENV['RACK_ENV'] || "development",
@@ -118,9 +122,11 @@ module TDiary
118
122
  :Host => options[:bind],
119
123
  :Port => options[:port],
120
124
  :pid => File.expand_path("tdiary.pid"),
121
- :AccessLog => $stderr,
122
125
  :config => File.expand_path("config.ru")
123
126
  }
127
+ if options[:log]
128
+ opts[:AccessLog] = [[File.open(options[:log], 'a'), WEBrick::AccessLog::CLF]]
129
+ end
124
130
  ::Rack::Server.start( opts )
125
131
  end
126
132
  end
@@ -37,7 +37,7 @@ class String
37
37
  end
38
38
 
39
39
  def emojify
40
- self.gsub(/:([a-zA-Z0-9_+-]+):/) do |match|
40
+ self.to_str.gsub(/:([a-zA-Z0-9_+-]+):/) do |match|
41
41
  emoji_alias = $1.downcase
42
42
  emoji_url = %Q[<img src='http://www.emoji-cheat-sheet.com/graphics/emojis/%s.png' width='20' height='20' title='%s' alt='%s' class='emoji' />]
43
43
  if emoji_alias == 'plus1' or emoji_alias == '+1'
@@ -0,0 +1,55 @@
1
+ module TDiary
2
+ class DiaryContainer
3
+ # YYYYMMDD
4
+ def self.find_by_day(conf, date)
5
+ # date: YYYYMMDD
6
+ m = date.match(/^(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})$/)
7
+ raise ArgumentError.new("date must be YYYYMMDD format") unless m
8
+ new(conf, m[:year], m[:month], m[:day])
9
+ end
10
+
11
+ def self.find_by_month(conf, date)
12
+ # date: YYYYMM
13
+ m = date.match(/^(?<year>\d{4})(?<month>\d{2})$/)
14
+ raise ArgumentError.new("date must be YYYYMM format") unless m
15
+ new(conf, m[:year], m[:month])
16
+ end
17
+
18
+ def initialize(conf, year, month, day = nil)
19
+ cgi = FakeCGI.new
20
+ if year && month && day
21
+ cgi.params['date'] = ["#{year}#{month}#{day}"]
22
+ @controller = TDiaryDayWithoutFilter::new(cgi, '', conf)
23
+ elsif year && month
24
+ cgi.params['date'] = ["#{year}#{month}"]
25
+ @controller = TDiaryMonthWithoutFilter::new(cgi, '', conf)
26
+ else
27
+ raise StandardError.new
28
+ end
29
+ end
30
+
31
+ def conf
32
+ @controller.conf
33
+ end
34
+
35
+ def diaries
36
+ # Hash of 'YYYYMMDD' => TDiary::Style::WikiDiary
37
+ @controller.diaries
38
+ end
39
+
40
+ class FakeCGI < CGI
41
+ def refeter; nil end
42
+ def user_agent; nil; end
43
+ def mobile_agent?; nil; end
44
+ def request_method; 'GET'; end
45
+ end
46
+ end
47
+ end
48
+
49
+ # Local Variables:
50
+ # mode: ruby
51
+ # indent-tabs-mode: t
52
+ # tab-width: 3
53
+ # ruby-indent-level: 3
54
+ # End:
55
+ # vim: ts=3
@@ -9,6 +9,9 @@ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../Gemfile', __FILE__)
9
9
 
10
10
  require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
11
11
 
12
+ # FIXME: workaround fix for tainted path from Gemfile.local
13
+ $LOAD_PATH.each{|lp| $LOAD_PATH << $LOAD_PATH.shift.dup.untaint}
14
+
12
15
  if defined?(Bundler)
13
16
  env = [:default]
14
17
  env << :development if ENV['RACK_ENV'].nil? || ENV['RACK_ENV'].empty?
@@ -342,12 +342,13 @@ def default_ogp
342
342
  uri = @conf.index.dup
343
343
  uri[0, 0] = base_url if %r|^https?://|i !~ @conf.index
344
344
  uri.gsub!( %r|/\./|, '/' )
345
+ image = File.join(uri, "#{theme_url}/ogimage.png")
345
346
  if @mode == 'day' then
346
347
  uri += anchor( @date.strftime( '%Y%m%d' ) )
347
348
  end
348
349
  %Q[<meta content="#{title_tag.gsub(/<[^>]*>/, "")}" property="og:title">
349
350
  <meta content="#{(@mode == 'day') ? 'article' : 'website'}" property="og:type">
350
- <meta content="#{h uri}#{h theme_url}/ogimage.png" property="og:image">
351
+ <meta content="#{h image}" property="og:image">
351
352
  <meta content="#{h uri}" property="og:url">]
352
353
  end
353
354
  end
@@ -383,7 +384,7 @@ def script_tag
383
384
  require 'uri'
384
385
  query = script_tag_query_string
385
386
  html = @javascripts.sort.map {|script|
386
- if URI(script).scheme
387
+ if URI(script).scheme or script =~ %r|\A//|
387
388
  %Q|<script src="#{script}" type="text/javascript"></script>|
388
389
  else
389
390
  %Q|<script src="#{js_url}/#{script}#{query}" type="text/javascript"></script>|
@@ -1001,7 +1002,7 @@ end
1001
1002
  # old ruby alert
1002
1003
  #
1003
1004
  def old_ruby_alert
1004
- if RUBY_VERSION < '1.9' and !@conf['old_ruby_alert.hide']
1005
+ if RUBY_VERSION < '2.0.0' and !@conf['old_ruby_alert.hide']
1005
1006
  %Q|<div class="alert-warn">
1006
1007
  <a href="#" class="action-button" id="alert-old-ruby">&times;</a>
1007
1008
  #{old_ruby_alert_message}
@@ -117,13 +117,15 @@ def referer_update( diary )
117
117
  end
118
118
 
119
119
  when 'day'
120
- referer_load_current( diary )
121
- referer_save_current( diary, @cgi.referer )
122
- if latest_day?( diary ) then
123
- referer_load_volatile( @referer_volatile )
124
- elsif @cgi.referer
125
- referer_load_volatile( @referer_volatile )
126
- referer_save_volatile( @referer_volatile, @cgi.referer )
120
+ if diary
121
+ referer_load_current( diary )
122
+ referer_save_current( diary, @cgi.referer )
123
+ if latest_day?( diary ) then
124
+ referer_load_volatile( @referer_volatile )
125
+ elsif @cgi.referer
126
+ referer_load_volatile( @referer_volatile )
127
+ referer_save_volatile( @referer_volatile, @cgi.referer )
128
+ end
127
129
  end
128
130
 
129
131
  when "edit"
data/lib/tdiary/plugin.rb CHANGED
@@ -31,6 +31,7 @@ module TDiary
31
31
  @conf_procs = {}
32
32
  @conf_genre_label = {}
33
33
  @content_procs = {}
34
+ @startup_procs = []
34
35
  @cookies = []
35
36
  @javascripts = []
36
37
  @javascript_setting = []
@@ -337,6 +338,16 @@ module TDiary
337
338
  @content_procs[key].call( date )
338
339
  end
339
340
 
341
+ def add_startup_proc( block = Proc::new )
342
+ @startup_procs << block
343
+ end
344
+
345
+ def startup_proc( app )
346
+ @startup_procs.each do |proc|
347
+ proc.call( app )
348
+ end
349
+ end
350
+
340
351
  def remove_tag( str )
341
352
  str.gsub( /<[^"'<>]*(?:"[^"]*"[^"'<>]*|'[^']*'[^"'<>]*)*(?:>|(?=<)|$)/, '' )
342
353
  end
@@ -344,8 +355,8 @@ module TDiary
344
355
  def apply_plugin( str, remove_tag = false )
345
356
  return '' unless str
346
357
  r = str.dup
347
- if @conf.options['apply_plugin'] and str.index( '<%' ) then
348
- r = str.untaint if $SAFE < 3
358
+ if @conf.options['apply_plugin'] and r.index( '<%' ) then
359
+ r = r.untaint if $SAFE < 3
349
360
  Safe::safe( @conf.secure ? 4 : 1 ) do
350
361
  begin
351
362
  r = ERB::new( r ).result( binding )
@@ -3,7 +3,7 @@ require 'webrick/httpauth/htpasswd'
3
3
 
4
4
  module TDiary
5
5
  module Rack
6
- module Auth
6
+ class Auth
7
7
  class PasswordFileNotFound < StandardError; end
8
8
 
9
9
  class Basic
@@ -0,0 +1,64 @@
1
+ require 'omniauth'
2
+
3
+ module TDiary
4
+ module Rack
5
+ class Auth
6
+ class OmniAuth
7
+ class Authorization
8
+ def initialize(app, provider, &block)
9
+ @app = app
10
+ @provider = provider
11
+ @authz = block
12
+ end
13
+
14
+ def call(env)
15
+ if not authenticate?(env)
16
+ # phase 1: request phase
17
+ login(env)
18
+ elsif env['REQUEST_PATH'].match(%r|auth/#{@provider}/callback|)
19
+ # phase 2: callback phase
20
+ callback(env)
21
+ else
22
+ # phase 3: authorization phase
23
+ auth = env['rack.session']['auth']
24
+ env['REMOTE_USER'] = "#{auth.uid}@#{auth.provider}"
25
+ return forbidden unless @authz.call(auth)
26
+ @app.call(env)
27
+ end
28
+ end
29
+
30
+ def login(env)
31
+ STDERR.puts "use #{@provider} authentication strategy"
32
+ req = ::Rack::Request.new(env)
33
+ env['rack.session']['tdiary.auth.redirect'] = "#{req.base_url}#{req.fullpath}"
34
+ redirect = File.join("#{req.base_url}#{req.path}", "#{::OmniAuth.config.path_prefix}/#{@provider}")
35
+ [302, {'Content-Type' => 'text/plain', 'Location' => redirect}, []]
36
+ end
37
+
38
+ def logout(env)
39
+ env['rack.session']['user_id'] = nil
40
+ end
41
+
42
+ def forbidden
43
+ [403, {'Content-Type' => 'text/plain'}, ['forbidden']]
44
+ end
45
+
46
+ def callback(env)
47
+ # reset sesstion to prevend session fixation attack
48
+ # see: http://www.ipa.go.jp/security/vuln/documents/website_security.pdf (section 1.4)
49
+ env['rack.session.options'][:renew] = true
50
+ auth = env['omniauth.auth']
51
+ env['rack.session']['auth'] = auth
52
+ env['REMOTE_USER'] = "#{auth.uid}@#{auth.provider}"
53
+ redirect = env['rack.session']['tdiary.auth.redirect'] || '/'
54
+ [302, {'Content-Type' => 'text/plain', 'Location' => redirect}, []]
55
+ end
56
+
57
+ def authenticate?(env)
58
+ env['omniauth.auth'] || env['rack.session']['auth']
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -1,51 +1,83 @@
1
1
  require 'omniauth'
2
+ require 'tdiary/rack/auth/omniauth/authorization'
2
3
 
3
- module TDiary
4
- module Rack
5
- module Auth
6
- class OmniAuth
7
- def initialize(app, provider, &block)
8
- @app = app
9
- @provider = provider
10
- @authz = block
11
- end
12
-
13
- def call(env)
14
- auth = env['rack.session']['auth']
15
- return login(env) unless auth
16
- env['REMOTE_USER'] = "#{auth.uid}@#{auth.provider}"
17
- return forbidden unless @authz.call(auth)
18
- @app.call(env)
19
- end
20
-
21
- def login(env)
22
- env['rack.session']['tdiary.auth.redirect'] =
23
- "#{env['REQUEST_PATH']}?#{env['QUERY_STRING']}"
24
- redirect = File.join(File.dirname(env['REQUEST_PATH']), "#{::OmniAuth.config.path_prefix}/#{@provider}")
25
- [302, {'Content-Type' => 'text/plain', 'Location' => redirect}, []]
26
- end
27
-
28
- def logout(env)
29
- env['rack.session']['user_id'] = nil
30
- end
31
-
32
- def forbidden
33
- [403, {'Content-Type' => 'text/plain'}, ['forbidden']]
34
- end
35
-
36
- class CallbackHandler
37
- def call(env)
38
- # reset sesstion to prevend session fixation attack
39
- # see: http://www.ipa.go.jp/security/vuln/documents/website_security.pdf (section 1.4)
40
- env['rack.session.options'][:renew] = true
41
- auth = env['omniauth.auth']
42
- env['rack.session']['auth'] = auth
43
- env['REMOTE_USER'] = "#{auth.uid}@#{auth.provider}"
44
- redirect = env['rack.session']['tdiary.auth.redirect'] || '/'
45
- [302, {'Content-Type' => 'text/plain', 'Location' => redirect}, []]
46
- end
47
- end
48
- end
4
+ class TDiary::Rack::Auth::OmniAuth
5
+ class NoStrategyFoundError < StandardError; end
6
+ @provider_procs = {}
7
+
8
+ class << self
9
+ attr_reader :provider_procs
10
+ end
11
+
12
+ def self.add_provider(name, &block)
13
+ @provider_procs[name] = block
14
+ end
15
+
16
+ def initialize(app)
17
+ provider = enabled_providers.first
18
+ unless provider
19
+ raise NoStrategyFoundError.new("Not found any strategies. Write the omniauth strategy in your Gemfile.local.")
20
+ end
21
+
22
+ @builder = ::Rack::Builder.new(app) {
23
+ use TDiary::Rack::Session
24
+ }
25
+ @builder.instance_eval(&self.class.provider_procs[provider])
26
+ end
27
+
28
+ def call(env)
29
+ @builder.call(env)
30
+ end
31
+
32
+ add_provider(:Twitter) do
33
+ # https://apps.twitter.com/
34
+ # https://github.com/arunagw/omniauth-twitter
35
+ use ::OmniAuth::Builder do
36
+ provider :twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']
37
+ end
38
+ use TDiary::Rack::Auth::OmniAuth::Authorization, :twitter do |auth|
39
+ ENV['TWITTER_NAME'].split(/,/).include?(auth.info.nickname)
40
+ end
41
+ end
42
+
43
+ add_provider(:Facebook) do
44
+ # https://developers.facebook.com/apps/
45
+ # https://github.com/mkdynamic/omniauth-facebook
46
+ use ::OmniAuth::Builder do
47
+ provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET']
48
+ end
49
+ use TDiary::Rack::Auth::OmniAuth::Authorization, :facebook do |auth|
50
+ ENV['FACEBOOK_EMAIL'].split(/,/).include?(auth.info.email)
51
+ end
52
+ end
53
+
54
+ add_provider(:GitHub) do
55
+ # https://github.com/settings/applications
56
+ # https://github.com/intridea/omniauth-github
57
+ use ::OmniAuth::Builder do
58
+ provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET']
59
+ end
60
+ use TDiary::Rack::Auth::OmniAuth::Authorization, :github do |auth|
61
+ ENV['GITHUB_NAME'].split(/,/).include?(auth.info.nickname)
62
+ end
63
+ end
64
+
65
+ add_provider(:GoogleOauth2) do
66
+ # https://code.google.com/apis/console/
67
+ # https://github.com/zquestz/omniauth-google-oauth2
68
+ use ::OmniAuth::Builder do
69
+ provider :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"]
70
+ end
71
+ use TDiary::Rack::Auth::OmniAuth::Authorization, :google_oauth2 do |auth|
72
+ ENV['GOOGLE_EMAIL'].split(/,/).include?(auth.info.email)
73
+ end
74
+ end
75
+
76
+ private
77
+
78
+ def enabled_providers
79
+ ::OmniAuth::Strategies.constants.select do |name|
80
+ self.class.provider_procs.has_key?(name)
49
81
  end
50
82
  end
51
83
  end
@@ -0,0 +1,20 @@
1
+ module TDiary
2
+ module Rack
3
+ class Auth
4
+ autoload :Basic, 'tdiary/rack/auth/basic'
5
+ autoload :OmniAuth, 'tdiary/rack/auth/omniauth'
6
+
7
+ def initialize(app)
8
+ if defined? ::OmniAuth
9
+ @app = TDiary::Rack::Auth::OmniAuth.new(app)
10
+ else
11
+ @app = TDiary::Rack::Auth::Basic.new(app, '.htpasswd')
12
+ end
13
+ end
14
+
15
+ def call(env)
16
+ @app.call(env)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,35 @@
1
+ begin
2
+ require 'rack/session/dalli'
3
+ rescue LoadError
4
+ end
5
+
6
+ module TDiary
7
+ module Rack
8
+ class Session
9
+ def initialize(app)
10
+ @app = session_middleware(app)
11
+ end
12
+
13
+ def call(env)
14
+ @app.call(env)
15
+ end
16
+
17
+ private
18
+
19
+ def session_middleware(app)
20
+ if ::Rack::Session.const_defined? :Dalli
21
+ ::Rack::Session::Dalli.new(
22
+ app,
23
+ cache: Dalli::Client.new,
24
+ expire_after: 2592000
25
+ )
26
+ else
27
+ ::Rack::Session::Pool.new(
28
+ app,
29
+ expire_after: 2592000
30
+ )
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
data/lib/tdiary/rack.rb CHANGED
@@ -4,16 +4,13 @@ module TDiary
4
4
  module Rack
5
5
  autoload :HtmlAnchor, 'tdiary/rack/html_anchor'
6
6
  autoload :ValidRequestPath, 'tdiary/rack/valid_request_path'
7
+ autoload :Session, 'tdiary/rack/session'
7
8
  autoload :Static, 'tdiary/rack/static'
9
+ autoload :Auth, 'tdiary/rack/auth'
8
10
 
9
11
  module Assets
10
12
  autoload :Precompile, 'tdiary/rack/assets/precompile'
11
13
  end
12
-
13
- module Auth
14
- autoload :Basic, 'tdiary/rack/auth/basic'
15
- autoload :OmniAuth, 'tdiary/rack/auth/omniauth'
16
- end
17
14
  end
18
15
  end
19
16
 
data/lib/tdiary/style.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'tdiary/comment_manager'
4
4
  require 'tdiary/referer_manager'
5
+ require 'erb'
5
6
 
6
7
  module TDiary
7
8
  module Style
@@ -46,7 +47,7 @@ module TDiary
46
47
  end
47
48
 
48
49
  module BaseDiary
49
- include ERB::Util
50
+ include ::ERB::Util
50
51
  include CommentManager
51
52
  include RefererManager
52
53
 
@@ -13,7 +13,7 @@ namespace :assets do
13
13
  desc "copy assets files"
14
14
  task :copy do
15
15
  require 'fileutils'
16
- assets_path = File.dirname(__FILE__) + '/../../public/assets'
16
+ assets_path = File.dirname(__FILE__) + '/../../../public/assets'
17
17
 
18
18
  FileUtils.mkdir_p assets_path
19
19
  FileList['{js,theme}/*'].each do |file|