balaclava 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 94d63b7d24667a0a158018eb7dcd218585d261eba703ca346c3905f9173ca169
4
+ data.tar.gz: bd961d7dd7eebe028e5f310b494b07c02952e6f4655f67a67e198bb1ce1491a0
5
+ SHA512:
6
+ metadata.gz: d46efbbf262f8b66762ebde8a0b16df2a0e66f7b98f4c829275583e71bb1f6399d6d38994c893d9999a4f12bd708ba63b53a3b52a75f5b2561675f0c78402289
7
+ data.tar.gz: 5a51499354ed9e388f8f165ecb99484550e55f753a2ab326f336ebfdb687dda412288dbb55edef8e8c0884a5b3d80d5a185ab79b351ff862fc4294547e9f91d6
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-03-02
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
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 ADDED
@@ -0,0 +1,67 @@
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 ADDED
@@ -0,0 +1,8 @@
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 ADDED
@@ -0,0 +1,38 @@
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
Binary file
@@ -0,0 +1,51 @@
1
+ require 'securerandom'
2
+ require_relative 'errors/invalid_code_error'
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)
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ class InvalidCodeError < StandardError
2
+ def initialize(message = "Invalid verification code")
3
+ super
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Balaclava
4
+ VERSION = "0.1.0"
5
+ end
data/lib/balaclava.rb ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "balaclava/version"
4
+ require_relative "balaclava/authenticator"
5
+
6
+ module Balaclava
7
+ class << self
8
+ attr_accessor :user_model
9
+ end
10
+
11
+ def self.configure
12
+ yield self
13
+ end
14
+ end
@@ -0,0 +1,19 @@
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 ADDED
@@ -0,0 +1,4 @@
1
+ module Balaclava
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: balaclava
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sante
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-03-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mail
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Validate users througt 6 digits codes sent by email.
28
+ email:
29
+ - imsantiago@proton.me
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - CHANGELOG.md
36
+ - CODE_OF_CONDUCT.md
37
+ - README.md
38
+ - Rakefile
39
+ - balaclava.gemspec
40
+ - balaclava_logo.png
41
+ - lib/balaclava.rb
42
+ - lib/balaclava/authenticator.rb
43
+ - lib/balaclava/errors/invalid_code_error.rb
44
+ - lib/balaclava/version.rb
45
+ - lib/generators/install_generator.rb
46
+ - sig/balaclava.rbs
47
+ homepage: https://github.com/santedime/balaclava
48
+ licenses: []
49
+ metadata:
50
+ homepage_uri: https://github.com/santedime/balaclava
51
+ source_code_uri: https://github.com/santedime/balaclava
52
+ changelog_uri: https://github.com/santedime/balaclava/blob/main/CHANGELOG.md
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.6.0
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.3.5
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: A minimalistic way to validate your users.
72
+ test_files: []