Skip to content

Commit 699da53

Browse files
author
Enda
authored
feat: print sso url in login (#167)
1 parent 558dbcc commit 699da53

File tree

4 files changed

+50
-18
lines changed

4 files changed

+50
-18
lines changed

.vscode/launch.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
"program": "${workspaceFolder}/cmd/rhoas",
1313
"env": {},
1414
"args": [
15-
"login",
16-
"--url=localhost:8000"
15+
"login"
1716
]
1817
},
1918
{

docs/commands/rhoas_login.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ rhoas login [flags]
1717
--client-id string OpenID client identifier. (default "rhoas-cli-prod")
1818
-h, --help help for login
1919
--insecure Enables insecure communication with the server. This disables verification of TLS certificates and host names.
20+
--print-sso-url Prints the login URL to the console so you can control which browser to open it in. Useful if you need to log in with a user that is different to the one logged in on your default web browser.
2021
--url string URL of the API gateway. The value can be the complete URL or an alias. The valid aliases are 'production', 'staging', 'integration', 'development' and their shorthands. (default "https://api.stage.openshift.com")
2122
....
2223

2324
=== SEE ALSO
2425

2526
* link:rhoas.adoc[rhoas] - rhoas cli
2627

27-
==== Auto generated by spf13/cobra on 10-Dec-2020
28+
==== Auto generated by spf13/cobra on 14-Dec-2020

pkg/auth/token/token.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ func (c *Token) IsValid() (tokenIsValid bool, err error) {
1919
var expires bool
2020
var left time.Duration
2121
var accessToken *jwt.Token
22-
accessToken, err = parseToken(c.AccessToken)
22+
accessToken, err = Parse(c.AccessToken)
2323
if err != nil {
2424
return
2525
}
26-
expires, left, err = getTokenExpiry(accessToken, now)
26+
expires, left, err = GetExpiry(accessToken, now)
2727
if err != nil {
2828
return
2929
}
@@ -36,11 +36,11 @@ func (c *Token) IsValid() (tokenIsValid bool, err error) {
3636
var expires bool
3737
var left time.Duration
3838
var refreshToken *jwt.Token
39-
refreshToken, err = parseToken(c.RefreshToken)
39+
refreshToken, err = Parse(c.RefreshToken)
4040
if err != nil {
4141
return
4242
}
43-
expires, left, err = getTokenExpiry(refreshToken, now)
43+
expires, left, err = GetExpiry(refreshToken, now)
4444
if err != nil {
4545
return
4646
}
@@ -52,7 +52,7 @@ func (c *Token) IsValid() (tokenIsValid bool, err error) {
5252
return
5353
}
5454

55-
func parseToken(textToken string) (token *jwt.Token, err error) {
55+
func Parse(textToken string) (token *jwt.Token, err error) {
5656
parser := new(jwt.Parser)
5757
token, _, err = parser.ParseUnverified(textToken, jwt.MapClaims{})
5858
if err != nil {
@@ -62,12 +62,21 @@ func parseToken(textToken string) (token *jwt.Token, err error) {
6262
return token, nil
6363
}
6464

65-
func getTokenExpiry(token *jwt.Token, now time.Time) (expires bool,
66-
left time.Duration, err error) {
65+
func MapClaims(token *jwt.Token) (jwt.MapClaims, error) {
6766
claims, ok := token.Claims.(jwt.MapClaims)
6867
if !ok {
69-
err = fmt.Errorf("expected map claims bug got %T", claims)
70-
return
68+
err := fmt.Errorf("expected map claims bug got %T", claims)
69+
return nil, err
70+
}
71+
72+
return claims, nil
73+
}
74+
75+
func GetExpiry(token *jwt.Token, now time.Time) (expires bool,
76+
left time.Duration, err error) {
77+
claims, err := MapClaims(token)
78+
if err != nil {
79+
return false, 0, err
7180
}
7281
var exp float64
7382
claim, ok := claims["exp"]

pkg/cmd/login/login.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import (
1111
"net/url"
1212
"os"
1313

14+
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/auth/token"
15+
16+
"github.com/MakeNowJust/heredoc"
17+
1418
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/browser"
1519
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
1620

@@ -26,7 +30,7 @@ import (
2630
"github.com/spf13/cobra"
2731
)
2832

29-
var (
33+
const (
3034
devURL = "http://localhost:8000"
3135
productionURL = "https://api.openshift.com"
3236
stagingURL = "https://api.stage.openshift.com"
@@ -73,6 +77,7 @@ var args struct {
7377
authURL string
7478
clientID string
7579
insecureSkipTLSVerify bool
80+
printURL bool
7681
}
7782

7883
// NewLoginCmd gets the command that's log the user in
@@ -88,6 +93,7 @@ func NewLoginCmd() *cobra.Command {
8893
cmd.Flags().BoolVar(&args.insecureSkipTLSVerify, "insecure", false, "Enables insecure communication with the server. This disables verification of TLS certificates and host names.")
8994
cmd.Flags().StringVar(&args.clientID, "client-id", defaultClientID, "OpenID client identifier.")
9095
cmd.Flags().StringVar(&args.authURL, "auth-url", connection.DefaultAuthURL, "SSO Authentication server")
96+
cmd.Flags().BoolVar(&args.printURL, "print-sso-url", false, "Prints the login URL to the console so you can control which browser to open it in. Useful if you need to log in with a user that is different to the one logged in on your default web browser.")
9197

9298
return cmd
9399
}
@@ -162,8 +168,6 @@ func runLogin(cmd *cobra.Command, _ []string) error {
162168
Addr: redirectURL.Host,
163169
}
164170

165-
fmt.Fprintln(os.Stderr, "Logging in...")
166-
167171
sm.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
168172
http.Redirect(w, r, authCodeURL, http.StatusFound)
169173
})
@@ -219,12 +223,31 @@ func runLogin(cmd *cobra.Command, _ []string) error {
219223

220224
w.Header().Set("Content-Type", "text/html; charset=utf-8")
221225
fmt.Fprintln(w, PostLoginPage)
222-
fmt.Fprintln(os.Stderr, "Successfully logged in to RHOAS")
226+
227+
accessTkn, _ := token.Parse(resp.OAuth2Token.AccessToken)
228+
tknClaims, _ := token.MapClaims(accessTkn)
229+
userName, ok := tknClaims["preferred_username"]
230+
if !ok {
231+
fmt.Fprintln(os.Stderr, "\nYou are now logged in")
232+
} else {
233+
fmt.Fprintf(os.Stderr, "\nYou are now logged in as %v\n", userName)
234+
}
223235
cancel()
236+
return
224237
})
225238

226-
openBrowserExec, _ := browser.GetOpenBrowserCommand(authCodeURL)
227-
_ = openBrowserExec.Run()
239+
if args.printURL {
240+
fmt.Println(heredoc.Docf(`
241+
Login URL:
242+
243+
%v`, authCodeURL))
244+
} else {
245+
openBrowserExec, _ := browser.GetOpenBrowserCommand(authCodeURL)
246+
err = openBrowserExec.Run()
247+
if err != nil {
248+
return err
249+
}
250+
}
228251

229252
go func() {
230253
if err := server.ListenAndServe(); err != nil {

0 commit comments

Comments
 (0)