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) ⇒ HTTPAgent

Returns a new instance of HTTPAgent.



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

def initialize(url)
  @url = url
  @keys = []
  @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

#add_key(key) ⇒ Object



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

def add_key(key)
  @keys << Orthrus::SSH.load_private(key)
end

#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

#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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/orthrus/ssh/http_agent.rb', line 50

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

    sig = k.hexsign(data)

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

  if Agent.available?
    agent = Agent.connect
    agent.identities.each do |k|
      sid, data = check(user, k)
      next unless sid

      type, sig = agent.hexsign k, data

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

  raise "Unable to find key to authenticate with"
end