Class: Lita::Handlers::Openvpnas

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/openvpnas.rb

Instance Method Summary collapse

Instance Method Details

#openvpn_as_otp_unlock(response) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lita/handlers/openvpnas.rb', line 18

def openvpn_as_otp_unlock(response)
  user = response.matches[0][3]
  ssh_user = config.ssh_user || 'lita'
  ssh_host = config.hostname
  path_to_sacli = config.sacli_dir || '/usr/local/openvpn_as/scripts'
  username = response.user.name.split(/\s/).first

  response.reply("#{username}, let me unlock that user's OpenVPN authenticator for you.")

  exception = nil

  remote = Rye::Box.new(
    ssh_host,
    user: ssh_user,
    auth_methods: ['publickey'],
    password_prompt: false
  )

  result = begin
    Timeout::timeout(60) do
      remote.cd 'path_to_sacli'
      # Need to use sudo
      remote.enable_sudo
      # scary...
      remote.disable_safe_mode

      remote.execute "./sacli -u #{user} --lock 0 GoogleAuthLock 2>&1"
    end
  rescue Rye::Err => e
    exception = e
  rescue StandardError => e
    exception = e
  ensure
    remote.disconnect
  end

  if exception
    response.reply_with_mention "That OpenVPN authenticator didn't seem to unlock... ;-("
    response.reply "/code " + exception.message
  end

  # build a reply
  response.reply_with_mention("That OpenVPN authenticator is now available for #{user}!")
end