sorcery 0.8.5 → 0.8.6

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sorcery might be problematic. Click here for more details.

Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +60 -4
  4. data/CHANGELOG.md +15 -1
  5. data/Gemfile +9 -18
  6. data/Gemfile.rails4 +8 -10
  7. data/README.md +31 -11
  8. data/VERSION +1 -1
  9. data/gemfiles/active_record-rails41.gemfile +6 -0
  10. data/gemfiles/mongo_mapper-rails41.gemfile +8 -0
  11. data/gemfiles/mongoid-rails41.gemfile +11 -0
  12. data/lib/sorcery.rb +20 -28
  13. data/lib/sorcery/controller.rb +6 -11
  14. data/lib/sorcery/controller/submodules/external.rb +30 -15
  15. data/lib/sorcery/controller/submodules/session_timeout.rb +1 -1
  16. data/lib/sorcery/model.rb +102 -70
  17. data/lib/sorcery/model/adapters/active_record.rb +7 -2
  18. data/lib/sorcery/model/adapters/datamapper.rb +123 -0
  19. data/lib/sorcery/model/adapters/mongo_mapper.rb +8 -4
  20. data/lib/sorcery/model/adapters/mongoid.rb +6 -6
  21. data/lib/sorcery/model/submodules/activity_logging.rb +24 -0
  22. data/lib/sorcery/model/submodules/brute_force_protection.rb +16 -0
  23. data/lib/sorcery/model/submodules/remember_me.rb +19 -4
  24. data/lib/sorcery/model/submodules/reset_password.rb +30 -13
  25. data/lib/sorcery/model/submodules/user_activation.rb +53 -22
  26. data/lib/sorcery/{controller/submodules/external/protocols → protocols}/certs/ca-bundle.crt +0 -0
  27. data/lib/sorcery/protocols/oauth.rb +42 -0
  28. data/lib/sorcery/protocols/oauth2.rb +47 -0
  29. data/lib/sorcery/providers/base.rb +27 -0
  30. data/lib/sorcery/providers/facebook.rb +63 -0
  31. data/lib/sorcery/providers/github.rb +51 -0
  32. data/lib/sorcery/providers/google.rb +51 -0
  33. data/lib/sorcery/providers/linkedin.rb +66 -0
  34. data/lib/sorcery/providers/liveid.rb +53 -0
  35. data/lib/sorcery/providers/twitter.rb +59 -0
  36. data/lib/sorcery/providers/vk.rb +61 -0
  37. data/lib/sorcery/providers/xing.rb +64 -0
  38. data/lib/sorcery/test_helpers/internal.rb +3 -3
  39. data/lib/sorcery/test_helpers/internal/rails.rb +14 -3
  40. data/lib/sorcery/test_helpers/rails.rb +1 -10
  41. data/lib/sorcery/test_helpers/rails/controller.rb +17 -0
  42. data/lib/sorcery/test_helpers/rails/integration.rb +26 -0
  43. data/sorcery.gemspec +14 -18
  44. data/spec/active_record/controller_activity_logging_spec.rb +5 -116
  45. data/spec/active_record/controller_brute_force_protection_spec.rb +69 -47
  46. data/spec/active_record/controller_http_basic_auth_spec.rb +24 -18
  47. data/spec/active_record/controller_oauth2_spec.rb +112 -187
  48. data/spec/active_record/controller_oauth_spec.rb +41 -37
  49. data/spec/active_record/controller_remember_me_spec.rb +39 -38
  50. data/spec/active_record/controller_session_timeout_spec.rb +31 -16
  51. data/spec/active_record/controller_spec.rb +4 -178
  52. data/spec/active_record/integration_spec.rb +1 -1
  53. data/spec/active_record/user_activation_spec.rb +1 -1
  54. data/spec/active_record/user_activity_logging_spec.rb +1 -1
  55. data/spec/active_record/user_brute_force_protection_spec.rb +1 -1
  56. data/spec/active_record/user_oauth_spec.rb +1 -1
  57. data/spec/active_record/user_remember_me_spec.rb +1 -1
  58. data/spec/active_record/user_reset_password_spec.rb +1 -1
  59. data/spec/active_record/user_spec.rb +7 -8
  60. data/spec/datamapper/controller_activity_logging_spec.rb +17 -0
  61. data/spec/datamapper/controller_spec.rb +8 -0
  62. data/spec/datamapper/user_activation_spec.rb +10 -0
  63. data/spec/datamapper/user_activity_logging_spec.rb +9 -0
  64. data/spec/datamapper/user_brute_force_protection_spec.rb +9 -0
  65. data/spec/datamapper/user_oauth_spec.rb +9 -0
  66. data/spec/datamapper/user_remember_me_spec.rb +8 -0
  67. data/spec/datamapper/user_reset_password_spec.rb +8 -0
  68. data/spec/datamapper/user_spec.rb +27 -0
  69. data/spec/mongo_mapper/controller_spec.rb +4 -171
  70. data/spec/mongo_mapper/user_activation_spec.rb +1 -2
  71. data/spec/mongo_mapper/user_activity_logging_spec.rb +1 -1
  72. data/spec/mongo_mapper/user_brute_force_protection_spec.rb +1 -1
  73. data/spec/mongo_mapper/user_oauth_spec.rb +1 -1
  74. data/spec/mongo_mapper/user_remember_me_spec.rb +1 -1
  75. data/spec/mongo_mapper/user_reset_password_spec.rb +1 -1
  76. data/spec/mongo_mapper/user_spec.rb +7 -8
  77. data/spec/mongoid/controller_activity_logging_spec.rb +4 -99
  78. data/spec/mongoid/controller_spec.rb +4 -182
  79. data/spec/mongoid/user_activation_spec.rb +1 -2
  80. data/spec/mongoid/user_activity_logging_spec.rb +1 -2
  81. data/spec/mongoid/user_brute_force_protection_spec.rb +1 -2
  82. data/spec/mongoid/user_oauth_spec.rb +1 -2
  83. data/spec/mongoid/user_remember_me_spec.rb +1 -2
  84. data/spec/mongoid/user_reset_password_spec.rb +1 -2
  85. data/spec/mongoid/user_spec.rb +8 -9
  86. data/spec/orm/active_record.rb +2 -0
  87. data/spec/orm/datamapper.rb +34 -0
  88. data/spec/orm/mongo_mapper.rb +1 -0
  89. data/spec/orm/mongoid.rb +1 -0
  90. data/spec/rails_app/app/controllers/sorcery_controller.rb +64 -59
  91. data/spec/rails_app/app/datamapper/authentication.rb +8 -0
  92. data/spec/rails_app/app/datamapper/user.rb +7 -0
  93. data/spec/rails_app/config/routes.rb +18 -13
  94. data/spec/shared_examples/controller_activity_logging_shared_examples.rb +125 -0
  95. data/spec/shared_examples/controller_oauth2_shared_examples.rb +32 -36
  96. data/spec/shared_examples/controller_oauth_shared_examples.rb +19 -26
  97. data/spec/shared_examples/controller_shared_examples.rb +203 -0
  98. data/spec/shared_examples/user_activation_shared_examples.rb +107 -90
  99. data/spec/shared_examples/user_activity_logging_shared_examples.rb +10 -10
  100. data/spec/shared_examples/user_brute_force_protection_shared_examples.rb +14 -13
  101. data/spec/shared_examples/user_oauth_shared_examples.rb +23 -15
  102. data/spec/shared_examples/user_remember_me_shared_examples.rb +32 -23
  103. data/spec/shared_examples/user_reset_password_shared_examples.rb +136 -115
  104. data/spec/shared_examples/user_shared_examples.rb +206 -146
  105. data/spec/sorcery_crypto_providers_spec.rb +28 -28
  106. data/spec/spec_helper.rb +15 -6
  107. metadata +83 -127
  108. data/lib/sorcery/controller/submodules/external/protocols/oauth1.rb +0 -46
  109. data/lib/sorcery/controller/submodules/external/protocols/oauth2.rb +0 -50
  110. data/lib/sorcery/controller/submodules/external/providers/base.rb +0 -21
  111. data/lib/sorcery/controller/submodules/external/providers/facebook.rb +0 -99
  112. data/lib/sorcery/controller/submodules/external/providers/github.rb +0 -93
  113. data/lib/sorcery/controller/submodules/external/providers/google.rb +0 -92
  114. data/lib/sorcery/controller/submodules/external/providers/linkedin.rb +0 -103
  115. data/lib/sorcery/controller/submodules/external/providers/liveid.rb +0 -93
  116. data/lib/sorcery/controller/submodules/external/providers/twitter.rb +0 -94
  117. data/lib/sorcery/controller/submodules/external/providers/vk.rb +0 -101
  118. data/lib/sorcery/controller/submodules/external/providers/xing.rb +0 -98
  119. data/lib/sorcery/test_helpers.rb +0 -5
@@ -0,0 +1,63 @@
1
+ module Sorcery
2
+ module Providers
3
+ # This class adds support for OAuth with facebook.com.
4
+ #
5
+ # config.facebook.key = <key>
6
+ # config.facebook.secret = <secret>
7
+ # ...
8
+ #
9
+ class Facebook < Base
10
+
11
+ include Protocols::Oauth2
12
+
13
+ attr_reader :mode, :param_name, :parse
14
+ attr_accessor :access_permissions, :display, :scope, :token_url,
15
+ :user_info_path
16
+
17
+ def initialize
18
+ super
19
+
20
+ @site = 'https://graph.facebook.com'
21
+ @user_info_path = '/me'
22
+ @scope = 'email,offline_access'
23
+ @display = 'page'
24
+ @token_url = 'oauth/access_token'
25
+ @mode = :query
26
+ @parse = :query
27
+ @param_name = 'access_token'
28
+ end
29
+
30
+ def get_user_hash(access_token)
31
+ response = access_token.get(user_info_path)
32
+
33
+ {}.tap do |h|
34
+ h[:user_info] = JSON.parse(response.body)
35
+ h[:uid] = h[:user_info]['id']
36
+ end
37
+ end
38
+
39
+ # calculates and returns the url to which the user should be redirected,
40
+ # to get authenticated at the external provider's site.
41
+ def login_url(params, session)
42
+ authorize_url
43
+ end
44
+
45
+ # overrides oauth2#authorize_url to allow customized scope.
46
+ def authorize_url
47
+ @scope = access_permissions.present? ? access_permissions.join(',') : scope
48
+ super
49
+ end
50
+
51
+ # tries to login the user from access token
52
+ def process_callback(params, session)
53
+ args = {}.tap do |a|
54
+ a[:code] = params[:code] if params[:code]
55
+ end
56
+
57
+ get_access_token(args, token_url: token_url, mode: mode,
58
+ param_name: param_name, parse: parse)
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,51 @@
1
+ module Sorcery
2
+ module Providers
3
+ # This class adds support for OAuth with github.com.
4
+ #
5
+ # config.github.key = <key>
6
+ # config.github.secret = <secret>
7
+ # ...
8
+ #
9
+ class Github < Base
10
+
11
+ include Protocols::Oauth2
12
+
13
+ attr_accessor :auth_path, :scope, :token_url, :user_info_path
14
+
15
+ def initialize
16
+ super
17
+
18
+ @scope = nil
19
+ @site = 'https://github.com/'
20
+ @user_info_path = 'https://api.github.com/user'
21
+ @auth_path = '/login/oauth/authorize'
22
+ @token_url = '/login/oauth/access_token'
23
+ end
24
+
25
+ def get_user_hash(access_token)
26
+ response = access_token.get(user_info_path)
27
+
28
+ {}.tap do |h|
29
+ h[:user_info] = JSON.parse(response.body)
30
+ h[:uid] = h[:user_info]['id']
31
+ end
32
+ end
33
+
34
+ # calculates and returns the url to which the user should be redirected,
35
+ # to get authenticated at the external provider's site.
36
+ def login_url(params, session)
37
+ authorize_url({ authorize_url: auth_path })
38
+ end
39
+
40
+ # tries to login the user from access token
41
+ def process_callback(params, session)
42
+ args = {}.tap do |a|
43
+ a[:code] = params[:code] if params[:code]
44
+ end
45
+
46
+ get_access_token(args, token_url: token_url, token_method: :post)
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,51 @@
1
+ module Sorcery
2
+ module Providers
3
+ # This class adds support for OAuth with google.com.
4
+ #
5
+ # config.google.key = <key>
6
+ # config.google.secret = <secret>
7
+ # ...
8
+ #
9
+ class Google < Base
10
+
11
+ include Protocols::Oauth2
12
+
13
+ attr_accessor :auth_url, :scope, :token_url, :user_info_url
14
+
15
+ def initialize
16
+ super
17
+
18
+ @site = 'https://accounts.google.com'
19
+ @auth_url = '/o/oauth2/auth'
20
+ @token_url = '/o/oauth2/token'
21
+ @user_info_url = 'https://www.googleapis.com/oauth2/v1/userinfo'
22
+ @scope = 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'
23
+ end
24
+
25
+ def get_user_hash(access_token)
26
+ response = access_token.get(user_info_url)
27
+
28
+ {}.tap do |h|
29
+ h[:user_info] = JSON.parse(response.body)
30
+ h[:uid] = h[:user_info]['id']
31
+ end
32
+ end
33
+
34
+ # calculates and returns the url to which the user should be redirected,
35
+ # to get authenticated at the external provider's site.
36
+ def login_url(params, session)
37
+ authorize_url({ authorize_url: auth_url })
38
+ end
39
+
40
+ # tries to login the user from access token
41
+ def process_callback(params, session)
42
+ args = {}.tap do |a|
43
+ a[:code] = params[:code] if params[:code]
44
+ end
45
+
46
+ get_access_token(args, token_url: token_url, token_method: :post)
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,66 @@
1
+ module Sorcery
2
+ module Providers
3
+ # This class adds support for OAuth with Linkedin.com.
4
+ #
5
+ # config.linkedin.key = <key>
6
+ # config.linkedin.secret = <secret>
7
+ # ...
8
+ #
9
+ class Linkedin < Base
10
+
11
+ include Protocols::Oauth
12
+
13
+ attr_accessor :authorize_path, :access_permissions, :access_token_path,
14
+ :request_token_path, :user_info_fields, :user_info_path
15
+
16
+ def initialize
17
+ @configuration = {
18
+ site: 'https://api.linkedin.com',
19
+ authorize_path: '/uas/oauth/authenticate',
20
+ request_token_path: '/uas/oauth/requestToken',
21
+ access_token_path: '/uas/oauth/accessToken'
22
+ }
23
+ @user_info_path = '/v1/people/~'
24
+ end
25
+
26
+ # Override included get_consumer method to provide authorize_path
27
+ def get_consumer
28
+ # Add access permissions to request token path
29
+ @configuration[:request_token_path] += '?scope=' + access_permissions.join('+') unless access_permissions.blank? or @configuration[:request_token_path].include? '?scope='
30
+ ::OAuth::Consumer.new(@key, @secret, @configuration)
31
+ end
32
+
33
+ def get_user_hash(access_token)
34
+ fields = self.user_info_fields.join(',')
35
+ response = access_token.get("#{@user_info_path}:(#{fields})", 'x-li-format' => 'json')
36
+
37
+ {}.tap do |h|
38
+ h[:user_info] = JSON.parse(response.body)
39
+ h[:uid] = h[:user_info]['id'].to_s
40
+ end
41
+ end
42
+
43
+ # calculates and returns the url to which the user should be redirected,
44
+ # to get authenticated at the external provider's site.
45
+ def login_url(params, session)
46
+ req_token = get_request_token
47
+ session[:request_token] = req_token.token
48
+ session[:request_token_secret] = req_token.secret
49
+ authorize_url({ request_token: req_token.token, request_token_secret: req_token.secret })
50
+ end
51
+
52
+ # tries to login the user from access token
53
+ def process_callback(params, session)
54
+ args = {
55
+ oauth_verifier: params[:oauth_verifier],
56
+ request_token: session[:request_token],
57
+ request_token_secret: session[:request_token_secret]
58
+ }
59
+
60
+ args.merge!({ code: params[:code] }) if params[:code]
61
+ get_access_token(args)
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,53 @@
1
+ module Sorcery
2
+ module Providers
3
+ # This class adds support for OAuth with microsoft liveid.
4
+ #
5
+ # config.liveid.key = <key>
6
+ # config.liveid.secret = <secret>
7
+ # ...
8
+ #
9
+ class Liveid < Base
10
+
11
+ include Protocols::Oauth2
12
+
13
+ attr_accessor :auth_url, :token_path, :user_info_url, :scope
14
+
15
+ def initialize
16
+ super
17
+
18
+ @site = 'https://oauth.live.com/'
19
+ @auth_url = '/authorize'
20
+ @token_path = '/token'
21
+ @user_info_url = 'https://apis.live.net/v5.0/me'
22
+ @scope = 'wl.basic wl.emails wl.offline_access'
23
+ end
24
+
25
+ def get_user_hash(access_token)
26
+ access_token.token_param = 'access_token'
27
+ response = access_token.get(user_info_url)
28
+
29
+ {}.tap do |h|
30
+ h[:user_info] = JSON.parse(response.body)
31
+ h[:uid] = h[:user_info]['id']
32
+ end
33
+ end
34
+
35
+ # calculates and returns the url to which the user should be redirected,
36
+ # to get authenticated at the external provider's site.
37
+ def login_url(params, session)
38
+ self.authorize_url({ authorize_url: auth_url })
39
+ end
40
+
41
+ # tries to login the user from access token
42
+ def process_callback(params, session)
43
+ args = {}.tap do |a|
44
+ a[:code] = params[:code] if params[:code]
45
+ end
46
+
47
+ get_access_token(args, access_token_path: token_path,
48
+ access_token_method: :post)
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,59 @@
1
+ module Sorcery
2
+ module Providers
3
+ # This class adds support for OAuth with Twitter.com.
4
+ #
5
+ # config.twitter.key = <key>
6
+ # config.twitter.secret = <secret>
7
+ # ...
8
+ #
9
+ class Twitter < Base
10
+
11
+ include Protocols::Oauth
12
+
13
+ attr_accessor :state, :user_info_path
14
+
15
+ def initialize
16
+ super
17
+
18
+ @site = 'https://api.twitter.com'
19
+ @user_info_path = '/1.1/account/verify_credentials.json'
20
+ end
21
+
22
+ # Override included get_consumer method to provide authorize_path
23
+ def get_consumer
24
+ ::OAuth::Consumer.new(@key, secret, site: site, authorize_path: '/oauth/authenticate')
25
+ end
26
+
27
+ def get_user_hash(access_token)
28
+ response = access_token.get(user_info_path)
29
+
30
+ {}.tap do |h|
31
+ h[:user_info] = JSON.parse(response.body)
32
+ h[:uid] = h[:user_info]['id'].to_s
33
+ end
34
+ end
35
+
36
+ # calculates and returns the url to which the user should be redirected,
37
+ # to get authenticated at the external provider's site.
38
+ def login_url(params, session)
39
+ req_token = self.get_request_token
40
+ session[:request_token] = req_token.token
41
+ session[:request_token_secret] = req_token.secret
42
+ self.authorize_url({ request_token: req_token.token, request_token_secret: req_token.secret })
43
+ end
44
+
45
+ # tries to login the user from access token
46
+ def process_callback(params, session)
47
+ args = {
48
+ oauth_verifier: params[:oauth_verifier],
49
+ request_token: session[:request_token],
50
+ request_token_secret: session[:request_token_secret]
51
+ }
52
+
53
+ args.merge!({ code: params[:code] }) if params[:code]
54
+ get_access_token(args)
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,61 @@
1
+ module Sorcery
2
+ module Providers
3
+ # This class adds support for OAuth with vk.com.
4
+ #
5
+ # config.vk.key = <key>
6
+ # config.vk.secret = <secret>
7
+ # ...
8
+ #
9
+ class Vk < Base
10
+
11
+ include Protocols::Oauth2
12
+
13
+ attr_accessor :auth_path, :token_path, :user_info_url, :scope
14
+
15
+ def initialize
16
+ super
17
+
18
+ @site = 'https://oauth.vk.com/'
19
+ @user_info_url = 'https://api.vk.com/method/getProfiles'
20
+ @auth_path = '/authorize'
21
+ @token_path = '/access_token'
22
+ @scope = ''
23
+ end
24
+
25
+ def get_user_hash(access_token)
26
+ user_hash = {}
27
+
28
+ params = {
29
+ access_token: access_token.token,
30
+ uids: access_token.params['user_id'],
31
+ fields: user_info_mapping.values.join(','),
32
+ scope: scope,
33
+ }
34
+
35
+ response = access_token.get(user_info_url, params: params)
36
+ if user_hash[:user_info] = JSON.parse(response.body)
37
+ user_hash[:user_info] = user_hash[:user_info]['response'][0]
38
+ user_hash[:user_info]['full_name'] = [user_hash[:user_info]['first_name'], user_hash[:user_info]['last_name']].join
39
+ user_hash[:uid] = user_hash[:user_info]['uid']
40
+ end
41
+ user_hash
42
+ end
43
+
44
+ # calculates and returns the url to which the user should be redirected,
45
+ # to get authenticated at the external provider's site.
46
+ def login_url(params, session)
47
+ self.authorize_url({ authorize_url: auth_path })
48
+ end
49
+
50
+ # tries to login the user from access token
51
+ def process_callback(params, session)
52
+ args = {}.tap do |a|
53
+ a[:code] = params[:code] if params[:code]
54
+ end
55
+
56
+ get_access_token(args, token_url: token_path, token_method: :post)
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,64 @@
1
+ module Sorcery
2
+ module Providers
3
+ # This class adds support for OAuth with xing.com.
4
+ #
5
+ # config.xing.key = <key>
6
+ # config.xing.secret = <secret>
7
+ # ...
8
+ #
9
+ class Xing < Base
10
+
11
+ include Protocols::Oauth
12
+
13
+ attr_accessor :access_token_path, :authorize_path, :request_token_path,
14
+ :user_info_path
15
+
16
+
17
+ def initialize
18
+ @configuration = {
19
+ site: 'https://api.xing.com/v1',
20
+ authorize_path: '/authorize',
21
+ request_token_path: '/request_token',
22
+ access_token_path: '/access_token'
23
+ }
24
+ @user_info_path = '/users/me'
25
+ end
26
+
27
+ # Override included get_consumer method to provide authorize_path
28
+ def get_consumer
29
+ ::OAuth::Consumer.new(@key, @secret, @configuration)
30
+ end
31
+
32
+ def get_user_hash(access_token)
33
+ response = access_token.get(user_info_path)
34
+
35
+ {}.tap do |h|
36
+ h[:user_info] = JSON.parse(response.body)['users'].first
37
+ h[:uid] = user_hash[:user_info]['id'].to_s
38
+ end
39
+ end
40
+
41
+ # calculates and returns the url to which the user should be redirected,
42
+ # to get authenticated at the external provider's site.
43
+ def login_url(params, session)
44
+ req_token = get_request_token
45
+ session[:request_token] = req_token.token
46
+ session[:request_token_secret] = req_token.secret
47
+ authorize_url({ request_token: req_token.token, request_token_secret: req_token.secret })
48
+ end
49
+
50
+ # tries to login the user from access token
51
+ def process_callback(params, session)
52
+ args = {
53
+ oauth_verifier: params[:oauth_verifier],
54
+ request_token: session[:request_token],
55
+ request_token_secret: session[:request_token_secret]
56
+ }
57
+
58
+ args.merge!({ code: params[:code] }) if params[:code]
59
+ get_access_token(args)
60
+ end
61
+
62
+ end
63
+ end
64
+ end