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,36 @@
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
+ class CreateSessions < ActiveRecord::Migration
29
+ def change
30
+ create_table(:sessions) do |t|
31
+ t.string :user_id
32
+ t.string :session_token
33
+ end
34
+ add_index :sessions, :session_token, :unique => true
35
+ end
36
+ end
@@ -0,0 +1,40 @@
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
+ class CreateOwners < ActiveRecord::Migration
29
+ def self.up
30
+ create_table :owners do |t|
31
+ t.timestamps
32
+ t.string :username
33
+ end
34
+ add_index :owners, [:username]
35
+ end
36
+
37
+ def self.down
38
+ drop_table :owners
39
+ end
40
+ end
@@ -0,0 +1,50 @@
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
+ class CreateOauth2Authorizations < ActiveRecord::Migration
29
+ def self.up
30
+ create_table :oauth2_authorizations do |t|
31
+ t.timestamps
32
+ t.string :oauth2_resource_owner_type
33
+ t.integer :oauth2_resource_owner_id
34
+ t.belongs_to :oauth2_client
35
+ t.string :scope
36
+ t.string :code, :limit => 40
37
+ t.string :access_token, :limit => 40
38
+ t.string :refresh_token, :limit => 40
39
+ t.datetime :expires_at
40
+ end
41
+ add_index :oauth2_authorizations, [:oauth2_client_id, :code]
42
+ add_index :oauth2_authorizations, [:access_token]
43
+ add_index :oauth2_authorizations, [:oauth2_client_id, :access_token], :name => 'access_token_index'
44
+ add_index :oauth2_authorizations, [:oauth2_client_id, :refresh_token], :name => 'refresh_token_index'
45
+ end
46
+
47
+ def self.down
48
+ drop_table :oauth2_authorizations
49
+ end
50
+ end
@@ -0,0 +1,45 @@
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
+ class CreateOauth2Clients < ActiveRecord::Migration
29
+ def self.up
30
+ create_table :oauth2_clients do |t|
31
+ t.string :name
32
+ t.string :client_id
33
+ t.string :client_secret_hash
34
+ t.string :redirect_uri
35
+ t.string :basic_code
36
+ t.timestamps
37
+ end
38
+ add_index :oauth2_clients, :client_id, :unique => true
39
+ end
40
+
41
+ def self.down
42
+ drop_table :oauth2_clients
43
+ end
44
+
45
+ end
@@ -0,0 +1,203 @@
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
+ class Oauth2Authorization < ActiveRecord::Base
29
+ belongs_to :client, :class_name => 'Oauth2Client'
30
+ =begin
31
+ Process each oauth api requests for required results.
32
+ =end
33
+ def self.api_call(params,env,endpoint)
34
+ if User.validate_params?(params,endpoint)
35
+ case endpoint
36
+ when "register"
37
+ expected_response,response_message = Oauth2Client.register(params)
38
+ when "request_token"
39
+ expected_response,response_message = Oauth2Client.process_request(params,env,"code")
40
+ when "authorize"
41
+ expected_response,response_message = Oauth2Client.process_request(params,env,"authorize")
42
+ when "access_token"
43
+ expected_response,response_message = Oauth2Client.process_request(params,env,"token")
44
+ when "token"
45
+ expected_response,response_message = Owner.process_bearer_request(params,env,"bearer_token")
46
+ when "invalidate_token"
47
+ expected_response,response_message = Owner.process_bearer_request(params,env,"invalidate")
48
+ end
49
+ return expected_response,response_message
50
+ else
51
+ error = "Parameters missing or invalid."
52
+ error_response = Oauth2Authorization.error_response(error)
53
+ return error_response,false
54
+ end
55
+ end
56
+ =begin
57
+ Creates and returns the basic oauth details.
58
+ =end
59
+ def get_token(owner,client, attributes = {})
60
+ return nil unless owner and client
61
+ @instance = owner.oauth2_authorization(client,owner) ||
62
+ Oauth2Authorization.new do |authorization|
63
+ authorization.oauth2_resource_owner_id = owner.id
64
+ authorization.oauth2_client_id = client.id
65
+ end
66
+ case attributes[:response_type]
67
+ when 'code'
68
+ @instance.code ||= create_code(client)
69
+ when 'token'
70
+ @instance.access_token ||= create_access_token
71
+ @instance.refresh_token ||= create_refresh_token(client)
72
+ end
73
+
74
+ if @instance.expires_at.nil?
75
+ @instance.expires_at = attributes[:duration].present? ? Time.now + attributes[:duration].to_i : nil
76
+ elsif attributes[:invalidate]
77
+ @instance.expires_at = Time.now
78
+ end
79
+
80
+ if @instance.scope.nil?
81
+ @instance.scope = attributes[:scope].present? ? attributes[:scope] : nil
82
+ elsif attributes[:scope].present?
83
+ @instance.scope += "," + attributes[:scope] unless @instance.scope.include? attributes[:scope]
84
+ end
85
+
86
+ @instance.save
87
+ return @instance
88
+
89
+ rescue Object => error
90
+ raise error
91
+ end
92
+ =begin
93
+ Creates and returns the basic api response.
94
+ =end
95
+ def api_response(redirect_uri)
96
+ redirect_to_url = self.build_url(redirect_uri,"token")
97
+ self.refresh_access_token if self.expired?
98
+ return redirect_to_url
99
+ end
100
+ =begin
101
+ Refreshes the expired access token.
102
+ =end
103
+ def refresh_access_token
104
+ self.expires_at = Time.now + 3600
105
+ save
106
+ end
107
+ =begin
108
+ Creates and returns the request token code.
109
+ =end
110
+ def create_code(client)
111
+ Songkick::OAuth2.generate_id do |code|
112
+ return code
113
+ end
114
+ end
115
+ =begin
116
+ Creates and returns the access token.
117
+ =end
118
+ def create_access_token
119
+ hash = nil
120
+ Songkick::OAuth2.generate_id do |token|
121
+ hash = Songkick::OAuth2.hashify(token)
122
+ end
123
+ return hash
124
+ end
125
+ =begin
126
+ Creates and returns the request token hash.
127
+ =end
128
+ def create_refresh_token(client)
129
+ verified_client = Oauth2Client.find_by_client_id(client.client_id)
130
+ Songkick::OAuth2.generate_id do |refresh_token|
131
+ if verified_client
132
+ hash = Songkick::OAuth2.hashify(refresh_token)
133
+ else
134
+ hash = nil
135
+ end
136
+ end
137
+ return hash
138
+ end
139
+ =begin
140
+ Handles the scope attribute.
141
+ =end
142
+ def scopes
143
+ scopes = scope ? scope.split(/\s+/) : []
144
+ scopes = attributes[:scope]
145
+ Set.new(scopes).to_s
146
+ end
147
+ =begin
148
+ Checks the presence of scope attribute value.
149
+ =end
150
+ def in_scope?(request_scope)
151
+ [*request_scope].all?(&scopes.method(:include?))
152
+ end
153
+ =begin
154
+ Checks the expiry of access token.
155
+ =end
156
+ def expired?
157
+ return false unless expires_at
158
+ expires_at < Time.now
159
+ end
160
+ =begin
161
+ Creates and returns the access token hash.
162
+ =end
163
+ def generate_access_token
164
+ self.access_token ||= self.create_access_token
165
+ save && access_token
166
+ end
167
+ =begin
168
+ Creates and generates the request token code.
169
+ =end
170
+ def generate_code
171
+ self.code ||= self.create_code(client)
172
+ save && code
173
+ end
174
+ =begin
175
+ Creates and returns the error response.
176
+ =end
177
+ def self.error_response(error)
178
+ error_response = {
179
+ :error => "Unauthorized access",
180
+ :description => error,
181
+ :status => 401
182
+ }
183
+ end
184
+ =begin
185
+ Creates and returns the redirect url.
186
+ =end
187
+ def build_url(redirect_uri,type)
188
+ path = redirect_uri.split('#',2).first if redirect_uri.include? "#"
189
+ path = redirect_uri.split('?',2).first if redirect_uri.include? "?"
190
+ case type
191
+ when "token"
192
+ return path + "?access_token=#{self.access_token}"
193
+ when "code"
194
+ return path + "?request_token=#{self.code}"
195
+ end
196
+ end
197
+ =begin
198
+ Creates and returns the redirect url basic path.
199
+ =end
200
+ def redirect(auth)
201
+ return auth.redirect_uri.split('#',2).first
202
+ end
203
+ end
@@ -0,0 +1,216 @@
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
+ class Oauth2Client < ActiveRecord::Base
29
+ has_many :oauth2_authorizations
30
+ attr_accessible :name, :client_id, :client_secret_hash, :redirect_uri
31
+ validates_presence_of :name, :client_id, :client_secret_hash, :redirect_uri
32
+ validates_uniqueness_of :client_id
33
+
34
+ before_validation :generate_keys, :on => :create
35
+ =begin
36
+ Registers a new oauth client and returns the required details.
37
+ =end
38
+ def self.register(params)
39
+ if @client = Oauth2Client.find_by_name(params.name)
40
+ error = "Client already exists."
41
+ error_message = Oauth2Authorization.error_response(error)
42
+ return error_message, false
43
+ else
44
+ @oauth2_client = Oauth2Client.create!(:name => params.name, :redirect_uri => params.redirect_uri)
45
+ string = "#{@oauth2_client.client_id}:#{@oauth2_client.client_secret_hash}"
46
+ @oauth2_client.update_attribute(:basic_code, Base64.encode64(string))
47
+ redirect_url = @oauth2_client.redirect_to_url(params.callback_url)
48
+ return redirect_url, true
49
+ end
50
+ end
51
+ =begin
52
+ Checks for valid authorization basic code.
53
+ =end
54
+ def self.valid_authorization?(params)
55
+ authorization_decoded = Base64.decode64(params.authorization)
56
+ @client = Oauth2Client.find_by_client_id(params.client_id)
57
+ if @client
58
+ unless authorization_decoded.eql?("#{@client.client_id}:#{@client.client_secret_hash}")
59
+ return false, "present"
60
+ else
61
+ return true, "present"
62
+ end
63
+ else
64
+ return false, "absent"
65
+ end
66
+ end
67
+
68
+ =begin
69
+ Processes the /access_token /authorize and /request_token endpoint and returns the required oauth details.
70
+ =end
71
+ def self.process_request(params,env,action)
72
+ if User.logged_in?(params)
73
+ case action
74
+ when "code"
75
+ expected_response,response_message = Oauth2Client.grant_code(params,env)
76
+ when "token"
77
+ expected_response,response_message = Oauth2Client.grant_access(params,env,"user")
78
+ when "authorize"
79
+ @oauth2 = Songkick::OAuth2::Provider.parse(@owner, env)
80
+ expected_response = "Redirection url to authorization page"
81
+ response_message = true
82
+ end
83
+ return expected_response,response_message
84
+ else
85
+ error = "Invalid user session."
86
+ error_response = Oauth2Authorization.error_response(error)
87
+ return error_response,false
88
+ end
89
+ end
90
+ =begin
91
+ Creates and returns request token for the client.
92
+ =end
93
+ def self.grant_code(params,env)
94
+ @owner = Owner.find_by_username(params.username)
95
+ @owner = Owner.create(:username => params.username) if @owner.nil?
96
+
97
+ @oauth2 = Songkick::OAuth2::Provider.parse(@owner, env)
98
+ if @oauth2.valid?
99
+ @auth = Songkick::OAuth2::Provider::Authorization.new(@owner, params)
100
+ @authenticated_owner = Oauth2Authorization.find_by_oauth2_resource_owner_id_and_oauth2_client_id(@owner.id,@auth.client.id)
101
+ unless @authenticated_owner
102
+ @instance = Oauth2Client.obtain_token(params, @auth,"code")
103
+ else
104
+ @instance = @authenticated_owner
105
+ end
106
+ if @instance.code.nil?
107
+ error_message = Oauth2Authorization.error_response(@oauth2.error_description)
108
+ return error_message, false
109
+ else
110
+ redirect_to_url = @instance.build_url(@auth.redirect_uri,"code")
111
+ @instance.refresh_access_token if @instance.expired?
112
+ return redirect_to_url, true
113
+ end
114
+ else
115
+ error_message = Oauth2Authorization.error_response(@oauth2.error_description)
116
+ return error_message, false
117
+ end
118
+ end
119
+ =begin
120
+ Creates and returns access token for the client.
121
+ =end
122
+ def self.grant_access(params,env,request_type)
123
+ if request_type == "user"
124
+ @owner = Owner.find_by_username(params.username)
125
+ @owner = Owner.create(:username => params.username) if @owner.nil?
126
+ else
127
+ @owner = Owner.find_by_username(params.host_name+"_bearer")
128
+ @owner = Owner.create(:username => params.host_name+"_bearer") if @owner.nil?
129
+ end
130
+
131
+ @oauth2 = Songkick::OAuth2::Provider.parse(@owner, env)
132
+ if @oauth2.valid?
133
+ @auth = Songkick::OAuth2::Provider::Authorization.new(@owner, params)
134
+ @authenticated_owner = Oauth2Authorization.find_by_oauth2_resource_owner_id_and_oauth2_client_id(@owner.id,@auth.client.id)
135
+ unless @authenticated_owner
136
+ @instance = Oauth2Client.obtain_token(params, @auth,"token")
137
+ else
138
+ @instance = @authenticated_owner
139
+ end
140
+ if @instance.access_token.nil?
141
+ error_message = Oauth2Authorization.error_response(@oauth2.error_description)
142
+ return error_message, false
143
+ elsif @instance.code.nil? && request_type == "user"
144
+ error = "Invalid request. Request token generation required."
145
+ error_message = Oauth2Authorization.error_response(error)
146
+ return error_message, false
147
+ else
148
+ if request_type == "bearer"
149
+ redirect_to_url = @instance.api_response(@auth.redirect_uri)
150
+ return redirect_to_url, true
151
+ elsif @instance.code == params.request_token && request_type == "user"
152
+ redirect_to_url = @instance.api_response(@auth.redirect_uri)
153
+ return redirect_to_url, true
154
+ else
155
+ error = "Invalid request token."
156
+ error_message = Oauth2Authorization.error_response(error)
157
+ return error_message, false
158
+ end
159
+ end
160
+ else
161
+ error_message = Oauth2Authorization.error_response(@oauth2.error_description)
162
+ return error_message, false
163
+ end
164
+ end
165
+ =begin
166
+ Invalidates the bearer token for the specifies client.
167
+ =end
168
+ def self.invalidate_token(params,env)
169
+ @owner = Owner.find_by_username(params.host_name+"_bearer")
170
+ if @owner.nil?
171
+ error = "No Bearer token issued to this client."
172
+ error_message = Oauth2Authorization.error_response(error)
173
+ return error_message, false
174
+ else
175
+ @oauth2 = Songkick::OAuth2::Provider.parse(@owner, env)
176
+ if @oauth2.valid?
177
+ @auth = Songkick::OAuth2::Provider::Authorization.new(@owner, params)
178
+ @oauth2_authorization_instance = Oauth2Authorization.new()
179
+ @instance = @oauth2_authorization_instance.get_token(@auth.owner,@auth.client,
180
+ :response_type => "token",
181
+ :invalidate => true)
182
+ return @instance.redirect(@auth), true
183
+ else
184
+ error_message = Oauth2Authorization.error_response(@oauth2.error_description)
185
+ return error_message, false
186
+ end
187
+ end
188
+ end
189
+ =begin
190
+ Builds the redirect url for oauth.
191
+ =end
192
+ def redirect_to_url(callback_url)
193
+ client_details = "client_id=#{self.client_id}"
194
+ return callback_url + "?#{client_details}"
195
+ end
196
+
197
+ protected
198
+ =begin
199
+ Creates and returns oauth client secret key hash and id.
200
+ =end
201
+ def generate_keys
202
+ self.client_id = OAuth::Helper.generate_key(40)[0,40]
203
+ self.client_secret_hash = OAuth::Helper.generate_key(40)[0,40]
204
+ end
205
+ =begin
206
+ Creates and returns the basic oauth details.
207
+ =end
208
+ def self.obtain_token(params, auth,action)
209
+ @oauth2_authorization_instance = Oauth2Authorization.new()
210
+ @instance = @oauth2_authorization_instance.get_token(auth.owner, auth.client,
211
+ :response_type => action,
212
+ :scope => params["scope"].present? ? params["scope"] : nil,
213
+ :duration => params["duration"].present? ? params["duration"] : 3600)
214
+ return @instance
215
+ end
216
+ end
@@ -0,0 +1,65 @@
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
+ class Owner < ActiveRecord::Base
29
+ has_many :oauth2_authorizations
30
+ =begin
31
+ Returns oauth details for specified client id.
32
+ =end
33
+ def oauth2_authorization_for(client)
34
+ Oauth2Authorization.find_by_oauth2_client_id(client.id)
35
+ end
36
+ =begin
37
+ Returns oauth details for specified client id and owner id.
38
+ =end
39
+ def oauth2_authorization(client,owner)
40
+ Oauth2Authorization.find_by_oauth2_client_id_and_oauth2_resource_owner_id(client.id,owner.id)
41
+ end
42
+ =begin
43
+ Processes the /token and /invalidate_token endpoint and returns the required doauth details.
44
+ =end
45
+ def self.process_bearer_request(params,env,action)
46
+ flag, void_value = Oauth2Client.valid_authorization?(params)
47
+ if flag && void_value == "present"
48
+ case action
49
+ when "bearer_token"
50
+ expected_response,response_message = Oauth2Client.grant_access(params,env,"bearer")
51
+ when "invalidate"
52
+ expected_response,response_message = Oauth2Client.invalidate_token(params,env)
53
+ end
54
+ return expected_response,response_message
55
+ elsif void_value == "present"
56
+ error = "Invalid authorization code"
57
+ error_response = Oauth2Authorization.error_response(error)
58
+ return error_response,false
59
+ else
60
+ error = "Invalid client id"
61
+ error_response = Oauth2Authorization.error_response(error)
62
+ return error_response,false
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,30 @@
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
+ class Session < ActiveRecord::Base
29
+ has_many :users
30
+ end