Class: Xplenty::Kensa::Sso
- Inherits:
-
Object
- Object
- Xplenty::Kensa::Sso
- Defined in:
- lib/xplenty/kensa/sso.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#proxy_port ⇒ Object
Returns the value of attribute proxy_port.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
-
#token ⇒ Object
Returns the value of attribute token.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #base64_url_variant(text) ⇒ Object
- #find_available_port ⇒ Object
- #full_url ⇒ Object (also: #get_url)
-
#initialize(data) ⇒ Sso
constructor
A new instance of Sso.
- #make_token(t) ⇒ Object
- #message ⇒ Object
- #path ⇒ Object
- #POST? ⇒ Boolean
- #post_url ⇒ Object
- #query_data ⇒ Object
- #query_params ⇒ Object
- #querystring ⇒ Object
- #run_proxy ⇒ Object
- #sample_nav_data ⇒ Object
- #sso_url ⇒ Object
- #start ⇒ Object
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/xplenty/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
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/xplenty/kensa/sso.rb', line 7 def id @id end |
#proxy_port ⇒ Object
Returns the value of attribute proxy_port.
7 8 9 |
# File 'lib/xplenty/kensa/sso.rb', line 7 def proxy_port @proxy_port end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
7 8 9 |
# File 'lib/xplenty/kensa/sso.rb', line 7 def @timestamp end |
#token ⇒ Object
Returns the value of attribute token.
7 8 9 |
# File 'lib/xplenty/kensa/sso.rb', line 7 def token @token end |
#url ⇒ Object
Returns the value of attribute url.
7 8 9 |
# File 'lib/xplenty/kensa/sso.rb', line 7 def url @url end |
Instance Method Details
#base64_url_variant(text) ⇒ Object
95 96 97 98 |
# File 'lib/xplenty/kensa/sso.rb', line 95 def base64_url_variant(text) base64 = [text].pack('m').gsub('=', '').gsub("\n", '') base64.tr('+/','-_') end |
#find_available_port ⇒ Object
113 114 115 116 117 118 |
# File 'lib/xplenty/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_url ⇒ Object Also known as: get_url
44 45 46 |
# File 'lib/xplenty/kensa/sso.rb', line 44 def full_url [ url, path, querystring ].join end |
#make_token(t) ⇒ Object
58 59 60 |
# File 'lib/xplenty/kensa/sso.rb', line 58 def make_token(t) Digest::SHA1.hexdigest([@id, @salt, t].join(':')) end |
#message ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/xplenty/kensa/sso.rb', line 100 def if self.POST? "POSTing #{query_data} to #{post_url} via proxy on port #{@proxy_port}" else "Opening #{full_url}" end end |
#path ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/xplenty/kensa/sso.rb', line 24 def path if self.POST? URI.parse(url).path else "/xplenty/resources/#{id}" end end |
#POST? ⇒ Boolean
32 33 34 |
# File 'lib/xplenty/kensa/sso.rb', line 32 def POST? @use_post end |
#post_url ⇒ Object
49 50 51 |
# File 'lib/xplenty/kensa/sso.rb', line 49 def post_url url end |
#query_data ⇒ Object
67 68 69 |
# File 'lib/xplenty/kensa/sso.rb', line 67 def query_data query_params.map{|p| p.join('=')}.join('&') end |
#query_params ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/xplenty/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 |
#querystring ⇒ Object
62 63 64 65 |
# File 'lib/xplenty/kensa/sso.rb', line 62 def querystring return '' unless @salt '?' + query_data end |
#run_proxy ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/xplenty/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_data ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/xplenty/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_url ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/xplenty/kensa/sso.rb', line 36 def sso_url if self.POST? "http://localhost:#{@proxy_port}/" else full_url end end |
#start ⇒ Object
108 109 110 111 |
# File 'lib/xplenty/kensa/sso.rb', line 108 def start run_proxy self end |