jwt-secure 0.1.3 → 0.1.6
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/lib/jwt_secure/controllers.rb +69 -0
- data/lib/{jwt-secure → jwt_secure}/engine.rb +1 -0
- data/lib/jwt_secure.rb +6 -0
- metadata +5 -6
- data/app/config/routes.rb +0 -3
- data/app/controller/jwt-secure/jwt-secure_controller.rb +0 -7
- data/lib/jwt-secure.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43d28a0811cdcfcbab6de745f66369d211f0e81bb10f29fd9c1d7c2547f32021
|
4
|
+
data.tar.gz: 8725be701d18c64342bddc30584c57995b5364fc25598160a21be1919f5459cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a491a39f3bf21ca112a19818afc1aadff133d31105310b4c8afa7dccaafa7664c5a0e5272ab509fab2c8ceb410cadea3506fb0b93b16be5ad4125dc205c541ac
|
7
|
+
data.tar.gz: fc815a37549eef245b718893d5dba562f0d1002f20c3ffd35115ee210f853328c49262d3df6b7545986aaa9b2fadc544582a9a8a9c6f45fb4d8c856a7908c175
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require "jwt"
|
2
|
+
|
3
|
+
|
4
|
+
class JsonWebToken
|
5
|
+
def self.encode(payload, key)
|
6
|
+
JWT.encode(payload,key)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.decode(token, key)
|
10
|
+
puts key
|
11
|
+
JWT.decode(token,key).first
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
module JwtSecure
|
17
|
+
class ApiJwtController < ApplicationController
|
18
|
+
before_action(:authenticate_jwtsecure)
|
19
|
+
|
20
|
+
|
21
|
+
def authenticate_jwtsecure()
|
22
|
+
begin
|
23
|
+
payload = JsonWebToken.decode(get_auth_token, @jwtsecure_secret)
|
24
|
+
if payload.present?
|
25
|
+
@current_user = User.find(payload["user_id"])
|
26
|
+
else
|
27
|
+
render json: {errors: ["Invalid Token, user not found!"]}, status: :unauthorized
|
28
|
+
end
|
29
|
+
rescue
|
30
|
+
render json: {errors: ["token not found!"]}, status: :unauthorized
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_auth_token()
|
35
|
+
@auth_token ||= cookies.encrypted[@jwtsecure_cookiename]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class AuthController < ApplicationController
|
40
|
+
def login
|
41
|
+
# find user
|
42
|
+
user = @jwtsecure_usermodel.find_by(@jwtsecure_findby)
|
43
|
+
|
44
|
+
if user && user.authenticate(@jwtsecure_password)
|
45
|
+
# password is correct -> proced to login
|
46
|
+
# set toke inside httpOnly Cookie
|
47
|
+
jwt_token = JsonWebToken.encode({user_id: user.id},@jwtsecure_secret)
|
48
|
+
cookies.encrypted[@jwtsecure_cookiename] = {
|
49
|
+
value: jwt_token,
|
50
|
+
http_only: true,
|
51
|
+
same_site: :strict
|
52
|
+
}
|
53
|
+
render json: {message: "Successfull login!", success: true, user: user}, status: :ok
|
54
|
+
else
|
55
|
+
# password incorrect -> failed login
|
56
|
+
render json: {errors: ["Invalid email or password"], success: false}, status: :unauthorized
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def logout
|
61
|
+
cookies.encrypted[@jwtsecure_cookiename] = {
|
62
|
+
value: "",
|
63
|
+
http_only: true,
|
64
|
+
same_site: :strict
|
65
|
+
}
|
66
|
+
render json: {message: "logged out, cookie removed", success: true}, status: :ok
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/jwt_secure.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jwt-secure
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- M Alvee
|
@@ -45,10 +45,9 @@ executables: []
|
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
|
-
-
|
49
|
-
-
|
50
|
-
- lib/
|
51
|
-
- lib/jwt-secure/engine.rb
|
48
|
+
- lib/jwt_secure.rb
|
49
|
+
- lib/jwt_secure/controllers.rb
|
50
|
+
- lib/jwt_secure/engine.rb
|
52
51
|
homepage: https://github.com/0xMALVEE/jwt-secure
|
53
52
|
licenses:
|
54
53
|
- MIT
|
@@ -71,5 +70,5 @@ requirements: []
|
|
71
70
|
rubygems_version: 3.4.6
|
72
71
|
signing_key:
|
73
72
|
specification_version: 4
|
74
|
-
summary: Secure JWT authentication for Ruby on Rails.
|
73
|
+
summary: Secure JWT authentication for Ruby on Rails that uses http only cookies.
|
75
74
|
test_files: []
|
data/app/config/routes.rb
DELETED
data/lib/jwt-secure.rb
DELETED