Skip to content

Commit

Permalink
Merge pull request #33 from vapor/secure-entry
Browse files Browse the repository at this point in the history
add secure entry code
  • Loading branch information
loganwright authored Apr 10, 2017
2 parents 8ee7872 + 711f5cf commit 6642472
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 56 deletions.
9 changes: 7 additions & 2 deletions Sources/Console/Console/Console+Ask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ extension ConsoleProtocol {
Requests input from the console
after displaying the desired prompt.
*/
public func ask(_ prompt: String, style: ConsoleStyle = .info) -> String {
public func ask(_ prompt: String, style: ConsoleStyle = .info, secure: Bool = false) -> String {
output(prompt, style: style)
output("> ", style: style, newLine: false)
return input()
if secure {
return secureInput()
} else {
return input()
}
}
}

extension ConsoleProtocol {
public func askList(withTitle title: String, from list: [String]) -> String? {
info(title)
Expand Down
94 changes: 43 additions & 51 deletions Sources/Console/Console/Console.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,61 @@ import libc
import Foundation
import Core

/**
Protocol for powering styled Console I/O.
*/

/// Protocol for powering styled Console I/O.
public protocol ConsoleProtocol {
/**
Outputs a String in the given style to
the console. If newLine is true, the next
output will appear on a new line.
*/
/// Outputs a String in the given style to
/// the console. If newLine is true, the next
/// output will appear on a new line.
func output(_ string: String, style: ConsoleStyle, newLine: Bool)

/**
Returns a String of input read from the
console until a line feed character was found.

The line feed character should not be included.
*/
/// Returns a String of input read from the
/// console until a line feed character was found.
///
/// The line feed character should not be included.
func input() -> String

/**
Clears previously printed Console outputs
according to the ConsoleClear type given.
*/
/// Returns a string of input read from the console
/// until a line feed character was found,
/// hides entry for security
func secureInput() -> String

/// Clears previously printed Console outputs
/// according to the ConsoleClear type given.
func clear(_ clear: ConsoleClear)

/**
Executes a task using the supplied
FileHandles for IO.
*/
/// Executes a task using the supplied
/// FileHandles for IO.
func execute(program: String, arguments: [String], input: Int32?, output: Int32?, error: Int32?) throws

/**
When set, all `confirm(_ prompt:)` methods
will return the value. When nil, the confirm
calls will wait for input from `input()`
*/
/// When set, all `confirm(_ prompt:)` methods
/// will return the value. When nil, the confirm
/// calls will wait for input from `input()`
var confirmOverride: Bool? { get }

/**
The size of the console window used for centering.
*/
/// The size of the console window used for centering.
var size: (width: Int, height: Int) { get }

/**
Executes a command using the console's POSIX subsystem.
The input, output, and error streams will appear
as though they are coming from the console program.

- parameter program: Program to be executed
- parameter arguments: Input arguments to the program

- throws: ConsoleError.execute(Int)
*/
/// Executes a command using the console's POSIX subsystem.
/// The input, output, and error streams will appear
/// as though they are coming from the console program.
///
/// - parameter program: Program to be executed
/// - parameter arguments: Input arguments to the program
///
/// - throws: ConsoleError.execute(Int)
func foregroundExecute(program: String, arguments: [String]) throws

/**
Executes a command using the console's POSIX subsystem.
The input, output, and error streams will be input
and returned as strings.

- parameter program: Program to be executed
- parameter arguments: Input arguments to the program

- throws: ConsoleError.subexecute(Int, String)
/// Executes a command using the console's POSIX subsystem.
/// The input, output, and error streams will be input
/// and returned as strings.
///
/// - parameter program: Program to be executed
/// - parameter arguments: Input arguments to the program

- returns: The return string from the method
*/
/// - throws: ConsoleError.subexecute(Int, String)
///
/// - returns: The return string from the method
func backgroundExecute(program: String, arguments: [String]) throws -> String

/// Upon a console instance being killed for example w/ ctrl+c
Expand Down Expand Up @@ -137,6 +125,10 @@ extension ConsoleProtocol {
public func backgroundExecute<Type: BytesInitializable>(commands: String...) throws -> Type {
return try backgroundExecute(commands: commands)
}

public func registerKillListener(_ listener: @escaping (Int32) -> Void) {

}
}

extension ConsoleProtocol {
Expand Down
14 changes: 11 additions & 3 deletions Sources/Console/Terminal/Terminal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,21 @@ public class Terminal: ConsoleProtocol {
#endif
}

/**
Reads a line of input from the terminal.
*/
public func input() -> String {
return readLine(strippingNewline: true) ?? ""
}

public func secureInput() -> String {
// http://stackoverflow.com/a/30878869/2611971
let entry: UnsafeMutablePointer<Int8> = getpass("")
let pointer: UnsafePointer<CChar> = .init(entry)
var pass = String(validatingUTF8: pointer) ?? ""
if pass.hasSuffix("\n") {
pass = pass.makeBytes().dropLast().makeString()
}
return pass
}

public func execute(program: String, arguments: [String], input: Int32? = nil, output: Int32? = nil, error: Int32? = nil) throws {
var pid = UnsafeMutablePointer<pid_t>.allocate(capacity: 1)
pid.initialize(to: pid_t())
Expand Down
6 changes: 6 additions & 0 deletions Tests/ConsoleTests/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import XCTest
import Console

class TestConsole: ConsoleProtocol {

var inputBuffer: String = ""
var outputBuffer: String = ""

Expand All @@ -22,6 +23,11 @@ class TestConsole: ConsoleProtocol {
return input
}

func secureInput() -> String {
return input()
}


func clear(_ clear: ConsoleClear) {
//
}
Expand Down

0 comments on commit 6642472

Please sign in to comment.