Class: Socky::Authenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/socky/authenticator.rb,
lib/socky/authenticator/version.rb

Constant Summary

DEFAULT_RIGHTS =
{
  :read => true,
  :write => false,
  :hide => false
}
VERSION =
'0.5.0'

Class Attribute Summary (collapse)

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Authenticator) initialize(params, opts = {})

Returns a new instance of Authenticator

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
# File 'lib/socky/authenticator.rb', line 23

def initialize(params, opts = {})
  @params = (params.is_a?(String) ? JSON.parse(params) : params) rescue nil
  raise ArgumentError, 'Expected hash or JSON' unless @params.kind_of?(Hash)
  @secret = opts[:secret] || opts['secret'] || self.class.secret
  @method = opts[:method] || opts['method'] || :websocket
  @allow_changing_rights = opts[:allow_changing_rights] || false
end

Class Attribute Details

+ (Object) secret

Returns the value of attribute secret



14
15
16
# File 'lib/socky/authenticator.rb', line 14

def secret
  @secret
end

Instance Attribute Details

- (Object) method

Returns the value of attribute method



21
22
23
# File 'lib/socky/authenticator.rb', line 21

def method
  @method
end

- (Object) salt

Returns the value of attribute salt



21
22
23
# File 'lib/socky/authenticator.rb', line 21

def salt
  @salt ||= Digest::MD5.hexdigest(rand.to_s)
end

- (Object) secret

Returns the value of attribute secret



21
22
23
# File 'lib/socky/authenticator.rb', line 21

def secret
  @secret
end

Class Method Details

+ (Object) authenticate(params, opts = {})



16
17
18
# File 'lib/socky/authenticator.rb', line 16

def authenticate(params, opts = {})
  self.new(params, opts).result
end

Instance Method Details

- (Object) auth



42
43
44
# File 'lib/socky/authenticator.rb', line 42

def auth
  [salt, signature].join(':')
end

- (Object) channel



65
66
67
# File 'lib/socky/authenticator.rb', line 65

def channel
  @params[:channel] || @params['channel']
end

- (Object) connection_id



61
62
63
# File 'lib/socky/authenticator.rb', line 61

def connection_id
  @params[:connection_id] || @params['connection_id']
end

- (Object) event



69
70
71
# File 'lib/socky/authenticator.rb', line 69

def event
  @params[:event] || @params['event']
end

- (Boolean) presence?

Returns:

  • (Boolean)


89
90
91
# File 'lib/socky/authenticator.rb', line 89

def presence?
  self.channel.is_a?(String) && !!self.channel.match(/\Apresence-/)
end

- (Object) result

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
# File 'lib/socky/authenticator.rb', line 31

def result
  raise ArgumentError, 'set Authenticator.secret first' unless self.secret
  raise ArgumentError, 'expected connection_id' unless self.connection_id
  raise ArgumentError, 'expected channel' unless self.channel
  raise ArgumentError, 'expected event' unless self.method != :http || self.event
  
  r = { 'auth' => auth }
  r.merge!('data' => user_data) unless user_data.nil?
  r
end

- (Object) rights



73
74
75
76
77
78
79
# File 'lib/socky/authenticator.rb', line 73

def rights
  {
    :read => read_right,
    :write => write_right,
    :hide => hide_right
  }
end

- (Object) signature



46
47
48
# File 'lib/socky/authenticator.rb', line 46

def signature
  HMAC::SHA256.hexdigest(self.secret, string_to_sign)
end

- (Object) string_to_sign



50
51
52
53
54
55
# File 'lib/socky/authenticator.rb', line 50

def string_to_sign
  args = [salt, connection_id, channel]
  args << (@method == :websocket ? rights_string : event.to_s)
  args << user_data unless user_data.nil?
  args.collect(&:to_s).join(":")
end

- (Object) user_data



81
82
83
84
85
86
87
# File 'lib/socky/authenticator.rb', line 81

def user_data
  @user_data ||= case (@params[:data] || @params['data'])
    when NilClass then nil
    when String then @params['data']
    else @params['data'].to_json
  end
end