Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ Once pushed, a [GitHub Action](https://github.com/redhat-developer/app-services-

> NOTE: To create a pre-release, the tag should have appropriate suffix, e.g v0.20.1-alpha1

### Environment variables

RHOASCONFIG="./config.json" - custom configuration location (useful for testing)
RHOAS_CONTEXT="./context.json" - custom context location
RHOAS_TELEMETRY=false - Enables/Disables telemetry (should happen automatically in non tty sessions)
EDITOR=Code -w - controls CLI editor
KUBECONFIG=./config.json - custom kubernetes config used for other commands

### Changelog generation

[git-chglog](https://github.com/git-chglog/git-chglog) is used to generate a changelog for the current release.
Expand Down
3 changes: 0 additions & 3 deletions cmd/rhoas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ func main() {
err = executeCommandWithTelemetry(rootCmd, cmdFactory)

if err == nil {
if flagutil.DebugEnabled() {
build.CheckForUpdate(cmdFactory.Context, build.Version, cmdFactory.Logger, localizer)
}
return
}
cmdFactory.Logger.Errorf("%v\n", rootError(err, localizer))
Expand Down
48 changes: 7 additions & 41 deletions pkg/cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"net/url"

"github.com/redhat-developer/app-services-cli/pkg/core/auth/login"
"github.com/redhat-developer/app-services-cli/pkg/core/auth/token"
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"

"github.com/redhat-developer/app-services-cli/pkg/core/config"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon"
Expand Down Expand Up @@ -156,67 +154,35 @@ func runLogin(opts *options) (err error) {
return err
}
}

if opts.offlineToken != "" {
if err = loginWithOfflineToken(opts); err != nil {
spinner.Stop()
opts.Logger.Info()
return err
}
}
spinner.Stop()

cfg, err := opts.Config.Load()
if err != nil {
return err
}

if opts.offlineToken != "" {
cfg.RefreshToken = opts.offlineToken
}

cfg.APIUrl = gatewayURL.String()
cfg.Insecure = opts.insecureSkipTLSVerify
cfg.ClientID = opts.clientID
cfg.AuthURL = opts.authURL
cfg.Scopes = opts.scopes
// Reset access token on login to avoid reusing previous users valid token
cfg.AccessToken = ""

if err = opts.Config.Save(cfg); err != nil {
return err
}

username, ok := token.GetUsername(cfg.AccessToken)

opts.Logger.Info()
if !ok {
opts.Logger.Info(icon.SuccessPrefix(), opts.localizer.MustLocalize("login.log.info.loginSuccessNoUsername"))
} else {
opts.Logger.Info(icon.SuccessPrefix(), opts.localizer.MustLocalize("login.log.info.loginSuccess", localize.NewEntry("Username", username)))
}

// debug mode checks this for a version update also.
// so we check if is enabled first so as not to print it twice
if !flagutil.DebugEnabled() {
build.CheckForUpdate(opts.Context, build.Version, opts.Logger, opts.localizer)
}
opts.Logger.Info(icon.SuccessPrefix(), opts.localizer.MustLocalize("login.log.info.loginSuccess"))

return nil
}

func loginWithOfflineToken(opts *options) (err error) {
cfg, err := opts.Config.Load()
if err != nil {
return err
}
cfg.Insecure = opts.insecureSkipTLSVerify
cfg.ClientID = opts.clientID
cfg.AuthURL = opts.authURL
cfg.Scopes = opts.scopes
cfg.RefreshToken = opts.offlineToken

if err = opts.Config.Save(cfg); err != nil {
return err
}

return err
}

func createTransport(insecure bool) *http.Transport {
// #nosec 402
return &http.Transport{
Expand Down
12 changes: 10 additions & 2 deletions pkg/core/auth/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ func MapClaims(token *jwt.Token) (claims jwt.MapClaims, err error) {

func GetExpiry(tokenStr string, now time.Time) (expires bool,
left time.Duration, err error) {

if tokenStr == "" {
return true, 0, nil
}
token, err := Parse(tokenStr)
if err != nil {
return false, 0, err
Expand Down Expand Up @@ -120,7 +122,13 @@ func GetExpiry(tokenStr string, now time.Time) (expires bool,

// GetUsername extracts the username claim value from the JWT
func GetUsername(tokenStr string) (username string, ok bool) {
accessTkn, _ := Parse(tokenStr)
if tokenStr == "" {
return "", false
}
accessTkn, err := Parse(tokenStr)
if err != nil {
return "", false
}
tknClaims, _ := MapClaims(accessTkn)
u, ok := tknClaims["preferred_username"]
if ok {
Expand Down
17 changes: 0 additions & 17 deletions pkg/core/cmdutil/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@
package profile

import (
"os"
"strconv"

"github.com/spf13/cobra"
)

const DevPreviewEnv = "RHOAS_DEV"

// ApplyDevPreviewLabel adds visual element displayed in help
func ApplyDevPreviewLabel(cmd *cobra.Command) {
cmd.Short = "[beta] " + cmd.Short
Expand All @@ -24,15 +19,3 @@ func ApplyDevPreviewLabel(cmd *cobra.Command) {
func DevPreviewAnnotation() map[string]string {
return map[string]string{"channel": "preview"}
}

// DevModeEnabled Check if development mode is enabled
func DevModeEnabled() bool {
rawEnvVal := os.Getenv(DevPreviewEnv)

boolVal, err := strconv.ParseBool(rawEnvVal)
if err != nil {
return false
}

return boolVal
}
4 changes: 0 additions & 4 deletions pkg/core/localize/locales/en/cmd/login.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ one = 'Redirected to callback URL: {{.URL}}'

[login.log.info.loginSuccess]
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]
Expand Down