Skip to content

Commit

Permalink
feat(login): add the ability to log in using an offline token (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
Enda authored Mar 12, 2021
1 parent c448bbc commit c6246d4
Show file tree
Hide file tree
Showing 37 changed files with 299 additions and 171 deletions.
2 changes: 1 addition & 1 deletion cmd/rhoas/pkged.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/commands/rhoas_login.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ $ rhoas login --print-sso-url
--mas-auth-url string The URL of the MAS-SSO Authentication server. (default "https://keycloak-edge-redhat-rhoam-user-sso.apps.mas-sso-stage.1gzl.s1.devshift.org/auth/realms/mas-sso-staging")
--print-sso-url Prints the console login URL, which you can use to log in to RHOAS from a different web browser. This is useful if you need to log in with different credentials than the credentials you used in your default web browser.
--scope stringArray Override the default OpenID scope. To specify multiple scopes, use a separate --scope for each scope. (default [openid])
-t, --token string Allows you to log in using an offline token, which can be obtained at https://cloud.redhat.com/openshift/token.
....

=== Options inherited from parent commands
Expand Down
19 changes: 19 additions & 0 deletions locales/cmd/login/active.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ one = "The URL of the SSO Authentication server."
description = 'Description for the --auth-url flag'
one = "The URL of the MAS-SSO Authentication server."

[login.flag.token]
one = "Allows you to log in using an offline token, which can be obtained at https://cloud.redhat.com/openshift/token."

[login.flag.printSsoUrl]
description = 'Description for the --print-sso-url'
one = "Prints the console login URL, which you can use to log in to RHOAS from a different web browser. This is useful if you need to log in with different credentials than the credentials you used in your default web browser."
Expand Down Expand Up @@ -80,10 +83,26 @@ one = 'Redirected to callback URL: {{.URL}}'
description = 'Log in success message'
one = 'You are now logged in as "{{.Username}}"'

[login.log.info.loginSuccessNoUsername]
description = 'Log in success message'
one = 'You are now logged in'

[login.log.info.openSSOUrl]
description = 'Info message for opening auth URL instructions'
one = 'Open the following URL in your browser to log in:'

[login.log.error.unableToStartServer]
description = 'Error message for when unable to start server'
one = 'unable to start server: {{.Error}}'

[login.log.info.loggingIn]
one = 'Logging in...'

[login.log.info.loggedIn]
one = 'Logged in successfully'

[login.log.info.loggingInMAS]
one = 'Logging in to MAS-SSO...'

[login.log.info.loggedInMAS]
one = 'Logged in successfully to MAS-SSO'
5 changes: 4 additions & 1 deletion locales/cmd/whoami/active.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ description = 'Examples of how to use the command'
one = '''
# print current username
$ rhoas whoami
'''
'''

[whoami.log.info.tokenHasNoUsername]
one = 'Token has no username'
3 changes: 3 additions & 0 deletions locales/connection/active.en.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[connection.error.notLoggedInError]
one = 'not logged in. Run "rhoas login" to authenticate'

[connection.error.notLoggedInMASError]
one = 'not logged in to MAS-SSO. Run "rhoas login" to authenticate'

[connection.error.sessionExpiredError]
one = 'session expired. Run "rhoas login" to authenticate'
4 changes: 4 additions & 0 deletions pkg/auth/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ type SSOConfig struct {
// https://tools.ietf.org/html/rfc6749#section-4.1
func (a *AuthorizationCodeGrant) Execute(ctx context.Context, ssoCfg *SSOConfig, masSSOCfg *SSOConfig) (err error) {
// log in to SSO
a.Logger.Info(localizer.MustLocalizeFromID("login.log.info.loggingIn"))
if err = a.loginSSO(ctx, ssoCfg); err != nil {
return err
}
a.Logger.Info(localizer.MustLocalizeFromID("login.log.info.loggedIn"))

a.Logger.Info(localizer.MustLocalizeFromID("login.log.info.loggingInMAS"))
// log in to MAS-SSO
if err = a.loginMAS(ctx, masSSOCfg); err != nil {
return err
}
a.Logger.Info(localizer.MustLocalizeFromID("login.log.info.loggedInMAS"))

return nil
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/cluster/connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package connect
import (
"context"
"errors"

"github.com/bf2fc6cc711aee1a0c2a/cli/internal/config"
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/localizer"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cluster"
Expand All @@ -16,7 +17,7 @@ import (

type Options struct {
Config config.IConfig
Connection func() (connection.Connection, error)
Connection func(connectionCfg *connection.Config) (connection.Connection, error)
Logger func() (logging.Logger, error)
IO *iostreams.IOStreams

Expand Down Expand Up @@ -65,7 +66,7 @@ func NewConnectCommand(f *factory.Factory) *cobra.Command {
}

func runBind(opts *Options) error {
connection, err := opts.Connection()
connection, err := opts.Connection(connection.DefaultConfigSkipMasAuth)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/cluster/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

type Options struct {
Config config.IConfig
Connection func() (connection.Connection, error)
Connection factory.ConnectionFunc
Logger func() (logging.Logger, error)
IO *iostreams.IOStreams

Expand Down Expand Up @@ -53,7 +53,7 @@ func NewStatusCommand(f *factory.Factory) *cobra.Command {
}

func runStatus(opts *Options) error {
connection, err := opts.Connection()
connection, err := opts.Connection(connection.DefaultConfigSkipMasAuth)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/factory/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func New(cliVersion string) *Factory {
return logger, nil
}

connectionFunc := func() (connection.Connection, error) {
connectionFunc := func(connectionCfg *connection.Config) (connection.Connection, error) {
if conn != nil {
return conn, nil
}
Expand Down Expand Up @@ -94,6 +94,8 @@ func New(cliVersion string) *Factory {
return nil, err
}

builder.WithConnectionConfig(connectionCfg)

conn, err = builder.Build()
if err != nil {
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ type Factory struct {
// Interface to read/write to the config
Config config.IConfig
// Creates a connection to the API
Connection func() (connection.Connection, error)
Connection ConnectionFunc
// Returns a logger to create leveled logs in the application
Logger func() (logging.Logger, error)
}

type ConnectionFunc func(cfg *connection.Config) (connection.Connection, error)
8 changes: 4 additions & 4 deletions pkg/cmd/kafka/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/flag"
flagutil "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmdutil/flags"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/kafka"

"github.com/bf2fc6cc711aee1a0c2a/cli/internal/localizer"
Expand All @@ -21,7 +22,6 @@ import (
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/dump"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/iostreams"

"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
pkgKafka "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/kafka"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/logging"

Expand All @@ -46,7 +46,7 @@ type Options struct {

IO *iostreams.IOStreams
Config config.IConfig
Connection func() (connection.Connection, error)
Connection factory.ConnectionFunc
Logger func() (logging.Logger, error)
}

Expand Down Expand Up @@ -122,7 +122,7 @@ func runCreate(opts *Options) error {
return err
}

connection, err := opts.Connection()
connection, err := opts.Connection(connection.DefaultConfigSkipMasAuth)
if err != nil {
return err
}
Expand Down Expand Up @@ -200,7 +200,7 @@ func runCreate(opts *Options) error {

// Show a prompt to allow the user to interactively insert the data for their Kafka
func promptKafkaPayload(opts *Options) (payload *kasclient.KafkaRequestPayload, err error) {
connection, err := opts.Connection()
connection, err := opts.Connection(connection.DefaultConfigSkipMasAuth)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/cmd/kafka/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestNewCreateCommand(t *testing.T) {
AccessToken: "valid",
RefreshToken: "valid",
}),
Connection: func() (connection.Connection, error) {
Connection: func(connectionCfg *connection.Config) (connection.Connection, error) {
mockDefaultAPI := &kasclient.DefaultApiMock{}
mockDefaultAPI.CreateKafkaFunc = func(ctx context.Context) kasclient.ApiCreateKafkaRequest {
req := kasclient.ApiCreateKafkaRequest{
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestNewCreateCommand(t *testing.T) {
AccessToken: "valid",
RefreshToken: "valid",
}),
Connection: func() (connection.Connection, error) {
Connection: func(connectionCfg *connection.Config) (connection.Connection, error) {
mockDefaultAPI := &kasclient.DefaultApiMock{}
mockDefaultAPI.CreateKafkaFunc = func(ctx context.Context) kasclient.ApiCreateKafkaRequest {
req := kasclient.ApiCreateKafkaRequest{
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestNewCreateCommand(t *testing.T) {
AccessToken: "valid",
RefreshToken: "valid",
}),
Connection: func() (connection.Connection, error) {
Connection: func(connectionCfg *connection.Config) (connection.Connection, error) {
mockDefaultAPI := &kasclient.DefaultApiMock{}
mockDefaultAPI.CreateKafkaFunc = func(ctx context.Context) kasclient.ApiCreateKafkaRequest {
req := kasclient.ApiCreateKafkaRequest{
Expand Down Expand Up @@ -214,7 +214,7 @@ func TestNewCreateCommand(t *testing.T) {
AccessToken: "valid",
RefreshToken: "valid",
}),
Connection: func() (connection.Connection, error) {
Connection: func(connectionCfg *connection.Config) (connection.Connection, error) {
mockDefaultAPI := &kasclient.DefaultApiMock{}
mockDefaultAPI.CreateKafkaFunc = func(ctx context.Context) kasclient.ApiCreateKafkaRequest {
req := kasclient.ApiCreateKafkaRequest{
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/kafka/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

kasclient "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/api/kas/client"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmdutil"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"

"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/iostreams"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/kafka"
Expand All @@ -17,7 +18,6 @@ import (
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/config"
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/localizer"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/factory"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
"github.com/spf13/cobra"
)

Expand All @@ -28,7 +28,7 @@ type options struct {

IO *iostreams.IOStreams
Config config.IConfig
Connection func() (connection.Connection, error)
Connection factory.ConnectionFunc
Logger func() (logging.Logger, error)
}

Expand Down Expand Up @@ -109,7 +109,7 @@ func runDelete(opts *options) error {
return err
}

connection, err := opts.Connection()
connection, err := opts.Connection(connection.DefaultConfigSkipMasAuth)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/kafka/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"

flagutil "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmdutil/flags"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/iostreams"

"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/flag"
Expand All @@ -15,7 +16,6 @@ import (
kasclient "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/api/kas/client"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/factory"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmdutil"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/dump"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/kafka"
"github.com/spf13/cobra"
Expand All @@ -29,7 +29,7 @@ type Options struct {

IO *iostreams.IOStreams
Config config.IConfig
Connection func() (connection.Connection, error)
Connection factory.ConnectionFunc
}

// NewDescribeCommand describes a Kafka instance, either by passing an `--id flag`
Expand Down Expand Up @@ -96,7 +96,7 @@ func NewDescribeCommand(f *factory.Factory) *cobra.Command {
}

func runDescribe(opts *Options) error {
connection, err := opts.Connection()
connection, err := opts.Connection(connection.DefaultConfigSkipMasAuth)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/kafka/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

kasclient "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/api/kas/client"
flagutil "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmdutil/flags"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/iostreams"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/kafka"

Expand All @@ -19,7 +20,6 @@ import (
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/localizer"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/factory"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/flag"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/logging"

"gopkg.in/yaml.v2"
Expand All @@ -43,7 +43,7 @@ type options struct {

IO *iostreams.IOStreams
Config config.IConfig
Connection func() (connection.Connection, error)
Connection factory.ConnectionFunc
Logger func() (logging.Logger, error)
}

Expand Down Expand Up @@ -94,7 +94,7 @@ func runList(opts *options) error {
return err
}

connection, err := opts.Connection()
connection, err := opts.Connection(connection.DefaultConfigSkipMasAuth)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/kafka/topic/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/bf2fc6cc711aee1a0c2a/cli/internal/localizer"

"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/kafka/topic"

"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/flag"
Expand All @@ -19,7 +20,6 @@ import (

"github.com/bf2fc6cc711aee1a0c2a/cli/internal/config"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/factory"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/logging"

"github.com/spf13/cobra"
Expand All @@ -39,7 +39,7 @@ type Options struct {

IO *iostreams.IOStreams
Config config.IConfig
Connection func() (connection.Connection, error)
Connection factory.ConnectionFunc
Logger func() (logging.Logger, error)
}

Expand Down Expand Up @@ -108,7 +108,7 @@ func NewCreateTopicCommand(f *factory.Factory) *cobra.Command {
}

func runCmd(opts *Options) error {
conn, err := opts.Connection()
conn, err := opts.Connection(connection.DefaultConfigRequireMasAuth)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit c6246d4

Please sign in to comment.