authentication-logic 0.1.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 (48) hide show
  1. checksums.yaml +7 -0
  2. data/bin/console +11 -0
  3. data/bin/setup +8 -0
  4. data/lib/auth/logic/acts_as_authentic/base.rb +118 -0
  5. data/lib/auth/logic/acts_as_authentic/email.rb +32 -0
  6. data/lib/auth/logic/acts_as_authentic/logged_in_status.rb +87 -0
  7. data/lib/auth/logic/acts_as_authentic/login.rb +65 -0
  8. data/lib/auth/logic/acts_as_authentic/magic_columns.rb +40 -0
  9. data/lib/auth/logic/acts_as_authentic/password.rb +362 -0
  10. data/lib/auth/logic/acts_as_authentic/perishable_token.rb +125 -0
  11. data/lib/auth/logic/acts_as_authentic/persistence_token.rb +72 -0
  12. data/lib/auth/logic/acts_as_authentic/queries/case_sensitivity.rb +55 -0
  13. data/lib/auth/logic/acts_as_authentic/queries/find_with_case.rb +85 -0
  14. data/lib/auth/logic/acts_as_authentic/session_maintenance.rb +189 -0
  15. data/lib/auth/logic/acts_as_authentic/single_access_token.rb +85 -0
  16. data/lib/auth/logic/config.rb +41 -0
  17. data/lib/auth/logic/controller_adapters/abstract_adapter.rb +121 -0
  18. data/lib/auth/logic/controller_adapters/rack_adapter.rb +74 -0
  19. data/lib/auth/logic/controller_adapters/rails_adapter.rb +49 -0
  20. data/lib/auth/logic/controller_adapters/sinatra_adapter.rb +69 -0
  21. data/lib/auth/logic/cookie_credentials.rb +65 -0
  22. data/lib/auth/logic/crypto_providers/bcrypt.rb +116 -0
  23. data/lib/auth/logic/crypto_providers/md5/v2.rb +37 -0
  24. data/lib/auth/logic/crypto_providers/md5.rb +38 -0
  25. data/lib/auth/logic/crypto_providers/scrypt.rb +96 -0
  26. data/lib/auth/logic/crypto_providers/sha1/v2.rb +42 -0
  27. data/lib/auth/logic/crypto_providers/sha1.rb +43 -0
  28. data/lib/auth/logic/crypto_providers/sha256/v2.rb +60 -0
  29. data/lib/auth/logic/crypto_providers/sha256.rb +61 -0
  30. data/lib/auth/logic/crypto_providers/sha512/v2.rb +41 -0
  31. data/lib/auth/logic/crypto_providers/sha512.rb +40 -0
  32. data/lib/auth/logic/crypto_providers.rb +89 -0
  33. data/lib/auth/logic/errors.rb +52 -0
  34. data/lib/auth/logic/i18n/translator.rb +20 -0
  35. data/lib/auth/logic/i18n.rb +100 -0
  36. data/lib/auth/logic/random.rb +18 -0
  37. data/lib/auth/logic/session/base.rb +2205 -0
  38. data/lib/auth/logic/session/magic_column/assigns_last_request_at.rb +49 -0
  39. data/lib/auth/logic/test_case/mock_api_controller.rb +53 -0
  40. data/lib/auth/logic/test_case/mock_controller.rb +59 -0
  41. data/lib/auth/logic/test_case/mock_cookie_jar.rb +112 -0
  42. data/lib/auth/logic/test_case/mock_logger.rb +14 -0
  43. data/lib/auth/logic/test_case/mock_request.rb +36 -0
  44. data/lib/auth/logic/test_case/rails_request_adapter.rb +40 -0
  45. data/lib/auth/logic/test_case.rb +216 -0
  46. data/lib/auth/logic/version.rb +7 -0
  47. data/lib/auth/logic.rb +46 -0
  48. metadata +426 -0
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Authentication
4
+ module Logic
5
+ # Parent class of all Authentication::Logic errors.
6
+ class Error < StandardError
7
+ end
8
+
9
+ # :nodoc:
10
+ class InvalidCryptoProvider < Error
11
+ end
12
+
13
+ # :nodoc:
14
+ class NilCryptoProvider < InvalidCryptoProvider
15
+ def message
16
+ <<~EOS
17
+ In version 5, Authentication::Logic used SCrypt by default. As of version 6, there
18
+ is no default. We still recommend SCrypt. If you previously relied on
19
+ this default, then, in your User model (or equivalent), please set the
20
+ following:
21
+
22
+ acts_as_authentic do |c|
23
+ c.crypto_provider = ::Authentication::Logic::CryptoProviders::SCrypt
24
+ end
25
+
26
+ Furthermore, the Authentication::Logic gem no longer depends on the scrypt gem. In
27
+ your Gemfile, please add scrypt.
28
+
29
+ gem "scrypt", "~> 3.0"
30
+
31
+ We have made this change in Authentication::Logic 6 so that users of other crypto
32
+ providers no longer need to install the scrypt gem.
33
+ EOS
34
+ end
35
+ end
36
+
37
+ # :nodoc:
38
+ class ModelSetupError < Error
39
+ def message
40
+ <<-EOS
41
+ You must establish a database connection and run the migrations before
42
+ using acts_as_authentic. If you need to load the User model before the
43
+ database is set up correctly, please set the following:
44
+
45
+ acts_as_authentic do |c|
46
+ c.raise_on_model_setup_error = false
47
+ end
48
+ EOS
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Authentication
4
+ module Logic
5
+ module I18n
6
+ # The default translator used by auth/logic/i18n.rb
7
+ class Translator
8
+ # If the I18n gem is present, calls +I18n.translate+ passing all
9
+ # arguments, else returns +options[:default]+.
10
+ def translate(key, options = {})
11
+ if defined?(::I18n)
12
+ ::I18n.translate key, **options
13
+ else
14
+ options[:default]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Authentication
4
+ module Logic
5
+ # This class allows any message in Authentication::Logic to use internationalization. In
6
+ # earlier versions of Authentication::Logic each message was translated via configuration.
7
+ # This cluttered up the configuration and cluttered up Authentication::Logic. So all
8
+ # translation has been extracted out into this class. Now all messages pass
9
+ # through this class, making it much easier to implement in I18n library /
10
+ # plugin you want. Use this as a layer that sits between Authentication::Logic and
11
+ # whatever I18n library you want to use.
12
+ #
13
+ # By default this uses the rails I18n library, if it exists. If it doesn't
14
+ # exist it just returns the default English message. The Authentication::Logic I18n class
15
+ # works EXACTLY like the rails I18n class. This is because the arguments are
16
+ # delegated to this class.
17
+ #
18
+ # Here is how all messages are translated internally with Authentication::Logic:
19
+ #
20
+ # Authentication::Logic::I18n.t('error_messages.password_invalid', :default => "is invalid")
21
+ #
22
+ # If you use a different I18n library just replace the build-in
23
+ # I18n::Translator class with your own. For example:
24
+ #
25
+ # class MyAuthentication::LogicI18nTranslator
26
+ # def translate(key, options = {})
27
+ # # you will have key which will be something like:
28
+ # # "error_messages.password_invalid"
29
+ # # you will also have options[:default], which will be the default
30
+ # # English version of the message
31
+ # # do whatever you want here with the arguments passed to you.
32
+ # end
33
+ # end
34
+ #
35
+ # Authentication::Logic::I18n.translator = MyAuthentication::LogicI18nTranslator.new
36
+ #
37
+ # That it's! Here is a complete list of the keys that are passed. Just define
38
+ # these however you wish:
39
+ #
40
+ # Authentication::Logic:
41
+ # error_messages:
42
+ # login_blank: can not be blank
43
+ # login_not_found: is not valid
44
+ # login_invalid: should use only letters, numbers, spaces, and .-_@+ please.
45
+ # consecutive_failed_logins_limit_exceeded: >
46
+ # Consecutive failed logins limit exceeded, account is disabled.
47
+ # email_invalid: should look like an email address.
48
+ # email_invalid_international: should look like an international email address.
49
+ # password_blank: can not be blank
50
+ # password_invalid: is not valid
51
+ # not_active: Your account is not active
52
+ # not_confirmed: Your account is not confirmed
53
+ # not_approved: Your account is not approved
54
+ # no_authentication_details: You did not provide any details for authentication.
55
+ # general_credentials_error: Login/Password combination is not valid
56
+ # session_invalid: Your session is invalid and has the following errors:
57
+ # models:
58
+ # user_session: UserSession (or whatever name you are using)
59
+ # attributes:
60
+ # user_session: (or whatever name you are using)
61
+ # login: login
62
+ # email: email
63
+ # password: password
64
+ # remember_me: remember me
65
+ module I18n
66
+ @@scope = :auth_logic
67
+ @@translator = nil
68
+
69
+ class << self
70
+ # Returns the current scope. Defaults to :auth_logic
71
+ def scope
72
+ @@scope
73
+ end
74
+
75
+ # Sets the current scope. Used to set a custom scope.
76
+ def scope=(scope)
77
+ @@scope = scope
78
+ end
79
+
80
+ # Returns the current translator. Defaults to +Translator+.
81
+ def translator
82
+ @@translator ||= Translator.new
83
+ end
84
+
85
+ # Sets the current translator. Used to set a custom translator.
86
+ def translator=(translator)
87
+ @@translator = translator
88
+ end
89
+
90
+ # All message translation is passed to this method. The first argument is
91
+ # the key for the message. The second is options, see the rails I18n
92
+ # library for a list of options used.
93
+ def translate(key, options = {})
94
+ translator.translate key, { scope: I18n.scope }.merge(options)
95
+ end
96
+ alias t translate
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Authentication
4
+ module Logic
5
+ # Generates random strings using ruby's SecureRandom library.
6
+ module Random
7
+ def self.hex_token
8
+ SecureRandom.hex(64)
9
+ end
10
+
11
+ # Returns a string in base64url format as defined by RFC-3548 and RFC-4648.
12
+ # We call this a "friendly" token because it is short and safe for URLs.
13
+ def self.friendly_token
14
+ SecureRandom.urlsafe_base64(15)
15
+ end
16
+ end
17
+ end
18
+ end