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
41 changes: 38 additions & 3 deletions internal/cmd/curl/curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package curl

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -170,11 +171,29 @@ func getBearerToken(p *print.Printer) (string, error) {
p.Debug(print.ErrorLevel, "configure authentication: %v", err)
return "", &errors.AuthError{}
}
token, err := auth.GetAuthField(auth.ACCESS_TOKEN)

userSessionExpired, err := auth.UserSessionExpired()
if err != nil {
return "", err
}
if userSessionExpired {
return "", &errors.SessionExpiredError{}
}

accessToken, err := auth.GetAccessToken()
if err != nil {
return "", fmt.Errorf("get access token: %w", err)
return "", err
}

accessTokenExpired, err := auth.TokenExpired(accessToken)
if err != nil {
return "", err
}
if accessTokenExpired {
return "", &errors.AccessTokenExpiredError{}
}
return token, nil

return accessToken, nil
}

func buildRequest(model *inputModel, bearerToken string) (*http.Request, error) {
Expand Down Expand Up @@ -213,6 +232,22 @@ func outputResponse(p *print.Printer, model *inputModel, resp *http.Response) er
if err != nil {
return fmt.Errorf("read response body: %w", err)
}

if strings.Contains(strings.ToLower(string(respBody)), "jwt is expired") {
return &errors.SessionExpiredError{}
}

if strings.Contains(strings.ToLower(string(respBody)), "jwt is missing") {
return &errors.AuthError{}
}

var prettyJSON bytes.Buffer
if json.Valid(respBody) {
if err := json.Indent(&prettyJSON, respBody, "", " "); err == nil {
respBody = prettyJSON.Bytes()
} // if indenting fails, fall back to original body
}

output = append(output, respBody...)

if model.OutputFile == nil {
Expand Down
18 changes: 18 additions & 0 deletions internal/cmd/curl/curl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"os"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -427,6 +429,22 @@ func TestOutputResponse(t *testing.T) {
},
wantErr: false,
},
{
name: "expired jwt curl",
args: args{
model: fixtureInputModel(),
resp: &http.Response{Body: io.NopCloser(strings.NewReader("Jwt is expired"))},
},
wantErr: true,
},
{
name: "mssing jwt curl",
args: args{
model: fixtureInputModel(),
resp: &http.Response{Body: io.NopCloser(strings.NewReader("Jwt is missing"))},
},
wantErr: true,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
Expand Down