Repository navigation
Expand file tree
/
Copy pathkeychain.js
More file actions
106 lines (103 loc) · 2.46 KB
/
Copy pathkeychain.js
File metadata and controls
106 lines (103 loc) · 2.46 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
const execa = require('execa')
const chalk = require('chalk')
const keychain = {
askForSudo: async () => {
try {
const process = await execa('sudo', ['echo', '"OK"'])
// console.log(chalk.yellow(process.cmd))
if (process.stderr) {
console.log(chalk.red(process.stderr))
return false
}
return true
} catch (error) {
// console.log(chalk.red(error.cmd))
console.log(error.stderr)
return false
}
},
getKeychain: async (domain, keychain = 'login') => {
try {
const process = await execa('security', ['list-keychains', '-d', domain])
// console.log(chalk.yellow(process.cmd))
const keychains = process.stdout.split('\n').map(str =>
str
.trim()
.toLowerCase()
.replace(/"/g, '')
)
const activeKeychain = keychains.find(s => s.search(keychain) >= 0)
if (activeKeychain) {
return activeKeychain
} else {
return false
}
} catch (error) {
// console.log(chalk.red(error.cmd))
console.log(error.stderr)
return false
}
},
find: async (name, keychain) => {
try {
const process = await execa('security', [
'find-certificate',
'-c',
name,
'-p',
keychain,
])
// console.log(chalk.yellow(process.cmd))
if (process.stdout.includes('-----BEGIN CERTIFICATE-----')) {
return true // found cert
}
return false
} catch (error) {
// console.log(chalk.red(error.cmd))
if (error.code == 44) {
return false
}
throw new Error(error.stderr)
}
},
add: async (cert, keychain) => {
try {
const process = await execa('security', [
'add-trusted-cert',
'-r',
'trustRoot',
'-k',
keychain,
cert,
])
// console.log(chalk.yellow(process.cmd))
if (process.stderr) {
return false
}
return true
} catch (error) {
console.log(error.stderr)
return false
}
},
remove: async (name, keychain) => {
try {
const process = await execa('security', [
'delete-certificate',
'-c',
name,
'-t',
keychain,
])
// console.log(chalk.yellow(process.cmd))
if (process.stderr) {
return false
}
return true
} catch (error) {
console.log(error.stderr)
return false
}
},
}
module.exports = keychain