mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
fix(auth): print only a token that belongs to the selected account
Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
3 files changed
+17
-2
No files matched your search
@@ -801,6 +801,16 @@ $ tea auth token --host gitea.com --user alice`,
|
||||
if instance, err := settings.ResolveHost(a.tokenInstance(), s.Hosts.Hosts); err != nil || instance != t.Host {
|
||||
token = ""
|
||||
}
|
||||
if token != "" && t.User != "" {
|
||||
// Like connect, print the environment token only for the account it authenticates.
|
||||
actual, err := services.CurrentAccount(cmd.Context(), a.deps.Client(t.Host, token, t.host.Insecure))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !services.MatchAccount(t.User, actual) {
|
||||
return clierr.Wrap(clierr.Authentication, errEnvironmentAccount)
|
||||
}
|
||||
}
|
||||
if token == "" {
|
||||
account, exists := t.host.Users[t.User]
|
||||
switch {
|
||||
|
||||
@@ -742,7 +742,9 @@ func TestAuthSwitchSelectsInstanceAndAccount(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAuthTokenPrintsTheCredential(t *testing.T) {
|
||||
const instance = "https://example.org"
|
||||
server := newAuthenticatedServer(http.NotFoundHandler())
|
||||
defer server.Close()
|
||||
instance := server.URL
|
||||
deps, out, _ := testDeps(t)
|
||||
configureAccounts(t, deps, instance, "alice", "alice")
|
||||
require.NoError(t, Execute(context.Background(), deps, []string{"auth", "token"}))
|
||||
|
||||
@@ -37,6 +37,9 @@ type target struct {
|
||||
|
||||
var errRepositoryRequired = errors.New("repository required: use --repo owner/name")
|
||||
|
||||
// errEnvironmentAccount must not reveal which account the GITEA_TOKEN belongs to.
|
||||
var errEnvironmentAccount = errors.New("GITEA_TOKEN does not belong to the selected account; supply its matching --user or remove the environment token to use stored credentials")
|
||||
|
||||
func (a *application) resolve(ctx context.Context, requireRepo bool) (target, error) {
|
||||
return a.resolveTarget(ctx, requireRepo, true)
|
||||
}
|
||||
@@ -185,7 +188,7 @@ func (a *application) connect(ctx context.Context, req targetRequest) (target, e
|
||||
}
|
||||
// The message must not reveal which account the token belongs to.
|
||||
if !services.MatchAccount(t.User, actual) {
|
||||
return t, clierr.Wrap(clierr.Authentication, errors.New("GITEA_TOKEN does not belong to the selected account; supply its matching --user or remove the environment token to use stored credentials"))
|
||||
return t, clierr.Wrap(clierr.Authentication, errEnvironmentAccount)
|
||||
}
|
||||
if t.User == "" {
|
||||
t.UserSource = "GITEA_TOKEN authenticated account"
|
||||
|
||||
Reference in new issue
Block a user