Commit 61e07344 authored by Timo Furrer's avatar Timo Furrer
Browse files

refactor: use unauthenticated auth source from GitLab client-go

This refactoring includes the new unauthenticated auth source from
gitlab-org/api/client-go!2761
parent 89ad9a1d
Loading
Loading
Loading
Loading
+1 −14
Original line number Diff line number Diff line
@@ -6,10 +6,7 @@ import (
	gitlab "gitlab.com/gitlab-org/api/client-go"
)

var (
	_ gitlab.AuthSource = (*oauth2AccessTokenOnlyAuthSource)(nil)
	_ gitlab.AuthSource = (*UnauthenticatedAuthSource)(nil)
)
var _ gitlab.AuthSource = (*oauth2AccessTokenOnlyAuthSource)(nil)

type oauth2AccessTokenOnlyAuthSource struct {
	token string
@@ -22,13 +19,3 @@ func (as oauth2AccessTokenOnlyAuthSource) Init(context.Context, *gitlab.Client)
func (as oauth2AccessTokenOnlyAuthSource) Header(_ context.Context) (string, string, error) {
	return "Authorization", "Bearer " + as.token, nil
}

type UnauthenticatedAuthSource struct{}

func (as UnauthenticatedAuthSource) Init(context.Context, *gitlab.Client) error {
	return nil
}

func (as UnauthenticatedAuthSource) Header(_ context.Context) (string, string, error) {
	return gitlab.AccessTokenHeaderName, "", nil
}
+1 −1
Original line number Diff line number Diff line
@@ -344,7 +344,7 @@ func NewClientFromConfig(repoHost string, cfg config.Config, isGraphQL bool, use
	default:
		// NOTE: use an unauthenticated client.
		newAuthSource = func(*http.Client) (gitlab.AuthSource, error) {
			return UnauthenticatedAuthSource{}, nil
			return gitlab.Unauthenticated{}, nil
		}
	}

+1 −1
Original line number Diff line number Diff line
@@ -154,7 +154,7 @@ func (o *options) run() responseType {
				Token: as.Token,
			},
		}
	case api.UnauthenticatedAuthSource:
	case gitlab.Unauthenticated:
		return errorResponse{Message: "glab is not authenticated. Use glab auth login to authenticate"}
	default:
		return errorResponse{Message: "unable to determine token"}
+1 −2
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ import (

	gitlab "gitlab.com/gitlab-org/api/client-go"

	"gitlab.com/gitlab-org/cli/internal/api"
	"gitlab.com/gitlab-org/cli/internal/config"
	"gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
)
@@ -181,7 +180,7 @@ func TestCredentialHelper_ApiClientUnauthenticated(t *testing.T) {
		NewCmd,
		false,
		cmdtest.WithBaseRepo("OWNER", "REPO", "gitlab.example.com"),
		cmdtest.WithApiClient(cmdtest.NewTestAuthSourceApiClient(t, nil, api.UnauthenticatedAuthSource{}, "gitlab.example.com")),
		cmdtest.WithApiClient(cmdtest.NewTestAuthSourceApiClient(t, nil, gitlab.Unauthenticated{}, "gitlab.example.com")),
	)

	out, err := exec("")