Class: Heroku::Kensa::Sso

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Sso

Returns a new instance of Sso.



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

def initialize(data)
  @id   = data[:id]
  @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

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/heroku/kensa/sso.rb', line 7

def id
  @id
end

#proxy_portObject

Returns the value of attribute proxy_port.



7
8
9
# File 'lib/heroku/kensa/sso.rb', line 7

def proxy_port
  @proxy_port
end

#timestampObject

Returns the value of attribute timestamp.



7
8
9
# File 'lib/heroku/kensa/sso.rb', line 7

def timestamp
  @timestamp
end

#tokenObject

Returns the value of attribute token.



7
8
9
# File 'lib/heroku/kensa/sso.rb', line 7

def token
  @token
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/heroku/kensa/sso.rb', line 7

def url
  @url
end

Instance Method Details

#base64_url_variant(text) ⇒ Object



95
96
97
98
# File 'lib/heroku/kensa/sso.rb', line 95

def base64_url_variant(text)
  base64 = [text].pack('m').gsub('=', '').gsub("\n", '')
  base64.tr('+/','-_')
end

#find_available_portObject



113
114
115
116
117
118
# File 'lib/heroku/kensa/sso.rb', line 113

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



44
45
46
# File 'lib/heroku/kensa/sso.rb', line 44

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

#make_token(t) ⇒ Object



58
59
60
# File 'lib/heroku/kensa/sso.rb', line 58

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

#messageObject



100
101
102
103
104
105
106
# File 'lib/heroku/kensa/sso.rb', line 100

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

#pathObject



24
25
26
27
28
29
30
# File 'lib/heroku/kensa/sso.rb', line 24

def path
  if self.POST?
    URI.parse(url).path
  else
    "/heroku/resources/#{id}"
  end
end

#POST?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/heroku/kensa/sso.rb', line 32

def POST?
  @use_post
end

#post_urlObject



49
50
51
# File 'lib/heroku/kensa/sso.rb', line 49

def post_url
  url
end

#query_dataObject



67
68
69
# File 'lib/heroku/kensa/sso.rb', line 67

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

#query_paramsObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/heroku/kensa/sso.rb', line 71

def query_params
  { 'token'     => @token,
    'timestamp' => @timestamp.to_s,
    'nav-data'  => sample_nav_data,
    'email'     => '[email protected]',
    'app'       => 'myapp'
  }.tap do |params|
    params.merge!('id' => @id) if self.POST?
  end
end

#querystringObject



62
63
64
65
# File 'lib/heroku/kensa/sso.rb', line 62

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

#run_proxyObject



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/heroku/kensa/sso.rb', line 120

def run_proxy
  return unless self.POST?
  server = PostProxy.new self
  @proxy = server

  trap("INT") { server.stop }
  pid = fork do
    server.start
  end
  at_exit { server.stop; Process.waitpid pid }
end

#sample_nav_dataObject



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/heroku/kensa/sso.rb', line 82

def sample_nav_data
  json = OkJson.encode({
    :addon => 'Your Addon',
    :appname => 'myapp',
    :addons => [
      { :slug => 'cron', :name => 'Cron' },
      { :slug => 'custom_domains+wildcard', :name => 'Custom Domains + Wildcard' },
      { :slug => 'youraddon', :name => 'Your Addon', :current => true },
    ]
  })
  base64_url_variant(json)
end

#sso_urlObject



36
37
38
39
40
41
42
# File 'lib/heroku/kensa/sso.rb', line 36

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

#startObject



108
109
110
111
# File 'lib/heroku/kensa/sso.rb', line 108

def start
  run_proxy
  self
end