passwordless 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
+ SHA1:
3
+ metadata.gz: 7975d0dabfd39211005c2a34f3f3d967755b0a70
4
+ data.tar.gz: 621a7327a0375aeeb6e4dafb990bce78d4bf32dc
5
+ SHA512:
6
+ metadata.gz: 55ca6da5e10b2a0d8e92d0dad1cc048214efd7e32a9d6fecc40a2ebce102744772c96b6b128fae53d1b14b3709bb49f1225c82c6d5a08507eef8db29fc1ce0f9
7
+ data.tar.gz: 925dbd8b6d98ac9a68d5c12bdc940bc38a1dc8c4e82b47c941d2de768b0cdd43edf2681a900e213eac366fd1e17dab9031650869cc2ce6801bd24881989e6320
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Mikkel Malmberg
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 ADDED
@@ -0,0 +1,9 @@
1
+ # Passwordless
2
+
3
+ Add authentication to your Rails app without all the icky-ness of passwords.
4
+
5
+ _WIP_
6
+
7
+ # License
8
+
9
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Passwordless'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ require 'bundler/gem_tasks'
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'test'
31
+ t.pattern = 'test/**/*_test.rb'
32
+ t.verbose = false
33
+ end
34
+
35
+
36
+ task default: :test
@@ -0,0 +1,5 @@
1
+ module Passwordless
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ end
5
+ end
@@ -0,0 +1,53 @@
1
+ module Passwordless
2
+ class SessionsController < ApplicationController
3
+ include ControllerHelpers
4
+
5
+ def new
6
+ @email_field = authenticatable_class.passwordless_email_field
7
+
8
+ @session = Session.new
9
+ end
10
+
11
+ def create
12
+ email_field = authenticatable_class.passwordless_email_field
13
+ authenticatable = authenticatable_class.where(
14
+ "lower(#{email_field}) = ?", params[:passwordless][email_field]
15
+ ).first
16
+
17
+ session = Session.new.tap do |us|
18
+ us.remote_addr = request.remote_addr
19
+ us.user_agent = request.env['HTTP_USER_AGENT']
20
+ us.authenticatable = authenticatable
21
+ end
22
+
23
+ if session.save
24
+ Mailer.magic_link(session).deliver_now
25
+ end
26
+
27
+ render
28
+ end
29
+
30
+ def show
31
+ session = Session.valid.find_by!(
32
+ authenticatable_type: params[:authenticatable],
33
+ token: params[:token]
34
+ )
35
+
36
+ sign_in! session.authenticatable
37
+
38
+ redirect_to main_app.root_path
39
+ end
40
+
41
+ def destroy
42
+ sign_out! authenticatable_class
43
+
44
+ redirect_to main_app.root_path
45
+ end
46
+
47
+ private
48
+
49
+ def authenticatable_class
50
+ params[:authenticatable].constantize
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,30 @@
1
+ module Passwordless
2
+ module ControllerHelpers
3
+
4
+ def authenticate!(authenticatable_class)
5
+ key = :"#{authenticatable_class.to_s.underscore}_id"
6
+ authenticatable_id = cookies.encrypted[key]
7
+ return unless authenticatable_id
8
+
9
+ authenticatable_class.find_by(id: authenticatable_id)
10
+ end
11
+
12
+ def sign_in!(authenticatable)
13
+ key = :"#{authenticatable.class.to_s.underscore}_id"
14
+ cookies.encrypted.permanent[key] = { value: authenticatable.id }
15
+ authenticatable
16
+ end
17
+
18
+ def sign_out!(authenticatable_class)
19
+ key = :"#{authenticatable_class.to_s.underscore}_id"
20
+ cookies.encrypted.permanent[key] = { value: nil }
21
+ cookies.delete(key)
22
+ end
23
+
24
+ # def self.included(kls)
25
+ # kls.class_eval do
26
+ # # helper_method :current_user
27
+ # end
28
+ # end
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ module Passwordless
2
+ class Mailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+
5
+ def magic_link(session)
6
+ @session = session
7
+
8
+ email_field = @session.authenticatable.class.passwordless_email_field
9
+ mail to: @session.authenticatable.send(email_field), subject: "Your magic link ✨"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module Passwordless
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,30 @@
1
+ module Passwordless
2
+ class Session < ApplicationRecord
3
+ belongs_to :authenticatable, polymorphic: true
4
+
5
+ validates \
6
+ :timeout_at,
7
+ :expires_at,
8
+ :user_agent,
9
+ :remote_addr,
10
+ :token,
11
+ presence: true
12
+
13
+ before_validation :set_defaults
14
+
15
+ scope :valid, lambda {
16
+ where('timeout_at > ? AND expires_at > ?', Time.current, Time.current)
17
+ }
18
+
19
+ private
20
+
21
+ def set_defaults
22
+ self.expires_at ||= 1.year.from_now
23
+ self.timeout_at ||= 1.hour.from_now
24
+ self.token ||= loop do
25
+ token = SecureRandom.urlsafe_base64(32)
26
+ break token unless Session.find_by(token: token)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,2 @@
1
+ Here's your link:
2
+ <%= passwordless.token_sign_in_url @session.token %>
@@ -0,0 +1 @@
1
+ <p>If we found you in the system, we've sent you an email.</p>
@@ -0,0 +1,5 @@
1
+ <%= form_for @session, url: passwordless.sign_in_path do |f| %>
2
+ <% email_field_name = :"passwordless[#{@email_field}]" %>
3
+ <%= text_field_tag email_field_name, params.fetch(email_field_name, nil) %>
4
+ <%= f.submit 'Send magic link' %>
5
+ <% end %>
data/config/routes.rb ADDED
@@ -0,0 +1,7 @@
1
+ Passwordless::Engine.routes.draw do
2
+ get '/sign_in', to: 'sessions#new', as: :sign_in
3
+ post '/sign_in', to: 'sessions#create'
4
+ get '/sign_in_create', to: 'sessions#create_test'
5
+ get '/sign_in/:token', to: 'sessions#show', as: :token_sign_in
6
+ match '/sign_out', to: 'sessions#destroy', via: %i[get delete], as: :sign_out
7
+ end
@@ -0,0 +1,18 @@
1
+ class CreatePasswordlessSessions < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :passwordless_sessions do |t|
4
+ t.belongs_to(
5
+ :authenticatable,
6
+ polymorphic: true,
7
+ index: { name: 'authenticatable' }
8
+ )
9
+ t.datetime :timeout_at, null: false
10
+ t.datetime :expires_at, null: false
11
+ t.text :user_agent, null: false
12
+ t.string :remote_addr, null: false
13
+ t.string :token, null: false
14
+
15
+ t.timestamps
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ require "passwordless/engine"
2
+
3
+ module Passwordless
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,5 @@
1
+ module Passwordless
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Passwordless
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module Passwordless
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :passwordless do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: passwordless
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mikkel Malmberg
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.1.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - mikkel@brnbw.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - app/controllers/passwordless/application_controller.rb
52
+ - app/controllers/passwordless/sessions_controller.rb
53
+ - app/lib/passwordless/controller_helpers.rb
54
+ - app/mailers/passwordless/mailer.rb
55
+ - app/models/passwordless/application_record.rb
56
+ - app/models/passwordless/session.rb
57
+ - app/views/passwordless/mailer/magic_link.text.erb
58
+ - app/views/passwordless/sessions/create.html.erb
59
+ - app/views/passwordless/sessions/new.html.erb
60
+ - config/routes.rb
61
+ - db/migrate/20171104221735_create_passwordless_sessions.rb
62
+ - lib/passwordless.rb
63
+ - lib/passwordless/engine.rb
64
+ - lib/passwordless/version.rb
65
+ - lib/tasks/passwordless_tasks.rake
66
+ homepage: https://github.com/mikker/passwordless
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.6.13
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Add authentication to your Rails app without all the icky-ness of passwords.
90
+ test_files: []