Skip to content

emn178/faye-jwt

Repository files navigation

faye-jwt

Build Status Coverage Status

A gem that provides authentication by JWT(json web token) with faye.

Installation

Add this line to your application's Gemfile:

gem 'faye-jwt'

And then execute:

$ bundle

Or install it yourself as:

$ gem install faye-jwt

Usage

faye-jwt will authenicate all requests by access token. If the access token can not be decoded, it will fail to authenicate.

Server

For faye-rails, set up faye server in you initializers config, eg. initializers/faye.rb

Rails.application.config.middleware.use FayeRails::Middleware do
  add_extension(FayeJwt::Server.new('YOUR SECRET'))
  # others...
end

You can use jwt to generate access token and export it.

access_token = JWT.encode({:maybe => :userinfo}, 'YOUR SECRET')

Payload and Header

You can implement your custom server extension. You will get the decoded payload and header if you add your extension after faye-jwt.

class MyServer
  def incoming(message, callback)
    message['jwt']['payload'] # {"maybe" => "userinfo"}
    message['jwt']['header'] # eg. {"typ"=>"JWT", "alg"=>"HS256"}
    callback.call(message)
  end
end

And modify config

Rails.application.config.middleware.use FayeRails::Middleware do
  add_extension(FayeJwt::Server.new('YOUR SECRET'))
  add_extension(MyServer.new)
  # others...
end

JavaScript Client

Require javascripts

//= require faye
//= require faye-jwt

Add extension

var client = new Faye.Client('/faye'); // Your faye path
client.addExtension(new FayeJWT.Client(access_token));

Ruby Client

client = Faye::Client.new('http://localhost:3000/faye')
client.add_extension(FayeJwt::Client.new(access_token))

License

The gem is available as open source under the terms of the MIT License.

Contact

The project's website is located at https://github.com/emn178/faye-jwt
Author: Chen, Yi-Cyuan (emn178@gmail.com)

About

A gem that provides authentication by JWT(json web token) with faye.

Resources

License

Stars

Watchers

Forks

Packages

No packages published