# frozen_string_literal: true

require 'tty-prompt'

module PWN
  module Plugins
    # This plugin is used to assist in masking a password when entered in via
    # STDIN to prevent would-be shoulder surfers from obtaining password
    # information.  This plugin is useful when demonstrating the functionality
    # of other SP plugins/modules.
    module AuthenticationHelper
      # Supported Method Parameters::
      # PWN::Plugins::AuthenticationHelper.username(
      #   prompt: 'optional - string to display at prompt (Default: Username)'
      # )

      public_class_method def self.username
        prompt = opts[:prompt] ||= 'Username'
        user = TTY::Prompt.new.ask("#{prompt}: ")
        user.to_s.strip.chomp.scrub
      rescue StandardError => e
        raise e
      end

      # Supported Method Parameters::
      # PWN::Plugins::AuthenticationHelper.mask_password(
      #   prompt: 'optional - string to display at prompt (Default: Password)'
      # )

      public_class_method def self.mask_password(opts = {})
        prompt = opts[:prompt] ||= 'Password'

        pass = TTY::Prompt.new.mask("#{prompt}: ")
        pass.to_s.strip.chomp.scrub
      rescue Interrupt
        puts "#{self}.#{__method__} => Goodbye."
      rescue StandardError => e
        raise e
      end

      # Supported Method Parameters::
      # PWN::Plugins::AuthenticationHelper.mfa(
      #   prompt: 'optional - string to display at prompt (Default: MFA Token)'
      # )

      public_class_method def self.mfa(opts = {})
        prompt = opts[:prompt] ||= 'MFA Token'

        mfa = TTY::Prompt.new.ask("#{prompt}: ")
        mfa.to_s.strip.chomp.scrub
      rescue Interrupt
        puts "#{self}.#{__method__} => Goodbye."
      rescue StandardError => e
        raise e
      end

      # Author(s):: 0day Inc. <support@0dayinc.com>

      public_class_method def self.authors
        "AUTHOR(S):
          0day Inc. <support@0dayinc.com>
        "
      end

      # Display Usage for this Module

      public_class_method def self.help
        puts "USAGE:
          #{self}.username(
            prompt: 'optional - string to display at prompt'
          )

          #{self}.mask_password(
            prompt: 'optional - string to display at prompt'
          )

          #{self}.mfa(
            prompt: 'optional - string to display at prompt'
          )

          #{self}.authors
        "
      end
    end
  end
end
