yaag 0.0.1 → 0.0.19

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: 5392a92361ab06888e9a382e419a2fd2a9c2eb550073476aa255ebafae4093a3
4
- data.tar.gz: d3783baaf6d30da9bf568c7ce585824880c1ca25e6f62f98758f57e7bc0ad47f
3
+ metadata.gz: 38c30b61f84668afa0e65809ee40daedd23bade1abd83915f01db62b80241978
4
+ data.tar.gz: e85146f65486aa58c9df32b42b524121b5196be33fc66f37a620d588e128c421
5
5
  SHA512:
6
- metadata.gz: ccfec1abfd5f86d6be13cef7b367fe8f774cfa9e8f00cf94d20ab595dc3a9577a51163cf71070470d79b3773c4efdde016a906e4cb62ba4391b14b7ae08e01a2
7
- data.tar.gz: 45aa07c8b3c8cac0f0063604f4350aaabc43ddf81dc7a2d3e7c43c63177ae136e021df87b0fcf53ead546400b0d992c6a65171c5e412428bf48c56b640a791a1
6
+ metadata.gz: 92d04143a6e0592b38e19200ba796be3934f671bb3ccb5ab2b471bba34e9388740c3c0094dc17541a286c2dd5ae0ff1f71dcbf926432a4919dd6a2f95ed2300e
7
+ data.tar.gz: cfa7aaea9e9de5e05d51d374fbddcccc764066676aa80948c2a0418046f34692723f8bb7924dc98c6687eb17c69699c7906a5fbe7747e19a0dd3104f09b2927e
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,35 +1,64 @@
1
- # Yaag
1
+ # Yet Another Authentication Gem
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ Yet Another Authentication Gem (YAAG) provides passwordless authentication for your rails app, all it takes is the user's e-mail address.
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/yaag`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ No password. No e-mail confirmation. No registration.
6
6
 
7
- ## Installation
7
+ ## TL;DR
8
8
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
9
+ ```
10
+ bundle add yaag
11
+ bundle install
10
12
 
11
- Install the gem and add to the application's Gemfile by executing:
13
+ rails g authentication:install
14
+ rails db:migrate
15
+ ```
12
16
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
17
+ ## How it works
14
18
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
19
+ It's the same mechanics of password reset in traditional authentication: the user provides an e-amil address and receives a link with a token to sign in. At its core, it implements the same logic of the built-in rails authentication (in fact most of the gem's code came from the rails library).
16
20
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ## Installation
22
+ Add this line to your application's Gemfile:
18
23
 
19
- ## Usage
24
+ ```bash
25
+ bundle add yaag
26
+ ```
20
27
 
21
- TODO: Write usage instructions here
28
+ And then execute:
29
+ ```bash
30
+ bundle
31
+ ```
22
32
 
23
- ## Development
33
+ Generate the authentication components (migrations, etc):
34
+ ```bash
35
+ $ gem install authentication:install
36
+ ```
24
37
 
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+ Follow the instructions post-installation.
26
39
 
27
- 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).
40
+ ## Usage
28
41
 
29
- ## Contributing
42
+ By default, all controllers require signing in via the inclusion of `include Authentication` in the `ApplicationController`. If you have a route that doesn't require signing in, add `allow_unauthenticated_access` to it:
30
43
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/yaag.
44
+ ```
45
+ class GuestController < ApplicationController
46
+ allow_unauthenticated_access only: %i[ index ]
47
+ ...
48
+ end
49
+ ```
32
50
 
33
- ## License
51
+ Get the current user with `Current.user` and the current session with `Current.session`
52
+
53
+ ```
54
+ class MyController < ApplicationController
55
+ def something
56
+ user = Current.user
57
+ session = Current.session
58
+ ...
59
+ end
60
+ end
61
+ ```
34
62
 
63
+ ## License
35
64
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,12 +1,10 @@
1
- # frozen_string_literal: true
1
+ require "bundler/setup"
2
2
 
3
- require "bundler/gem_tasks"
4
- require "minitest/test_task"
5
-
6
- Minitest::TestTask.create
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
7
5
 
8
- require "rubocop/rake_task"
9
-
10
- RuboCop::RakeTask.new
6
+ require "bundler/gem_tasks"
11
7
 
12
- task default: %i[test rubocop]
8
+ task :version do
9
+ p "v#{Yaag::VERSION}"
10
+ end
@@ -0,0 +1,52 @@
1
+ module Authentication
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ before_action :require_authentication
6
+ helper_method :authenticated?
7
+ end
8
+
9
+ class_methods do
10
+ def allow_unauthenticated_access(**options)
11
+ skip_before_action :require_authentication, **options
12
+ end
13
+ end
14
+
15
+ private
16
+ def authenticated?
17
+ resume_session
18
+ end
19
+
20
+ def require_authentication
21
+ resume_session || request_authentication
22
+ end
23
+
24
+ def resume_session
25
+ Current.session ||= find_session_by_cookie
26
+ end
27
+
28
+ def find_session_by_cookie
29
+ Session.find_by(id: cookies.signed[:session_id]) if cookies.signed[:session_id]
30
+ end
31
+
32
+ def request_authentication
33
+ session[:return_to_after_authenticating] = request.url
34
+ redirect_to new_signin_path
35
+ end
36
+
37
+ def after_authentication_url
38
+ session.delete(:return_to_after_authenticating) || root_url
39
+ end
40
+
41
+ def start_new_session_for(user)
42
+ user.sessions.create!(user_agent: request.user_agent, ip_address: request.remote_ip).tap do |session|
43
+ Current.session = session
44
+ cookies.signed.permanent[:session_id] = { value: session.id, httponly: true, same_site: :lax }
45
+ end
46
+ end
47
+
48
+ def terminate_session
49
+ Current.session.destroy
50
+ cookies.delete(:session_id)
51
+ end
52
+ end
@@ -0,0 +1,24 @@
1
+ class SessionsController < ApplicationController
2
+ allow_unauthenticated_access only: %i[ new create ]
3
+ before_action :set_user_by_token, only: %i[ create ]
4
+ rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_path, alert: I18n.t("yaag.errors.rate_limit") }
5
+
6
+ def new
7
+ end
8
+
9
+ def create
10
+ start_new_session_for @user
11
+ redirect_to after_authentication_url
12
+ end
13
+
14
+ def destroy
15
+ terminate_session
16
+ redirect_to new_signin_path, notice: I18n.t("yaag.sessions.successful"), status: :see_other
17
+ end
18
+ private
19
+ def set_user_by_token
20
+ @user = User.find_by_email_address_login_token!(params[:token])
21
+ rescue ActiveSupport::MessageVerifier::InvalidSignature
22
+ redirect_to new_signin_path, alert: I18n.t("yaag.errors.invalid_token"), status: :see_other
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ class SigninsController < ApplicationController
2
+ allow_unauthenticated_access
3
+ rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_signin_path, alert: I18n.t("yaag.errors.rate_limit") }
4
+ def new
5
+ end
6
+
7
+ def create
8
+ user = User.find_or_create_by(email_address: params[:email_address])
9
+ SigninsMailer.token(user).deliver_now
10
+ redirect_to new_session_path, notice: I18n.t("yaag.signins.email_sent"), status: :see_other
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ class SigninsMailer < ApplicationMailer
2
+ def token(user)
3
+ @user = user
4
+ mail subject: I18n.t("yaag.signins.email_subject"), to: user.email_address
5
+ end
6
+ end
@@ -0,0 +1,4 @@
1
+ class Current < ActiveSupport::CurrentAttributes
2
+ attribute :session
3
+ delegate :user, to: :session, allow_nil: true
4
+ end
@@ -0,0 +1,3 @@
1
+ class Session < ApplicationRecord
2
+ belongs_to :user
3
+ end
@@ -0,0 +1,7 @@
1
+ class User < ApplicationRecord
2
+ include Yaag::PasswordlessLogin
3
+ has_passwordless_login
4
+ has_many :sessions, dependent: :destroy
5
+
6
+ normalizes :email_address, with: ->(e) { e.strip.downcase }
7
+ end
@@ -0,0 +1,2 @@
1
+ <%= tag.div(flash[:alert], style: "color:red") if flash[:alert] %>
2
+ <%= tag.div(flash[:notice], style: "color:green") if flash[:notice] %>
@@ -0,0 +1,8 @@
1
+ <%= tag.div(flash[:alert], style: "color:red") if flash[:alert] %>
2
+ <%= tag.div(flash[:notice], style: "color:green") if flash[:notice] %>
3
+
4
+ <%= form_with url: signin_path do |form| %>
5
+ <%= form.email_field :email_address, required: true, autofocus: true, autocomplete: "username", placeholder: t("yaag.forms.signin_placeholder"), value: params[:email_address] %><br>
6
+ <%= form.submit t("yaag.forms.signin_button") %>
7
+ <% end %>
8
+ <br>
@@ -0,0 +1,5 @@
1
+ <p>
2
+ You can signin <%= link_to "with this link", create_session_url(@user.email_address_login_token) %>.
3
+
4
+ This link will expire in <%= distance_of_time_in_words(0, @user.email_address_login_token_expires_in) %>.
5
+ </p>
@@ -0,0 +1,4 @@
1
+ You can signin with the following link:
2
+ <%= create_session_url(@user.email_address_login_token) %>
3
+
4
+ This link will expire in <%= distance_of_time_in_words(0, @user.email_address_login_token_expires_in) %>.
@@ -0,0 +1,14 @@
1
+
2
+ en:
3
+ yaag:
4
+ errors:
5
+ invalid_token: "Signin link is invalid or has expired."
6
+ rate_limit: "Try again later."
7
+ forms:
8
+ signin_button: "Sign in"
9
+ signin_placeholder: "Enter your email address"
10
+ sessions:
11
+ successful: "Signed in successfully."
12
+ signins:
13
+ email_sent: "Sign in address sent to provided e-mail."
14
+ email_subject: "Sign in link"
data/config/routes.rb ADDED
@@ -0,0 +1,6 @@
1
+ Rails.application.routes.draw do
2
+ resource :signin, only: [ :new, :create ]
3
+ resource :session, only: [ :new, :destroy ] do
4
+ get "/:token/create" => "sessions#create", as: :create
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ class CreateUsers < ActiveRecord::Migration[8.1]
2
+ def change
3
+ create_table :users do |t|
4
+ t.string :email_address, null: false
5
+
6
+ t.timestamps
7
+ end
8
+ add_index :users, :email_address, unique: true
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ class CreateSessions < ActiveRecord::Migration[8.1]
2
+ def change
3
+ create_table :sessions do |t|
4
+ t.references :user, null: false, foreign_key: true
5
+ t.string :ip_address
6
+ t.string :user_agent
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ Description:
2
+ Generates a basic passwordless authentication system with users and sessions.
3
+
4
+ Example:
5
+ bin/rails generate authentication:install
6
+
@@ -0,0 +1,18 @@
1
+ module Authentication
2
+ class InstallGenerator < Rails::Generators::Base
3
+ source_root File.expand_path("templates", __dir__)
4
+
5
+ def configure_application_controller
6
+ inject_into_class "app/controllers/application_controller.rb", "ApplicationController", " include Authentication\n"
7
+ end
8
+
9
+ def generate_migrations
10
+ generate "migration", "CreateUsers", "email_address:string!:uniq", "--force"
11
+ generate "migration", "CreateSessions", "user:references ip_address:string user_agent:string", "--force"
12
+ end
13
+
14
+ def show_readme
15
+ readme "README" if behavior == :invoke
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,33 @@
1
+
2
+ Yet Another Authentication Gem (YAAG)
3
+
4
+ ===============================================================
5
+
6
+ To complete the installation, run a database migration:
7
+
8
+ ./bin/rails db:migrate
9
+
10
+ ===============================================================
11
+
12
+ Make sure root route is present in config/routes.rb. Example:
13
+
14
+ Rails.application.routes.draw do
15
+ ...
16
+ root "controller#action"
17
+ ...
18
+ end
19
+
20
+ ===============================================================
21
+
22
+ By default, all controllers require user signin.
23
+
24
+ To allow unauthenticated access, add allow_unauthenticated_access to your controller. Example:
25
+
26
+ class MyController < ApplicationController
27
+ allow_unauthenticated_access only: %i[ index ]
28
+ ...
29
+ end
30
+
31
+ ===============================================================
32
+
33
+ See more at: https://github.com/nu12/yaag
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :yaag do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,4 @@
1
+ module Yaag
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
data/lib/yaag/version.rb CHANGED
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module Yaag
4
- VERSION = "0.0.1"
2
+ VERSION = "0.0.19"
5
3
  end
data/lib/yaag.rb CHANGED
@@ -1,8 +1,51 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "yaag/version"
1
+ require "yaag/version"
2
+ require "yaag/engine"
4
3
 
5
4
  module Yaag
6
- class Error < StandardError; end
7
- # Your code goes here...
5
+ module PasswordlessLogin
6
+ extend ActiveSupport::Concern
7
+
8
+ DEFAULT_LOGIN_TOKEN_EXPIRES_IN = 15.minutes
9
+
10
+ class << self
11
+ end
12
+
13
+ module ClassMethods
14
+ def has_passwordless_login(attribute = :email_address)
15
+ include InstanceMethodsOnActivation.new(attribute)
16
+
17
+ if respond_to?(:generates_token_for)
18
+ login_token_expires_in = DEFAULT_LOGIN_TOKEN_EXPIRES_IN
19
+
20
+ silence_redefinition_of_method(:"#{attribute}_login_token_expires_in")
21
+ define_method(:"#{attribute}_login_token_expires_in") { login_token_expires_in }
22
+
23
+ generates_token_for :"#{attribute}_login", expires_in: login_token_expires_in do
24
+ public_send(:"#{attribute}")&.first(10)
25
+ end
26
+
27
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
28
+ silence_redefinition_of_method :find_by_#{attribute}_login_token
29
+ def self.find_by_#{attribute}_login_token(token)
30
+ find_by_token_for(:#{attribute}_login, token)
31
+ end
32
+
33
+ silence_redefinition_of_method :find_by_#{attribute}_login_token!
34
+ def self.find_by_#{attribute}_login_token!(token)
35
+ find_by_token_for!(:#{attribute}_login, token)
36
+ end
37
+ RUBY
38
+ end
39
+ end
40
+ end
41
+
42
+ class InstanceMethodsOnActivation < Module
43
+ def initialize(attribute)
44
+ # attr_accessor attribute
45
+ define_method("#{attribute}_login_token") do
46
+ generate_token_for(:"#{attribute}_login")
47
+ end
48
+ end
49
+ end
50
+ end
8
51
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - nu12
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-12-27 00:00:00.000000000 Z
11
+ date: 2026-01-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Passwordless authentication.
14
14
  email:
@@ -17,14 +17,31 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - ".rubocop.yml"
21
- - LICENSE.txt
20
+ - MIT-LICENSE
22
21
  - README.md
23
22
  - Rakefile
23
+ - app/controllers/concerns/authentication.rb
24
+ - app/controllers/sessions_controller.rb
25
+ - app/controllers/signins_controller.rb
26
+ - app/mailers/signins_mailer.rb
27
+ - app/models/current.rb
28
+ - app/models/session.rb
29
+ - app/models/user.rb
30
+ - app/views/sessions/new.html.erb
31
+ - app/views/signins/new.html.erb
32
+ - app/views/signins_mailer/token.html.erb
33
+ - app/views/signins_mailer/token.text.erb
34
+ - config/locales/en.yml
35
+ - config/routes.rb
36
+ - db/migrate/20260108164812_create_users.rb
37
+ - db/migrate/20260108165215_create_sessions.rb
38
+ - lib/generators/authentication/USAGE
39
+ - lib/generators/authentication/install_generator.rb
40
+ - lib/generators/authentication/templates/README
41
+ - lib/tasks/yaag_tasks.rake
24
42
  - lib/yaag.rb
43
+ - lib/yaag/engine.rb
25
44
  - lib/yaag/version.rb
26
- - mise.toml
27
- - sig/yaag.rbs
28
45
  homepage: https://github.com/nu12/yaag
29
46
  licenses:
30
47
  - MIT
data/.rubocop.yml DELETED
@@ -1,8 +0,0 @@
1
- AllCops:
2
- TargetRubyVersion: 3.0
3
-
4
- Style/StringLiterals:
5
- EnforcedStyle: double_quotes
6
-
7
- Style/StringLiteralsInInterpolation:
8
- EnforcedStyle: double_quotes
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2025 TODO: Write your name
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
data/mise.toml DELETED
@@ -1,2 +0,0 @@
1
- [tools]
2
- ruby = "3.2.8"
data/sig/yaag.rbs DELETED
@@ -1,4 +0,0 @@
1
- module Yaag
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end