forked from samuong/alpaca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnomad_darwin.go
61 lines (55 loc) · 1.83 KB
/
nomad_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
package main
import (
"errors"
"github.com/keybase/go-keychain"
"os/exec"
"strings"
)
var testKeychain *keychain.Keychain
var execCommand = exec.Command
func init() {
getCredentialsFromKeyring = getCredentialsFromNoMAD
}
func readDefaultForNoMAD(key string) (string, error) {
cmd := execCommand("defaults", "read", "com.trusourcelabs.NoMAD", key)
out, err := cmd.Output()
if err != nil {
return "", errors.New("NoMAD configuration key not found.")
}
return strings.TrimSpace(string(out)), nil
}
func readPasswordFromKeychain(userPrincipal string) string {
// https://nomad.menu/help/keychain-usage/
query := keychain.NewItem()
if testKeychain != nil {
query.SetMatchSearchList(*testKeychain)
}
query.SetSecClass(keychain.SecClassGenericPassword)
query.SetAccount(userPrincipal)
query.SetReturnAttributes(true)
query.SetReturnData(true)
results, err := keychain.QueryItem(query)
if err != nil || len(results) != 1 || results[0].Label != "NoMAD" {
return ""
}
return string(results[0].Data)
}
func getCredentialsFromNoMAD() (authenticator, error) {
useKeychain, err := readDefaultForNoMAD("UseKeychain")
if err != nil {
return authenticator{}, err
} else if useKeychain != "1" {
return authenticator{}, errors.New(`NoMAD found, but UseKeychain != 1. To sync your AD password to the system keychain (and have Alpaca automatically retrieve it from there) open NoMAD's Preferences dialog and check "Use Keychain".`)
}
userPrincipal, err := readDefaultForNoMAD("UserPrincipal")
if err != nil {
return authenticator{}, err
}
substrs := strings.Split(userPrincipal, "@")
if len(substrs) != 2 {
return authenticator{}, errors.New("Couldn't retrieve AD domain and username from NoMAD.")
}
user, domain := substrs[0], substrs[1]
password := readPasswordFromKeychain(userPrincipal)
return authenticator{domain, user, password}, nil
}