Skip to content

Commit

Permalink
Recent updates to Swift causes getpass to fail in headless terminals
Browse files Browse the repository at this point in the history
  • Loading branch information
mikenachbaur-okta committed Nov 21, 2023
1 parent 4e1ae36 commit 0d07c3f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ import OktaOAuth2

enum UserPasswordError: Error {
case missingUsername
case missingPassword
case invalidDomain
}

extension UserPasswordSignIn {
func promptUsername() throws -> String {
if let username {
return username
}

print("Username: ", terminator: "")
guard let username = readLine(strippingNewline: true) else {
throw UserPasswordError.missingUsername
Expand All @@ -28,8 +33,15 @@ extension UserPasswordSignIn {
}

func promptPassword() throws -> String {
print("Password: ")
if let password {
return password
}

print("Password: ", terminator: "")
let password = String(cString: getpass(""))
if password.isEmpty {
throw UserPasswordError.missingPassword
}
return password
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ struct UserPasswordSignIn: Command {
@Option(help: "The scopes to use.")
var scopes: String = "openid profile"

@Option(help: "Username to use")
var username: String?

@Option(help: "Password")
var password: String?

#if swift(>=5.6)
mutating func run() async throws {
guard #available(macOS 12, *) else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ class SignInTests: XCTestCase {
let command = Command(commandPath, arguments: [
"--issuer", "https://\(domain)/oauth2/default",
"--client-id", clientId,
"--scopes", scopes
"--scopes", scopes,
"--username", username,
"--password", password,
])

command.expect("Username:", response: username)
command.expect("Password:", response: password)

try command.run()

XCTAssertEqual(command.output?["Username"], username)
Expand Down

0 comments on commit 0d07c3f

Please sign in to comment.