Skip to content

Commit

Permalink
A few small updates
Browse files Browse the repository at this point in the history
1 - used apple's swift-format

2 - switched the output to stderr rather than stdout

3 - limited the length of prompt context to 100 characters just-in-case

4 - updated the readme with some more info and added comments at the top of the code.
  • Loading branch information
singe committed Jun 16, 2024
1 parent 7f43f21 commit 60894a3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
33 changes: 23 additions & 10 deletions Sources/main.swift
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
/*
tidcli - Touch ID Command Line
by @singe

tidcli is a simple utility to invoke Touch ID and produce an appropriate exit
code for a successful or failed authentication.

It's primary use is to add TouchID auth to shell scripts.
*/

import Darwin // required for simple fputs stderr output
import Foundation
import LocalAuthentication

let arguments = CommandLine.arguments
var promptMessage = "authenticate to proceed"
if arguments.count > 1 {
promptMessage = arguments[1]
// Limit potential input to 100 characters
promptMessage = String(arguments[1].prefix(100))
}

let context = LAContext()
var error: NSError?

if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: promptMessage) { success, evaluationError in
if success {
print("Authentication was successful.")
} else {
print("Authentication failed.")
}
exit(success ? 0 : 1)
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: promptMessage) {
success, evaluationError in
if success {
fputs("Authentication was successful.\n", stderr)
} else {
fputs("Authentication failed.\n", stderr)
}
exit(success ? 0 : 1)
}
} else {
print("Touch ID is not available.")
exit(1)
fputs("Touch ID is not available.\n", stderr)
exit(1)
}

// Keep the run loop running to wait for the async authentication callback.
Expand Down
29 changes: 26 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
# Outrageously simple touch ID command line prompter

TouchID CLI (tidcli) simply pops a TouchID prompt.
Touch ID Command Line Interface (tidcli) simply pops a Touch ID prompt.

It returns an exit code of 0 on success and 1 on failure.

You can use this to embed additional authentication steps into your shell script or the like.

Custom prompt information can be passed as the first argument.

<img width="372" alt="image" src="https://github.com/singe/tidcli/assets/1150684/999a9a41-75d6-4366-b97b-ee6b425e1c1e">

# Usage

`tidcli [optional prompt context]`

Optional custom prompt information can be passed as the first argument. This is limited to 100 characters.

An example of using it in a bash shell script to exit if there is an authentication failure is:

```
tidcli "EXAMPLE SCRIPT"
if [[ "$?" -ne 0 ]]; then
exit 1
fi
```

# Building

Expand All @@ -17,3 +30,13 @@ Build a release binary with swift by running:
`swift build -c release`

The resulting binary will be in the `.build/release` directory as `tidcli`.

# Notes

## Password fallback not allowed

The Touch ID prompt will contain a "Use Password" button, but only biometric authentication is allowed, so clicking it will result in an authentication failure. If you wanted to change this you could use `.deviceOwnerAuthentication` instead of `.deviceOwnerAuthenticationWithBiometrics`.

## Output on standard error

Success and failure messages are output to standard error.

0 comments on commit 60894a3

Please sign in to comment.