restpack_web 0.2.21 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YzkyYjAzMWU0NjA1MzUyYjc5NWU3MzM3NDg5ZmY0ZGE1MDI5MDcxZg==
5
- data.tar.gz: !binary |-
6
- Njc5OTE3NGJhYjRkYjkwY2IyMjVlNWM3N2I5M2JiYmMyNDcxZjFlYQ==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- OTg1ZWZlMGM5ZmQxMGRkZTQwYjk1Yzg3ZTEyNjA2MDY5NDc2Y2IzNWM0MWNh
10
- MDNjMWMzNmRmYWMxMzMzYTEyMDg3YWVjOGRhYmIzMTcyMDFmZjI2YTcyMGIw
11
- MTllMjNiOWY3ZDVjNDlmZDZlNThjZDg5MDMwZjBiYjY0YmRkNzY=
12
- data.tar.gz: !binary |-
13
- OGY1OTI2MmZjMTM3ZTEzNWFmZjU5MDExMjE3MTQxMzJiYjMzOGZkMmM2NmQw
14
- Y2JmNGM5ZDZjNjQ4NGJjZWU0NTNlOGJkNjFhMjhjN2ZhNjgwZDNmMmJlMDI3
15
- MmQxMmMxNWIwYjQ2MmY1MDVkZWE2ZjNjYWM0NTRmY2EyNTVlNWY=
2
+ SHA1:
3
+ metadata.gz: 5747a127d3d522bcf4968a47bf9ec9006b68acf0
4
+ data.tar.gz: 9e7e8ebf35d75e3fce8d99dd980820383f66bf47
5
+ SHA512:
6
+ metadata.gz: c9abd53df497790f5ed9904466a79bc2c930ab812ae6a43a5812a74461b65de24edadb8e3c710bccb4cfad3b17a62df1e23a9f9838200dce1e4ed2206e10b732
7
+ data.tar.gz: 7b3b5dbed34dc462c45158f3696a785fa6d55c1a2b450ce26f391583828cecd14ee1fe4984c66aff27d916ca00a3e69de35176abd228523c7346e90d945de860
data/.gitignore CHANGED
@@ -14,4 +14,4 @@ rdoc
14
14
  spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
- tmp
17
+ tmp
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ gem 'encrypted_cookie', path: '../encrypted_cookie'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Gavin Joyce
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,30 +1,3 @@
1
- ## Install in Rails 3
2
- Add `restpack-web` to Gemfile
3
- ```ruby
4
- gem 'restpack-web', '~> 0.2.16', :git => 'git://github.com/RestPack/restpack-web.git'
5
- ```
6
-
7
- Use `RestPack::Web::App` as a middleware in `application.rb`
8
- ```ruby
9
- config.middleware.use RestPack::Web::App
10
- ```
11
-
12
- Set necessary environment variables:
13
- ```ruby
14
- ENV['RESTPACK_ACCESS_KEY'] = 'secret'
15
- ENV['RESTPACK_CORE_SERVICE'] = 'restpack-core-service.restpack.org'
16
- ```
17
-
18
- ## Testing your app
19
- You can stub all requests from restpack_web to the core service.
20
- ```ruby
21
- require 'restpack_web/test_support'
22
- RestPack::Web::TestSupport.stub_restpack_web
23
- ```
24
-
25
- ## Testing
26
- `bundle exec rake test`
27
-
28
- ## Bump to a new version
29
- `bundle exec bump patch && rake tag`
1
+ # restpack_web
30
2
 
3
+ Rack middleware for your RestPack web apps
data/Rakefile CHANGED
@@ -1,39 +1,2 @@
1
- require 'bundler/setup'
2
- require 'rake/testtask'
3
- require 'bump/tasks'
4
- require_relative 'lib/restpack_web/version'
5
-
6
- task :default => [:test]
7
-
8
- Rake::TestTask.new do |t|
9
- t.pattern = 'spec/**/*_spec.rb'
10
- end
11
-
12
- task :gem do
13
- ["gem:bump", "gem:tag", "gem:build", "gem:push"].each do |task|
14
- Rake::Task[task].reenable
15
- Rake::Task[task].invoke
16
- end
17
- end
18
-
19
- namespace :gem do
20
- task :build do
21
- sh "gem build restpack_web.gemspec"
22
- end
23
-
24
- task :push do
25
- require 'bump'
26
- sh "gem push restpack_web-#{Bump::Bump.current}.gem"
27
- end
28
-
29
- task :tag do
30
- require 'bump'
31
- version = Bump::Bump.current
32
- puts "tagging v#{version}"
33
- `git push && git tag v#{version} && git push --tags`
34
- end
35
-
36
- task :bump do
37
- Rake::Task["bump:patch"].invoke
38
- end
39
- end
1
+ require "restpack_gem"
2
+ RestPack::Gem::Tasks.load_tasks
@@ -1,6 +1,11 @@
1
1
  require 'restpack_web/version'
2
+
3
+ require 'restpack_web/configuration'
2
4
  require 'restpack_web/context'
3
- require 'restpack_web/app'
5
+
6
+ require 'restpack_web/rack/domain'
7
+ require 'restpack_web/rack/user'
8
+ require 'restpack_web/rack/session'
4
9
 
5
10
  require 'restpack_web/rails/controller'
6
11
  require 'restpack_web/sinatra/app'
@@ -0,0 +1,16 @@
1
+ module RestPack::Web
2
+ class Configuration
3
+ attr_accessor :authentication_port
4
+
5
+ def initialize
6
+ @authentication_port = nil
7
+ end
8
+ end
9
+
10
+ mattr_accessor :config
11
+ @@config = Configuration.new
12
+
13
+ def self.setup
14
+ yield @@config
15
+ end
16
+ end
@@ -1,119 +1,70 @@
1
- require 'public_suffix'
2
-
3
- module RestPack
4
- module Web
5
- class Context
6
- attr_accessor :request, :domain, :application, :channel, :configurations, :user, :user_id
7
-
8
- def initialize(env)
9
- restpack = env[:restpack]
10
-
11
- if restpack
12
- @request = restpack[:request]
13
- @domain = restpack[:domain]
14
- @application = restpack[:application]
15
- @channel = restpack[:channel]
16
- @configurations = restpack[:configurations]
17
- @user = restpack[:user]
18
- @user_id = @user[:id] if @user
19
- end
1
+ module RestPack::Web
2
+ class Context
3
+ attr_accessor :domain, :application, :user, :account
4
+
5
+ def initialize(env)
6
+ restpack = env['restpack']
7
+
8
+ if restpack
9
+ @domain = restpack[:domain]
10
+ @application = restpack[:application]
11
+ @user = restpack[:user]
12
+ @account = restpack[:account]
20
13
  end
14
+ end
21
15
 
22
- def is_authenticated?
23
- !@user.nil?
24
- end
25
-
26
- def get_configuration(key)
27
- @configurations.find { |c| c.key == key }
28
- end
29
-
30
- def services
31
- get_configuration('services').value
32
- end
33
-
34
- def get_service(name)
35
- services.find { |s| s[:name] == name }
36
- end
37
-
38
- def get_service_domain(name)
39
- get_service(name)[:domain]
40
- end
41
-
42
- def home_domain
43
- @application.home_domain || "www.#{root_domain}"
44
- end
45
-
46
- def auth_domain
47
- @application.auth_domain || "auth.#{root_domain}"
48
- end
49
-
50
- def root_domain
51
- PublicSuffix.parse(@domain.host).domain
52
- end
53
-
54
- def logout_url(next_url = nil) #TODO: GJ: whitelist the next_url
55
- next_url ||= "http://#{home_domain}/"
56
- "http://#{auth_domain}/auth/logout?next=#{next_url}"
57
- end
58
-
59
- def login_url(provider = :twitter, next_url = nil)
60
- next_url ||= "http://#{home_domain}/"
61
- "http://#{auth_domain}/auth/#{provider}?next=#{next_url}"
62
- end
63
-
64
- def debug_info #TODO: GJ: move to a mixin
65
- user_debug_info = ""
66
- if is_authenticated?
67
- image_debug_info = ""
68
- image_debug_info = " * **image** : #{@user[:image]} ![Image](#{@user[:image]}" unless @user[:image].nil?
69
-
70
- user_debug_info = %{
71
- * **id** : #{@user[:id]}
72
- * **name** : #{@user[:name]}
73
- * **nickname** : #{@user[:nickname]}
74
- * **location** : #{@user[:location]}
75
- * **description** : #{@user[:description]}
76
- #{image_debug_info})
77
- }
78
- end
16
+ def authenticated?
17
+ !@user.nil?
18
+ end
79
19
 
80
- %{
81
- ### RestPack Context:
20
+ def user_id
21
+ authenticated? ? @user[:id] : nil
22
+ end
82
23
 
83
- #### User:
24
+ def account_id
25
+ authenticated? ? @account[:id] : nil
26
+ end
84
27
 
85
- * **is authenticated**: #{is_authenticated?}
86
- #{user_debug_info}
28
+ def application_id
29
+ @application[:id]
30
+ end
87
31
 
88
- #### Channel:
32
+ def domain_id
33
+ @domain[:id]
34
+ end
89
35
 
90
- * **id**: #{@channel.id}
91
- * **name**: #{@channel.name}
92
- * **applications**: #{@channel.applications.map { |a| a.name }.join(', ')}
36
+ def home_domain
37
+ "www.#{@domain[:identifier]}"
38
+ end
93
39
 
94
- #### Application:
40
+ def auth_domain
41
+ "auth.#{@domain[:identifier]}"
42
+ end
95
43
 
96
- * **id**: #{@application.id}
97
- * **name**: #{@application.name}
98
- * **domains**: #{@application.domains.map { |a| a.host }.join(', ')}
44
+ def logout_url(next_url = nil)
45
+ next_url ||= "http://#{home_domain}/"
46
+ auth_url('/auth/logout', next_url)
47
+ end
99
48
 
100
- #### Domain:
49
+ def login_url(provider = :twitter, next_url = nil)
50
+ auth_url("/auth/#{provider}", next_url)
51
+ end
101
52
 
102
- * **id**: #{@domain.id}
103
- * **host**: #{@domain.host}
53
+ def debug_info
54
+ "todo"
55
+ end
104
56
 
105
- #### Configuration:
57
+ private
106
58
 
107
- * **keys**: #{@configurations.map { |c| c.key }.join(', ')}
108
- * **services**: #{services.map { |s| s[:name] + ': ' + s[:domain] }.join(', ')}
59
+ def auth_url(path, next_url = nil)
60
+ #TODO: GJ: whitelist the next_url?
61
+ #TODO: GJ: URI encode next_url?
62
+ port = RestPack::Web.config.authentication_port
109
63
 
110
- #### Authentication:
64
+ port_part = port ? ":#{port}" : ""
65
+ next_part = next_url ? "?next=#{next_url}" : ""
111
66
 
112
- * **logout**: #{logout_url}
113
- * **twitter oauth**: #{login_url(:twitter)}
114
- * **google oauth**: #{login_url(:google_oauth2)}
115
- }
116
- end
67
+ "http://#{auth_domain}#{port_part}#{path}#{next_part}"
117
68
  end
118
69
  end
119
70
  end
@@ -0,0 +1,36 @@
1
+ module RestPack::Web::Rack
2
+ class Domain
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ identifier = Rack::Request.new(env).host
9
+
10
+ response = Commands::Core::Domain::ByIdentifier.run({
11
+ identifier: identifier,
12
+ include: 'applications'
13
+ })
14
+
15
+ if response.status == :ok
16
+ domain = response.result[:domains][0]
17
+ application = response.result[:applications][0]
18
+
19
+ env['restpack'] ||= {}
20
+ env['restpack'][:domain] = domain
21
+ env['restpack'][:application] = application
22
+ env['restpack'][:application_id] = application[:id]
23
+
24
+ env['restpack.session.options'] ||= {}
25
+ env['restpack.session.options'][:key] = 'restpack.session'
26
+ env['restpack.session.options'][:secret] = domain[:session_secret]
27
+ env['restpack.session.options'][:domain] = domain[:identifier]
28
+ else
29
+ #TODO: GJ: better exceptions based on response status
30
+ raise "[#{identifier}] is not a RestPack domain"
31
+ end
32
+
33
+ @app.call(env)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,7 @@
1
+ require 'encrypted_cookie'
2
+
3
+ module RestPack::Web::Rack
4
+ class Session < Rack::Session::EncryptedCookie
5
+
6
+ end
7
+ end
@@ -0,0 +1,35 @@
1
+ require 'restpack_user_service'
2
+ require 'restpack_account_service'
3
+
4
+ module RestPack::Web::Rack
5
+ class User
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ session = env["restpack.session"]
12
+ user_id = session[:user_id]
13
+ account_id = session[:account_id]
14
+
15
+ if user_id && account_id
16
+ response = Commands::Users::User::Get.run({
17
+ id: user_id,
18
+ application_id: env['restpack'][:application_id]
19
+ })
20
+
21
+ raise "Error getting user" unless response.success?
22
+ env['restpack'][:user] = response.result[:users][0]
23
+
24
+ response = Commands::Accounts::Account::Get.run({
25
+ id: account_id,
26
+ application_id: env['restpack'][:application_id]
27
+ })
28
+ raise "Error getting account" unless response.success?
29
+ env['restpack'][:account] = response.result[:accounts][0]
30
+ end
31
+
32
+ @app.call(env)
33
+ end
34
+ end
35
+ end
@@ -1,15 +1,11 @@
1
- module RestPack
2
- module Web
3
- module Rails
4
- module Controller
5
- def self.included(base)
6
- base.send(:before_filter, :include_restpack)
7
- end
8
-
9
- def include_restpack
10
- @restpack = RestPack::Web::Context.new(request.env)
11
- end
12
- end
1
+ module RestPack::Web::Rails
2
+ module Controller
3
+ def self.included(base)
4
+ base.send(:before_filter, :setup_restpack_context)
5
+ end
6
+
7
+ def setup_restpack_context
8
+ @restpack = RestPack::Web::Context.new(request.env)
13
9
  end
14
10
  end
15
- end
11
+ end
@@ -1,20 +1,13 @@
1
- require 'rack-auto-session-domain'
1
+ module RestPack::Web::Sinatra
2
+ module App
3
+ def self.included(base)
4
+ base.use RestPack::Web::Rack::Domain
5
+ base.use RestPack::Web::Rack::Session
6
+ base.use RestPack::Web::Rack::User
2
7
 
3
- module RestPack
4
- module Web
5
- module Sinatra
6
- module App
7
- def self.included(base)
8
- base.use Rack::AutoSessionDomain
9
- base.use RestPack::Web::App
10
- base.enable :sessions
11
- base.set :session_secret, 'c40b83f9048c497891f0f71e4b040a3f' #TODO: GJ: from restpack config
12
-
13
- base.before do
14
- @restpack = RestPack::Web::Context.new(env)
15
- end
16
- end
8
+ base.before do
9
+ @restpack = RestPack::Web::Context.new(env)
17
10
  end
18
11
  end
19
12
  end
20
- end
13
+ end