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
@@ -1,138 +0,0 @@
1
- require 'oauth2'
2
- require 'songkick/oauth2/provider'
3
- require 'oauth'
4
- require 'ruby_regex'
5
- module Rammer
6
-
7
- class OauthApis < Grape::API
8
- Songkick::OAuth2::Provider.realm = 'PocketAPI Oauth Server'
9
- version 'v1', :using => :path
10
- format :json
11
-
12
- =begin
13
- This handles api calls for request token generation with the request parameters:
14
- {"name"=> Client's name,
15
- "redirect_uri" => URL to which the oauth should be redirected
16
- }
17
- =end
18
- [:get, :post].each do |method|
19
- __send__ method, '/oauth/register_client' do
20
- if User.validate_params?(params,"register")
21
- expected_response,response_message = Oauth2Client.register(params)
22
- if response_message then redirect expected_response else expected_response end
23
- else
24
- error = "Parameters missing or invalid."
25
- Oauth2Authorization.error_response(error)
26
- end
27
- end
28
- end
29
- =begin
30
- This handles api calls for request token generation with the request parameters:
31
- {"client_id"=> Client's registered ID,
32
- "username" => Authorized user's session id,
33
- "redirect_uri" => URL to which the oauth should be redirected,
34
- "response_type" => "code" (Keyword to return request token)
35
- }
36
- =end
37
- [:get, :post].each do |method|
38
- __send__ method, '/oauth/authorize' do
39
- =begin
40
- Specify redirection url to the respective authorization page into 'redirect_to_url'
41
- and uncomment the following code to enable functionality.
42
-
43
- if User.validate_params?(params,"authorize")
44
- if User.logged_in?(params)
45
- @oauth2 = Songkick::OAuth2::Provider.parse(@owner, env)
46
- redirect_to_url = "Redirection url to authorization page"
47
- redirect redirect_to_url
48
- else
49
- error = "Sign in first."
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
- =begin
60
- This handles api calls for access token generation with the request parameters:
61
- {"client_id"=> Client's registered ID,
62
- "username" => Authorized user's session id,
63
- "redirect_uri" => URL to which the oauth should be redirected,
64
- "response_type" => "token" (Keyword to return access token)
65
- }
66
- =end
67
- [:get, :post].each do |method|
68
- __send__ method, '/oauth/access_token' do
69
- if User.validate_params?(params,"access_token")
70
- if User.logged_in?(params)
71
- expected_response,response_message = Oauth2Client.grant_access(params,env,"user")
72
- if response_message then redirect expected_response else expected_response end
73
- else
74
- error = "Invalid user session."
75
- Oauth2Authorization.error_response(error)
76
- end
77
- else
78
- error = "Parameters missing or invalid."
79
- Oauth2Authorization.error_response(error)
80
- end
81
- end
82
- end
83
- =begin
84
- This handles api calls for bearer token generation with the request parameters:
85
- {"client_id"=> Client's registered ID,
86
- "authorization" => Basic authorization key generated while client registration,
87
- "host_name" => Thirs party client's name,
88
- "redirect_uri" => URL to which the oauth should be redirected,
89
- "response_type" => "token" (Keyword to return bearer token),
90
- }
91
- Optional parameters:
92
- {"scope" => Indicates the API's the application is requesting,
93
- "duration" => Lifetime of bearer token
94
- }
95
- =end
96
- [:get, :post].each do |method|
97
- __send__ method, '/oauth/token' do
98
- if User.validate_params?(params,"token")
99
- if Oauth2Client.valid_authorization?(params)
100
- expected_response,response_message = Oauth2Client.grant_access(params,env,"bearer")
101
- if response_message then redirect expected_response else expected_response end
102
- else
103
- error = "Invalid authorization code"
104
- Oauth2Authorization.error_response(error)
105
- end
106
- else
107
- error = "Parameters missing or invalid."
108
- Oauth2Authorization.error_response(error)
109
- end
110
- end
111
- end
112
- =begin
113
- This handles api calls for access token generation with the request parameters:
114
- {"client_id"=> Client's registered ID,
115
- "authorization" => Basic authorization key generated while client registration,
116
- "host_name" => Thirs party client's name,
117
- "redirect_uri" => URL to which the oauth should be redirected,
118
- "response_type" => "token" (Keyword for invalidation of bearer token only),
119
- }
120
- =end
121
- [:get, :post].each do |method|
122
- __send__ method, '/oauth/invalidate_token' do
123
- if User.validate_params?(params,"token")
124
- if Oauth2Client.valid_authorization?(params)
125
- expected_response,response_message = Oauth2Client.invalidate_token(params,env)
126
- if response_message then redirect expected_response else expected_response end
127
- else
128
- error = "Invalid authorization code"
129
- Oauth2Authorization.error_response(error)
130
- end
131
- else
132
- error = "Parameters missing or invalid."
133
- Oauth2Authorization.error_response(error)
134
- end
135
- end
136
- end
137
- end
138
- end
@@ -1,10 +0,0 @@
1
- class Owner < ActiveRecord::Base
2
- has_many :oauth2_authorizations
3
- def oauth2_authorization_for(client)
4
- Oauth2Authorization.find_by_oauth2_client_id(client.id)
5
- end
6
-
7
- def oauth2_authorization(client,owner)
8
- Oauth2Authorization.find_by_oauth2_client_id_and_oauth2_resource_owner_id(client.id,owner.id)
9
- end
10
- end
@@ -1,11 +0,0 @@
1
- require "rubygems"
2
- require "bundler/setup"
3
- require 'goliath'
4
- require 'em-synchrony/activerecord'
5
- require 'grape'
6
- require './tree'
7
-
8
- class Application < Goliath::API
9
- def response(env)
10
- end
11
- end
@@ -1,3 +0,0 @@
1
- class Session < ActiveRecord::Base
2
- has_many :users
3
- end
data/lib/template/tree.rb DELETED
@@ -1,3 +0,0 @@
1
- Dir[File.join("./app", "**/*.rb")].each do |file|
2
- require file
3
- end
data/lib/template/user.rb DELETED
@@ -1,78 +0,0 @@
1
- class User < ActiveRecord::Base
2
-
3
- def self.validate_params?(params,base_api)
4
- case(base_api)
5
- when "register"
6
- if params.name && params.redirect_uri
7
- return true if User.valid_redirect_uri?(params.redirect_uri)
8
- end
9
- when "sign_up", "sign_in"
10
- if params.email && params.password && params.redirect_uri
11
- return true if User.valid_email?(params.email) && User.valid_password?(params.password) && User.valid_redirect_uri?(params.redirect_uri)
12
- end
13
- when "sign_out"
14
- if params.email && params.session_token && params.redirect_uri
15
- return true if User.valid_email?(params.email) && User.valid_redirect_uri?(params.redirect_uri)
16
- end
17
- when "authorize", "access_token"
18
- if params.username && params.redirect_uri
19
- return true if User.validate_oauth_params(params)
20
- end
21
- when "token", "invalidate_token"
22
- if params.host_name && params.authorization && params.redirect_uri
23
- return true if User.validate_oauth_params(params)
24
- end
25
- else
26
- return false
27
- end
28
- end
29
-
30
- def self.validate_oauth_params(params)
31
- if params.client_id && params.response_type && User.valid_redirect_uri?(params.redirect_uri)
32
- return true if params.response_type == "token"
33
- end
34
- end
35
-
36
- def self.sign_up(params)
37
- @user = User.create!(:email => params.email, :encrypted_password => params.password)
38
- @session = @user.sign_in(params)
39
- return @user, @session
40
- end
41
-
42
- def self.valid_email?(email)
43
- return true if email =~ RubyRegex::Email
44
- end
45
-
46
- def self.valid_password?(password)
47
- return true if password =~ /^[0-9a-f]{32}$/i
48
- end
49
-
50
- def self.valid_redirect_uri?(redirect_uri)
51
- return true if !redirect_uri.empty? && redirect_uri =~ RubyRegex::Url
52
- end
53
-
54
- def sign_in(params)
55
- token = Digest::SHA1.hexdigest("#{SecureRandom.base64}" + "#{self.id}")
56
- @session = Session.create!(:user_id => self.id, :session_token => token)
57
- self.update_attribute(:sign_in_count, self.sign_in_count+1)
58
- return @session
59
- end
60
-
61
- def sign_out(params)
62
- @session = Session.find_by_session_token_and_user_id(params.session_token,self.id)
63
- @session.destroy
64
- end
65
-
66
- def signed_in?(params)
67
- Session.find_by_user_id_and_session_token(self.id,params.session_token)
68
- end
69
-
70
- def self.logged_in?(params)
71
- Session.find_by_session_token(params.username)
72
- end
73
-
74
- def redirect_url(params,session)
75
- redirect_to_url = params.redirect_uri + "?session_token=#{session.session_token}"
76
- end
77
-
78
- end