faye-jwt 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/.rspec +1 -0
- data/.travis.yml +9 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +87 -0
- data/Rakefile +6 -0
- data/app/assets/javascripts/faye-jwt.js +29 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/faye-jwt.gemspec +28 -0
- data/lib/faye-jwt.rb +7 -0
- data/lib/faye-jwt/client.rb +27 -0
- data/lib/faye-jwt/engine.rb +9 -0
- data/lib/faye-jwt/server.rb +36 -0
- data/lib/faye-jwt/version.rb +3 -0
- metadata +159 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: eda6eecbf69145a79ddbda3b2bba3e6fbe9b17b1
|
4
|
+
data.tar.gz: 01d311e1e24107be66936f5c6dc70e6797b6af50
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2e4bf8ec37a5e140830e8e6743f921dceeb3e248c3be9805cf30a83d6cbe5b54fb63a02b203abcbc408e41c51adc2fe5c770d6f546aad14adbbd99ef9232f62d
|
7
|
+
data.tar.gz: cf4a43146d25b86c97b6fbfbe3b05390c758e9c8d05e939dad7b0a908dd0665943d082426b47a034b55db3e0de2469f443112635194bd33d64d2100650850154
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Chen, Yi-Cyuan
|
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,87 @@
|
|
1
|
+
# faye-jwt
|
2
|
+
|
3
|
+
[![Build Status](https://api.travis-ci.org/emn178/faye-jwt.png)](https://travis-ci.org/emn178/faye-jwt)
|
4
|
+
[![Coverage Status](https://coveralls.io/repos/emn178/faye-jwt/badge.svg?branch=master)](https://coveralls.io/r/emn178/faye-jwt?branch=master)
|
5
|
+
|
6
|
+
A gem that provides authentication by JWT(json web token) with faye.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'faye-jwt'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install faye-jwt
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
faye-jwt will authenicate all requests by access token. If the access token can not be decoded, it will fail to authenicate.
|
26
|
+
|
27
|
+
### Server
|
28
|
+
|
29
|
+
For faye-rails, set up faye server in you initializers config, eg. `initializers/faye.rb`
|
30
|
+
```Ruby
|
31
|
+
Rails.application.config.middleware.use FayeRails::Middleware do
|
32
|
+
add_extension(FayeJwt::Server.new('YOUR SECRET'))
|
33
|
+
# others...
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
You can use jwt to generate access token and export it.
|
38
|
+
```Ruby
|
39
|
+
access_token = JWT.encode({:maybe => :userinfo}, 'YOUR SECRET')
|
40
|
+
```
|
41
|
+
|
42
|
+
#### Payload and Header
|
43
|
+
You can implement your custom server extension. You will get the decoded payload and header if you add your extension after faye-jwt.
|
44
|
+
|
45
|
+
```Ruby
|
46
|
+
class MyServer
|
47
|
+
def incoming(message, callback)
|
48
|
+
message['jwt']['payload'] # {"maybe" => "userinfo"}
|
49
|
+
message['jwt']['header'] # eg. {"typ"=>"JWT", "alg"=>"HS256"}
|
50
|
+
callback.call(message)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
```
|
54
|
+
And modify config
|
55
|
+
```Ruby
|
56
|
+
Rails.application.config.middleware.use FayeRails::Middleware do
|
57
|
+
add_extension(FayeJwt::Server.new('YOUR SECRET'))
|
58
|
+
add_extension(MyServer.new)
|
59
|
+
# others...
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
### JavaScript Client
|
64
|
+
Require javascripts
|
65
|
+
```JavaScript
|
66
|
+
//= require faye
|
67
|
+
//= require faye-jwt
|
68
|
+
```
|
69
|
+
Add extension
|
70
|
+
```JavaScript
|
71
|
+
var client = new Faye.Client('/faye'); // Your faye path
|
72
|
+
client.addExtension(new FayeJWT.Client(access_token));
|
73
|
+
```
|
74
|
+
|
75
|
+
### Ruby Client
|
76
|
+
```Ruby
|
77
|
+
client = Faye::Client.new('http://localhost:3000/faye')
|
78
|
+
client.add_extension(FayeJwt::Client.new(access_token))
|
79
|
+
```
|
80
|
+
|
81
|
+
## License
|
82
|
+
|
83
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
84
|
+
|
85
|
+
## Contact
|
86
|
+
The project's website is located at https://github.com/emn178/faye-jwt
|
87
|
+
Author: Chen, Yi-Cyuan (emn178@gmail.com)
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
(function($) {
|
2
|
+
function Client(accessToken) {
|
3
|
+
this.accessToken = accessToken;
|
4
|
+
}
|
5
|
+
|
6
|
+
Client.prototype.outgoing = function(message, callback) {
|
7
|
+
message.Authorization = 'Bearer ' + this.accessToken;
|
8
|
+
callback(message);
|
9
|
+
};
|
10
|
+
|
11
|
+
Client.prototype.incoming = function(message, callback) {
|
12
|
+
if (message.error != 'Authentication failed.') {
|
13
|
+
callback(message);
|
14
|
+
}
|
15
|
+
};
|
16
|
+
|
17
|
+
Client.publish = function (url, channel, data, accessToken, callback) {
|
18
|
+
message = {channel: channel, data: data, Authorization: 'Bearer ' + accessToken};
|
19
|
+
return $.ajax({
|
20
|
+
url: url,
|
21
|
+
method: 'POST',
|
22
|
+
success: callback
|
23
|
+
});
|
24
|
+
};
|
25
|
+
|
26
|
+
window.FayeJwt = {
|
27
|
+
Client: Client
|
28
|
+
};
|
29
|
+
})(jQuery);
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "faye/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/faye-jwt.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'faye-jwt/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "faye-jwt"
|
8
|
+
spec.version = FayeJwt::VERSION
|
9
|
+
spec.authors = ["Chen, Yi-Cyuan"]
|
10
|
+
spec.email = ["emn178@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A gem that provides authentication by JWT(json web token) with faye.}
|
13
|
+
spec.description = %q{A gem that provides authentication by JWT(json web token) with faye.}
|
14
|
+
spec.homepage = "https://github.com/emn178/faye-jwt"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "jwt"
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "rspec-its"
|
26
|
+
spec.add_development_dependency "simplecov"
|
27
|
+
spec.add_development_dependency "coveralls"
|
28
|
+
end
|
data/lib/faye-jwt.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
module FayeJwt
|
5
|
+
class Client
|
6
|
+
attr_accessor :access_token
|
7
|
+
|
8
|
+
def initialize(access_token)
|
9
|
+
self.access_token = access_token
|
10
|
+
end
|
11
|
+
|
12
|
+
def outgoing(message, callback)
|
13
|
+
message['Authorization'] = "Bearer #{access_token}"
|
14
|
+
callback.call(message)
|
15
|
+
end
|
16
|
+
|
17
|
+
def incoming(message, callback)
|
18
|
+
callback.call(message) if message['error'] != 'Authentication failed.'
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.publish(url, channel, data, access_token)
|
22
|
+
message = {'channel' => channel, 'data' => data, 'Authorization' => "Bearer #{access_token}"}
|
23
|
+
uri = URI.parse(url)
|
24
|
+
Net::HTTP.post_form(uri, message: [message].to_json).body
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'jwt'
|
2
|
+
|
3
|
+
module FayeJwt
|
4
|
+
class Server
|
5
|
+
attr_accessor :secret
|
6
|
+
|
7
|
+
def initialize(secret)
|
8
|
+
self.secret = secret
|
9
|
+
end
|
10
|
+
|
11
|
+
def incoming(message, callback)
|
12
|
+
message['error'] = 'Authentication failed.' unless authenticate(message)
|
13
|
+
callback.call(message)
|
14
|
+
end
|
15
|
+
|
16
|
+
def outgoing(message, callback)
|
17
|
+
message.delete('Authorization')
|
18
|
+
message.delete('jwt')
|
19
|
+
callback.call(message)
|
20
|
+
end
|
21
|
+
|
22
|
+
def authenticate(message)
|
23
|
+
payload, header = decode(message['Authorization'])
|
24
|
+
return false if payload.nil?
|
25
|
+
message['jwt'] = { 'payload' => payload, 'header' => header }
|
26
|
+
true
|
27
|
+
end
|
28
|
+
|
29
|
+
def decode(authorization)
|
30
|
+
return nil if authorization.nil?
|
31
|
+
type, access_token = authorization.split(' ')
|
32
|
+
return nil if type.to_s.downcase != 'bearer' || access_token.nil?
|
33
|
+
JWT.decode(access_token, secret, true) rescue nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: faye-jwt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chen, Yi-Cyuan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jwt
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-its
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: coveralls
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: A gem that provides authentication by JWT(json web token) with faye.
|
112
|
+
email:
|
113
|
+
- emn178@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".travis.yml"
|
121
|
+
- CHANGELOG.md
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- app/assets/javascripts/faye-jwt.js
|
127
|
+
- bin/console
|
128
|
+
- bin/setup
|
129
|
+
- faye-jwt.gemspec
|
130
|
+
- lib/faye-jwt.rb
|
131
|
+
- lib/faye-jwt/client.rb
|
132
|
+
- lib/faye-jwt/engine.rb
|
133
|
+
- lib/faye-jwt/server.rb
|
134
|
+
- lib/faye-jwt/version.rb
|
135
|
+
homepage: https://github.com/emn178/faye-jwt
|
136
|
+
licenses:
|
137
|
+
- MIT
|
138
|
+
metadata: {}
|
139
|
+
post_install_message:
|
140
|
+
rdoc_options: []
|
141
|
+
require_paths:
|
142
|
+
- lib
|
143
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - ">="
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
requirements: []
|
154
|
+
rubyforge_project:
|
155
|
+
rubygems_version: 2.4.8
|
156
|
+
signing_key:
|
157
|
+
specification_version: 4
|
158
|
+
summary: A gem that provides authentication by JWT(json web token) with faye.
|
159
|
+
test_files: []
|