Module: Alondra::SessionParser

Extended by:
SessionParser
Included in:
SessionParser
Defined in:
lib/alondra/session_parser.rb

Instance Method Summary collapse

Instance Method Details

#marshallObject



53
54
55
# File 'lib/alondra/session_parser.rb', line 53

def marshall
  Rails.application.config.session_options[:coder]
end

#parse(websocket) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/alondra/session_parser.rb', line 11

def parse(websocket)
  cookie = websocket.request['cookie'] || websocket.request['Cookie']
  token  = websocket.request['query']['token']

  if token.present?
    SessionParser.parse_token(token)
  elsif cookie.present?
    SessionParser.parse_cookie(cookie)
  else
    Hash.new
  end
end


24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/alondra/session_parser.rb', line 24

def parse_cookie(cookie)
  begin
    cookies = cookie.split(';')
    session_key = Rails.application.config.session_options[:key]

    encoded_session = cookies.detect{|c| c.include?(session_key)}.gsub("#{session_key}=",'').strip
    verifier.verify(CGI.unescape(encoded_session))
  rescue ActiveSupport::MessageVerifier::InvalidSignature => ex
    Log.error "invalid session cookie: #{cookie}"
    Hash.new
  rescue Exception => ex
    Log.error "Exception parsing session from cookie: #{ex.message}"
  end
end

#parse_token(token) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/alondra/session_parser.rb', line 39

def parse_token(token)
  begin
    decoded_token = verifier.verify(token)
    ActiveSupport::JSON.decode(decoded_token)
  rescue ActiveSupport::MessageVerifier::InvalidSignature => ex
    Log.error "invalid session token: #{token}"
    Hash.new
  end
end

#session_keyObject



49
50
51
# File 'lib/alondra/session_parser.rb', line 49

def session_key
  Rails.application.config.session_options.key
end

#verifierObject



7
8
9
# File 'lib/alondra/session_parser.rb', line 7

def verifier
  @verifier ||= ActiveSupport::MessageVerifier.new(Rails.application.config.secret_token)
end