-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_title_editor_credentials.sh
More file actions
604 lines (520 loc) · 19 KB
/
Copy pathsetup_title_editor_credentials.sh
File metadata and controls
604 lines (520 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#!/usr/bin/env bash
#
# Title Editor API Credentials Setup Script
# Version: 1.3.2
# Revised: 2026.03.16
#
# Stores Title Editor API host/username/password securely in macOS Keychain
# for use with title_editor_menu.sh.
#
# Usage:
# bash setup_title_editor_credentials.sh
# bash setup_title_editor_credentials.sh --verify
# bash setup_title_editor_credentials.sh --verify --debug
#
# Copyright (c) 2026 University of Utah, Marriott Library IT.
# All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appears in all copies and
# that both that copyright notice and this permission notice appear
# in supporting documentation, and that the name of The University
# of Utah not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission. This software is supplied as is without expressed or
# implied warranties of any kind.
set -euo pipefail
SCRIPT_VERSION="1.3.2"
KEYCHAIN_SERVICE="TitleEditorAPI"
KEYCHAIN_ACCOUNT_HOST="title_editor_host"
KEYCHAIN_ACCOUNT_USER="title_editor_user"
KEYCHAIN_ACCOUNT_PASS="title_editor_password"
KEYCHAIN_PATH="${HOME}/Library/Keychains/login.keychain-db"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TITLE_EDITOR_MENU_PATH="${SCRIPT_DIR}/title_editor_menu.sh"
SECURITY_PATH="/usr/bin/security"
DEBUG_MODE=false
VERIFY_MODE=false
MIGRATE_MODE=false
TITLE_EDITOR_API_CTRL_PATH=""
VERIFY_HTTP_STATUS=""
VERIFY_HTTP_BODY=""
VERIFY_CURL_OUTPUT=""
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1" >&2; }
debug_log() {
[[ "$DEBUG_MODE" == "true" ]] || return 0
echo -e "${YELLOW}[DEBUG]${NC} $1"
}
secret_fingerprint() {
local value="$1"
if [[ -z "$value" ]]; then
printf '%s' "empty"
return 0
fi
if command -v shasum >/dev/null 2>&1; then
printf '%s' "$value" | shasum -a 256 | awk '{print substr($1,1,12)}'
elif command -v openssl >/dev/null 2>&1; then
printf '%s' "$value" | openssl dgst -sha256 | awk '{print substr($NF,1,12)}'
else
printf '%s' "nohash"
fi
}
debug_secret() {
local label="$1"
local value="$2"
debug_log "${label}: len=${#value}, fp=$(secret_fingerprint "$value")"
}
show_usage() {
cat <<EOF
Title Editor Credentials Setup v${SCRIPT_VERSION}
Usage:
$0 Store credentials (prompts for host/user/password)
$0 --verify Verify credentials currently stored in Keychain
$0 --migrate Migrate existing credentials to login keychain
$0 --debug Enable safe debug output (no plaintext secrets)
Options:
--verify, -v Verify keychain credentials via live API auth test
--migrate, -m Copy existing keychain credentials into login keychain
--debug, -d Print debug details (host/user and password fingerprint/length)
--help, -h Show this help
EOF
}
parse_args() {
local arg
for arg in "$@"; do
case "$arg" in
--verify|-v) VERIFY_MODE=true ;;
--migrate|-m) MIGRATE_MODE=true ;;
--debug|-d) DEBUG_MODE=true ;;
--help|-h) show_usage; exit 0 ;;
*)
log_error "Unknown option: $arg"
show_usage
exit 1
;;
esac
done
}
find_api_ctrl_script() {
local script_dir candidate
script_dir="$(cd "$(dirname "$0")" && pwd)"
for candidate in \
"${script_dir}/title_editor_api_ctrl.sh" \
"${HOME}/title_editor_api_ctrl.sh" \
"/usr/local/bin/title_editor_api_ctrl.sh"
do
if [[ -f "$candidate" ]]; then
TITLE_EDITOR_API_CTRL_PATH="$candidate"
return 0
fi
done
return 1
}
test_api_credentials_via_ctrl() {
local host="$1"
local user="$2"
local pass="$3"
local output rc
host=$(normalize_host "$host")
find_api_ctrl_script || return 10
output=$({
source "$TITLE_EDITOR_API_CTRL_PATH"
TITLE_EDITOR_API_HOST="$host"
TITLE_EDITOR_API_USER="$user"
TITLE_EDITOR_API_PW="$pass"
TITLE_EDITOR_API_KEEP_ALIVE=false
TITLE_EDITOR_API_PW_FALLBACK=false
title_editor_api_connect >/dev/null
} 2>&1)
rc=$?
VERIFY_CURL_OUTPUT="$output"
if [[ $rc -eq 0 ]]; then
VERIFY_HTTP_STATUS="200"
VERIFY_HTTP_BODY="Connected via title_editor_api_ctrl.sh"
return 0
fi
if echo "$output" | grep -q "AuthenticationError\|Incorrect user name or password"; then
VERIFY_HTTP_STATUS="401"
VERIFY_HTTP_BODY="$output"
return 2
fi
VERIFY_HTTP_STATUS="000"
VERIFY_HTTP_BODY="$output"
return 4
}
require_macos_security() {
if ! command -v security >/dev/null 2>&1; then
log_error "macOS security CLI not found. This script must run on macOS."
exit 1
fi
if [[ ! -f "$KEYCHAIN_PATH" ]]; then
log_error "Login keychain not found at: $KEYCHAIN_PATH"
exit 1
fi
}
delete_entry_if_exists() {
local account="$1"
security delete-generic-password -s "$KEYCHAIN_SERVICE" -a "$account" "$KEYCHAIN_PATH" >/dev/null 2>&1 || true
}
store_value() {
local account="$1"
local label="$2"
local value="$3"
delete_entry_if_exists "$account"
if security add-generic-password \
-s "$KEYCHAIN_SERVICE" \
-a "$account" \
-l "$label" \
-D "application password" \
-T "$SECURITY_PATH" \
-T "$TITLE_EDITOR_MENU_PATH" \
-w "$value" \
"$KEYCHAIN_PATH" >/dev/null 2>&1; then
return 0
fi
return 1
}
read_required() {
local prompt="$1"
local value
read -r -p "$prompt" value
if [[ -z "$value" ]]; then
log_error "Value cannot be empty."
exit 1
fi
printf '%s' "$value"
}
read_password_confirm() {
local pw1 pw2
read -r -s -p "Title Editor password: " pw1
printf '\n' >&2
read -r -s -p "Confirm password: " pw2
printf '\n' >&2
if [[ -z "$pw1" ]]; then
log_error "Password cannot be empty."
exit 1
fi
if [[ "$pw1" != "$pw2" ]]; then
log_error "Passwords do not match."
exit 1
fi
printf '%s' "$pw1"
}
verify_entries() {
local host user pass
host=$(security find-generic-password -s "$KEYCHAIN_SERVICE" -a "$KEYCHAIN_ACCOUNT_HOST" -w "$KEYCHAIN_PATH" 2>/dev/null || true)
user=$(security find-generic-password -s "$KEYCHAIN_SERVICE" -a "$KEYCHAIN_ACCOUNT_USER" -w "$KEYCHAIN_PATH" 2>/dev/null || true)
pass=$(security find-generic-password -s "$KEYCHAIN_SERVICE" -a "$KEYCHAIN_ACCOUNT_PASS" -w "$KEYCHAIN_PATH" 2>/dev/null || true)
[[ -n "$host" && -n "$user" && -n "$pass" ]]
}
normalize_host() {
local host="$1"
host="${host#https://}"
host="${host#http://}"
host="${host%%/*}"
printf '%s' "$host"
}
read_stored_credentials() {
local host user pass
host=$(security find-generic-password -s "$KEYCHAIN_SERVICE" -a "$KEYCHAIN_ACCOUNT_HOST" -w "$KEYCHAIN_PATH" 2>/dev/null || true)
user=$(security find-generic-password -s "$KEYCHAIN_SERVICE" -a "$KEYCHAIN_ACCOUNT_USER" -w "$KEYCHAIN_PATH" 2>/dev/null || true)
pass=$(security find-generic-password -s "$KEYCHAIN_SERVICE" -a "$KEYCHAIN_ACCOUNT_PASS" -w "$KEYCHAIN_PATH" 2>/dev/null || true)
if [[ -z "$host" || -z "$user" || -z "$pass" ]]; then
return 1
fi
printf '%s\n%s\n%s\n' "$host" "$user" "$pass"
}
read_credentials_from_any_keychain() {
local host user pass
host=$(security find-generic-password -s "$KEYCHAIN_SERVICE" -a "$KEYCHAIN_ACCOUNT_HOST" -w 2>/dev/null || true)
user=$(security find-generic-password -s "$KEYCHAIN_SERVICE" -a "$KEYCHAIN_ACCOUNT_USER" -w 2>/dev/null || true)
pass=$(security find-generic-password -s "$KEYCHAIN_SERVICE" -a "$KEYCHAIN_ACCOUNT_PASS" -w 2>/dev/null || true)
if [[ -z "$host" || -z "$user" || -z "$pass" ]]; then
return 1
fi
printf '%s\n%s\n%s\n' "$host" "$user" "$pass"
}
debug_keychain_presence() {
[[ "$DEBUG_MODE" == "true" ]] || return 0
local accounts account value
accounts=("$KEYCHAIN_ACCOUNT_HOST" "$KEYCHAIN_ACCOUNT_USER" "$KEYCHAIN_ACCOUNT_PASS")
debug_log "Checking expected entries in keychain: $KEYCHAIN_PATH"
for account in "${accounts[@]}"; do
value=$(security find-generic-password -s "$KEYCHAIN_SERVICE" -a "$account" -w "$KEYCHAIN_PATH" 2>/dev/null || true)
if [[ -n "$value" ]]; then
if [[ "$account" == "$KEYCHAIN_ACCOUNT_PASS" ]]; then
debug_secret "Entry '$account' (login keychain)" "$value"
else
debug_log "Entry '$account' (login keychain): $value"
fi
else
debug_log "Entry '$account' (login keychain): <missing>"
# Diagnostic only: check if the same service/account exists in any other keychain
value=$(security find-generic-password -s "$KEYCHAIN_SERVICE" -a "$account" -w 2>/dev/null || true)
if [[ -n "$value" ]]; then
if [[ "$account" == "$KEYCHAIN_ACCOUNT_PASS" ]]; then
debug_secret "Entry '$account' found in another keychain" "$value"
else
debug_log "Entry '$account' found in another keychain: $value"
fi
debug_log "Tip: re-run setup to store all entries in login.keychain-db for consistency."
fi
fi
done
}
test_api_credentials() {
local host="$1"
local user="$2"
local pass="$3"
local ctrl_rc
local tmp_body raw_status status
# Preferred path: use the same auth flow as title_editor_menu.sh
if test_api_credentials_via_ctrl "$host" "$user" "$pass"; then
return 0
fi
ctrl_rc=$?
case "$ctrl_rc" in
2)
# Cross-check with direct curl to avoid false negatives from controller path.
;;
4)
# Fall through to curl fallback for additional diagnostics.
;;
*)
# Unknown controller failure; try curl fallback.
;;
esac
host=$(normalize_host "$host")
tmp_body=$(mktemp /tmp/title_editor_verify.XXXXXX)
raw_status=$(curl \
--silent \
--show-error \
--location \
--location-trusted \
--max-time 60 \
--connect-timeout 60 \
--header "Content-Type: application/json" \
--header "Accept: application/json" \
--header "User-Agent: title-editor-credentials-verify/${SCRIPT_VERSION}" \
--output "$tmp_body" \
--write-out "%{http_code}" \
--request POST \
--user "${user}:${pass}" \
"https://${host}/v2/auth/tokens" 2>&1)
status=$(printf '%s' "$raw_status" | grep -Eo '[0-9]{3}$' || true)
[[ -z "$status" ]] && status="000"
VERIFY_HTTP_STATUS="$status"
VERIFY_CURL_OUTPUT="$raw_status"
VERIFY_HTTP_BODY=$(cat "$tmp_body" 2>/dev/null || true)
rm -f "$tmp_body" >/dev/null 2>&1 || true
case "$VERIFY_HTTP_STATUS" in
200|201)
if [[ "$ctrl_rc" -eq 2 ]]; then
debug_log "Controller auth returned 401, but direct curl auth succeeded. Proceeding with curl result."
fi
return 0
;;
401)
return 2
;;
000)
return 4
;;
*)
return 3
;;
esac
}
verify_mode() {
require_macos_security
print_banner
log_info "Verifying Title Editor credentials from Keychain..."
local creds host user pass
if ! creds=$(read_stored_credentials); then
log_error "Missing one or more Keychain entries for service '${KEYCHAIN_SERVICE}'."
debug_keychain_presence
log_info "Run setup to store credentials:"
log_info " bash ${SCRIPT_DIR}/setup_title_editor_credentials.sh"
log_info "Or migrate existing entries from another keychain:"
log_info " bash ${SCRIPT_DIR}/setup_title_editor_credentials.sh --migrate"
exit 1
fi
host=$(printf '%s\n' "$creds" | sed -n '1p')
user=$(printf '%s\n' "$creds" | sed -n '2p')
pass=$(printf '%s\n' "$creds" | sed -n '3p')
debug_log "Using keychain path: $KEYCHAIN_PATH"
debug_secret "Stored password" "$pass"
log_info "Host: $(normalize_host "$host")"
log_info "User: $user"
log_info "Testing API authentication..."
if test_api_credentials "$host" "$user" "$pass"; then
log_success "Credentials are valid (API authentication succeeded)."
exit 0
fi
case "$VERIFY_HTTP_STATUS" in
401)
log_error "Credentials found in Keychain but authentication failed (401)."
log_info "Update credentials with:"
log_info " bash ${SCRIPT_DIR}/setup_title_editor_credentials.sh"
;;
000)
log_error "Could not verify credentials due to curl/network failure (HTTP 000)."
log_info "Host checked: $(normalize_host "$host")"
if [[ -n "$VERIFY_CURL_OUTPUT" ]]; then
log_info "curl output: ${VERIFY_CURL_OUTPUT}"
fi
;;
*)
log_error "Could not verify credentials (HTTP status: ${VERIFY_HTTP_STATUS:-unknown})."
if [[ -n "$VERIFY_HTTP_BODY" ]]; then
log_info "Response excerpt:"
printf '%s\n' "$VERIFY_HTTP_BODY" | head -5 | sed 's/^/[INFO] /'
fi
log_info "Check host/network and try again."
;;
esac
exit 1
}
migrate_mode() {
require_macos_security
print_banner
log_info "Migrating Title Editor credentials into login keychain..."
if verify_entries; then
log_success "Entries already exist in login keychain. No migration needed."
exit 0
fi
local creds host user pass
if ! creds=$(read_credentials_from_any_keychain); then
log_error "No complete '${KEYCHAIN_SERVICE}' credentials found in other keychains."
log_info "Run setup to enter and save credentials:"
log_info " bash ${SCRIPT_DIR}/setup_title_editor_credentials.sh"
exit 1
fi
host=$(printf '%s\n' "$creds" | sed -n '1p')
user=$(printf '%s\n' "$creds" | sed -n '2p')
pass=$(printf '%s\n' "$creds" | sed -n '3p')
debug_log "Migrating host: $host"
debug_log "Migrating user: $user"
debug_secret "Migrating password" "$pass"
if ! store_value "$KEYCHAIN_ACCOUNT_HOST" "Title Editor API Host" "$host"; then
log_error "Failed to store host during migration."
exit 1
fi
if ! store_value "$KEYCHAIN_ACCOUNT_USER" "Title Editor API Username" "$user"; then
log_error "Failed to store username during migration."
exit 1
fi
if ! store_value "$KEYCHAIN_ACCOUNT_PASS" "Title Editor API Password" "$pass"; then
log_error "Failed to store password during migration."
exit 1
fi
if verify_entries; then
log_success "Migration completed. Entries are now stored in login keychain."
log_info "Next: verify with:"
log_info " bash ${SCRIPT_DIR}/setup_title_editor_credentials.sh --verify --debug"
exit 0
fi
log_error "Migration did not verify in login keychain."
exit 1
}
print_banner() {
echo -e "${BLUE}================================================${NC}"
echo -e "${BLUE} Title Editor Credentials Setup v${SCRIPT_VERSION}${NC}"
echo -e "${BLUE}================================================${NC}"
echo ""
}
main() {
parse_args "$@"
if [[ "$MIGRATE_MODE" == "true" ]]; then
migrate_mode
fi
if [[ "$VERIFY_MODE" == "true" ]]; then
verify_mode
fi
require_macos_security
print_banner
if [[ ! -f "$TITLE_EDITOR_MENU_PATH" ]]; then
log_warn "title_editor_menu.sh not found at: $TITLE_EDITOR_MENU_PATH"
log_warn "Keychain ACL will still be set to this path; ensure the menu script exists there."
else
log_info "Keychain ACL trusted applications:"
log_info " $SECURITY_PATH"
log_info " $TITLE_EDITOR_MENU_PATH"
fi
log_info "Credentials will be stored in your login Keychain"
log_info "Service: ${KEYCHAIN_SERVICE}"
echo ""
local host user password
host=$(read_required "Title Editor server hostname (e.g. yourtenant.appcatalog.jamfcloud.com): ")
host=$(normalize_host "$host")
user=$(read_required "Title Editor username: ")
password=$(read_password_confirm)
debug_log "Using keychain path: $KEYCHAIN_PATH"
debug_log "Entered host: $host"
debug_log "Entered user: $user"
debug_secret "Entered password" "$password"
log_info "Testing API authentication with provided credentials..."
if ! test_api_credentials "$host" "$user" "$password"; then
case "$VERIFY_HTTP_STATUS" in
401)
log_error "Authentication failed (401). Username/password are not valid for Title Editor API."
;;
000)
log_error "Could not contact API endpoint (HTTP 000)."
if [[ -n "$VERIFY_CURL_OUTPUT" ]]; then
log_info "curl output: ${VERIFY_CURL_OUTPUT}"
fi
;;
*)
log_error "Authentication test failed (HTTP ${VERIFY_HTTP_STATUS:-unknown})."
if [[ -n "$VERIFY_HTTP_BODY" ]]; then
log_info "Response excerpt:"
printf '%s\n' "$VERIFY_HTTP_BODY" | head -5 | sed 's/^/[INFO] /'
fi
;;
esac
log_info "Credentials were not saved. Re-run setup with correct values."
exit 1
fi
log_info "Saving credentials to Keychain..."
if ! store_value "$KEYCHAIN_ACCOUNT_HOST" "Title Editor API Host" "$host"; then
log_error "Failed to store host."
exit 1
fi
if ! store_value "$KEYCHAIN_ACCOUNT_USER" "Title Editor API Username" "$user"; then
log_error "Failed to store username."
exit 1
fi
if ! store_value "$KEYCHAIN_ACCOUNT_PASS" "Title Editor API Password" "$password"; then
log_error "Failed to store password."
exit 1
fi
if verify_entries; then
local stored
if stored=$(read_stored_credentials); then
local shost suser spass
shost=$(printf '%s\n' "$stored" | sed -n '1p')
suser=$(printf '%s\n' "$stored" | sed -n '2p')
spass=$(printf '%s\n' "$stored" | sed -n '3p')
debug_log "Stored host: $shost"
debug_log "Stored user: $suser"
debug_secret "Stored password" "$spass"
fi
log_success "Title Editor credentials saved and verified."
echo ""
echo "Next step: run title_editor_menu.sh — it will auto-use Keychain credentials."
else
log_error "Verification failed after saving credentials."
exit 1
fi
}
main "$@"