Class: FayeJwt::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/faye-jwt/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret) ⇒ Server

Returns a new instance of Server.



7
8
9
# File 'lib/faye-jwt/server.rb', line 7

def initialize(secret)
  self.secret = secret
end

Instance Attribute Details

#secretObject

Returns the value of attribute secret.



5
6
7
# File 'lib/faye-jwt/server.rb', line 5

def secret
  @secret
end

Instance Method Details

#authenticate(message) ⇒ Object



22
23
24
25
26
27
# File 'lib/faye-jwt/server.rb', line 22

def authenticate(message)
  payload, header = decode(message['Authorization'])
  return false if payload.nil?
  message['jwt'] = { 'payload' => payload, 'header' => header }
  true
end

#decode(authorization) ⇒ Object



29
30
31
32
33
34
# File 'lib/faye-jwt/server.rb', line 29

def decode(authorization)
  return nil if authorization.nil?
  type, access_token = authorization.split(' ')
  return nil if type.to_s.downcase != 'bearer' || access_token.nil?
  JWT.decode(access_token, secret, true) rescue nil
end

#incoming(message, callback) ⇒ Object



11
12
13
14
# File 'lib/faye-jwt/server.rb', line 11

def incoming(message, callback)
  message['error'] = 'Authentication failed.' unless authenticate(message)
  callback.call(message)
end

#outgoing(message, callback) ⇒ Object



16
17
18
19
20
# File 'lib/faye-jwt/server.rb', line 16

def outgoing(message, callback)
  message.delete('Authorization')
  message.delete('jwt')
  callback.call(message)
end