Module: Faye::Authentication
- Defined in:
- lib/faye/authentication.rb,
lib/faye/authentication/engine.rb,
lib/faye/authentication/version.rb,
lib/faye/authentication/http_client.rb,
lib/faye/authentication/client_extension.rb,
lib/faye/authentication/server_extension.rb
Defined Under Namespace
Classes: AuthError, ClientExtension, Engine, ExpiredError, HTTPClient, PayloadError, ServerExtension
Constant Summary collapse
- VERSION =
"0.4.0"
Class Method Summary collapse
- .authentication_required?(message) ⇒ Boolean
-
.decode(signature, secret) ⇒ Object
Return signed payload or raise.
- .public_channel?(channel) ⇒ Boolean
-
.sign(payload, secret, options = {}) ⇒ Object
Return jwt signature, pass hash of payload including channel and client_id.
-
.validate(signature, channel, clientId, secret) ⇒ Object
Return true if signature is valid and correspond to channel and clientId or raise.
Class Method Details
.authentication_required?(message) ⇒ Boolean
35 36 37 38 |
# File 'lib/faye/authentication.rb', line 35 def self.authentication_required?() subscription_or_channel = ['subscription'] || ['channel'] !public_channel?(subscription_or_channel) && (['channel'] == '/meta/subscribe' || (!(['channel'].start_with?('/meta/')))) end |
.decode(signature, secret) ⇒ Object
Return signed payload or raise
21 22 23 24 25 |
# File 'lib/faye/authentication.rb', line 21 def self.decode(signature, secret) payload, _ = JWT.decode(signature, secret) rescue raise(AuthError) raise ExpiredError if Time.at(payload['exp'].to_i) < Time.now payload end |
.public_channel?(channel) ⇒ Boolean
40 41 42 |
# File 'lib/faye/authentication.rb', line 40 def self.public_channel?(channel) channel.start_with?('/public/') and not channel.include?('*') end |
.sign(payload, secret, options = {}) ⇒ Object
Return jwt signature, pass hash of payload including channel and client_id
15 16 17 18 |
# File 'lib/faye/authentication.rb', line 15 def self.sign(payload, secret, = {}) = {expires_at: Time.now + 12*3600, algorithm: 'HS256'}.merge() JWT.encode(payload.merge(exp: [:expires_at].to_i), secret, [:algorithm]) end |
.validate(signature, channel, clientId, secret) ⇒ Object
Return true if signature is valid and correspond to channel and clientId or raise
28 29 30 31 32 33 |
# File 'lib/faye/authentication.rb', line 28 def self.validate(signature, channel, clientId, secret) payload = self.decode(signature, secret) raise PayloadError if channel.to_s.empty? || clientId.to_s.empty? raise PayloadError unless channel == payload['channel'] && clientId == payload['clientId'] true end |