Class: Orthrus::SSH::HTTPAgent

Inherits:
Object
  • Object
show all
Defined in:
lib/orthrus/ssh/http_agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, key_manager = nil) ⇒ HTTPAgent

Returns a new instance of HTTPAgent.



10
11
12
13
14
# File 'lib/orthrus/ssh/http_agent.rb', line 10

def initialize(url, key_manager=nil)
  @url = url
  @key_manager ||= KeyManager.new
  @access_token = nil
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



16
17
18
# File 'lib/orthrus/ssh/http_agent.rb', line 16

def access_token
  @access_token
end

Instance Method Details

#check(user, k) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/orthrus/ssh/http_agent.rb', line 22

def check(user, k)
  id = Rack::Utils.escape(k.public_identity)
  user = Rack::Utils.escape(user)

  url = @url + "?state=find&user=#{user}&id=#{id}"
  response = Net::HTTP.get_response url
  params = Rack::Utils.parse_query response.body

  return nil unless params["code"] == "check"

  [params['session_id'], params['nonce']]
end

#load_key(key) ⇒ Object



18
19
20
# File 'lib/orthrus/ssh/http_agent.rb', line 18

def load_key(key)
  @key_manager.load_key key
end

#negotiate(k, sid, sig) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/orthrus/ssh/http_agent.rb', line 35

def negotiate(k, sid, sig)
  sig = Rack::Utils.escape sig

  url = @url + "?state=signed&sig=#{sig}&session_id=#{sid}"

  response = Net::HTTP.get_response url
  params = Rack::Utils.parse_query response.body

  if params['code'] == "verified"
    return params['access_token']
  end

  return nil
end

#start(user) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/orthrus/ssh/http_agent.rb', line 50

def start(user)
  @key_manager.each_key do |k|
    sid, data = check(user, k)
    next unless sid

    sig = @key_manager.sign k, data, true

    token = negotiate(k, sid, sig)
    if token
      @access_token = token
      return
    end
  end

  raise "Unable to find key to authenticate with"
end