aboutsummaryrefslogtreecommitdiffstats
path: root/commands/bridge_auth_addtoken.go
blob: 8eda28acd58e3c3a50383aa5e2ae80fcf97b7bb3 (plain) (blame)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package commands

import (
	"bufio"
	"fmt"
	"os"
	"strings"

	"github.com/mattn/go-isatty"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"

	"github.com/MichaelMure/git-bug/bridge"
	"github.com/MichaelMure/git-bug/bridge/core"
	"github.com/MichaelMure/git-bug/bridge/core/auth"
	"github.com/MichaelMure/git-bug/cache"
	"github.com/MichaelMure/git-bug/util/interrupt"
)

var (
	bridgeAuthAddTokenTarget string
	bridgeAuthAddTokenLogin  string
	bridgeAuthAddTokenUser   string
)

func runBridgeTokenAdd(cmd *cobra.Command, args []string) error {
	// Note: as bridgeAuthAddTokenLogin is not checked against the remote bug-tracker,
	// it's possible to register a credential with an incorrect login (including bad case).
	// The consequence is that it will not get picked later by the bridge. I find that
	// checking it would require a cumbersome UX (need to provide a base URL for some bridges, ...)
	// so it's probably not worth it, unless we refactor that entirely.

	if bridgeAuthAddTokenTarget == "" {
		return fmt.Errorf("flag --target is required")
	}
	if bridgeAuthAddTokenLogin == "" {
		return fmt.Errorf("flag --login is required")
	}

	backend, err := cache.NewRepoCache(repo)
	if err != nil {
		return err
	}
	defer backend.Close()
	interrupt.RegisterCleaner(backend.Close)

	if !core.TargetExist(bridgeAuthAddTokenTarget) {
		return fmt.Errorf("unknown target")
	}

	var value string

	if len(args) == 1 {
		value = args[0]
	} else {
		// Read from Stdin
		if isatty.IsTerminal(os.Stdin.Fd()) {
			fmt.Println("Enter the token:")
		}
		reader := bufio.NewReader(os.Stdin)
		raw, err := reader.ReadString('\n')
		if err != nil {
			return fmt.Errorf("reading from stdin: %v", err)
		}
		value = strings.TrimSuffix(raw, "\n")
	}

	var user *cache.IdentityCache

	if bridgeAuthAddTokenUser == "" {
		user, err = backend.GetUserIdentity()
	} else {
		user, err = backend.ResolveIdentityPrefix(bridgeAuthAddTokenUser)
	}
	if err != nil {
		return err
	}

	metaKey, _ := bridge.LoginMetaKey(bridgeAuthAddTokenTarget)
	login, ok := user.ImmutableMetadata()[metaKey]

	switch {
	case ok && login == bridgeAuthAddTokenLogin:
		// nothing to do
	case ok && login != bridgeAuthAddTokenLogin:
		return fmt.Errorf("this user is already tagged with a different %s login", bridgeAuthAddTokenTarget)
	default:
		user.SetMetadata(metaKey, bridgeAuthAddTokenLogin)
		err = user.Commit()
		if err != nil {
			return err
		}
	}

	token := auth.NewToken(bridgeAuthAddTokenTarget, value)
	token.SetMetadata(auth.MetaKeyLogin, bridgeAuthAddTokenLogin)

	if err := token.Validate(); err != nil {
		return errors.Wrap(err, "invalid token")
	}

	err = auth.Store(repo, token)
	if err != nil {
		return err
	}

	fmt.Printf("token %s added\n", token.ID())
	return nil
}

var bridgeAuthAddTokenCmd = &cobra.Command{
	Use:     "add-token [<token>]",
	Short:   "Store a new token",
	PreRunE: loadRepoEnsureUser,
	RunE:    runBridgeTokenAdd,
	Args:    cobra.MaximumNArgs(1),
}

func init() {
	bridgeAuthCmd.AddCommand(bridgeAuthAddTokenCmd)
	bridgeAuthAddTokenCmd.Flags().StringVarP(&bridgeAuthAddTokenTarget, "target", "t", "",
		fmt.Sprintf("The target of the bridge. Valid values are [%s]", strings.Join(bridge.Targets(), ",")))
	bridgeAuthAddTokenCmd.Flags().StringVarP(&bridgeAuthAddTokenLogin,
		"login", "l", "", "The login in the remote bug-tracker")
	bridgeAuthAddTokenCmd.Flags().StringVarP(&bridgeAuthAddTokenUser,
		"user", "u", "", "The user to add the token to. Default is the current user")
	bridgeAuthAddTokenCmd.Flags().SortFlags = false
}