forked from FiloSottile/yubikey-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt_darwin.go
75 lines (68 loc) · 1.93 KB
/
prompt_darwin.go
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
// Copyright 2020 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/zalando/go-keyring"
"os/exec"
"text/template"
)
var scriptTemplate = template.Must(template.New("script").Parse(`
var app = Application.currentApplication()
app.includeStandardAdditions = true
app.displayDialog(
"YubiKey serial number: {{ .Serial }} " +
"({{ .Tries }} tries remaining)\n\n" +
"Please enter your PIN:", {
defaultAnswer: "",
withTitle: "yubikey-agent PIN prompt",
buttons: ["Cancel", "OK", "OK and save to keychain"],
defaultButton: "OK",
cancelButton: "Cancel",
hiddenAnswer: true,
})`))
var keychainServiceName = "yubikey-agent"
func getPIN(serial uint32, retries int) (string, error) {
keychainUserName := fmt.Sprintf("yubikey-agent-%d", serial)
if retries >= 3 {
// get password
secret, err := keyring.Get(keychainServiceName, keychainUserName)
if err != nil && err != keyring.ErrNotFound {
return "", err
}
if err == nil {
return secret, nil
}
}
script := new(bytes.Buffer)
if err := scriptTemplate.Execute(script, map[string]interface{}{
"Serial": serial, "Tries": retries,
}); err != nil {
return "", err
}
c := exec.Command("osascript", "-s", "se", "-l", "JavaScript")
c.Stdin = script
out, err := c.Output()
if err != nil {
return "", fmt.Errorf("failed to execute osascript: %v", err)
}
var x struct {
PIN string `json:"textReturned"`
PressedButtonLabel string `json:"buttonReturned"`
}
if err := json.Unmarshal(out, &x); err != nil {
return "", fmt.Errorf("failed to parse osascript output: %v", err)
}
if x.PressedButtonLabel == "OK and save to keychain" {
err := keyring.Set(keychainServiceName, keychainUserName, x.PIN)
if err != nil {
return "", err
}
}
return x.PIN, nil
}