diff --git a/authz/browser.go b/auth/authcode.go similarity index 87% rename from authz/browser.go rename to auth/authcode.go index 29a9f0b0..9eed7503 100644 --- a/authz/browser.go +++ b/auth/authcode.go @@ -1,7 +1,9 @@ -package authz +package auth import ( "context" + "crypto/rand" + "encoding/binary" "fmt" "log" "net/http" @@ -18,9 +20,9 @@ type BrowserAuthCodeFlow struct { // GetToken returns a token. func (f *BrowserAuthCodeFlow) GetToken(ctx context.Context) (*oauth2.Token, error) { f.Config.RedirectURL = fmt.Sprintf("http://localhost:%d/", f.Port) - state, err := generateOAuthState() + state, err := generateState() if err != nil { - return nil, err + return nil, fmt.Errorf("Could not generate state parameter: %s", err) } log.Printf("Open http://localhost:%d for authorization", f.Port) code, err := f.getCode(ctx, &f.Config, state) @@ -34,6 +36,14 @@ func (f *BrowserAuthCodeFlow) GetToken(ctx context.Context) (*oauth2.Token, erro return token, nil } +func generateState() (string, error) { + var n uint64 + if err := binary.Read(rand.Reader, binary.LittleEndian, &n); err != nil { + return "", err + } + return fmt.Sprintf("%x", n), nil +} + func (f *BrowserAuthCodeFlow) getCode(ctx context.Context, config *oauth2.Config, state string) (string, error) { codeCh := make(chan string) errCh := make(chan error) diff --git a/authn/authn.go b/auth/oidc.go similarity index 94% rename from authn/authn.go rename to auth/oidc.go index 91705b5a..cc622d81 100644 --- a/authn/authn.go +++ b/auth/oidc.go @@ -1,11 +1,10 @@ -package authn +package auth import ( "context" "fmt" oidc "github.com/coreos/go-oidc" - "github.com/int128/kubelogin/authz" "golang.org/x/oauth2" ) @@ -27,7 +26,7 @@ func GetTokenSet(ctx context.Context, issuer string, clientID string, clientSecr if err != nil { return nil, fmt.Errorf("Could not access OIDC issuer: %s", err) } - flow := authz.BrowserAuthCodeFlow{ + flow := BrowserAuthCodeFlow{ Port: 8000, Config: oauth2.Config{ Endpoint: provider.Endpoint(), diff --git a/authz/authz.go b/authz/authz.go deleted file mode 100644 index b9279eb9..00000000 --- a/authz/authz.go +++ /dev/null @@ -1,12 +0,0 @@ -package authz - -import ( - "context" - - "golang.org/x/oauth2" -) - -// Flow represents an authorization method. -type Flow interface { - GetToken(context.Context) (*oauth2.Token, error) -} diff --git a/authz/cli.go b/authz/cli.go deleted file mode 100644 index 4c56dead..00000000 --- a/authz/cli.go +++ /dev/null @@ -1,35 +0,0 @@ -package authz - -import ( - "context" - "fmt" - "log" - - "golang.org/x/oauth2" -) - -// CLIAuthCodeFlow is a flow to get a token by keyboard interaction. -type CLIAuthCodeFlow struct { - oauth2.Config -} - -// GetToken returns a token by browser interaction. -func (f *CLIAuthCodeFlow) GetToken(ctx context.Context) (*oauth2.Token, error) { - f.Config.RedirectURL = "urn:ietf:wg:oauth:2.0:oob" - state, err := generateOAuthState() - if err != nil { - return nil, err - } - authCodeURL := f.Config.AuthCodeURL(state) - log.Printf("Open %s for authorization", authCodeURL) - fmt.Print("Enter code: ") - var code string - if _, err := fmt.Scanln(&code); err != nil { - return nil, err - } - token, err := f.Config.Exchange(ctx, code) - if err != nil { - return nil, fmt.Errorf("Could not exchange oauth code: %s", err) - } - return token, nil -} diff --git a/authz/state.go b/authz/state.go deleted file mode 100644 index 548f10e5..00000000 --- a/authz/state.go +++ /dev/null @@ -1,15 +0,0 @@ -package authz - -import ( - "crypto/rand" - "encoding/binary" - "fmt" -) - -func generateOAuthState() (string, error) { - var n uint64 - if err := binary.Read(rand.Reader, binary.LittleEndian, &n); err != nil { - return "", err - } - return fmt.Sprintf("%x", n), nil -} diff --git a/cli/cli.go b/cli/cli.go index e2620c35..cfe78ca8 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -7,7 +7,7 @@ import ( "log" "net/http" - "github.com/int128/kubelogin/authn" + "github.com/int128/kubelogin/auth" "github.com/int128/kubelogin/kubeconfig" flags "github.com/jessevdk/go-flags" homedir "github.com/mitchellh/go-homedir" @@ -70,7 +70,7 @@ func (c *CLI) Run() error { }} ctx := context.Background() ctx = context.WithValue(ctx, oauth2.HTTPClient, client) - token, err := authn.GetTokenSet(ctx, authProvider.IDPIssuerURL(), authProvider.ClientID(), authProvider.ClientSecret()) + token, err := auth.GetTokenSet(ctx, authProvider.IDPIssuerURL(), authProvider.ClientID(), authProvider.ClientSecret()) if err != nil { return fmt.Errorf("Authentication error: %s", err) }