Class: Cloudkey::SecurityPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudkey/security_policy.rb

Constant Summary collapse

IP_FORMAT_ERROR =
Class.new(Exception)
REFERERS_FORMAT_ERROR =
Class.new(Exception)

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ SecurityPolicy

Returns a new instance of SecurityPolicy.



10
11
12
# File 'lib/cloudkey/security_policy.rb', line 10

def initialize opts={}
  @options = {:expires_in => 7200}.merge(opts)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cloudkey/security_policy.rb', line 14

def method_missing method_id, *args, &block
  method_id = method_id.to_s
  
  affectation = method_id.include? "="
  method_id.gsub!("=",'')
  
  if affectation
    @options[method_id.to_sym] = args.first
  end
    
  @options[method_id.to_sym]
end

Instance Method Details

#as_number?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/cloudkey/security_policy.rb', line 39

def as_number?
  !!@options[:as_number]
end

#countries?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/cloudkey/security_policy.rb', line 58

def countries?
  @options[:countries] && !@options[:countries].empty?
end

#delegate?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/cloudkey/security_policy.rb', line 35

def delegate?
  @options[:delegate]
end

#encoded_public_parametersObject



88
89
90
# File 'lib/cloudkey/security_policy.rb', line 88

def encoded_public_parameters
  Base64.encode64(Zlib::Deflate.deflate(public_parameters.join('&'))).chomp if public_parameters
end

#expiresObject



27
28
29
# File 'lib/cloudkey/security_policy.rb', line 27

def expires
  @options[:expires_in] + Time.now.tv_sec
end

#ip?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/cloudkey/security_policy.rb', line 43

def ip?
  if @options[:ip]
    raise IP_FORMAT_ERROR unless @options[:ip].match(/\b(?:\d{1,3}\.){3}\d{1,3}\b/)
    true
  end
end

#levelObject



92
93
94
95
96
97
98
# File 'lib/cloudkey/security_policy.rb', line 92

def level
  result = 0
  API::SecurityLevel.constants.each do |constant|
    result |= API::SecurityLevel.const_get(constant) if send("#{constant.to_s.downcase}?")
  end
  result
end

#none?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/cloudkey/security_policy.rb', line 31

def none?
  @options.empty? || @options.keys == [:expires_in]
end

#private_parametersObject



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

def private_parameters
  parameters = []
  parameters << as_number if as_number?
  parameters << ip if ip?
  parameters << user_agent if user_agent?
  parameters 
end

#public_parametersObject



81
82
83
84
85
86
# File 'lib/cloudkey/security_policy.rb', line 81

def public_parameters
  parameters = []
  parameters << "cc=#{countries.collect{|c| c.downcase}.join(',')}" if countries?
  parameters << "rf=#{CGI.escape(referers.collect{|r| r.gsub(' ', '%20')}.join(' '))}" if referers?
  parameters unless parameters.empty?
end

#referers?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
# File 'lib/cloudkey/security_policy.rb', line 62

def referers?
  if @options[:referers] && !@options[:referers].empty?
    raise REFERERS_FORMAT_ERROR unless @options[:referers].all? { |url| url.match(/https?:\/\/(.*)/) }
    true
  end
end

#set(opts = {}) ⇒ Object



69
70
71
# File 'lib/cloudkey/security_policy.rb', line 69

def set opts={}
  @options = opts
end

#use_once?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/cloudkey/security_policy.rb', line 54

def use_once?
  @options[:use_once]
end

#user_agent?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/cloudkey/security_policy.rb', line 50

def user_agent?
  @options[:user_agent] && !@options[:user_agent].empty?
end