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 +4 -4
- data/MIT-LICENSE +1 -1
- data/README.md +118 -23
- data/Rakefile +3 -29
- data/app/controllers/rails_jwt_admin/application_controller.rb +3 -2
- data/app/controllers/rails_jwt_admin/authentication_controller.rb +4 -2
- data/app/controllers/rails_jwt_admin/users_controller.rb +4 -2
- data/config/routes.rb +2 -2
- data/db/migrate/20201202013006_create_rails_jwt_admin_users.rb +5 -4
- data/lib/generators/rails_jwt_admin/install_generator.rb +0 -5
- data/lib/rails_jwt_admin/version.rb +1 -1
- data/lib/vendors/token.rb +6 -2
- metadata +52 -10
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 12e5b16df86e414638bf8f1ddc1f99caa51c243a89edf567ce3434954b711e4d
|
|
4
|
+
data.tar.gz: 1b7f6b9d4423e633b227998ff60afafe894086dcfb90d6374cf413f3eb048c4c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: aed76e5d45e8ab3ab6d6b504c8d3169c0b44ccf4ecae6813716a5bdd08ab9b5d17f26ee28a7e3bcd36012a6185ee34593e990a5deadde037a449e629d1e80d09
|
|
7
|
+
data.tar.gz: ac93744aa679de8942ef3c49350370cc855d547e078d5a3c2f1f417e2f02cd14cc1547bbcebd495efcda8f9b4d62eae5b375d4a9b85a48809138a5ac0f6c21e7
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
|
@@ -1,33 +1,128 @@
|
|
|
1
|
-
#
|
|
2
|
-
>
|
|
1
|
+
# RailsJwtAdmin
|
|
2
|
+
> JWT-based authentication system for Rails admin panel.
|
|
3
3
|
|
|
4
|
-
##
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
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
|
-
|
|
13
|
-
|
|
33
|
+
4. Run database migration:
|
|
34
|
+
```bash
|
|
35
|
+
rails db:migrate
|
|
14
36
|
```
|
|
15
37
|
|
|
16
|
-
##
|
|
17
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
##
|
|
27
|
-
-
|
|
28
|
-
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
-
|
|
33
|
-
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
6
|
+
require "bundler/gem_tasks"
|
|
@@ -13,8 +13,9 @@ module RailsJwtAdmin
|
|
|
13
13
|
|
|
14
14
|
private
|
|
15
15
|
|
|
16
|
-
def render_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
|
-
|
|
8
|
-
|
|
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,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
|
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,
|
|
10
|
+
JWT.encode(payload, secret_key, 'HS256')
|
|
7
11
|
end
|
|
8
12
|
|
|
9
13
|
def self.decode(token)
|
|
10
|
-
HashWithIndifferentAccess.new(JWT.decode(token,
|
|
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.
|
|
4
|
+
version: 0.1.7
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
-
|
|
8
|
-
autorequire:
|
|
7
|
+
- aric.zheng
|
|
8
|
+
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-02-14 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
|
-
name:
|
|
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: :
|
|
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.
|
|
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: []
|