Class: Conify::Sso

Inherits:
Object
  • Object
show all
Defined in:
lib/conify/sso.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Sso

Returns a new instance of Sso.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/conify/sso.rb', line 8

def initialize(data)
  @external_uuid = data[:external_uuid]
  @external_username = data[:external_username]
  @salt = data['api']['sso_salt']
  env = data.fetch('env', 'test')

  if @url = data['api'][env]['sso_url']
    @use_post = true
    @proxy_port = find_available_port
  else
    @url = data['api'][env].chomp('/')
  end

  @timestamp  = Time.now.to_i
  @token = make_token(@timestamp)
end

Instance Attribute Details

#external_uuidObject

Returns the value of attribute external_uuid.



6
7
8
# File 'lib/conify/sso.rb', line 6

def external_uuid
  @external_uuid
end

#proxy_portObject

Returns the value of attribute proxy_port.



6
7
8
# File 'lib/conify/sso.rb', line 6

def proxy_port
  @proxy_port
end

#timestampObject

Returns the value of attribute timestamp.



6
7
8
# File 'lib/conify/sso.rb', line 6

def timestamp
  @timestamp
end

#tokenObject

Returns the value of attribute token.



6
7
8
# File 'lib/conify/sso.rb', line 6

def token
  @token
end

#urlObject

Returns the value of attribute url.



6
7
8
# File 'lib/conify/sso.rb', line 6

def url
  @url
end

Instance Method Details

#find_available_portObject



81
82
83
84
85
86
# File 'lib/conify/sso.rb', line 81

def find_available_port
  server = TCPServer.new('127.0.0.1', 0)
  server.addr[1]
ensure
  server.close if server
end

#full_urlObject Also known as: get_url



37
38
39
# File 'lib/conify/sso.rb', line 37

def full_url
  [ url, path, querystring ].join
end

#make_token(t) ⇒ Object



51
52
53
# File 'lib/conify/sso.rb', line 51

def make_token(t)
  Digest::SHA1.hexdigest([external_uuid, @salt, t].join(':'))
end

#messageObject



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

def message
  if self.POST?
    "POSTing #{query_data} to #{post_url} via proxy on port #{@proxy_port}"
  else
    "Opening #{full_url}"
  end
end

#pathObject



25
26
27
# File 'lib/conify/sso.rb', line 25

def path
  self.POST? ? URI.parse(url).path : "/conflux/resources/#{external_uuid}"
end

#POST?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/conify/sso.rb', line 29

def POST?
  @use_post
end

#post_urlObject



42
43
44
# File 'lib/conify/sso.rb', line 42

def post_url
  url
end

#query_dataObject



60
61
62
# File 'lib/conify/sso.rb', line 60

def query_data
  query_params.map{|p| p.join('=')}.join('&')
end

#query_paramsObject



64
65
66
67
68
69
70
71
# File 'lib/conify/sso.rb', line 64

def query_params
  {
    'id' => external_uuid,
    'token' => @token,
    'timestamp' => @timestamp.to_s,
    'email' => @external_username
  }
end

#querystringObject



55
56
57
58
# File 'lib/conify/sso.rb', line 55

def querystring
  return '' unless @salt
  '?' + query_data
end

#sso_urlObject



33
34
35
# File 'lib/conify/sso.rb', line 33

def sso_url
  self.POST? ? "http://localhost:#{@proxy_port}/" : full_url
end