rails_jwt_admin 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: fa0138f72063844bfe41d165ae7e9f767f05ac7ba449101e83f2b833d524026e
4
+ data.tar.gz: 9c9300c0eac1f2d816ca28dbae9bb77e6d6557a26716b924040c343a8e000832
5
+ SHA512:
6
+ metadata.gz: bdd8f063c72799549be709f767c2b5b8adfa4669fa8ed59a660209238097e0e0fa417ccef57fbffaea41772baf3a9de2e52305a8927e50eed54af20cb99e00e2
7
+ data.tar.gz: c051482415d9200d3ac53d3942e4fcd468bc8fe8c5632b0b5391d8d9c393561783926476a9322da5505fa490427f71613bff8d511549f21fe6923e82d2a5c3ba
@@ -0,0 +1,20 @@
1
+ Copyright 2020 afeiship
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,19 @@
1
+ # rails_jwt_admin
2
+ > Jwt for rails admin.
3
+
4
+ ## installation
5
+ ```shell
6
+ # gem
7
+ gem install 'rails_jwt_admin'
8
+
9
+ # rails - bundle install
10
+ gem 'rails_jwt_admin'
11
+ ```
12
+
13
+ ## resources
14
+ - https://edgeapi.rubyonrails.org/classes/Rails/Engine.html
15
+ - https://guides.rubyonrails.org/engines.html
16
+ - https://www.jianshu.com/p/56467f890516
17
+ - https://www.pluralsight.com/guides/token-based-authentication-with-ruby-on-rails-5-api
18
+ - https://github.com/jwt/ruby-jwt
19
+ - https://github.com/afeiship/rails-module-jwt
@@ -0,0 +1,32 @@
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 = 'RailsJwtAdmin'
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", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1,34 @@
1
+ module RailsJwtAdmin
2
+ class ApplicationController < ActionController::API
3
+ attr_accessor :current_user
4
+
5
+ protected
6
+
7
+ def authenticate!
8
+ render_failed and return unless token?
9
+ @current_user = User.find_by(id: auth_token[:user_id])
10
+ rescue JWT::VerificationError, JWT::DecodeError
11
+ render_failed
12
+ end
13
+
14
+ private
15
+
16
+ def render_failed(messages = ["Authenticate failed."])
17
+ render json: { errors: messages }, status: :unauthorized
18
+ end
19
+
20
+ def http_token
21
+ @http_token ||= if request.headers["Authorization"].present?
22
+ request.headers["Authorization"].split(" ").last
23
+ end
24
+ end
25
+
26
+ def auth_token
27
+ @auth_token ||= Token.decode(http_token)
28
+ end
29
+
30
+ def token?
31
+ http_token && auth_token && auth_token[:user_id].to_i
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,13 @@
1
+ require_dependency "rails_jwt_admin/application_controller"
2
+
3
+ module RailsJwtAdmin
4
+ class AuthenticationController < ApplicationController
5
+ def create
6
+ if user = User.find_by(username: params[:username]).try(:authenticate, params[:password])
7
+ render json: user.token
8
+ else
9
+ render json: { errors: ["Username or password error."] }, status: :unauthorized
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ require_dependency "rails_jwt_admin/application_controller"
2
+
3
+ module RailsJwtAdmin
4
+ class UsersController < ApplicationController
5
+ before_action :authenticate!
6
+
7
+ def profile
8
+ render json: current_user.to_json
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module RailsJwtAdmin
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ module RailsJwtAdmin
2
+ class User < ApplicationRecord
3
+ has_secure_password
4
+
5
+ def token
6
+ { token: Token.encode(user_id: self.id) }
7
+ end
8
+
9
+ def to_json
10
+ self.slice(:username, :email)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,4 @@
1
+ RailsJwtAdmin::Engine.routes.draw do
2
+ get "profile", to: "users#profile"
3
+ resources :authentication, only: :create
4
+ end
@@ -0,0 +1,11 @@
1
+ class CreateRailsJwtAdminUsers < ActiveRecord::Migration[6.0]
2
+ def change
3
+ create_table :rails_jwt_admin_users do |t|
4
+ t.string :username
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 "rails_jwt_admin/engine"
2
+
3
+ module RailsJwtAdmin
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,7 @@
1
+ module RailsJwtAdmin
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace RailsJwtAdmin
4
+ config.generators.api_only = true
5
+ config.autoload_paths += Dir["#{config.root}/lib/vendors"]
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module RailsJwtAdmin
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :rails_jwt_admin do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,15 @@
1
+ require "rails_jwt_admin/engine"
2
+
3
+ module RailsJwtAdmin
4
+ class Token
5
+ def self.encode(payload)
6
+ JWT.encode(payload, Rails.application.secrets.secret_key_base)
7
+ end
8
+
9
+ def self.decode(token)
10
+ HashWithIndifferentAccess.new(JWT.decode(token, Rails.application.secrets.secret_key_base)[0])
11
+ rescue
12
+ nil
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_jwt_admin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - afeiship
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-12-02 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.3
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 6.0.3.4
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 6.0.3
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 6.0.3.4
33
+ - !ruby/object:Gem::Dependency
34
+ name: sqlite3
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description:
48
+ email:
49
+ - 1290657123@qq.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - MIT-LICENSE
55
+ - README.md
56
+ - Rakefile
57
+ - app/controllers/rails_jwt_admin/application_controller.rb
58
+ - app/controllers/rails_jwt_admin/authentication_controller.rb
59
+ - app/controllers/rails_jwt_admin/users_controller.rb
60
+ - app/models/rails_jwt_admin/application_record.rb
61
+ - app/models/rails_jwt_admin/user.rb
62
+ - config/routes.rb
63
+ - db/migrate/20201202013006_create_rails_jwt_admin_users.rb
64
+ - lib/rails_jwt_admin.rb
65
+ - lib/rails_jwt_admin/engine.rb
66
+ - lib/rails_jwt_admin/version.rb
67
+ - lib/tasks/rails_jwt_admin_tasks.rake
68
+ - lib/vendors/token.rb
69
+ homepage: https://github.com/afeiship/rails_jwt_admin
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.0.3
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Summary of RailsJwtAdmin.
92
+ test_files: []