jwt_auth 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +78 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/jwt_auth-0.1.0.gem +0 -0
- data/jwt_auth.gemspec +35 -0
- data/lib/jwt_auth/json_web_token.rb +42 -0
- data/lib/jwt_auth/jwt_authentication.rb +41 -0
- data/lib/jwt_auth/resource.rb +12 -0
- data/lib/jwt_auth/version.rb +3 -0
- data/lib/jwt_auth.rb +10 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f4af4f30fd7d80ae248909c27651aee5ea3ea359
|
4
|
+
data.tar.gz: 4705002f691ce832cc4798bf145109333757ddcf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a48e46dc82f70af4933514864b2c1db26fe4d2e5a59d0d9f995db2dcd958dd7e273164d84c6deb47c2374a8091057bc4bf4bee94646cfecec08c2f9338d5e19a
|
7
|
+
data.tar.gz: 8a2d3ce02cfa9c940a35a2e930440e4bbd60149c03de8c0fd1bc5684295242d13eb1034681105dec6d67b5610313f677e629c6d8365377e3a6ba6ea40d73f865
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 shruti satsangi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# JwtAuth
|
2
|
+
|
3
|
+
It helps to authenticate devise user using JSON Web Token.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'jwt_auth'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install jwt_auth
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Install devise user. Right now it only works for 'User'.
|
24
|
+
|
25
|
+
Add following code :
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# in application_controller.rb
|
29
|
+
class ApplicationController < ActionController::Base
|
30
|
+
include JwtAuthentication
|
31
|
+
#...
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
# in models/user.rb
|
37
|
+
class User
|
38
|
+
include JWTUserAuth
|
39
|
+
#...
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
After sign_in devise will return auth_token in JSON object.
|
44
|
+
|
45
|
+
Ex:
|
46
|
+
|
47
|
+
```json
|
48
|
+
{
|
49
|
+
"id": 1,
|
50
|
+
"created_at": "2015-12-01T11:04:26.224Z",
|
51
|
+
"email": "*****@gmail.com",
|
52
|
+
"updated_at": "2015-12-01T11:07:55.354Z",
|
53
|
+
"auth_token": "eyJ0eXAiOiJKV1Qi******-CtGZag",
|
54
|
+
}
|
55
|
+
```
|
56
|
+
After this with each request where authentication is required in headers pass the 'Authorization' which has the same as auth_token received in above json.
|
57
|
+
|
58
|
+
Add devise default before filter for authentication.
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
before_filter authenticate_user!
|
62
|
+
```
|
63
|
+
|
64
|
+
## Development
|
65
|
+
|
66
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
67
|
+
|
68
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
69
|
+
|
70
|
+
## Contributing
|
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.
|
73
|
+
|
74
|
+
|
75
|
+
## License
|
76
|
+
|
77
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
78
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "jwt_auth"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/jwt_auth-0.1.0.gem
ADDED
Binary file
|
data/jwt_auth.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'jwt_auth/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "jwt_auth"
|
8
|
+
spec.version = JwtAuth::VERSION
|
9
|
+
spec.authors = ["shruti satsangi"]
|
10
|
+
spec.email = ["shruti@blazarsol.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{"Json Web Token Authentication system"}
|
13
|
+
spec.description = %q{Json Web Token Authentication for devise users. It can be use for APIs.}
|
14
|
+
spec.homepage = "https://github.com/suratpyari/jwt_authentication"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency "rails", ">= 4.0.0"
|
31
|
+
spec.add_dependency "devise", ">= 3.5.0"
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class JsonWebToken
|
2
|
+
|
3
|
+
include ActiveModel::Model
|
4
|
+
require 'jwt'
|
5
|
+
|
6
|
+
attr_reader :user_id, :payload
|
7
|
+
|
8
|
+
def self.secret_and_encoding
|
9
|
+
s = nil
|
10
|
+
e = nil
|
11
|
+
self.find_config do |app, secret, encoding|
|
12
|
+
s = secret
|
13
|
+
e = encoding
|
14
|
+
end
|
15
|
+
[s, e]
|
16
|
+
end
|
17
|
+
|
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
|
22
|
+
end
|
23
|
+
def self.encode(user_id, expiration = 24.hours.from_now)
|
24
|
+
secret, encoding = secret_and_encoding
|
25
|
+
JWT.encode({user_id: user_id, exp: expiration.to_i}, secret, encoding)
|
26
|
+
end
|
27
|
+
|
28
|
+
def initialize token
|
29
|
+
# begin
|
30
|
+
secret, encoding = secret_and_encoding
|
31
|
+
@payload = JWT.decode(token, secret, encoding).first.with_indifferent_access
|
32
|
+
@user_id = @payload[:user_id]["$oid"]
|
33
|
+
# rescue JWT::DecodeError
|
34
|
+
# nil
|
35
|
+
# end
|
36
|
+
end
|
37
|
+
|
38
|
+
def valid?
|
39
|
+
user_id.presence && Time.now < Time.at(@payload[:exp].to_i)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,41 @@
|
|
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}
|
7
|
+
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}
|
18
|
+
end
|
19
|
+
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
|
+
def show_authentication_messages
|
29
|
+
respond_to do |format|
|
30
|
+
format.html{super}
|
31
|
+
format.json{
|
32
|
+
if @user.errors.any?
|
33
|
+
render :json=> @user.errors, :status=>422
|
34
|
+
else
|
35
|
+
render :json=>{:success=>true}, :status=>201
|
36
|
+
end
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
data/lib/jwt_auth.rb
ADDED
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jwt_auth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- shruti satsangi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: devise
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.5.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.5.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.10'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.10'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: Json Web Token Authentication for devise users. It can be use for APIs.
|
70
|
+
email:
|
71
|
+
- shruti@blazarsol.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- CODE_OF_CONDUCT.md
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- jwt_auth-0.1.0.gem
|
85
|
+
- jwt_auth.gemspec
|
86
|
+
- lib/jwt_auth.rb
|
87
|
+
- lib/jwt_auth/json_web_token.rb
|
88
|
+
- lib/jwt_auth/jwt_authentication.rb
|
89
|
+
- lib/jwt_auth/resource.rb
|
90
|
+
- lib/jwt_auth/version.rb
|
91
|
+
homepage: https://github.com/suratpyari/jwt_authentication
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata:
|
95
|
+
allowed_push_host: https://rubygems.org
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.4.6
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: '"Json Web Token Authentication system"'
|
116
|
+
test_files: []
|