Repository: clouditor/clouditor
Pull request #603 (2022-03-29 16:12:41)

Fixed oauth2 support for command line tool
Fixes #601 

codecov[bot]: # [Codecov](https://codecov.io/gh/clouditor/clouditor/pull/603?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor) Report
> Merging [#603](https://codecov.io/gh/clouditor/clouditor/pull/603?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor) (ee3a7e9) into [main](https://codecov.io/gh/clouditor/clouditor/commit/4b0aa2839f320f767b1774eef5ca1070ba09750b?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor) (4b0aa28) will **decrease** coverage by `0.00%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##             main     #603      +/-   ##
==========================================
- Coverage   81.75%   81.75%   -0.01%     
==========================================
  Files          58       58              
  Lines        5163     5162       -1     
==========================================
- Hits         4221     4220       -1     
  Misses        723      723              
  Partials      219      219              
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `81.75% <100.00%> (-0.01%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/clouditor/clouditor/pull/603?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor) | Coverage Δ | |
|---|---|---|
| [cli/commands/login/login.go](https://codecov.io/gh/clouditor/clouditor/pull/603/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor#diff-Y2xpL2NvbW1hbmRzL2xvZ2luL2xvZ2luLmdv) | `81.18% <100.00%> (-0.19%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/clouditor/clouditor/pull/603?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/clouditor/clouditor/pull/603?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor). Last update [4b0aa28...ee3a7e9](https://codecov.io/gh/clouditor/clouditor/pull/603?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=clouditor).


Patch:
diff --git a/cli/commands/login/login.go b/cli/commands/login/login.go
index 7bf1f7cb..695cd5d9 100644
--- a/cli/commands/login/login.go
+++ b/cli/commands/login/login.go
@@ -38,14 +38,20 @@ import (
 )
 
 const (
-	// OAuth2ServerFlag is the viper flag for the OAuth 2.0 authorization server.
-	OAuth2ServerFlag = "oauth2-server"
+	// OAuth2AuthURLFlag is the viper flag for the OAuth 2.0 authorization endpoint.
+	OAuth2AuthURLFlag = "oauth2-auth-url"
+
+	// OAuth2TokenURLFlag is the viper flag for the OAuth 2.0 token endpoint.
+	OAuth2TokenURLFlag = "oauth2-token-url"
 
 	// OAuth2ClientIDFlag is the viper flag for the OAuth 2.0 client ID.
 	OAuth2ClientIDFlag = "oauth2-client-id"
 
-	// DefaultOAuth2Server is the default OAuth 2.0 authorization server.
-	DefaultOAuth2Server = "http://localhost:8080"
+	// DefaultOAuth2Server is the default OAuth 2.0 authorization endpoint.
+	DefaultOAuth2AuthURL = "http://localhost:8080/v1/auth/authorize"
+
+	// DefaultOAuth2TokenURL is the default OAuth 2.0 token endpoint.
+	DefaultOAuth2TokenURL = "http://localhost:8080/v1/auth/token"
 
 	// DefaultClientID is the default OAuth 2.0 client ID for the CLI.
 	DefaultClientID = "cli"
@@ -78,18 +84,14 @@ func NewLoginCommand() *cobra.Command {
 				sock    net.Listener
 				code    string
 				config  *oauth2.Config
-				authURL string
 			)
 
-			// Retrieve the URL of our authentication server
-			authURL = viper.GetString(OAuth2ServerFlag)
-
 			// Create an OAuth 2 config
 			config = &oauth2.Config{
-				ClientID: DefaultClientID,
+				ClientID: viper.GetString(OAuth2ClientIDFlag),
 				Endpoint: oauth2.Endpoint{
-					AuthURL:  fmt.Sprintf("%s/v1/auth/authorize", authURL),
-					TokenURL: fmt.Sprintf("%s/v1/auth/token", authURL),
+					AuthURL:  viper.GetString(OAuth2AuthURLFlag),
+					TokenURL: viper.GetString(OAuth2TokenURLFlag),
 				},
 				RedirectURL: DefaultCallback,
 			}
@@ -137,8 +139,11 @@ func NewLoginCommand() *cobra.Command {
 		},
 	}
 
-	cmd.PersistentFlags().String(OAuth2ServerFlag, DefaultOAuth2Server, "the URL of the OAuth 2.0 server")
-	_ = viper.BindPFlag(OAuth2ServerFlag, cmd.PersistentFlags().Lookup(OAuth2ServerFlag))
+	cmd.PersistentFlags().String(OAuth2AuthURLFlag, DefaultOAuth2AuthURL, "the authorization URL of the OAuth 2.0 server")
+	_ = viper.BindPFlag(OAuth2AuthURLFlag, cmd.PersistentFlags().Lookup(OAuth2AuthURLFlag))
+
+	cmd.PersistentFlags().String(OAuth2TokenURLFlag, DefaultOAuth2TokenURL, "the token URL of the OAuth 2.0 server")
+	_ = viper.BindPFlag(OAuth2TokenURLFlag, cmd.PersistentFlags().Lookup(OAuth2TokenURLFlag))
 
 	cmd.PersistentFlags().String(OAuth2ClientIDFlag, DefaultClientID, "the OAuth 2.0 client ID")
 	_ = viper.BindPFlag(OAuth2ClientIDFlag, cmd.PersistentFlags().Lookup(OAuth2ClientIDFlag))


Test patch:
diff --git a/cli/commands/login/login_test.go b/cli/commands/login/login_test.go
index 9a71574b..c06e6d94 100644
--- a/cli/commands/login/login_test.go
+++ b/cli/commands/login/login_test.go
@@ -80,7 +80,8 @@ func TestLogin(t *testing.T) {
 	assert.NoError(t, err)
 	assert.NotEmpty(t, dir)
 
-	viper.Set(OAuth2ServerFlag, fmt.Sprintf("http://localhost:%d", port))
+	viper.Set(OAuth2AuthURLFlag, fmt.Sprintf("http://localhost:%d/v1/auth/authorize", port))
+	viper.Set(OAuth2TokenURLFlag, fmt.Sprintf("http://localhost:%d/v1/auth/token", port))
 	viper.Set(cli.SessionFolderFlag, dir)
 
 	verifier = "012345678901234567890123456789"