simple_jwt_auth 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a5a1802af998b4df147f2580d95a5a0887f8f9eecbe1b35e6fd46a202afe7827
4
+ data.tar.gz: bc5d1fa7f4f92b1517e13d4dc77918272dd1218e97229bfd24296094708248d5
5
+ SHA512:
6
+ metadata.gz: 1c374ffc47c8eb076300aebc7c006386b786f75349775dcd39d9684400c6017ab8f7280e8d1e4a432d2fdf1d115fec232112b429b0dfdac01eec286196c51f88
7
+ data.tar.gz: 62ad01454f025183264ce3991bdfadba0ff895ca545305843f06115316a52ae0d45766b918940633b7329edc64d9fa6acb36287a50fea0095da4143cb021a186
@@ -0,0 +1,20 @@
1
+ Copyright 2019 brye
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.
@@ -0,0 +1,44 @@
1
+ # SimpleJwtAuth
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'simple_jwt_auth'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install simple_jwt_auth
22
+ ```
23
+
24
+ in order to create updated migrations run
25
+ ```bash
26
+ $ rails simple_jwt_auth:install:migrations
27
+ ```
28
+
29
+ then run
30
+ ```bash
31
+ $ rails db:migrate
32
+ ```
33
+
34
+ in order to access gems provided routes add
35
+
36
+ mount SimpleJwtAuth::Engine, at: "/auth"
37
+
38
+ to top of routes in your rails project
39
+
40
+ ## Contributing
41
+ Contribution directions go here.
42
+
43
+ ## License
44
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,19 @@
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 = 'SimpleJwtAuth'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ load 'rails/tasks/statistics.rake'
18
+
19
+ require 'bundler/gem_tasks'
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/simple_jwt_auth .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,5 @@
1
+ module SimpleJwtAuth
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :null_session
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+ require_dependency "simple_jwt_auth/application_controller"
2
+ require_dependency "jwt"
3
+
4
+ module SimpleJwtAuth
5
+ class SessionsController < ApplicationController
6
+ def create
7
+ user = User.find_by(email: session_params[:email])
8
+ if user && user.authenticate(session_params[:password])
9
+ jwt = JWT.encode(
10
+ {
11
+ user_id: user.id, # the data to encode
12
+ exp: 24.hours.from_now.to_i # the expiration time
13
+ },
14
+ Rails.application.credentials.fetch(:secret_key_base), # the secret key
15
+ "HS256" # the encryption algorithm
16
+ )
17
+ render json: { jwt: jwt, email: user.email, user_id: user.id }, status: :created
18
+ else
19
+ render json: {}, status: :unauthorized
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def session_params
26
+ params
27
+ .require(:session)
28
+ .permit(:email, :password)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,25 @@
1
+ require_dependency "simple_jwt_auth/application_controller"
2
+
3
+ module SimpleJwtAuth
4
+ class UsersController < ApplicationController
5
+ wrap_parameters :user, include: [:name, :email, :password, :password_confirmation]
6
+
7
+ def create
8
+ user = User.new(user_params)
9
+
10
+ if user.save
11
+ render json: {message: "User created successfully"}, status: :created
12
+ else
13
+ render json: {errors: user.errors.full_messages}, status: :bad_request
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def user_params
20
+ params
21
+ .require(:user)
22
+ .permit(:name, :email, :password, :password_confirmation)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,29 @@
1
+ require_dependency "jwt"
2
+
3
+ module SimpleJwtAuth
4
+ module ApplicationHelper
5
+ def current_user
6
+ auth_headers = request.headers["Authorization"]
7
+ if auth_headers.present? && auth_headers[/(?<=\A(Bearer ))\S+\z/]
8
+ token = auth_headers[/(?<=\A(Bearer ))\S+\z/]
9
+ begin
10
+ decoded_token = JWT.decode(
11
+ token,
12
+ Rails.application.credentials.fetch(:secret_key_base),
13
+ true,
14
+ { algorithm: "HS256" }
15
+ )
16
+ User.find_by(id: decoded_token[0]["user_id"])
17
+ rescue JWT::ExpiredSignature
18
+ nil
19
+ end
20
+ end
21
+ end
22
+
23
+ def authenticate_user
24
+ unless current_user
25
+ render json: {}, status: :unauthorized
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ module SimpleJwtAuth
2
+ module SessionsHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module SimpleJwtAuth
2
+ module UsersHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module SimpleJwtAuth
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module SimpleJwtAuth
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module SimpleJwtAuth
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ module SimpleJwtAuth
2
+ class User < ApplicationRecord
3
+ has_secure_password
4
+ validates :email, presence: true, uniqueness: true
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Simple jwt auth</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "simple_jwt_auth/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
@@ -0,0 +1,4 @@
1
+ SimpleJwtAuth::Engine.routes.draw do
2
+ resources :users, only: [:create]
3
+ resources :sessions, only: [:create]
4
+ end
@@ -0,0 +1,11 @@
1
+ class CreateSimpleJwtAuthUsers < ActiveRecord::Migration[6.0]
2
+ def change
3
+ create_table :simple_jwt_auth_users do |t|
4
+ t.string :name
5
+ t.string :email
6
+ t.string :password_digest
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ require "simple_jwt_auth/engine"
2
+
3
+ module SimpleJwtAuth
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,8 @@
1
+ module SimpleJwtAuth
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace SimpleJwtAuth
4
+ initializer "actualize_auth.load_helpers" do |app|
5
+ ActionController::Base.send :include, SimpleJwtAuth::Engine.helpers
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleJwtAuth
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :simple_jwt_auth do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_jwt_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - brye
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-22 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: 6.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: bcrypt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.1.7
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.1.7
41
+ - !ruby/object:Gem::Dependency
42
+ name: jwt
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: 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: JWT based authorization that includes current_user helper
70
+ email:
71
+ - bryewalks@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - app/assets/config/simple_jwt_auth_manifest.js
80
+ - app/assets/stylesheets/simple_jwt_auth/application.css
81
+ - app/assets/stylesheets/simple_jwt_auth/sessions.css
82
+ - app/assets/stylesheets/simple_jwt_auth/users.css
83
+ - app/controllers/simple_jwt_auth/application_controller.rb
84
+ - app/controllers/simple_jwt_auth/sessions_controller.rb
85
+ - app/controllers/simple_jwt_auth/users_controller.rb
86
+ - app/helpers/simple_jwt_auth/application_helper.rb
87
+ - app/helpers/simple_jwt_auth/sessions_helper.rb
88
+ - app/helpers/simple_jwt_auth/users_helper.rb
89
+ - app/jobs/simple_jwt_auth/application_job.rb
90
+ - app/mailers/simple_jwt_auth/application_mailer.rb
91
+ - app/models/simple_jwt_auth/application_record.rb
92
+ - app/models/simple_jwt_auth/user.rb
93
+ - app/views/layouts/simple_jwt_auth/application.html.erb
94
+ - config/routes.rb
95
+ - db/migrate/20191222061616_create_simple_jwt_auth_users.rb
96
+ - lib/simple_jwt_auth.rb
97
+ - lib/simple_jwt_auth/engine.rb
98
+ - lib/simple_jwt_auth/version.rb
99
+ - lib/tasks/simple_jwt_auth_tasks.rake
100
+ homepage: https://github.com/bryewalks/simple_auth
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubygems_version: 3.0.6
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: JWT based authorization
123
+ test_files: []