jwt_auth 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f4af4f30fd7d80ae248909c27651aee5ea3ea359
4
- data.tar.gz: 4705002f691ce832cc4798bf145109333757ddcf
3
+ metadata.gz: e84adb5e90385c341023942b7224f9f5816e9249
4
+ data.tar.gz: 87e2148ef8ad779b89abe0b35580b939e8394ad1
5
5
  SHA512:
6
- metadata.gz: a48e46dc82f70af4933514864b2c1db26fe4d2e5a59d0d9f995db2dcd958dd7e273164d84c6deb47c2374a8091057bc4bf4bee94646cfecec08c2f9338d5e19a
7
- data.tar.gz: 8a2d3ce02cfa9c940a35a2e930440e4bbd60149c03de8c0fd1bc5684295242d13eb1034681105dec6d67b5610313f677e629c6d8365377e3a6ba6ea40d73f865
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/[USERNAME]/jwt_auth. 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.
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
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
- yield Rails.application.class.parent_name.underscore,
20
- Rails.application.secrets.json_web_token_secret
21
- Rails.application.secrets.json_web_token_encoding
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 token
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
- def authenticate_user!(options={})
4
- respond_to do |format|
5
- format.html{super(options)}
6
- format.json{unauthorized! unless current_user}
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
- end
9
-
10
- def unauthorized!
11
- head :unauthorized
12
- end
13
-
14
- def current_user
15
- respond_to do |format|
16
- format.html{super}
17
- format.json{@current_user ||= set_current_user}
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
- if @user.errors.any?
33
- render :json=> @user.errors, :status=>422
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
@@ -1,3 +1,3 @@
1
1
  module JwtAuth
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.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: 2015-12-03 00:00:00.000000000 Z
11
+ date: 2016-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails