Skip to content

Commit fa138a7

Browse files
author
Steve Coffman
committed
Add QR Code functionality
1 parent 6fe4c76 commit fa138a7

File tree

11 files changed

+208
-78
lines changed

11 files changed

+208
-78
lines changed

aws-credential-helper.sh

Lines changed: 0 additions & 12 deletions
This file was deleted.

cmd/add.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
/*
2-
Copyright © 2019 NAME HERE <EMAIL ADDRESS>
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
2+
The MIT License (MIT)
3+
4+
Copyright © 2019 StevenACoffman
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
1523
*/
1624
package cmd
1725

cmd/list.go

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
/*
2-
Copyright © 2019 NAME HERE <EMAIL ADDRESS>
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
2+
The MIT License (MIT)
3+
4+
Copyright © 2019 StevenACoffman
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
1523
*/
1624
package cmd
1725

@@ -56,7 +64,7 @@ func parseValue(line string) string {
5664
return ""
5765
}
5866

59-
// List lists secret, identified by service, from the keyring.
67+
// List shows secret key names, identified by service, from the keyring.
6068
func List(service string) (string, error) {
6169
out, err := exec.Command(
6270
execPathKeychain,
@@ -67,13 +75,18 @@ func List(service string) (string, error) {
6775
}
6876
outString := string(out)
6977

70-
lines := strings.FieldsFunc(outString, func(r rune) bool {
78+
parseDump(outString)
79+
80+
return "", nil
81+
}
82+
83+
func parseDump(keychainDump string) {
84+
lines := strings.FieldsFunc(keychainDump, func(r rune) bool {
7185
if r == '\n' {
72-
return true
86+
return true
7387
}
7488
return false
7589
})
76-
7790
classMatches := false
7891
account := ""
7992
serviceMatches := false
@@ -90,15 +103,17 @@ func List(service string) (string, error) {
90103
if strings.HasPrefix(line, "class:") {
91104
classMatches = line == "class: \"genp\""
92105
}
93-
if strings.HasPrefix(line," \"acct\"<blob>=\"") {
106+
if strings.HasPrefix(line, " \"acct\"<blob>=\"") {
94107
account = parseValue(line)
95108
}
96-
if strings.HasPrefix(line, " \"svce\"<blob>=\"") {
109+
if strings.HasPrefix(line, " \"svce\"<blob>=\"") {
97110
serviceMatches = parseValue(line) == "keyfob"
98111
}
99112
}
100-
101-
return "",nil
113+
// if the very last one was a match, this catches it
114+
if classMatches && serviceMatches {
115+
fmt.Println(account)
116+
}
102117
}
103118

104119
func init() {

cmd/otp.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
/*
2-
Copyright © 2019 NAME HERE <EMAIL ADDRESS>
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
2+
The MIT License (MIT)
3+
4+
Copyright © 2019 StevenACoffman
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
1523
*/
1624
package cmd
1725

@@ -32,7 +40,7 @@ import (
3240
// otpCmd represents the otp command
3341
var otpCmd = &cobra.Command{
3442
Use: "otp [key name]",
35-
Short: "Generate a One Time Password",
43+
Short: "Generate a One Time Password for the named key",
3644
Long: `otp [key name] prints a two-factor authentication code from the key with the given name.
3745
If -clip is specified, otp also copies to the code to the system clipboard.
3846
With no arguments, otp prints two-factor authentication codes from all known time-based keys.
@@ -43,9 +51,9 @@ so it is important that the system clock have at least one-minute accuracy.`,
4351
Run: func(cmd *cobra.Command, args []string) {
4452

4553
service := "keyfob"
46-
user := args[0]
54+
keyName := args[0]
4755

48-
codeText, err := generateTOTP(service, user)
56+
codeText, err := generateTOTP(service, keyName)
4957
if err != nil {
5058
log.Fatal(err)
5159
return
@@ -60,8 +68,8 @@ so it is important that the system clock have at least one-minute accuracy.`,
6068
},
6169
}
6270

63-
func generateTOTP(service, user string) (string, error) {
64-
secret, err := keyring.Get(service, user)
71+
func generateTOTP(service, keyName string) (string, error) {
72+
secret, err := keyring.Get(service, keyName)
6573
if err != nil {
6674
return "", err
6775
}

cmd/qr.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright © 2019 StevenACoffman
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
*/
24+
package cmd
25+
26+
import (
27+
"encoding/base32"
28+
"fmt"
29+
"github.com/mdp/qrterminal"
30+
"github.com/spf13/cobra"
31+
"github.com/zalando/go-keyring"
32+
"log"
33+
"os"
34+
osUser "os/user"
35+
)
36+
37+
// qrCmd represents the qr command
38+
var qrCmd = &cobra.Command{
39+
Use: "qr [key name]",
40+
Short: "Generate a QR Code for the named key",
41+
Long: `qr [key name] prints a QR Code for the key with the given name.
42+
This can be useful for backing up QR Codes to Google Authenticator or Authy or whatever.`,
43+
44+
Run: func(cmd *cobra.Command, args []string) {
45+
46+
service := "keyfob"
47+
keyName := args[0]
48+
49+
err := generateQRCode(service, keyName)
50+
if err != nil {
51+
log.Fatal(err)
52+
return
53+
}
54+
},
55+
}
56+
57+
func generateQRCode(service, keyName string) error {
58+
secret, err := keyring.Get(service, keyName)
59+
if err != nil {
60+
return err
61+
}
62+
raw, err := decodeKey(secret)
63+
if err != nil {
64+
return fmt.Errorf("%s: malformed key", secret)
65+
}
66+
67+
currentUser, err := osUser.Current()
68+
if err != nil {
69+
return err
70+
}
71+
uri := fmt.Sprintf("otpauth://totp/%s@%s?secret=%s&issuer=%s",
72+
keyName+ ":" + currentUser.Username,
73+
keyName,
74+
base32.StdEncoding.EncodeToString(raw),
75+
keyName,
76+
)
77+
78+
qrterminal.Generate(uri, qrterminal.L, os.Stderr)
79+
return nil
80+
}
81+
func init() {
82+
rootCmd.AddCommand(qrCmd)
83+
}

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/*
2+
The MIT License (MIT)
3+
24
Copyright © 2019 StevenACoffman
35
46
Permission is hereby granted, free of charge, to any person obtaining a copy

cmd/vault.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
/*
2-
Copyright © 2019 NAME HERE <EMAIL ADDRESS>
2+
The MIT License (MIT)
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
4+
Copyright © 2019 StevenACoffman
75
8-
http://www.apache.org/licenses/LICENSE-2.0
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
912
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
1523
*/
1624
package cmd
1725

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/atotto/clipboard v0.1.2
77
github.com/danieljoos/wincred v1.0.2 // indirect
88
github.com/godbus/dbus v4.1.0+incompatible // indirect
9+
github.com/mdp/qrterminal v1.0.1
910
github.com/mitchellh/go-homedir v1.1.0
1011
github.com/spf13/cobra v0.0.5
1112
github.com/spf13/viper v1.4.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
6464
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
6565
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
6666
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
67+
github.com/mdp/qrterminal v1.0.1 h1:07+fzVDlPuBlXS8tB0ktTAyf+Lp1j2+2zK3fBOL5b7c=
68+
github.com/mdp/qrterminal v1.0.1/go.mod h1:Z33WhxQe9B6CdW37HaVqcRKzP+kByF3q/qLxOGe12xQ=
6769
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
6870
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
6971
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
@@ -158,3 +160,5 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
158160
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
159161
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
160162
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
163+
rsc.io/qr v0.2.0 h1:6vBLea5/NRMVTz8V66gipeLycZMl/+UlFmk8DvqQ6WY=
164+
rsc.io/qr v0.2.0/go.mod h1:IF+uZjkb9fqyeF/4tlBoynqmQxUoPfWEKh921coOuXs=

install.sh

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
#!/usr/bin/env bash
22

3-
# Script will snarf secrets from 2fa file
3+
# Installing kefob
4+
45
if [ ! -x "$(command -v keyfob)" ]; then
56
echo "keyfob is not installed, so I'm going to go grab the mac one for you"
6-
wget -O - https://github.com/StevenACoffman/keyfob/releases/download/v0.3.0/keyfob_0.3.0_Darwin_x86_64.tar.gz | tar xzvf
7-
mkdir -p /usr/local/bin
8-
mv keyfob /usr/local/bin
7+
if [! -x "(command -v brew)" ]; then
8+
KEYFOB_RELEASE='0.3.0'
9+
echo "Homebrew is not installed, so I'm going to grab the v${KEYFOB_RELEASE} current release from github"
10+
wget -O - "https://github.com/StevenACoffman/keyfob/releases/download/v${KEYFOB_RELEASE}/keyfob_${KEYFOB_RELEASE}_Darwin_x86_64.tar.gz" | tar xzvf
11+
mkdir -p /usr/local/bin
12+
mv keyfob /usr/local/bin
13+
else
14+
echo "Using homebrew and tapping StevenACoffman/keyfob"
15+
brew tap StevenACoffman/keyfob
16+
brew install keyfob
17+
fi
18+
919
fi
1020

1121
filename="${HOME}/.2fa"
1222

1323
if [ -f $filename ]; then
24+
echo "Snarfing secrets from 2fa for you"
1425
cat $filename | while read line
1526
do
1627
SIZE="$(echo $line | awk '{print $2}')"

0 commit comments

Comments
 (0)