rammer 1.1.2 → 2.0.0

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.
Files changed (53) hide show
  1. data/Gemfile +27 -0
  2. data/MODULE_FILES +34 -0
  3. data/README.md +17 -5
  4. data/Rakefile +37 -0
  5. data/bin/rammer +2 -3
  6. data/bin/viber +37 -38
  7. data/lib/modules/authentication/authentication_apis.rb +64 -0
  8. data/lib/modules/authorization/authorization_apis.rb +89 -0
  9. data/lib/modules/common/Gemfile +43 -0
  10. data/lib/{template → modules/common}/Gemfile.lock +0 -0
  11. data/lib/{template → modules/common}/Procfile +0 -0
  12. data/lib/{template → modules/common}/Rakefile +27 -0
  13. data/lib/modules/common/application.rb +48 -0
  14. data/lib/{template → modules/common}/database.yml +1 -1
  15. data/lib/modules/common/server.rb +38 -0
  16. data/lib/modules/common/tree.rb +30 -0
  17. data/lib/{template → modules/migrations}/01_create_users.rb +27 -0
  18. data/lib/modules/migrations/02_create_sessions.rb +36 -0
  19. data/lib/modules/migrations/03_create_owners.rb +40 -0
  20. data/lib/modules/migrations/04_create_oauth2_authorizations.rb +50 -0
  21. data/lib/modules/migrations/05_create_oauth2_clients.rb +45 -0
  22. data/lib/modules/models/oauth2_authorization.rb +203 -0
  23. data/lib/modules/models/oauth2_client.rb +216 -0
  24. data/lib/modules/models/owner.rb +65 -0
  25. data/lib/modules/models/session.rb +30 -0
  26. data/lib/modules/models/user.rb +135 -0
  27. data/lib/modules/oauth/oauth_apis.rb +92 -0
  28. data/lib/rammer/module_generator.rb +236 -0
  29. data/lib/rammer/rammer_generator.rb +160 -0
  30. data/lib/rammer/version.rb +28 -1
  31. data/lib/rammer.rb +24 -278
  32. data/rammer.gemspec +37 -24
  33. data/test/helper.rb +49 -0
  34. data/test/test_rammer_root_structure.rb +80 -0
  35. data/test/test_viber_module_plugin.rb +104 -0
  36. data/test/test_viber_module_unplug.rb +87 -0
  37. metadata +77 -29
  38. data/lib/template/02_create_sessions.rb +0 -9
  39. data/lib/template/03_create_owners.rb +0 -13
  40. data/lib/template/04_create_oauth2_authorizations.rb +0 -23
  41. data/lib/template/05_create_oauth2_clients.rb +0 -18
  42. data/lib/template/Gemfile +0 -16
  43. data/lib/template/application.rb +0 -21
  44. data/lib/template/authentication_apis.rb +0 -35
  45. data/lib/template/authorization_apis.rb +0 -59
  46. data/lib/template/oauth2_authorization.rb +0 -113
  47. data/lib/template/oauth2_client.rb +0 -100
  48. data/lib/template/oauth_apis.rb +0 -138
  49. data/lib/template/owner.rb +0 -10
  50. data/lib/template/server.rb +0 -11
  51. data/lib/template/session.rb +0 -3
  52. data/lib/template/tree.rb +0 -3
  53. data/lib/template/user.rb +0 -78
@@ -0,0 +1,87 @@
1
+ =begin
2
+ **************************************************************************
3
+ * The MIT License (MIT)
4
+
5
+ * Copyright (c) 2013-2014 QBurst Technologies Inc.
6
+
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ * THE SOFTWARE.
24
+
25
+ **************************************************************************
26
+ =end
27
+
28
+ require_relative './helper'
29
+
30
+ $test_file = "dummy"
31
+ $test_file_root = "#{Dir.pwd}/test"
32
+
33
+ class TestViberModuleUnmplug < Test::Unit::TestCase
34
+
35
+ AUTHENTICATE_MODULE_FILES = ["app/apis/#{$test_file}/modules/authentication_apis.rb"]
36
+ AUTHORIZE_MODULE_FILES = ["app/apis/#{$test_file}/modules/authorization_apis.rb"]
37
+ OAUTH_MODULE_FILES = ["app/apis/#{$test_file}/modules/oauth_apis.rb"]
38
+ MODULE_CLASS = $test_file.split('_').map(&:capitalize)*''
39
+
40
+ def test_generator_root_module_unmount_authenticate
41
+ dir_path = Dir.pwd
42
+ module_class = "::#{MODULE_CLASS}::AuthenticationApis"
43
+ options = { :project_name => "#{$test_file}", :module_class => module_class,
44
+ :module_name => "authentication", :action => "-u"}
45
+ generator = Rammer::ModuleGenerator.new(options)
46
+ generator.run
47
+ AUTHENTICATE_MODULE_FILES.each do |file|
48
+ assert_equal(false, File.file?("#{dir_path}/#{file}"))
49
+ end
50
+ end
51
+
52
+ def test_generator_root_module_unmount_authorize
53
+ dir_path = Dir.pwd
54
+ module_class = "::#{MODULE_CLASS}::AuthorizationApis"
55
+ options = { :project_name => "#{$test_file}", :module_class => module_class,
56
+ :module_name => "authorization", :action => "-u"}
57
+ generator = Rammer::ModuleGenerator.new(options)
58
+ generator.run
59
+ AUTHORIZE_MODULE_FILES.each do |file|
60
+ assert_equal(false, File.file?("#{dir_path}/#{file}"))
61
+ end
62
+ end
63
+
64
+ def test_generator_root_module_unmount_oauth
65
+ dir_path = Dir.pwd
66
+ module_class = "::#{MODULE_CLASS}::OauthApis"
67
+ options = { :project_name => "#{$test_file}", :module_class => module_class,
68
+ :module_name => "oauth", :action => "-u"}
69
+ generator = Rammer::ModuleGenerator.new(options)
70
+ generator.run
71
+ OAUTH_MODULE_FILES.each do |file|
72
+ assert_equal(false, File.file?("#{dir_path}/#{file}"))
73
+ end
74
+ end
75
+
76
+ def test_generator_root_unmouting_executed
77
+ dir_path = Dir.pwd
78
+ module_class = "::#{MODULE_CLASS}::AuthenticationApis"
79
+ options = { :project_name => "#{$test_file}", :module_class => module_class,
80
+ :module_name => "authentication", :action => "-u"}
81
+ generator = Rammer::ModuleGenerator.new(options)
82
+ generator.run
83
+ AUTHENTICATE_MODULE_FILES.each do |file|
84
+ assert_equal(false, File.file?("#{dir_path}/#{file}"))
85
+ end
86
+ end
87
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rammer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-12 00:00:00.000000000 Z
12
+ date: 2013-11-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '1.4'
21
+ version: 1.4.0.rc.1
22
22
  type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '1.4'
29
+ version: 1.4.0.rc.1
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: rake
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -43,6 +43,38 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: shoulda
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: simplecov
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
46
78
  description: Rammer is a framework dedicated to build high performance Async API servers
47
79
  on top of non-blocking (asynchronous) Ruby web server called Goliath. Rammer APIs
48
80
  are designed on top of REST-like API micro-framework Grape. Rammer is modular and
@@ -55,34 +87,41 @@ executables:
55
87
  extensions: []
56
88
  extra_rdoc_files: []
57
89
  files:
58
- - lib/rammer.rb
59
- - lib/rammer/version.rb
60
- - lib/template/01_create_users.rb
61
- - lib/template/02_create_sessions.rb
62
- - lib/template/03_create_owners.rb
63
- - lib/template/04_create_oauth2_authorizations.rb
64
- - lib/template/05_create_oauth2_clients.rb
65
- - lib/template/Gemfile
66
- - lib/template/Gemfile.lock
67
- - lib/template/Procfile
68
- - lib/template/Rakefile
69
- - lib/template/application.rb
70
- - lib/template/authentication_apis.rb
71
- - lib/template/authorization_apis.rb
72
- - lib/template/database.yml
73
- - lib/template/oauth2_authorization.rb
74
- - lib/template/oauth2_client.rb
75
- - lib/template/oauth_apis.rb
76
- - lib/template/owner.rb
77
- - lib/template/server.rb
78
- - lib/template/session.rb
79
- - lib/template/tree.rb
80
- - lib/template/user.rb
81
90
  - Gemfile
82
91
  - LICENSE.txt
83
92
  - README.md
84
93
  - Rakefile
94
+ - MODULE_FILES
85
95
  - rammer.gemspec
96
+ - lib/rammer.rb
97
+ - lib/rammer/version.rb
98
+ - lib/rammer/rammer_generator.rb
99
+ - lib/rammer/module_generator.rb
100
+ - lib/modules/migrations/01_create_users.rb
101
+ - lib/modules/migrations/02_create_sessions.rb
102
+ - lib/modules/migrations/03_create_owners.rb
103
+ - lib/modules/migrations/04_create_oauth2_authorizations.rb
104
+ - lib/modules/migrations/05_create_oauth2_clients.rb
105
+ - lib/modules/common/Gemfile
106
+ - lib/modules/common/Gemfile.lock
107
+ - lib/modules/common/Procfile
108
+ - lib/modules/common/Rakefile
109
+ - lib/modules/common/application.rb
110
+ - lib/modules/authentication/authentication_apis.rb
111
+ - lib/modules/authorization/authorization_apis.rb
112
+ - lib/modules/common/database.yml
113
+ - lib/modules/models/oauth2_authorization.rb
114
+ - lib/modules/models/oauth2_client.rb
115
+ - lib/modules/oauth/oauth_apis.rb
116
+ - lib/modules/models/owner.rb
117
+ - lib/modules/common/server.rb
118
+ - lib/modules/models/session.rb
119
+ - lib/modules/common/tree.rb
120
+ - lib/modules/models/user.rb
121
+ - test/helper.rb
122
+ - test/test_rammer_root_structure.rb
123
+ - test/test_viber_module_plugin.rb
124
+ - test/test_viber_module_unplug.rb
86
125
  - bin/rammer
87
126
  - bin/viber
88
127
  homepage: http://github.com/qburstruby/rammer
@@ -98,12 +137,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
98
137
  - - ! '>='
99
138
  - !ruby/object:Gem::Version
100
139
  version: '0'
140
+ segments:
141
+ - 0
142
+ hash: 2624680165410903349
101
143
  required_rubygems_version: !ruby/object:Gem::Requirement
102
144
  none: false
103
145
  requirements:
104
146
  - - ! '>='
105
147
  - !ruby/object:Gem::Version
106
148
  version: '0'
149
+ segments:
150
+ - 0
151
+ hash: 2624680165410903349
107
152
  requirements: []
108
153
  rubyforge_project:
109
154
  rubygems_version: 1.8.25
@@ -111,5 +156,8 @@ signing_key:
111
156
  specification_version: 3
112
157
  summary: Rammer is a framework dedicated to build high performance Async API servers
113
158
  on top of non-blocking (asynchronous) Ruby web server called Goliath.
114
- test_files: []
115
- has_rdoc:
159
+ test_files:
160
+ - test/helper.rb
161
+ - test/test_rammer_root_structure.rb
162
+ - test/test_viber_module_plugin.rb
163
+ - test/test_viber_module_unplug.rb
@@ -1,9 +0,0 @@
1
- class CreateSessions < ActiveRecord::Migration
2
- def change
3
- create_table(:sessions) do |t|
4
- t.string :user_id
5
- t.string :session_token
6
- end
7
- add_index :sessions, :session_token, :unique => true
8
- end
9
- end
@@ -1,13 +0,0 @@
1
- class CreateOwner < ActiveRecord::Migration
2
- def self.up
3
- create_table :owners do |t|
4
- t.timestamps
5
- t.string :username
6
- end
7
- add_index :owners, [:username]
8
- end
9
-
10
- def self.down
11
- drop_table :owners
12
- end
13
- end
@@ -1,23 +0,0 @@
1
- class CreateOauth2Authorizations < ActiveRecord::Migration
2
- def self.up
3
- create_table :oauth2_authorizations do |t|
4
- t.timestamps
5
- t.string :oauth2_resource_owner_type
6
- t.integer :oauth2_resource_owner_id
7
- t.belongs_to :oauth2_client
8
- t.string :scope
9
- t.string :code, :limit => 40
10
- t.string :access_token, :limit => 40
11
- t.string :refresh_token, :limit => 40
12
- t.datetime :expires_at
13
- end
14
- add_index :oauth2_authorizations, [:oauth2_client_id, :code]
15
- add_index :oauth2_authorizations, [:access_token]
16
- add_index :oauth2_authorizations, [:oauth2_client_id, :access_token], :name => 'access_token_index'
17
- add_index :oauth2_authorizations, [:oauth2_client_id, :refresh_token], :name => 'refresh_token_index'
18
- end
19
-
20
- def self.down
21
- drop_table :oauth2_authorizations
22
- end
23
- end
@@ -1,18 +0,0 @@
1
- class CreateOauth2Clients < ActiveRecord::Migration
2
- def self.up
3
- create_table :oauth2_clients do |t|
4
- t.string :name
5
- t.string :client_id
6
- t.string :client_secret_hash
7
- t.string :redirect_uri
8
- t.string :basic_code
9
- t.timestamps
10
- end
11
- add_index :oauth2_clients, :client_id, :unique => true
12
- end
13
-
14
- def self.down
15
- drop_table :oauth2_clients
16
- end
17
-
18
- end
data/lib/template/Gemfile DELETED
@@ -1,16 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
-
4
- gem 'pg'
5
- gem 'em-postgresql-adapter', :git => 'git://github.com/leftbee/em-postgresql-adapter.git'
6
- gem 'rack-fiber_pool', :require => 'rack/fiber_pool'
7
- gem 'em-synchrony', :git => 'git://github.com/igrigorik/em-synchrony.git',
8
- :require => ['em-synchrony', 'em-synchrony/activerecord', 'em-synchrony/mysql2']
9
-
10
- gem 'grape'
11
- gem 'goliath'
12
-
13
- gem "activerecord", "~> 3.1.1"
14
- gem 'rack-fiber_pool', :require => 'rack/fiber_pool'
15
- gem "mysql2"
16
-
@@ -1,21 +0,0 @@
1
- require 'uri'
2
- require 'em-synchrony/activerecord'
3
- require 'yaml'
4
- require 'erb'
5
-
6
- # Sets up database configuration
7
- db = URI.parse(ENV['DATABASE_URL'] || 'http://localhost')
8
- if db.scheme == 'postgres' # Heroku environment
9
- ActiveRecord::Base.establish_connection(
10
- :adapter => db.scheme == 'postgres' ? 'em_postgresql' : db.scheme,
11
- :host => db.host,
12
- :username => db.user,
13
- :password => db.password,
14
- :database => db.path[1..-1],
15
- :encoding => 'utf8'
16
- )
17
- else # local environment
18
- environment = ENV['DATABASE_URL'] ? 'production' : 'development'
19
- db = YAML.load(ERB.new(File.read('config/database.yml')).result)[environment]
20
- ActiveRecord::Base.establish_connection(db)
21
- end
@@ -1,35 +0,0 @@
1
- require 'oauth2'
2
- require 'songkick/oauth2/provider'
3
- require 'ruby_regex'
4
- module Rammer
5
-
6
- class AuthenticationApis < Grape::API
7
- Songkick::OAuth2::Provider.realm = 'PocketAPI Oauth Server'
8
- version 'v1', :using => :path
9
- format :json
10
- =begin
11
- This web service enables pockit server user sign up process with request parameters:
12
- {"email"=> User email,
13
- "password" => MD5 hash encrypted password,
14
- "redirect_uri" => Callback url for this api call.
15
- }
16
- =end
17
- [:get, :post].each do |method|
18
- __send__ method, '/authentication/sign_up' do
19
- if User.validate_params?(params,"sign_up")
20
- @existing_user = User.find_by_email(params.email)
21
- unless @existing_user
22
- @user, @session = User.sign_up(params)
23
- redirect @user.redirect_url(params,@session)
24
- else
25
- error = "User already exists."
26
- Oauth2Authorization.error_response(error)
27
- end
28
- else
29
- error = "Parameters missing or invalid."
30
- Oauth2Authorization.error_response(error)
31
- end
32
- end
33
- end
34
- end
35
- end
@@ -1,59 +0,0 @@
1
- require 'oauth2'
2
- require 'songkick/oauth2/provider'
3
- require 'ruby_regex'
4
- module Rammer
5
-
6
- class AuthorizationApis < Grape::API
7
- Songkick::OAuth2::Provider.realm = 'PocketAPI Oauth Server'
8
- version 'v1', :using => :path
9
- format :json
10
- =begin
11
- This web service enables pockit server user sign up process with request parameters:
12
- {"email"=> User email,
13
- "password" => MD5 hash encrypted password,
14
- "redirect_uri" => Callback url for this api call.
15
- }
16
- =end
17
- [:get, :post].each do |method|
18
- __send__ method, '/authorization/sign_in' do
19
- if User.validate_params?(params,"sign_in")
20
- @authroized_user = User.find_by_email_and_encrypted_password(params.email,params.password)
21
- if @authroized_user
22
- @session = @authroized_user.sign_in(params)
23
- redirect @authroized_user.redirect_url(params,@session)
24
- else
25
- error = "Not a registered user."
26
- Oauth2Authorization.error_response(error)
27
- end
28
- else
29
- error = "Parameters missing or invalid."
30
- Oauth2Authorization.error_response(error)
31
- end
32
- end
33
- end
34
- =begin
35
- This web service enables pockit server user sign up process with request parameters:
36
- {"email"=> User email,
37
- "session_token" => Session token obtained during sign in,
38
- "redirect_uri" => Callback url for this api call.
39
- }
40
- =end
41
- [:get, :post].each do |method|
42
- __send__ method, '/authorization/sign_out' do
43
- if User.validate_params?(params,"sign_out")
44
- @authroized_user = User.find_by_email(params.email)
45
- if @authroized_user && @authroized_user.signed_in?(params)
46
- @authroized_user.sign_out(params)
47
- redirect params.redirect_uri
48
- else
49
- error = "Invalid user or already signed out."
50
- Oauth2Authorization.error_response(error)
51
- end
52
- else
53
- error = "Parameters missing or invalid."
54
- Oauth2Authorization.error_response(error)
55
- end
56
- end
57
- end
58
- end
59
- end
@@ -1,113 +0,0 @@
1
- class Oauth2Authorization < ActiveRecord::Base
2
- belongs_to :client, :class_name => 'Oauth2Client'
3
-
4
- def get_token(owner,client, attributes = {})
5
- return nil unless owner and client
6
- @instance = owner.oauth2_authorization(client,owner) ||
7
- Oauth2Authorization.new do |authorization|
8
- authorization.oauth2_resource_owner_id = owner.id
9
- authorization.oauth2_client_id = client.id
10
- end
11
- case attributes[:response_type]
12
- when 'code'
13
- @instance.code ||= create_code(client)
14
- when 'token'
15
- @instance.access_token ||= create_access_token
16
- @instance.refresh_token ||= create_refresh_token(client)
17
- @instance.code ||= create_code(client)
18
- end
19
-
20
- if @instance.expires_at.nil?
21
- @instance.expires_at = attributes[:duration].present? ? Time.now + attributes[:duration].to_i : nil
22
- elsif attributes[:invalidate]
23
- @instance.expires_at = Time.now
24
- end
25
-
26
- if @instance.scope.nil?
27
- @instance.scope = attributes[:scope].present? ? attributes[:scope] : nil
28
- elsif attributes[:scope].present?
29
- @instance.scope += "," + attributes[:scope] unless @instance.scope.include? attributes[:scope]
30
- end
31
-
32
- @instance.save
33
- return @instance
34
-
35
- rescue Object => error
36
- raise error
37
- end
38
-
39
- def refresh_access_token
40
- self.expires_at = Time.now + 3600
41
- save
42
- end
43
-
44
- def create_code(client)
45
- Songkick::OAuth2.generate_id do |code|
46
- return code
47
- end
48
- end
49
-
50
- def create_access_token
51
- hash = nil
52
- Songkick::OAuth2.generate_id do |token|
53
- hash = Songkick::OAuth2.hashify(token)
54
- end
55
- return hash
56
- end
57
-
58
- def create_refresh_token(client)
59
- Songkick::OAuth2.generate_id do |refresh_token|
60
- hash = Songkick::OAuth2.hashify(refresh_token)
61
- end
62
- return hash
63
- end
64
-
65
- def scopes
66
- scopes = scope ? scope.split(/\s+/) : []
67
- scopes = attributes[:scope]
68
- Set.new(scopes).to_s
69
- end
70
-
71
- def in_scope?(request_scope)
72
- [*request_scope].all?(&scopes.method(:include?))
73
- end
74
-
75
- def expired?
76
- return false unless expires_at
77
- expires_at < Time.now
78
- end
79
-
80
- def generate_access_token
81
- self.access_token ||= self.create_access_token
82
- save && access_token
83
- end
84
-
85
- def generate_code
86
- self.code ||= self.create_code(client)
87
- save && code
88
- end
89
-
90
- def self.error_response(error)
91
- error_response = {
92
- :error => "Unauthorized access",
93
- :description => error,
94
- :status => 401
95
- }
96
- end
97
-
98
- def build_url(redirect_uri)
99
- if redirect_uri.include? "#access_token"
100
- redirect_url = redirect_uri.gsub!('#','?')
101
- elsif redirect_uri.include? "#"
102
- redirect_url = redirect_uri.gsub!('#','?')
103
- return redirect_uri + "access_token=#{self.access_token}"
104
- elsif redirect_uri.include? "access_token"
105
- return redirect_uri + "?access_token=#{self.access_token}"
106
- end
107
- end
108
-
109
- def redirect(auth)
110
- return auth.redirect_uri.split('#',2).first
111
- end
112
-
113
- end
@@ -1,100 +0,0 @@
1
- class Oauth2Client < ActiveRecord::Base
2
- has_many :oauth2_authorizations
3
- attr_accessible :name, :client_id, :client_secret_hash, :redirect_uri
4
- validates_presence_of :name, :client_id, :client_secret_hash, :redirect_uri
5
- validates_uniqueness_of :client_id
6
-
7
- before_validation :generate_keys, :on => :create
8
-
9
- def self.register(params)
10
- if @client = Oauth2Client.find_by_name(params.name)
11
- error = "Client already exists."
12
- error_message = Oauth2Authorization.error_response(error)
13
- return error_message, false
14
- else
15
- @oauth2_client = Oauth2Client.create!(params)
16
- string = "#{@oauth2_client.client_id}:#{@oauth2_client.client_secret_hash}"
17
- @oauth2_client.update_attribute(:basic_code, Base64.encode64(string))
18
- redirect_url = @oauth2_client.redirect_to_url
19
- return redirect_url, true
20
- end
21
- end
22
-
23
- def self.valid_authorization?(params)
24
- authorization_decoded = Base64.decode64(params.authorization)
25
- @client = Oauth2Client.find_by_client_id(params.client_id)
26
- if @client
27
- return authorization_decoded.eql?("#{@client.client_id}:#{@client.client_secret_hash}")? true : false
28
- end
29
- end
30
-
31
- def self.grant_access(params,env,request_type)
32
- if request_type == "user"
33
- @owner = Owner.find_by_username(params.username)
34
- @owner = Owner.create(:username => params.username) if @owner.nil?
35
- else
36
- @owner = Owner.find_by_username(params.host_name+"_bearer")
37
- @owner = Owner.create(:username => params.host_name+"_bearer") if @owner.nil?
38
- end
39
-
40
- @oauth2 = Songkick::OAuth2::Provider.parse(@owner, env)
41
- if @oauth2.valid?
42
- @auth = Songkick::OAuth2::Provider::Authorization.new(@owner, params)
43
- @authenticated_owner = Oauth2Authorization.find_by_oauth2_resource_owner_id_and_oauth2_client_id(@owner.id,@auth.client.id)
44
- unless @authenticated_owner
45
- @oauth2_authorization_instance = Oauth2Authorization.new()
46
- @instance = @oauth2_authorization_instance.get_token(@auth.owner, @auth.client,
47
- :response_type => "token",
48
- :scope => params["scope"].present? ? params["scope"] : nil,
49
- :duration => params["duration"].present? ? params["duration"] : 3600)
50
- else
51
- @instance = @authenticated_owner
52
- end
53
- if @instance.access_token.nil?
54
- error_message = Oauth2Authorization.error_response(@oauth2.error_description)
55
- return error_message, false
56
- else
57
- redirect_to_url = @instance.build_url(@auth.redirect_uri)
58
- @instance.refresh_access_token if @instance.expired?
59
- return redirect_to_url, true
60
- end
61
- else
62
- error_message = Oauth2Authorization.error_response(@oauth2.error_description)
63
- return error_message, false
64
- end
65
- end
66
-
67
- def self.invalidate_token(params,env)
68
- @owner = Owner.find_by_username(params.host_name+"_bearer")
69
- if @owner.nil?
70
- error = "No Bearer token issued to this client."
71
- error_message = Oauth2Authorization.error_response(error)
72
- return error_message, false
73
- else
74
- @oauth2 = Songkick::OAuth2::Provider.parse(@owner, env)
75
- if @oauth2.valid?
76
- @auth = Songkick::OAuth2::Provider::Authorization.new(@owner, params)
77
- @oauth2_authorization_instance = Oauth2Authorization.new()
78
- @instance = @oauth2_authorization_instance.get_token(@auth.owner,@auth.client,
79
- :response_type => "token",
80
- :invalidate => true)
81
- return @instance.redirect(@auth), true
82
- else
83
- error_message = Oauth2Authorization.error_response(@oauth2.error_description)
84
- return error_message, false
85
- end
86
- end
87
- end
88
-
89
- def redirect_to_url
90
- return self.redirect_uri + "?client_id=#{self.client_id}&client_secret_hash=#{self.client_secret_hash}
91
- &redirect_uri=#{self.redirect_uri}&authorization=#{self.basic_code}"
92
- end
93
-
94
- protected
95
-
96
- def generate_keys
97
- self.client_id = OAuth::Helper.generate_key(40)[0,40]
98
- self.client_secret_hash = OAuth::Helper.generate_key(40)[0,40]
99
- end
100
- end