Skip to content

Commit

Permalink
Lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-debricked committed Sep 18, 2024
1 parent 8054b15 commit d6a68c1
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 21 deletions.
14 changes: 11 additions & 3 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auth

import (
"context"
"strings"

"github.com/debricked/cli/internal/client"
"github.com/golang-jwt/jwt"
Expand Down Expand Up @@ -79,7 +80,12 @@ func (a Authenticator) Logout() error {

func validateJWT(token string) error {
claims := jwt.MapClaims{}
jwt.ParseWithClaims(token, claims, nil)
_, err := jwt.ParseWithClaims(token, claims, nil)
if err != nil && strings.Compare(err.Error(), "no Keyfunc was provided.") != 0 {

return err
}

return claims.Valid()
}

Expand All @@ -100,6 +106,7 @@ func (a Authenticator) Token() (*oauth2.Token, error) {
return nil, jwtErr
}
}

return &oauth2.Token{
RefreshToken: refreshToken,
AccessToken: accessToken,
Expand Down Expand Up @@ -127,8 +134,9 @@ func (a Authenticator) refresh(refreshToken string) (*oauth2.Token, error) {
if err != nil {
return nil, err
} else {
a.save(token)
return token, nil
err = a.save(token)

return token, err
}
}

Expand Down
15 changes: 12 additions & 3 deletions internal/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,25 @@ func TestMockedSaveTokenRefreshError(t *testing.T) {
assert.Error(t, err)
}

func TestMockedTokenErrorSegments(t *testing.T) {
authenticator := Authenticator{
SecretClient: testdata.MockInvalidSecretClient{},
OAuthConfig: nil,
}
token, err := authenticator.Token()
assert.Error(t, err)
assert.ErrorContains(t, err, "token contains an invalid number of segments")
assert.Nil(t, token)
}

func TestMockedToken(t *testing.T) {
authenticator := Authenticator{
SecretClient: testdata.MockSecretClient{},
OAuthConfig: nil,
}
token, err := authenticator.Token()

assert.NoError(t, err)
assert.Equal(t, token.RefreshToken, "token")
assert.Equal(t, token.AccessToken, "token")
assert.NotNil(t, token)
}

func TestMockedTokenExpired(t *testing.T) {
Expand Down
14 changes: 9 additions & 5 deletions internal/auth/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package auth
import (
"context"
"fmt"
"github.com/pkg/browser"
"log"
"net/http"
"time"

"github.com/pkg/browser"
)

type IAuthWebHelper interface {
Expand All @@ -20,6 +21,7 @@ type AuthWebHelper struct {

func NewAuthWebHelper() AuthWebHelper {
mux := http.NewServeMux()

return AuthWebHelper{
ServeMux: mux,
}
Expand All @@ -32,6 +34,7 @@ func (awh AuthWebHelper) Callback(state string) string {
awh.ServeMux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("state") != state {
http.Error(w, "Invalid state", http.StatusBadRequest)

return
}

Expand All @@ -49,10 +52,11 @@ func (awh AuthWebHelper) Callback(state string) string {
log.Fatalf("HTTP server error: %v", err)
}
}()

defer server.Shutdown(
context.Background(),
)
defer func() {
if err := server.Shutdown(context.Background()); err != nil {
log.Fatalf("HTTP server shutdown error: %v", err)
}
}()
authCode := <-code // Wait for the authorization code

return authCode
Expand Down
24 changes: 16 additions & 8 deletions internal/auth/callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

const testState = "test_state"
Expand All @@ -26,8 +28,10 @@ func TestCallback(t *testing.T) {
if err != nil {
t.Fatalf("Failed to make callback request: %v", err)
}
defer resp.Body.Close()

defer func() {
err := resp.Body.Close()
assert.NoError(t, err)
}()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status OK, got %v", resp.Status)
}
Expand Down Expand Up @@ -63,9 +67,15 @@ func TestCallbackInvalidState(t *testing.T) {
}

func TestCallbackServerError(t *testing.T) {
server := &http.Server{Addr: ":9096"}
go server.ListenAndServe()
defer server.Shutdown(context.Background())
server := &http.Server{Addr: ":9096", ReadHeaderTimeout: time.Second}
go func() {
err := server.ListenAndServe()
assert.Error(t, err) // Two servers trying to run on localhost:9096
}()
defer func() {
err := server.Shutdown(context.Background())
assert.NoError(t, err)
}()

awh := AuthWebHelper{}

Expand All @@ -83,9 +93,7 @@ func TestCallbackServerError(t *testing.T) {

select {
case err := <-resultChan:
if err == nil {
t.Error("Expected an error due to server already running, but got none")
}
assert.Error(t, err)
case <-time.After(2 * time.Second):
t.Fatal("Test timed out")
}
Expand Down
24 changes: 22 additions & 2 deletions internal/auth/testdata/auth_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package testdata
import (
"context"
"strings"
"time"

"github.com/golang-jwt/jwt"
"golang.org/x/oauth2"
)

Expand All @@ -26,7 +28,16 @@ func (msc MockSecretClient) Set(service, secret string) error {
}

func (msc MockSecretClient) Get(service string) (string, error) {
return "token", nil
var secretKey = []byte("secret-key")
token := jwt.NewWithClaims(
jwt.SigningMethodHS256,
jwt.MapClaims{
"username": "mockDebrickedClient",
"exp": time.Now().Add(time.Hour).Unix(),
},
)

return token.SignedString(secretKey)
}

func (msc MockSecretClient) Delete(service string) error {
Expand All @@ -38,7 +49,16 @@ func (msc MockExpiredSecretClient) Set(service, secret string) error {
}

func (msc MockExpiredSecretClient) Get(service string) (string, error) {
return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiIwMTkxOTQ2Mi03ZDZlLTc4ZTgtYWEyNC1iYTc3OTIxM2M5MGYiLCJqdGkiOiJlMTdhMmFlYTk0ZjgyNTdjYWU1NWM3ZjRiNTczNTRiMzI2YmNiYTZiZmY3ZGQ0ZWQ2NjU3NDA4MWE4ODFjN2VhMmM3OGU3Y2EzM2UxMjU5MyIsImlhdCI6MTY5NDU5NzkzNy4zNjAwMTUsIm5iZiI6MTY5NDU5NzkzNy4zNjAwMTcsImV4cCI6MTY5NDU5NzkzNy4zNTM3MDMsInN1YiI6ImZpbGlwLmhlZGVuK2FkbWluQGRlYnJpY2tlZC5jb20iLCJzY29wZXMiOlsic2VsZWN0IiwicHJvZmlsZSIsImJhc2ljUmVwbyJdfQ.CMqnQM9QFHTthDMv4K8q6gmkkFmbOIhrmKXwfo7kMWU", nil
var secretKey = []byte("secret-key")
token := jwt.NewWithClaims(
jwt.SigningMethodHS256,
jwt.MapClaims{
"username": "mockDebrickedClient",
"exp": time.Now().Add(-time.Hour).Unix(),
},
)

return token.SignedString(secretKey)
}

func (msc MockExpiredSecretClient) Delete(service string) error {
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/auth/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package login

import (
"fmt"

"github.com/debricked/cli/internal/auth"
"github.com/fatih/color"
"github.com/spf13/cobra"
Expand All @@ -18,6 +19,7 @@ func NewLoginCmd(authenticator auth.IAuthenticator) *cobra.Command {
},
RunE: RunE(authenticator),
}

return cmd
}

Expand Down
1 change: 1 addition & 0 deletions internal/cmd/auth/logout/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logout

import (
"fmt"

"github.com/debricked/cli/internal/auth"
"github.com/fatih/color"
"github.com/spf13/cobra"
Expand Down

0 comments on commit d6a68c1

Please sign in to comment.