rails_jwt_admin 0.1.6 → 0.1.7

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: f2f5f5a86b728dd53e5e8818c0c717d125eb3e1c0e65afb3762e6d1785010e60
4
- data.tar.gz: 0ea73870bb73f4ebf65cd4457ccd5d99f087c2ea4c49f5d8d07bf74a54d0083c
3
+ metadata.gz: 12e5b16df86e414638bf8f1ddc1f99caa51c243a89edf567ce3434954b711e4d
4
+ data.tar.gz: 1b7f6b9d4423e633b227998ff60afafe894086dcfb90d6374cf413f3eb048c4c
5
5
  SHA512:
6
- metadata.gz: 805b8f195f3a53f81930de9f5b9bf782fa4d763e1ff4700e5a7470a627d97c61e4e50126a2b4d6cf5e012785d895d2293ed448e2a92bfb8078ef24f78b33a7b6
7
- data.tar.gz: 1fae5650da230718930586ad7e4b2604287f2bf60565011dcbfdff7e67641ae47e9a8ed47a52a73420e22f32bc9eb57c645e1d0d45180961fd9dd4f3f501ec6f
6
+ metadata.gz: aed76e5d45e8ab3ab6d6b504c8d3169c0b44ccf4ecae6813716a5bdd08ab9b5d17f26ee28a7e3bcd36012a6185ee34593e990a5deadde037a449e629d1e80d09
7
+ data.tar.gz: ac93744aa679de8942ef3c49350370cc855d547e078d5a3c2f1f417e2f02cd14cc1547bbcebd495efcda8f9b4d62eae5b375d4a9b85a48809138a5ac0f6c21e7
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2020 afeiship
1
+ Copyright aric.zheng
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,33 +1,128 @@
1
- # rails_jwt_admin
2
- > Jwt for rails admin.
1
+ # RailsJwtAdmin
2
+ > JWT-based authentication system for Rails admin panel.
3
3
 
4
- ## installation
5
- ```shell
6
- # 1. add gen & bundle install
7
- gem 'rails_jwt_admin'
4
+ ## Features
5
+ - JWT token-based authentication
6
+ - Admin user management
7
+ - Secure API responses with consistent format
8
+ - Easy installation and configuration
8
9
 
9
- # 2. install admin
10
+ ## Installation
11
+
12
+ 1. Add the required gems to your Gemfile(If not already added):
13
+ ```ruby
14
+ # decode/encode methods
15
+ gem "jwt"
16
+ gem "bcrypt"
17
+
18
+ # normalize response
19
+ gem "rails_warp"
20
+ gem "rails_jwt_admin"
21
+ ```
22
+
23
+ 2. Run bundle install:
24
+ ```bash
25
+ bundle install
26
+ ```
27
+
28
+ 3. Install the admin:
29
+ ```bash
10
30
  rails g rails_jwt_admin:install
31
+ ```
11
32
 
12
- # 3. check table & rake db:migrate
13
- rake db:migrate
33
+ 4. Run database migration:
34
+ ```bash
35
+ rails db:migrate
14
36
  ```
15
37
 
16
- ## seed
17
- ```rb
38
+ ## Configuration
39
+
40
+ 1. Generate and edit your application credentials:
41
+ ```bash
42
+ EDITOR=vim rails credentials:edit
43
+ ```
44
+
45
+ 2. Add your JWT secret key:
46
+ ```yaml
47
+ jwt_secret: "your_secret_key_here"
48
+ ```
49
+
50
+ ## Setup Admin User
51
+
52
+ Create an initial admin user:
53
+ ```ruby
18
54
  RailsJwtAdmin::User.create(
19
- username: "admin",
20
- email: "example@qq.com",
21
- password: "123123",
22
- password_confirmation: "123123"
55
+ username: "admin",
56
+ email: "admin@example.com",
57
+ password: "your_secure_password",
58
+ password_confirmation: "your_secure_password"
23
59
  )
24
60
  ```
25
61
 
26
- ## resources
27
- - https://edgeapi.rubyonrails.org/classes/Rails/Engine.html
28
- - https://guides.rubyonrails.org/engines.html
29
- - https://www.jianshu.com/p/56467f890516
30
- - https://www.pluralsight.com/guides/token-based-authentication-with-ruby-on-rails-5-api
31
- - https://github.com/jwt/ruby-jwt
32
- - https://github.com/afeiship/rails-module-jwt
33
- - https://github.com/afeiship/rails_admin_users
62
+ ## API Usage
63
+ - POST `/rails_jwt_admin/auth` - Authenticate and get token
64
+ - GET `/rails_jwt_admin/me` - Get current user info
65
+
66
+ ### Authentication
67
+ Send POST request to `/rails_jwt_admin/auth` with:
68
+ - `username` - Admin username
69
+ - `password` - Admin password
70
+
71
+ ### Response Format
72
+ Successful authentication returns:
73
+ ```json
74
+ {
75
+ "success": true,
76
+ "code": 200,
77
+ "message": null,
78
+ "data": {
79
+ "token": "eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxfQ..."
80
+ }
81
+ }
82
+ ```
83
+
84
+ Get current user returns:
85
+ ```json
86
+ {
87
+ "success": true,
88
+ "code": 200,
89
+ "message": null,
90
+ "data": {
91
+ "id": 1,
92
+ "username": "admin",
93
+ "email": "example@qq.com"
94
+ }
95
+ }
96
+ ```
97
+
98
+ Failed authentication returns:
99
+ ```json
100
+ {
101
+ "success": false,
102
+ "code": 401,
103
+ "message": "Authentication failed",
104
+ "data": null
105
+ }
106
+ ```
107
+
108
+ ### Authorization
109
+ Include the token in your requests using the Authorization header:
110
+ ```
111
+ Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxfQ...
112
+ ```
113
+
114
+ ## Security Considerations
115
+
116
+ - Use a strong, unique JWT secret key
117
+ - Consider token expiration times for security
118
+ - Store sensitive credentials using Rails encrypted credentials
119
+ - Regularly update dependencies for security patches
120
+ - Follow Rails security best practices for session management and CSRF protection
121
+
122
+ ## Development Resources
123
+ - [Rails Engines Guide](https://guides.rubyonrails.org/engines.html)
124
+ - [JWT Ruby Implementation](https://github.com/jwt/ruby-jwt)
125
+ - [Rails Security Guide](https://guides.rubyonrails.org/security.html)
126
+
127
+ ## License
128
+ This gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,32 +1,6 @@
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
1
+ require "bundler/setup"
16
2
 
17
3
  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
4
+ load "rails/tasks/engine.rake"
31
5
 
32
- task default: :test
6
+ require "bundler/gem_tasks"
@@ -13,8 +13,9 @@ module RailsJwtAdmin
13
13
 
14
14
  private
15
15
 
16
- def render_failed(messages = ["Authenticate failed."])
17
- render json: { errors: messages }, status: :unauthorized
16
+ def render_failed(message = "Authenticate failed.")
17
+ # render json: { errors: messages }, status: :unauthorized
18
+ fail(message: message, code: 401)
18
19
  end
19
20
 
20
21
  def http_token
@@ -4,9 +4,11 @@ module RailsJwtAdmin
4
4
  class AuthenticationController < ApplicationController
5
5
  def create
6
6
  if user = User.find_by(username: params[:username]).try(:authenticate, params[:password])
7
- render json: user.token
7
+ # render json: user.token
8
+ ok(data: user.token)
8
9
  else
9
- render json: { errors: ["Username or password error."] }, status: :unauthorized
10
+ # render json: { errors: ["Username or password error."] }, status: :unauthorized
11
+ fail(errors: ["Username or password error."], code: 401)
10
12
  end
11
13
  end
12
14
  end
@@ -4,8 +4,10 @@ module RailsJwtAdmin
4
4
  class UsersController < ApplicationController
5
5
  before_action :authenticate!
6
6
 
7
- def profile
8
- render json: current_user.to_json
7
+ # 取当前用户信息
8
+ def me
9
+ data = current_user.as_json(only: [:id, :username, :email])
10
+ ok(data: data)
9
11
  end
10
12
  end
11
13
  end
data/config/routes.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  RailsJwtAdmin::Engine.routes.draw do
2
- get "profile", to: "users#profile"
3
- resources :authentication, only: :create
2
+ post "auth", to: "authentication#create"
3
+ get "me", to: "users#me"
4
4
  end
@@ -1,11 +1,12 @@
1
1
  class CreateRailsJwtAdminUsers < ActiveRecord::Migration[6.0]
2
2
  def change
3
3
  create_table :rails_jwt_admin_users do |t|
4
- t.string :username
5
- t.string :email
6
- t.string :password_digest
4
+ t.string :username, null: false, index: { unique: true }
5
+ t.string :email, null: false, index: { unique: true }
6
+ t.string :password_digest, null: false
7
7
 
8
8
  t.timestamps
9
9
  end
10
+
10
11
  end
11
- end
12
+ end
@@ -9,10 +9,5 @@ module RailsJwtAdmin
9
9
  def create_routes
10
10
  route 'mount RailsJwtAdmin::Engine => "/rails_jwt_admin"'
11
11
  end
12
-
13
- def add_gems
14
- gem "jwt"
15
- gem "bcrypt"
16
- end
17
12
  end
18
13
  end
@@ -1,3 +1,3 @@
1
1
  module RailsJwtAdmin
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
data/lib/vendors/token.rb CHANGED
@@ -2,12 +2,16 @@ require "rails_jwt_admin/engine"
2
2
 
3
3
  module RailsJwtAdmin
4
4
  class Token
5
+ def self.secret_key
6
+ Rails.application.credentials.jwt_secret
7
+ end
8
+
5
9
  def self.encode(payload)
6
- JWT.encode(payload, Rails.application.secrets.secret_key_base)
10
+ JWT.encode(payload, secret_key, 'HS256')
7
11
  end
8
12
 
9
13
  def self.decode(token)
10
- HashWithIndifferentAccess.new(JWT.decode(token, Rails.application.secrets.secret_key_base)[0])
14
+ HashWithIndifferentAccess.new(JWT.decode(token, secret_key, 'HS256')[0])
11
15
  rescue
12
16
  nil
13
17
  end
metadata CHANGED
@@ -1,30 +1,72 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_jwt_admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
- - afeiship
8
- autorequire:
7
+ - aric.zheng
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-16 00:00:00.000000000 Z
11
+ date: 2026-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: sqlite3
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jwt
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: bcrypt
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: rails_warp
15
57
  requirement: !ruby/object:Gem::Requirement
16
58
  requirements:
17
59
  - - ">="
18
60
  - !ruby/object:Gem::Version
19
61
  version: '0'
20
- type: :development
62
+ type: :runtime
21
63
  prerelease: false
22
64
  version_requirements: !ruby/object:Gem::Requirement
23
65
  requirements:
24
66
  - - ">="
25
67
  - !ruby/object:Gem::Version
26
68
  version: '0'
27
- description:
69
+ description:
28
70
  email:
29
71
  - 1290657123@qq.com
30
72
  executables: []
@@ -51,7 +93,7 @@ homepage: https://github.com/afeiship/rails_jwt_admin
51
93
  licenses:
52
94
  - MIT
53
95
  metadata: {}
54
- post_install_message:
96
+ post_install_message:
55
97
  rdoc_options: []
56
98
  require_paths:
57
99
  - lib
@@ -66,8 +108,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
108
  - !ruby/object:Gem::Version
67
109
  version: '0'
68
110
  requirements: []
69
- rubygems_version: 3.0.3
70
- signing_key:
111
+ rubygems_version: 3.5.22
112
+ signing_key:
71
113
  specification_version: 4
72
114
  summary: Summary of RailsJwtAdmin.
73
115
  test_files: []