Skip to content

Commit cf66aa2

Browse files
authored
Fix auth (google#462)
* Use github.com/docker/cli/cli/config * Convert authn.Authenticator type This now returns a struct instead of a string. * Handle oauth token flow
1 parent 4a5eb88 commit cf66aa2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+12685
-618
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44

55
**/zz_deepcopy_generated.go linguist-generated=true
66
cmd/crane/doc/crane*.md linguist-generated=true
7+
go.mod linguist-generated=true
8+
go.sum linguist-generated=true

go.mod

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.sum

+13-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/authn/anon.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package authn
1818
type anonymous struct{}
1919

2020
// Authorization implements Authenticator.
21-
func (a *anonymous) Authorization() (string, error) {
22-
return "", nil
21+
func (a *anonymous) Authorization() (*AuthConfig, error) {
22+
return &AuthConfig{}, nil
2323
}
2424

2525
// Anonymous is a singleton Authenticator for providing anonymous auth.

pkg/authn/anon_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
package authn
1616

1717
import (
18+
"reflect"
1819
"testing"
1920
)
2021

2122
func TestAnonymous(t *testing.T) {
22-
hdr, err := Anonymous.Authorization()
23+
cfg, err := Anonymous.Authorization()
2324
if err != nil {
24-
t.Errorf("Authorization() = %v", err)
25+
t.Fatalf("Authorization() = %v", err)
2526
}
26-
if hdr != "" {
27-
t.Errorf("Authorization(); got %v, wanted empty string", hdr)
27+
want := &AuthConfig{}
28+
if !reflect.DeepEqual(cfg, want) {
29+
t.Errorf("Authorization(); got %v, wanted {}", cfg)
2830
}
2931
}

pkg/authn/auth.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@
1414

1515
package authn
1616

17-
import (
18-
"fmt"
19-
)
20-
21-
// auth implements Authenticator for an "auth" entry of the docker config.
17+
// auth is an Authenticator that simply returns the wrapped AuthConfig.
2218
type auth struct {
23-
token string
19+
config AuthConfig
20+
}
21+
22+
// FromConfig returns an Authenticator that just returns the given AuthConfig.
23+
func FromConfig(cfg AuthConfig) Authenticator {
24+
return &auth{cfg}
2425
}
2526

2627
// Authorization implements Authenticator.
27-
func (a *auth) Authorization() (string, error) {
28-
return fmt.Sprintf("Basic %s", a.token), nil
28+
func (a *auth) Authorization() (*AuthConfig, error) {
29+
return &a.config, nil
2930
}

pkg/authn/authn.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,23 @@
1414

1515
package authn
1616

17+
// AuthConfig contains authorization information for connecting to a Registry
18+
// Inlined what we use from github.com/cli/cli/config/types
19+
type AuthConfig struct {
20+
Username string `json:"username,omitempty"`
21+
Password string `json:"password,omitempty"`
22+
Auth string `json:"auth,omitempty"`
23+
24+
// IdentityToken is used to authenticate the user and get
25+
// an access token for the registry.
26+
IdentityToken string `json:"identitytoken,omitempty"`
27+
28+
// RegistryToken is a bearer token to be sent to a registry
29+
RegistryToken string `json:"registrytoken,omitempty"`
30+
}
31+
1732
// Authenticator is used to authenticate Docker transports.
1833
type Authenticator interface {
1934
// Authorization returns the value to use in an http transport's Authorization header.
20-
Authorization() (string, error)
35+
Authorization() (*AuthConfig, error)
2136
}

pkg/authn/basic.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,16 @@
1414

1515
package authn
1616

17-
import (
18-
"encoding/base64"
19-
"fmt"
20-
)
21-
2217
// Basic implements Authenticator for basic authentication.
2318
type Basic struct {
2419
Username string
2520
Password string
2621
}
2722

2823
// Authorization implements Authenticator.
29-
func (b *Basic) Authorization() (string, error) {
30-
delimited := fmt.Sprintf("%s:%s", b.Username, b.Password)
31-
encoded := base64.StdEncoding.EncodeToString([]byte(delimited))
32-
return fmt.Sprintf("Basic %s", encoded), nil
24+
func (b *Basic) Authorization() (*AuthConfig, error) {
25+
return &AuthConfig{
26+
Username: b.Username,
27+
Password: b.Password,
28+
}, nil
3329
}

pkg/authn/basic_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@
1515
package authn
1616

1717
import (
18+
"reflect"
1819
"testing"
1920
)
2021

2122
func TestBasic(t *testing.T) {
22-
anon := &Basic{Username: "foo", Password: "bar"}
23+
basic := &Basic{Username: "foo", Password: "bar"}
2324

24-
got, err := anon.Authorization()
25+
got, err := basic.Authorization()
2526
if err != nil {
26-
t.Errorf("Authorization() = %v", err)
27+
t.Fatalf("Authorization() = %v", err)
2728
}
28-
want := "Basic Zm9vOmJhcg=="
29-
if got != want {
29+
want := &AuthConfig{Username: "foo", Password: "bar"}
30+
if !reflect.DeepEqual(got, want) {
3031
t.Errorf("Authorization(); got %v, want %v", got, want)
3132
}
3233
}

pkg/authn/bearer.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414

1515
package authn
1616

17-
import (
18-
"fmt"
19-
)
20-
2117
// Bearer implements Authenticator for bearer authentication.
2218
type Bearer struct {
2319
Token string `json:"token"`
2420
}
2521

2622
// Authorization implements Authenticator.
27-
func (b *Bearer) Authorization() (string, error) {
28-
return fmt.Sprintf("Bearer %s", b.Token), nil
23+
func (b *Bearer) Authorization() (*AuthConfig, error) {
24+
return &AuthConfig{
25+
RegistryToken: b.Token,
26+
}, nil
2927
}

pkg/authn/bearer_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ import (
2121
func TestBearer(t *testing.T) {
2222
anon := &Bearer{Token: "bazinga"}
2323

24-
got, err := anon.Authorization()
24+
auth, err := anon.Authorization()
2525
if err != nil {
2626
t.Errorf("Authorization() = %v", err)
2727
}
28-
want := "Bearer bazinga"
29-
if got != want {
28+
if got, want := auth.RegistryToken, "bazinga"; got != want {
3029
t.Errorf("Authorization(); got %v, want %v", got, want)
3130
}
3231
}

pkg/authn/helper.go

-102
This file was deleted.

0 commit comments

Comments
 (0)