jwt_auth 0.1.0 → 0.2.0
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/README.md +2 -2
- data/jwt_auth-0.1.0.gem +0 -0
- data/lib/jwt_auth/json_web_token.rb +5 -5
- data/lib/jwt_auth/jwt_authentication.rb +32 -27
- data/lib/jwt_auth/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e84adb5e90385c341023942b7224f9f5816e9249
|
|
4
|
+
data.tar.gz: 87e2148ef8ad779b89abe0b35580b939e8394ad1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 455612c576b44f39514338cf0a744598117f8ee1106111b6e88d9f9dfaf39da1fa02785c6c4d4d77983d0135b8da8a0c426e76238e2f34fb752b26c0d14bfc9f
|
|
7
|
+
data.tar.gz: aeea1cb926c73239ce39ac752238006ecf06a0fc005284a486c569366dc192579e3b31f243062e4632b20a7199a61d4344b6eff275bf217e0b81c64be262a429
|
data/README.md
CHANGED
|
@@ -58,7 +58,7 @@ After this with each request where authentication is required in headers pass th
|
|
|
58
58
|
Add devise default before filter for authentication.
|
|
59
59
|
|
|
60
60
|
```ruby
|
|
61
|
-
before_filter authenticate_user!
|
|
61
|
+
before_filter :authenticate_user!
|
|
62
62
|
```
|
|
63
63
|
|
|
64
64
|
## Development
|
|
@@ -69,7 +69,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
|
69
69
|
|
|
70
70
|
## Contributing
|
|
71
71
|
|
|
72
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
72
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/suratpyari/jwt_authentication. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
|
73
73
|
|
|
74
74
|
|
|
75
75
|
## License
|
data/jwt_auth-0.1.0.gem
CHANGED
|
Binary file
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
class JsonWebToken
|
|
2
2
|
|
|
3
|
-
include ActiveModel::Model
|
|
3
|
+
# include ActiveModel::Model
|
|
4
4
|
require 'jwt'
|
|
5
5
|
|
|
6
6
|
attr_reader :user_id, :payload
|
|
@@ -16,16 +16,16 @@ class JsonWebToken
|
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
def self.find_config
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
yield Rails.application.class.parent_name.underscore,
|
|
20
|
+
Rails.application.secrets.json_web_token_secret
|
|
21
|
+
Rails.application.secrets.json_web_token_encoding
|
|
22
22
|
end
|
|
23
23
|
def self.encode(user_id, expiration = 24.hours.from_now)
|
|
24
24
|
secret, encoding = secret_and_encoding
|
|
25
25
|
JWT.encode({user_id: user_id, exp: expiration.to_i}, secret, encoding)
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
-
def initialize
|
|
28
|
+
def initialize(token)
|
|
29
29
|
# begin
|
|
30
30
|
secret, encoding = secret_and_encoding
|
|
31
31
|
@payload = JWT.decode(token, secret, encoding).first.with_indifferent_access
|
|
@@ -1,41 +1,46 @@
|
|
|
1
1
|
module JwtAuthentication
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
Devise.mappings.keys.collect(&:to_s).each do |u|
|
|
3
|
+
define_method "authenticate_#{u}!" do
|
|
4
|
+
respond_to do |format|
|
|
5
|
+
format.html{
|
|
6
|
+
ActionController::Base.instance_method("authenticate_#{u}!".to_sym).bind(self).call
|
|
7
|
+
}
|
|
8
|
+
format.json{unauthorized! unless eval("current_#{u}")}
|
|
9
|
+
end
|
|
7
10
|
end
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
|
|
12
|
+
define_method "current_#{u}" do
|
|
13
|
+
respond_to do |format|
|
|
14
|
+
format.html{
|
|
15
|
+
ActionController::Base.instance_method("current_#{u}".to_sym).bind(self).call
|
|
16
|
+
}
|
|
17
|
+
format.json{eval("@current_#{u} ||= set_current_#{u}")}
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
define_method "set_current_#{u}" do
|
|
22
|
+
token = request.headers['Authorization'].to_s.split(' ').last
|
|
23
|
+
return unless token
|
|
24
|
+
payload = JsonWebToken.new(token)
|
|
25
|
+
eval("@current_#{u} = #{u.capitalize}.find(payload.user_id)") if payload.valid?
|
|
18
26
|
end
|
|
19
27
|
end
|
|
20
|
-
|
|
21
|
-
def set_current_user
|
|
22
|
-
token = request.headers['Authorization'].to_s.split(' ').last
|
|
23
|
-
return unless token
|
|
24
|
-
payload = JsonWebToken.new(token)
|
|
25
|
-
@current_user = User.find(payload.user_id) if payload.valid?
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
+
|
|
28
29
|
def show_authentication_messages
|
|
29
30
|
respond_to do |format|
|
|
30
31
|
format.html{super}
|
|
31
32
|
format.json{
|
|
32
|
-
|
|
33
|
-
render
|
|
34
|
-
else
|
|
35
|
-
render :json=>{:success=>true}, :status=>201
|
|
33
|
+
Devise.mappings.keys.collect(&:to_s).each do |u|
|
|
34
|
+
return render(:json=> eval("@#{u}.errors"), :status=>422) if eval("@#{u} && @#{u}.errors.any?")
|
|
36
35
|
end
|
|
36
|
+
render :json=>{:success=>true}, :status=>201
|
|
37
37
|
}
|
|
38
38
|
end
|
|
39
39
|
end
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def unauthorized!
|
|
43
|
+
head :unauthorized
|
|
44
|
+
end
|
|
40
45
|
|
|
41
46
|
end
|
data/lib/jwt_auth/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: jwt_auth
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- shruti satsangi
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2016-01-04 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|