balaclava 0.1.0 → 1.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 94d63b7d24667a0a158018eb7dcd218585d261eba703ca346c3905f9173ca169
4
- data.tar.gz: bd961d7dd7eebe028e5f310b494b07c02952e6f4655f67a67e198bb1ce1491a0
3
+ metadata.gz: 927fc1691b7cc79944f1cbd4f9c73c451daa7604054c4269e7f3448f43e8e823
4
+ data.tar.gz: 8341d445e3b5e7c49ea79cdd5fa68c19b1bbfd96df0293c71f45b8d7794d394f
5
5
  SHA512:
6
- metadata.gz: d46efbbf262f8b66762ebde8a0b16df2a0e66f7b98f4c829275583e71bb1f6399d6d38994c893d9999a4f12bd708ba63b53a3b52a75f5b2561675f0c78402289
7
- data.tar.gz: 5a51499354ed9e388f8f165ecb99484550e55f753a2ab326f336ebfdb687dda412288dbb55edef8e8c0884a5b3d80d5a185ab79b351ff862fc4294547e9f91d6
6
+ metadata.gz: fc3f635b1e407d538cd506fabb00e521d52f0eb9b9e9de67beaf05fd28d68c9e17a7bbd18b1bee0ecbff849985584db5b1d26aa10afcecbcf7bb75960869a0f8
7
+ data.tar.gz: ea523bacf09065e621cbd42eb4bb32f6bf790910354a82ced3e7f1c3c18f6c841ac0e1b208e075c0eb186941765cedc0763c90c7bfc81a9ec17ad614452db128
@@ -1,51 +1,52 @@
1
1
  require 'securerandom'
2
2
  require_relative 'errors/invalid_code_error'
3
3
 
4
- class Balaclava::Authenticator
5
- attr_accessor :cache
6
-
7
- def initialize(user_email, cache = Rails.cache)
8
- valid_email?(user_email)
9
- @user_email = user_email
10
- @cache = cache
11
- end
12
-
13
- def registration
14
- user = create_user
15
- verification_data = { code: generate_code.to_s }
16
- cache.write("verification_data#{user.id}", verification_data)
17
- user
18
- rescue => e
19
- raise StandardError, "Registration failed: #{e.message}"
20
- end
21
-
22
- def generate_code
23
- rand(100000..999999)
24
- end
25
-
26
- def validate_code(user_email, provided_code)
27
- user = find_user_by_email(user_email)
28
- cached_code = cache.read("verification_data#{user.id}")
29
- raise InvalidCodeError, "Invalid code" unless cached_code == provided_code
30
- true
31
- rescue => e
32
- raise InvalidCodeError, "Code validation failed: #{e.message}"
33
- end
34
-
35
- private
36
-
37
- def valid_email?(email)
38
- # Simple regex pattern for email validation
39
- email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
40
- raise StandardError, "Invalid email address" unless email =~ email_regex
41
- true
42
- end
43
-
44
- def create_user
45
- Balaclava.user_model.constantize.create(email: @user_email)
46
- end
47
-
48
- def find_user_by_email(email)
49
- Balaclava.user_model.constantize.find_by(email: email)
4
+ module Balaclava
5
+ class Authenticator
6
+ attr_reader :user_email, :cache
7
+
8
+ def initialize(user_email, cache = Rails.cache)
9
+ validate_email(user_email)
10
+ @user_email = user_email
11
+ @cache = cache
12
+ end
13
+
14
+ def registration
15
+ user = create_user
16
+ verification_data = { code: generate_code.to_s }
17
+ cache.write("verification_data#{user.id}", verification_data)
18
+ user
19
+ rescue StandardError => e
20
+ raise StandardError, "Registration failed: #{e.message}"
21
+ end
22
+
23
+ def validate_code(provided_code)
24
+ user = find_user_by_email(user_email)
25
+ cached_code = cache.read("verification_data#{user.id}")
26
+ raise InvalidCodeError, "Invalid code" unless cached_code == provided_code
27
+ true
28
+ rescue StandardError => e
29
+ raise InvalidCodeError, "Code validation failed: #{e.message}"
30
+ end
31
+
32
+ private
33
+
34
+ def validate_email(email)
35
+ # Simple regex pattern for email validation
36
+ email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
37
+ raise StandardError, "Invalid email address" unless email =~ email_regex
38
+ end
39
+
40
+ def generate_code
41
+ SecureRandom.random_number(100_000..999_999)
42
+ end
43
+
44
+ def create_user
45
+ Balaclava.user_model.constantize.create(email: user_email)
46
+ end
47
+
48
+ def find_user_by_email(email)
49
+ Balaclava.user_model.constantize.find_by(email: email)
50
+ end
50
51
  end
51
52
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Balaclava
4
- VERSION = "0.1.0"
4
+ VERSION = "1.2.0"
5
5
  end
@@ -0,0 +1,50 @@
1
+ require 'rails/generators/active_record'
2
+ require 'rails/generators/named_base'
3
+ require 'rails/generators/base'
4
+ require 'thor/shell'
5
+
6
+ module Balaclava
7
+ module Generators
8
+ # InstallGenerator sets up Balaclava within a Rails application by generating
9
+ # necessary configuration files, models, migrations, and mailers.
10
+ class InstallGenerator < Rails::Generators::NamedBase
11
+ include Thor::Shell
12
+ source_root File.expand_path('templates', __dir__)
13
+
14
+ desc "Generates Balaclava configuration files, models, and migrations."
15
+
16
+ # Prompts the user for the model name and sets a default if blank.
17
+ def ask_for_user_model_name
18
+ @user_model_name = ask("What is the name of your user model? [Default: User]") || 'User'
19
+ @user_model_name = 'User' if @user_model_name.blank?
20
+ end
21
+
22
+ # Creates an initializer file for Balaclava.
23
+ def create_initializer_file
24
+ template 'initializer.rb', 'config/initializers/balaclava.rb'
25
+ end
26
+
27
+ # Generates a model file within the application's models directory.
28
+ def create_model_file
29
+ template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
30
+ end
31
+
32
+ # Creates a migration file for Balaclava users.
33
+ def create_migration
34
+ migration_template 'migration.rb', 'db/migrate/create_balaclava_users.rb', migration_version: migration_version
35
+ end
36
+
37
+ # Generates a mailer file for Balaclava.
38
+ def create_mailer
39
+ template 'mailer.rb', 'app/mailers/balaclava_mailer.rb'
40
+ end
41
+
42
+ private
43
+
44
+ # Determines the Rails migration version syntax based on the Rails version.
45
+ def migration_version
46
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" if Gem::Version.new(Rails.version) >= Gem::Version.new('5.0.0')
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ Balaclava.configure do |config|
2
+ config.user_model = "<%= @user_model_name %>"
3
+ end
@@ -0,0 +1,6 @@
1
+ class BalaclavaMailer < ApplicationMailer
2
+ def authentication_code_email(user)
3
+ @user = user
4
+ mail(to: @user.email, subject: 'Authentication Code')
5
+ end
6
+ end
@@ -0,0 +1,12 @@
1
+ # db/migrate/<%= migration_timestamp %>_create_balaclava_users.rb
2
+
3
+ class CreateBalaclavaUsers < ActiveRecord::Migration<%= migration_version %>
4
+ def change
5
+ create_table :balaclava_users do |t|
6
+ t.string :email
7
+ t.timestamps
8
+ end
9
+
10
+ add_index :balaclava_users, :email, unique: true
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ class <%= class_name %> < ApplicationRecord
2
+
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: balaclava
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sante
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-03 00:00:00.000000000 Z
11
+ date: 2024-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mail
@@ -24,26 +24,64 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description: Validate users througt 6 digits codes sent by email.
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ description: Validate users through 6 digits codes sent by email.
28
70
  email:
29
71
  - imsantiago@proton.me
30
72
  executables: []
31
73
  extensions: []
32
74
  extra_rdoc_files: []
33
75
  files:
34
- - ".rspec"
35
- - CHANGELOG.md
36
- - CODE_OF_CONDUCT.md
37
- - README.md
38
- - Rakefile
39
- - balaclava.gemspec
40
- - balaclava_logo.png
41
76
  - lib/balaclava.rb
42
77
  - lib/balaclava/authenticator.rb
43
78
  - lib/balaclava/errors/invalid_code_error.rb
44
79
  - lib/balaclava/version.rb
45
- - lib/generators/install_generator.rb
46
- - sig/balaclava.rbs
80
+ - lib/generators/balaclava/install_generator.rb
81
+ - lib/generators/balaclava/templates/initializer.rb
82
+ - lib/generators/balaclava/templates/mailer.rb
83
+ - lib/generators/balaclava/templates/migration.rb
84
+ - lib/generators/balaclava/templates/model.rb
47
85
  homepage: https://github.com/santedime/balaclava
48
86
  licenses: []
49
87
  metadata:
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/CHANGELOG.md DELETED
@@ -1,5 +0,0 @@
1
- ## [Unreleased]
2
-
3
- ## [0.1.0] - 2024-03-02
4
-
5
- - Initial release
data/CODE_OF_CONDUCT.md DELETED
@@ -1,84 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
-
7
- We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
-
9
- ## Our Standards
10
-
11
- Examples of behavior that contributes to a positive environment for our community include:
12
-
13
- * Demonstrating empathy and kindness toward other people
14
- * Being respectful of differing opinions, viewpoints, and experiences
15
- * Giving and gracefully accepting constructive feedback
16
- * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
- * Focusing on what is best not just for us as individuals, but for the overall community
18
-
19
- Examples of unacceptable behavior include:
20
-
21
- * The use of sexualized language or imagery, and sexual attention or
22
- advances of any kind
23
- * Trolling, insulting or derogatory comments, and personal or political attacks
24
- * Public or private harassment
25
- * Publishing others' private information, such as a physical or email
26
- address, without their explicit permission
27
- * Other conduct which could reasonably be considered inappropriate in a
28
- professional setting
29
-
30
- ## Enforcement Responsibilities
31
-
32
- Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
-
34
- Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
-
36
- ## Scope
37
-
38
- This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
-
40
- ## Enforcement
41
-
42
- Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at heysante@proton.me. All complaints will be reviewed and investigated promptly and fairly.
43
-
44
- All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
-
46
- ## Enforcement Guidelines
47
-
48
- Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
-
50
- ### 1. Correction
51
-
52
- **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
-
54
- **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
-
56
- ### 2. Warning
57
-
58
- **Community Impact**: A violation through a single incident or series of actions.
59
-
60
- **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
-
62
- ### 3. Temporary Ban
63
-
64
- **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
-
66
- **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
-
68
- ### 4. Permanent Ban
69
-
70
- **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
-
72
- **Consequence**: A permanent ban from any sort of public interaction within the community.
73
-
74
- ## Attribution
75
-
76
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
- available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
-
79
- Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
-
81
- [homepage]: https://www.contributor-covenant.org
82
-
83
- For answers to common questions about this code of conduct, see the FAQ at
84
- https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/README.md DELETED
@@ -1,67 +0,0 @@
1
- # Balaclava
2
-
3
- ![Balaclava Logo](balaclava_logo.png)
4
-
5
- Balaclava is a minimalistic gem designed to provide simple user validation functionalities.
6
-
7
- ## Installation
8
-
9
- To install Balaclava, add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'balaclava'
13
- ```
14
-
15
- And then execute:
16
-
17
- ```bash
18
- bundle install
19
- ```
20
-
21
- Or install it yourself as:
22
-
23
- ```bash
24
- gem install balaclava
25
- ```
26
-
27
- ## Usage
28
-
29
- Once Balaclava is installed, you can use it as follows:
30
-
31
- 1. **Generate Balaclava Model**: To generate the Balaclava model for your user, run the following command:
32
-
33
- ```bash
34
- rails generate balaclava:install
35
- ```
36
-
37
- 2. **Initialize Balaclava Authenticator**: Initialize an instance of the `Balaclava::Authenticator` class with the user's email address and an optional cache object:
38
-
39
- ```ruby
40
- authenticator = Balaclava::Authenticator.new("user@example.com")
41
- ```
42
-
43
- 3. **Register a User**: Use the `registration` method to register a user and store verification data in the cache:
44
-
45
- ```ruby
46
- user = authenticator.registration
47
- ```
48
-
49
- 4. **Validate Code**: To validate a code provided by the user, use the `validate_code` method:
50
-
51
- ```ruby
52
- authenticator.validate_code("user@example.com", "provided_code")
53
- ```
54
-
55
- ## Development
56
-
57
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
-
59
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
60
-
61
- ## Contributing
62
-
63
- Bug reports and pull requests are welcome on GitHub at [https://github.com/santedime/balaclava](https://github.com/santedime/balaclava). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/santedime/balaclava/blob/master/CODE_OF_CONDUCT.md).
64
-
65
- ## Code of Conduct
66
-
67
- Everyone interacting in the Balaclava project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/santedime/balaclava/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
5
-
6
- RSpec::Core::RakeTask.new(:spec)
7
-
8
- task default: :spec
data/balaclava.gemspec DELETED
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/balaclava/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "balaclava"
7
- spec.version = Balaclava::VERSION
8
- spec.authors = ["Sante"]
9
- spec.email = ["imsantiago@proton.me"]
10
-
11
- spec.summary = "A minimalistic way to validate your users."
12
- spec.description = "Validate users througt 6 digits codes sent by email."
13
- spec.homepage = "https://github.com/santedime/balaclava"
14
- spec.required_ruby_version = ">= 2.6.0"
15
-
16
- spec.metadata["homepage_uri"] = spec.homepage
17
- spec.metadata["source_code_uri"] = "https://github.com/santedime/balaclava"
18
- spec.metadata["changelog_uri"] = "https://github.com/santedime/balaclava/blob/main/CHANGELOG.md"
19
-
20
- # Specify which files should be added to the gem when it is released.
21
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
- spec.files = Dir.chdir(__dir__) do
23
- `git ls-files -z`.split("\x0").reject do |f|
24
- (File.expand_path(f) == __FILE__) ||
25
- f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
26
- end
27
- end
28
- spec.bindir = "exe"
29
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
- spec.require_paths = ["lib"]
31
-
32
- # Uncomment to register a new dependency of your gem
33
- # spec.add_dependency "example-gem", "~> 1.0"
34
-
35
- # For more information and examples about making a new gem, check out our
36
- # guide at: https://bundler.io/guides/creating_gem.html
37
- spec.add_runtime_dependency 'mail'
38
- end
data/balaclava_logo.png DELETED
Binary file
@@ -1,19 +0,0 @@
1
- # lib/generators/install_generator.rb
2
- require 'rails/generators'
3
-
4
- module Balaclava
5
- class InstallGenerator < Rails::Generators::Base
6
- def ask_for_user_model_name
7
- @user_model_name = ask("What is the name of your user model?(sometimes it could be, client, user... I mean, what final user you need to validate???) [User will be set if you left it blank]")
8
- @user_model_name = "User" if @user_model_name.blank?
9
- end
10
-
11
- def create_initializer_file
12
- create_file "config/initializers/balaclava.rb", <<~EOF
13
- Balaclava.configure do |config|
14
- config.user_model = "#{@user_model_name}"
15
- end
16
- EOF
17
- end
18
- end
19
- end
data/sig/balaclava.rbs DELETED
@@ -1,4 +0,0 @@
1
- module Balaclava
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end