This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
credential_backup.go
76 lines (61 loc) · 1.95 KB
/
credential_backup.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
76
package secrethub
import (
"fmt"
"github.com/secrethub/secrethub-cli/internals/cli"
"github.com/secrethub/secrethub-cli/internals/cli/ui"
"github.com/secrethub/secrethub-go/pkg/secrethub/credentials"
)
// CredentialBackupCommand creates a backup code to restore a credential from a code.
type CredentialBackupCommand struct {
io ui.IO
newClient newClientFunc
}
// NewAccountInitCommand creates a new AccountInitCommand.
func NewCredentialBackupCommand(io ui.IO, newClient newClientFunc) *CredentialBackupCommand {
return &CredentialBackupCommand{
io: io,
newClient: newClient,
}
}
// Register registers the command, arguments and flags on the provided Registerer.
func (cmd *CredentialBackupCommand) Register(r cli.Registerer) {
clause := r.Command("backup", "Create a backup code for restoring your account.")
clause.BindAction(cmd.Run)
clause.BindArguments(nil)
}
// Run creates a backup code for the currently authenticated account.
func (cmd *CredentialBackupCommand) Run() error {
client, err := cmd.newClient()
if err != nil {
return err
}
// Get username and make sure client has a valid credential.
me, err := client.Me().GetUser()
if err != nil {
return err
}
question := fmt.Sprintf("This will create a new backup code for %s. "+
"This code can be used to obtain full access to your account.\n"+
"Do you want to continue?", me.Username)
ok, err := ui.AskYesNo(cmd.io, question, ui.DefaultYes)
if err != nil {
return err
}
if !ok {
fmt.Fprintln(cmd.io.Output(), "Aborting")
return nil
}
backupCode := credentials.CreateBackupCode()
_, err = client.Credentials().Create(backupCode, "")
if err != nil {
return err
}
code, err := backupCode.Code()
if err != nil {
return err
}
fmt.Fprintf(cmd.io.Output(), "This is your backup code: \n%s\n", code)
fmt.Fprintln(cmd.io.Output(), "Write it down and store it in a safe location! "+
"You can restore your account by running `secrethub init`.")
return nil
}