Skip to content

Commit cd94137

Browse files
authored
Merge pull request #275 from debricked/0-activate-auth
Activate auth command
2 parents 521aa21 + 2c66d3e commit cd94137

File tree

16 files changed

+112
-76
lines changed

16 files changed

+112
-76
lines changed

internal/auth/auth.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"strings"
66

7-
"github.com/debricked/cli/internal/client"
87
"github.com/golang-jwt/jwt"
98
"github.com/zalando/go-keyring"
109
"golang.org/x/oauth2"
@@ -50,7 +49,7 @@ func (dsc DebrickedSecretClient) Delete(service string) error {
5049
return keyring.Delete(service, dsc.User)
5150
}
5251

53-
func NewDebrickedAuthenticator(client client.IDebClient) Authenticator {
52+
func NewDebrickedAuthenticator(host string) Authenticator {
5453
return Authenticator{
5554
SecretClient: DebrickedSecretClient{
5655
User: "DebrickedCLI",
@@ -59,8 +58,8 @@ func NewDebrickedAuthenticator(client client.IDebClient) Authenticator {
5958
ClientID: "01919462-7d6e-78e8-aa24-ba779213c90f",
6059
ClientSecret: "",
6160
Endpoint: oauth2.Endpoint{
62-
AuthURL: client.Host() + "/app/oauth/authorize",
63-
TokenURL: client.Host() + "/app/oauth/token",
61+
AuthURL: host + "/app/oauth/authorize",
62+
TokenURL: host + "/app/oauth/token",
6463
},
6564
RedirectURL: "http://localhost:9096/callback",
6665
Scopes: []string{"select", "profile", "basicRepo", "fullApi"},

internal/auth/auth_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"testing"
66

77
"github.com/debricked/cli/internal/auth/testdata"
8-
clientTestdata "github.com/debricked/cli/internal/client/testdata"
98
"github.com/stretchr/testify/assert"
109
"github.com/zalando/go-keyring"
1110
"golang.org/x/oauth2"
@@ -15,7 +14,7 @@ const windowsOS = "windows"
1514
const macOS = "darwin"
1615

1716
func TestNewAuthenticator(t *testing.T) {
18-
res := NewDebrickedAuthenticator(clientTestdata.NewDebClientMock())
17+
res := NewDebrickedAuthenticator("")
1918
assert.NotNil(t, res)
2019
}
2120

internal/client/deb_client.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"net/http"
99
"os"
1010

11+
"github.com/debricked/cli/internal/auth"
12+
1113
"github.com/fatih/color"
1214
)
1315

@@ -23,13 +25,15 @@ type IDebClient interface {
2325
SetAccessToken(accessToken *string)
2426
IsEnterpriseCustomer(silent bool) bool
2527
Host() string
28+
Authenticator() auth.IAuthenticator
2629
}
2730

2831
type DebClient struct {
29-
host *string
30-
httpClient IClient
31-
accessToken *string
32-
jwtToken string
32+
host *string
33+
httpClient IClient
34+
accessToken *string
35+
jwtToken string
36+
authenticator auth.IAuthenticator
3337
}
3438

3539
func NewDebClient(accessToken *string, httpClient IClient) *DebClient {
@@ -39,10 +43,11 @@ func NewDebClient(accessToken *string, httpClient IClient) *DebClient {
3943
}
4044

4145
return &DebClient{
42-
host: &host,
43-
httpClient: httpClient,
44-
accessToken: initAccessToken(accessToken),
45-
jwtToken: "",
46+
host: &host,
47+
httpClient: httpClient,
48+
accessToken: accessToken,
49+
jwtToken: "",
50+
authenticator: auth.NewDebrickedAuthenticator(host),
4651
}
4752
}
4853

@@ -63,18 +68,11 @@ func (debClient *DebClient) Get(uri string, format string) (*http.Response, erro
6368
}
6469

6570
func (debClient *DebClient) SetAccessToken(accessToken *string) {
66-
debClient.accessToken = initAccessToken(accessToken)
71+
debClient.accessToken = accessToken
6772
}
6873

69-
func initAccessToken(accessToken *string) *string {
70-
if accessToken == nil {
71-
accessToken = new(string)
72-
}
73-
if len(*accessToken) == 0 {
74-
*accessToken = os.Getenv("DEBRICKED_TOKEN")
75-
}
76-
77-
return accessToken
74+
func (debClient *DebClient) Authenticator() auth.IAuthenticator {
75+
return debClient.authenticator
7876
}
7977

8078
type BillingPlan struct {

internal/client/deb_client_test.go

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"testing"
1212

13+
testdataAuth "github.com/debricked/cli/internal/auth/testdata"
1314
testdataClient "github.com/debricked/cli/internal/client/testdata/client"
1415
"github.com/stretchr/testify/assert"
1516
)
@@ -33,35 +34,6 @@ func TestNewDebClientWithNilToken(t *testing.T) {
3334
if *debClient.host != DefaultDebrickedUri {
3435
t.Error("failed to assert that host was set properly")
3536
}
36-
if debClient.accessToken == nil {
37-
t.Error("failed to assert that access token was set properly")
38-
}
39-
}
40-
41-
const debrickedTknEnvVar = "DEBRICKED_TOKEN"
42-
43-
func TestNewDebClientWithTokenEnvVariable(t *testing.T) {
44-
envVarTkn := "env-tkn"
45-
oldEnvValue := os.Getenv(debrickedTknEnvVar)
46-
err := os.Setenv(debrickedTknEnvVar, "env-tkn")
47-
if err != nil {
48-
t.Fatalf("failed to set env var %s", debrickedTknEnvVar)
49-
}
50-
defer func(key, value string) {
51-
err := os.Setenv(key, value)
52-
if err != nil {
53-
t.Fatalf("failed to reset env var %s", debrickedTknEnvVar)
54-
}
55-
}(debrickedTknEnvVar, oldEnvValue)
56-
57-
accessToken := ""
58-
debClient := NewDebClient(&accessToken, nil)
59-
if *debClient.host != DefaultDebrickedUri {
60-
t.Error("failed to assert that host was set properly")
61-
}
62-
if *debClient.accessToken != envVarTkn {
63-
t.Errorf("failed to assert that access token was set to %s. Got %s", envVarTkn, *debClient.accessToken)
64-
}
6537
}
6638

6739
func TestNewDebClientWithWithURI(t *testing.T) {
@@ -95,6 +67,16 @@ func TestClientUnauthorized(t *testing.T) {
9567
}
9668
}
9769

70+
func TestHost(t *testing.T) {
71+
debClient := NewDebClient(&tkn, nil)
72+
assert.Equal(t, *debClient.host, debClient.Host())
73+
}
74+
75+
func TestAuthenticator(t *testing.T) {
76+
debClient := NewDebClient(&tkn, nil)
77+
assert.NotNil(t, debClient.Authenticator())
78+
}
79+
9880
func TestGet(t *testing.T) {
9981
clientMock := testdataClient.NewMock()
10082
clientMock.AddMockResponse(testdataClient.MockResponse{
@@ -172,7 +154,7 @@ func TestPostWithTimeout(t *testing.T) {
172154
}
173155
}
174156

175-
func TestAuthenticate(t *testing.T) {
157+
func TestAuthenticateExplicitToken(t *testing.T) {
176158
tkn = "0501ac404fd1823d0d4c047f957637a912d3b94713ee32a6"
177159
jwtTkn := "jwt-tkn"
178160
clientMock := testdataClient.NewMock()
@@ -191,6 +173,23 @@ func TestAuthenticate(t *testing.T) {
191173
}
192174
}
193175

176+
func TestAuthenticateCachedToken(t *testing.T) {
177+
clientMock := testdataClient.NewMock()
178+
client = NewDebClient(nil, clientMock)
179+
client = &DebClient{
180+
host: nil,
181+
accessToken: nil,
182+
httpClient: clientMock,
183+
jwtToken: "",
184+
authenticator: testdataAuth.MockAuthenticator{},
185+
}
186+
187+
err := client.authenticate()
188+
if err != nil {
189+
t.Fatal("failed to assert that no error occurred")
190+
}
191+
}
192+
194193
func TestSetAccessToken(t *testing.T) {
195194
debClient := NewDebClient(nil, testdataClient.NewMock())
196195
debClient.accessToken = nil

internal/client/request.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,25 @@ type errorMessage struct {
117117
}
118118

119119
func (debClient *DebClient) authenticate() error {
120+
if debClient.accessToken != nil { // To avoid segfault
121+
if len(*debClient.accessToken) != 0 {
122+
return debClient.authenticateExplicitToken()
123+
}
124+
}
125+
126+
return debClient.authenticateCachedToken()
127+
}
128+
129+
func (debClient *DebClient) authenticateCachedToken() error {
130+
token, err := debClient.authenticator.Token()
131+
if err == nil {
132+
debClient.jwtToken = token.AccessToken
133+
}
134+
135+
return err
136+
}
137+
138+
func (debClient *DebClient) authenticateExplicitToken() error {
120139
uri := "/api/login_refresh"
121140

122141
data := map[string]string{"refresh_token": *debClient.accessToken}

internal/client/testdata/deb_client_mock.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"net/http"
88
"net/url"
99

10+
"github.com/debricked/cli/internal/auth"
11+
authTestdata "github.com/debricked/cli/internal/auth/testdata"
1012
"github.com/debricked/cli/internal/client"
1113
)
1214

@@ -146,3 +148,10 @@ func (mock *DebClientMock) IsEnterpriseCustomer(silent bool) bool {
146148

147149
return mock.isEnterprise
148150
}
151+
152+
func (mock *DebClientMock) Authenticator() auth.IAuthenticator {
153+
return auth.Authenticator{
154+
SecretClient: authTestdata.MockInvalidSecretClient{},
155+
OAuthConfig: nil,
156+
}
157+
}

internal/cmd/auth/auth_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ import (
44
"testing"
55

66
"github.com/debricked/cli/internal/auth"
7-
"github.com/debricked/cli/internal/client"
87
"github.com/stretchr/testify/assert"
98
)
109

1110
func TestNewAuthCmd(t *testing.T) {
12-
token := "token"
13-
deb_client := client.NewDebClient(&token, nil)
14-
authenticator := auth.NewDebrickedAuthenticator(deb_client)
11+
authenticator := auth.NewDebrickedAuthenticator("")
1512
cmd := NewAuthCmd(authenticator)
1613
commands := cmd.Commands()
1714
nbrOfCommands := 3

internal/cmd/auth/login/login_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@ import (
55

66
"github.com/debricked/cli/internal/auth"
77
"github.com/debricked/cli/internal/auth/testdata"
8-
"github.com/debricked/cli/internal/client"
98
"github.com/stretchr/testify/assert"
109
)
1110

1211
func TestNewLoginCmd(t *testing.T) {
13-
token := "token"
14-
deb_client := client.NewDebClient(&token, nil)
15-
authenticator := auth.NewDebrickedAuthenticator(deb_client)
12+
authenticator := auth.NewDebrickedAuthenticator("")
1613
cmd := NewLoginCmd(authenticator)
1714
commands := cmd.Commands()
1815
nbrOfCommands := 0

internal/cmd/auth/logout/logout_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@ import (
55

66
"github.com/debricked/cli/internal/auth"
77
"github.com/debricked/cli/internal/auth/testdata"
8-
"github.com/debricked/cli/internal/client"
98
"github.com/stretchr/testify/assert"
109
)
1110

1211
func TestNewLogoutCmd(t *testing.T) {
13-
token := "token"
14-
deb_client := client.NewDebClient(&token, nil)
15-
authenticator := auth.NewDebrickedAuthenticator(deb_client)
12+
authenticator := auth.NewDebrickedAuthenticator("")
1613
cmd := NewLogoutCmd(authenticator)
1714
commands := cmd.Commands()
1815
nbrOfCommands := 0

internal/cmd/auth/token/token_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ import (
55

66
"github.com/debricked/cli/internal/auth"
77
"github.com/debricked/cli/internal/auth/testdata"
8-
"github.com/debricked/cli/internal/client"
98
"github.com/spf13/viper"
109
"github.com/stretchr/testify/assert"
1110
)
1211

1312
func TestNewTokenCmd(t *testing.T) {
14-
token := "token"
15-
deb_client := client.NewDebClient(&token, nil)
16-
authenticator := auth.NewDebrickedAuthenticator(deb_client)
13+
authenticator := auth.NewDebrickedAuthenticator("")
1714
cmd := NewTokenCmd(authenticator)
1815
commands := cmd.Commands()
1916
nbrOfCommands := 0
@@ -57,6 +54,16 @@ func TestRunE(t *testing.T) {
5754
assert.NoError(t, err)
5855
}
5956

57+
func TestRunEJSONFlag(t *testing.T) {
58+
a := testdata.MockAuthenticator{}
59+
jsonFormat = true
60+
runE := RunE(a)
61+
62+
err := runE(nil, []string{})
63+
64+
assert.NoError(t, err)
65+
}
66+
6067
func TestRunEError(t *testing.T) {
6168
a := testdata.ErrorMockAuthenticator{}
6269
runE := RunE(a)

0 commit comments

Comments
 (0)