knock_once 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3e1ee1a4bfcaa9e4488162846e441581bb26c26e
4
+ data.tar.gz: 21858d81ee43780b5df9cdd4edeed77596e02d20
5
+ SHA512:
6
+ metadata.gz: ab78c146dc10fdfff5bf338bf56a34dbcb78b26a8106e91228ca5d5ce5ad8d2bc70764e45367ecc5124acb58dadc6932e8a454f1e82b3f504eb3f3936b4d0f62
7
+ data.tar.gz: 5e26f6aafe38cb4b0e9cd66fe965ecdc6225f2878c6aa571f2507cddd165a1f64ab364de5f887c2a0979da7aed533a57a5eb98c6ec750ffdf3712f0329aaeb9f
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Nicholas Shirley
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,43 @@
1
+ # KnockOnce
2
+ knock_once is a user auth gem built on top of [knock](https://github.com/nsarno/knock). The goals of knock_once are twofold:
3
+ * Provide the minimum functionality required to register, authenticate, and reset user passwords. In this sense, minimum means only basic functionality which should apply to the largest number of users.
4
+ * Provide easy extensibility and customization. Because the first goal is to provide only basic auth, it's expected that all or most users will want to add or modify functionality and development choices in this gem should be made with this in mind.
5
+
6
+ Though it is not released now, it is planned to have additional functionality available either here or through additional gems (e.g. confirming users, lockable accounts...). The goal here is to provide much of the functionality available in other packages, such as [devise](https://github.com/plataformatec/devise) and [devise_token_auth](https://github.com/lynndylanhurley/devise_token_auth), using JWT.
7
+
8
+ ## Installation
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'knock_once'
13
+ ```
14
+
15
+ And then execute:
16
+ ```bash
17
+ $ bundle
18
+ ```
19
+
20
+ Or install it yourself as:
21
+ ```bash
22
+ $ gem install knock_once
23
+ ```
24
+
25
+ Mount the gem in our `routes.rb` file:
26
+
27
+ ```ruby
28
+ mount KnockOnce::Engine, at: '/auth'
29
+ ```
30
+
31
+ Then run generators to generate migration
32
+ ```
33
+ rails g knock_once:install
34
+ ```
35
+
36
+ ## Current state
37
+ The gem has been extracted from a test project and as such has very little configurability (but it works at least within those parameters). The primary next steps are to add user configuration options, additional documentation, add tests for current functionality and improve the generators.
38
+
39
+ ## Contributing
40
+ Pull requests are very welcome.
41
+
42
+ ## License
43
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/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 = 'KnockOnce'
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 KnockOnce
2
+ class ApplicationController < ActionController::API
3
+ include Knock::Authenticable
4
+ end
5
+ end
@@ -0,0 +1,75 @@
1
+ require_dependency "knock_once/application_controller"
2
+
3
+ module KnockOnce
4
+ class PasswordsController < ApplicationController
5
+ before_action :authenticate_user, except: [:create, :edit, :validate]
6
+ include ActiveModel::SecurePassword
7
+
8
+ def create
9
+ @user = User.find_by_email(params[:email])
10
+ # if valid user
11
+ if @user
12
+ # generate a new token and save
13
+ Password.save_token_and_expiry(@user)
14
+ Password.email_reset(@user.email)
15
+ render status: 200, json: {
16
+ message: 'Your request has been received. If we have an email matching that account you will receive link to reset your password.'
17
+ }
18
+ # if invalid user
19
+ else
20
+ render status: 200, json: {
21
+ message: 'Your request has been received. If we have an email matching that account you will receive link to reset your password.'
22
+ }
23
+ end
24
+ end
25
+
26
+ def validate
27
+ @token = params[:token]
28
+ @user = User.find_by_password_reset_token(@token)
29
+ if @user && Time.now < @user.password_token_expiry
30
+ render status: 202
31
+ else
32
+ render status: 404, json: { message: 'Looks like something went wrong' }
33
+ end
34
+ end
35
+
36
+ def edit
37
+ @token = params[:token]
38
+ @user = User.find_by_password_reset_token(@token)
39
+
40
+ if @user && Time.now < @user.password_token_expiry
41
+ if @user.update(password: params[:password], password_confirmation: params[:password_confirmation])
42
+ render status: 200, json: { message: 'Your password has been updated' }
43
+ # delete token and exiry on successful update
44
+ @user.update(password_reset_token: nil, password_token_expiry: nil)
45
+ else
46
+ render status: :unprocessable_entity, json: @user.errors.full_messages
47
+ end
48
+ else
49
+ render status: :expectation_failed, json: { message: 'Looks like something went wrong' }
50
+ end
51
+ end
52
+
53
+ def update
54
+ @user = current_user
55
+ if @user.authenticate(params[:current_password])
56
+ if @user.update(password_params)
57
+ render json: {
58
+ user: @user,
59
+ message: 'Your password has been udpated!'
60
+ }
61
+ else
62
+ render json: @user.errors.full_messages, status: :unprocessable_entity
63
+ end
64
+ else
65
+ render status: :unprocessable_entity, json: ['Current password is incorrect']
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def password_params
72
+ params.permit(:password, :password_confirmation, :current_password, :email, :token)
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,4 @@
1
+ module KnockOnce
2
+ class UserTokenController < Knock::AuthTokenController
3
+ end
4
+ end
@@ -0,0 +1,53 @@
1
+ require_dependency "knock_once/application_controller"
2
+
3
+ module KnockOnce
4
+ class UsersController < ApplicationController
5
+ before_action :authenticate_user, except: [:create]
6
+ include ActiveModel::SecurePassword
7
+
8
+ def show
9
+ @user = current_user
10
+ render json: @user
11
+ end
12
+
13
+ def update
14
+ @user = current_user
15
+ if @user.authenticate(params[:current_password])
16
+ if @user.update(user_params)
17
+ render json: {
18
+ user: @user,
19
+ message: 'Your profile has been updated!'
20
+ }
21
+ else
22
+ render json: @user.errors.full_messages, status: :unprocessable_entity
23
+ end
24
+ else
25
+ render status: :unprocessable_entity, json: ['Current password is incorrect']
26
+ end
27
+ end
28
+
29
+ def destroy
30
+ @user = current_user
31
+ if @user.destroy
32
+ render json: :success
33
+ else
34
+ render json: @user.errors.full_messages
35
+ end
36
+ end
37
+
38
+ def create
39
+ @user = User.new(user_params)
40
+ if @user.save
41
+ render json: @user
42
+ else
43
+ render json: @user.errors.full_messages, status: :unprocessable_entity
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def user_params
50
+ params.permit(:user, :user_name, :email, :current_password, :password, :password_confirmation)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,4 @@
1
+ module KnockOnce
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module KnockOnce
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,14 @@
1
+ module KnockOnce
2
+ class PasswordResetMailer < ApplicationMailer
3
+ default from: 'no-reply@sporkbook.com'
4
+
5
+ def password_reset(user, token)
6
+ @user = user
7
+ @token = token
8
+
9
+ mail(
10
+ to: @user,
11
+ subject: 'Forgot password request from SporkBook')
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ module KnockOnce
2
+ class UserMailer < ApplicationMailer
3
+ def welcome_mailer(user)
4
+ @user = user
5
+ mail(to: @user.email,
6
+ subject: 'Welcome to Sporkbook!') do |format|
7
+ format.html { render 'welcome_mailer' }
8
+ format.text { render 'welcome_mailer' }
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module KnockOnce
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ module KnockOnce
2
+ class Password < ApplicationRecord
3
+ attr_accessor :token
4
+ attr_accessor :user
5
+
6
+ def self.generate_reset_token
7
+ @token = SecureRandom.urlsafe_base64(18, false)
8
+ end
9
+
10
+ def self.save_token_and_expiry(user)
11
+ @user = user
12
+ generate_reset_token
13
+ User.find_by_email(@user['email']).update_attributes(password_reset_token: @token, password_token_expiry: 1.hour.from_now)
14
+ end
15
+
16
+ def self.email_reset(email)
17
+ PasswordResetMailer.password_reset(@user, @token).deliver_now
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,38 @@
1
+ module KnockOnce
2
+ class User < ApplicationRecord
3
+ # Explicitly set the table name so users can use non-namespaced table
4
+ self.table_name = 'users'
5
+
6
+ # these allow passing non-DB params for different situations
7
+ attr_accessor :current_password
8
+ attr_accessor :token
9
+
10
+ has_secure_password
11
+
12
+ before_save { email.downcase! }
13
+
14
+ valid_email_regex = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
15
+
16
+ validates :email,
17
+ presence: true,
18
+ length: { maximum: 255 },
19
+ format: { with: valid_email_regex },
20
+ uniqueness: { case_sensitive: false }
21
+ validates :password_digest,
22
+ presence: true
23
+ validates :password,
24
+ presence: true,
25
+ length: { minimum: 8 },
26
+ allow_nil: true
27
+
28
+
29
+
30
+ # Send additional user info as payload for front end
31
+ def to_token_payload
32
+ {
33
+ sub: id,
34
+ email: email
35
+ }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,18 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
5
+ </head>
6
+ <body>
7
+ <h1>Password reset request</h1>
8
+ <p>
9
+ This is your requested token:
10
+
11
+ <h2><%= @token %></h2>
12
+ </p>
13
+
14
+ <H2>
15
+ <a href="http://localhost:8080/#/reset">Click here to reset your password</a>
16
+ </H2>
17
+ </body>
18
+ </html>
@@ -0,0 +1 @@
1
+ Welcome <%= @user.email %>!
@@ -0,0 +1 @@
1
+ Welcome <%= @user.email %>!
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
+ <style>
6
+ /* Email styles need to be inline */
7
+ </style>
8
+ </head>
9
+
10
+ <body>
11
+ <%= yield %>
12
+ </body>
13
+ </html>
@@ -0,0 +1 @@
1
+ <%= yield %>
@@ -0,0 +1,61 @@
1
+ require 'knock'
2
+
3
+ Knock.setup do |config|
4
+
5
+ ## Expiration claim
6
+ ## ----------------
7
+ ##
8
+ ## How long before a token is expired. If nil is provided, token will
9
+ ## last forever.
10
+ ##
11
+ ## Default:
12
+ # config.token_lifetime = 1.day
13
+
14
+
15
+ ## Audience claim
16
+ ## --------------
17
+ ##
18
+ ## Configure the audience claim to identify the recipients that the token
19
+ ## is intended for.
20
+ ##
21
+ ## Default:
22
+ # config.token_audience = nil
23
+
24
+ ## If using Auth0, uncomment the line below
25
+ # config.token_audience = -> { Rails.application.secrets.auth0_client_id }
26
+
27
+ ## Signature algorithm
28
+ ## -------------------
29
+ ##
30
+ ## Configure the algorithm used to encode the token
31
+ ##
32
+ ## Default:
33
+ # config.token_signature_algorithm = 'HS256'
34
+
35
+ ## Signature key
36
+ ## -------------
37
+ ##
38
+ ## Configure the key used to sign tokens.
39
+ ##
40
+ ## Default:
41
+ # config.token_secret_signature_key = -> { Rails.application.secrets.secret_key_base }
42
+
43
+ ## If using Auth0, uncomment the line below
44
+ # config.token_secret_signature_key = -> { JWT.base64url_decode Rails.application.secrets.auth0_client_secret }
45
+
46
+ ## Public key
47
+ ## ----------
48
+ ##
49
+ ## Configure the public key used to decode tokens, if required.
50
+ ##
51
+ ## Default:
52
+ # config.token_public_key = nil
53
+
54
+ ## Exception Class
55
+ ## ---------------
56
+ ##
57
+ ## Configure the exception to be used when user cannot be found.
58
+ ##
59
+ ## Default:
60
+ # config.not_found_exception_class_name = 'ActiveRecord::RecordNotFound'
61
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,17 @@
1
+ KnockOnce::Engine.routes.draw do
2
+ post '/user_token', to: 'user_token#create'
3
+
4
+ resource :users
5
+
6
+ ## Password routes
7
+
8
+ # password udpate (user knows current password)
9
+ patch '/passwords', to: 'passwords#update'
10
+ put '/passwords', to: 'passwords#update'
11
+
12
+ # password reset routes (user doesn't know current password)
13
+ post '/passwords/reset', to: 'passwords#create'
14
+ patch '/passwords/reset', to: 'passwords#edit'
15
+ put '/passwords/reset', to: 'passwords#edit'
16
+ post 'passwords/validate', to: 'passwords#validate'
17
+ end
@@ -0,0 +1,26 @@
1
+ module KnockOnce
2
+ class InstallGenerator < Rails::Generators::Base
3
+ include Rails::Generators::Migration
4
+
5
+ source_root File.expand_path("../../../templates", __FILE__)
6
+
7
+ def create_user_model
8
+ copy_file('user_model.rb', 'app/models/user.rb')
9
+ end
10
+
11
+ def copy_migrations
12
+ if self.class.migration_exists?("db/migrate", "create_knock_once_users")
13
+ say_status("skipped", "Migration create_knock_once_users already exists")
14
+ else
15
+ migration_template("create_knock_once_users.rb.erb", "db/migrate/create_knock_once_users.rb")
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ # Use to assign migration time otherwise generator will error
22
+ def self.next_migration_number(dir)
23
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,8 @@
1
+ require 'knock'
2
+
3
+ module KnockOnce
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace KnockOnce
6
+ config.generators.api_only = true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module KnockOnce
2
+ VERSION = '0.2.0'
3
+ end
data/lib/knock_once.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "knock_once/engine"
2
+
3
+ module KnockOnce
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :knock_once do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,18 @@
1
+ class CreateKnockOnceUsers < ActiveRecord::Migration<%= "[#{Rails::VERSION::STRING[0..2]}]" if Rails::VERSION::MAJOR > 4 %>
2
+ def change
3
+ create_table :users do |t|
4
+ # Required
5
+ t.string :email
6
+ t.string :password_digest
7
+ t.string :password_reset_token
8
+ t.datetime :password_token_expiry
9
+
10
+ # Add your custom user fields are required
11
+ # TODO add whitelist instructions here
12
+
13
+ t.timestamps
14
+ end
15
+
16
+ add_index :users, :email, unique: true
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ class User < KnockOnce::User
2
+
3
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knock_once
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicholas Shirley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-13 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: knock
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: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: pg
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: knock_once provides basic user email auth using knock.
70
+ email:
71
+ - nicholas@reallymy.email
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - app/controllers/knock_once/application_controller.rb
80
+ - app/controllers/knock_once/passwords_controller.rb
81
+ - app/controllers/knock_once/user_token_controller.rb
82
+ - app/controllers/knock_once/users_controller.rb
83
+ - app/jobs/knock_once/application_job.rb
84
+ - app/mailers/knock_once/application_mailer.rb
85
+ - app/mailers/knock_once/password_reset_mailer.rb
86
+ - app/mailers/knock_once/user_mailer.rb
87
+ - app/models/knock_once/application_record.rb
88
+ - app/models/knock_once/password.rb
89
+ - app/models/knock_once/user.rb
90
+ - app/views/knock_once/password_reset_mailer/password_reset.html.erb
91
+ - app/views/knock_once/user_mailer/welcome_mailer.html.erb
92
+ - app/views/knock_once/user_mailer/welcome_mailer.text.erb
93
+ - app/views/layouts/knock_once/mailer.html.erb
94
+ - app/views/layouts/knock_once/mailer.text.erb
95
+ - config/initializers/knock.rb
96
+ - config/routes.rb
97
+ - lib/generators/knock_once/install_generator.rb
98
+ - lib/knock_once.rb
99
+ - lib/knock_once/engine.rb
100
+ - lib/knock_once/version.rb
101
+ - lib/tasks/knock_once_tasks.rake
102
+ - lib/templates/create_knock_once_users.rb.erb
103
+ - lib/templates/user_model.rb
104
+ homepage: https://github.com/nicholasshirley/knock_once
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.6.11
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: A basic API user authorization engine built on knock.
128
+ test_files: []