Module: Faye::Authentication
- Extended by:
- Logging
- 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 =
File.read(File.join(File.dirname(__FILE__),'..', '..', '..', 'VERSION') ).strip
Class Method Summary collapse
- .authentication_required?(message, options = {}) ⇒ Boolean
-
.decode(signature, secret) ⇒ Object
Return signed payload or raise.
-
.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, options = {}) ⇒ Boolean
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/faye/authentication.rb', line 42 def self.authentication_required?(, = {}) subscription_or_channel = ['subscription'] || ['channel'] return false if ['channel'].nil? return false unless (['channel'].start_with?('/meta/subscribe') || (!(['channel'].start_with?('/meta/')))) whitelist_proc = [:whitelist] if whitelist_proc begin return !whitelist_proc.call(subscription_or_channel) rescue => e error("Error caught when evaluating whitelist lambda : #{e.}") end end true end |
.decode(signature, secret) ⇒ Object
Return signed payload or raise
25 26 27 28 29 30 31 32 |
# File 'lib/faye/authentication.rb', line 25 def self.decode(signature, secret) payload, _ = JWT.decode(signature, secret) payload rescue JWT::ExpiredSignature raise ExpiredError rescue raise AuthError end |
.sign(payload, secret, options = {}) ⇒ Object
Return jwt signature, pass hash of payload including channel and client_id
19 20 21 22 |
# File 'lib/faye/authentication.rb', line 19 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
35 36 37 38 39 40 |
# File 'lib/faye/authentication.rb', line 35 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 |