@@ -2,6 +2,7 @@ package curl
2
2
3
3
import (
4
4
"bytes"
5
+ "encoding/json"
5
6
"fmt"
6
7
"io"
7
8
"net/http"
@@ -170,11 +171,29 @@ func getBearerToken(p *print.Printer) (string, error) {
170
171
p .Debug (print .ErrorLevel , "configure authentication: %v" , err )
171
172
return "" , & errors.AuthError {}
172
173
}
173
- token , err := auth .GetAuthField (auth .ACCESS_TOKEN )
174
+
175
+ userSessionExpired , err := auth .UserSessionExpired ()
176
+ if err != nil {
177
+ return "" , err
178
+ }
179
+ if userSessionExpired {
180
+ return "" , & errors.SessionExpiredError {}
181
+ }
182
+
183
+ accessToken , err := auth .GetAccessToken ()
174
184
if err != nil {
175
- return "" , fmt .Errorf ("get access token: %w" , err )
185
+ return "" , err
186
+ }
187
+
188
+ accessTokenExpired , err := auth .TokenExpired (accessToken )
189
+ if err != nil {
190
+ return "" , err
191
+ }
192
+ if accessTokenExpired {
193
+ return "" , & errors.AccessTokenExpiredError {}
176
194
}
177
- return token , nil
195
+
196
+ return accessToken , nil
178
197
}
179
198
180
199
func buildRequest (model * inputModel , bearerToken string ) (* http.Request , error ) {
@@ -213,6 +232,22 @@ func outputResponse(p *print.Printer, model *inputModel, resp *http.Response) er
213
232
if err != nil {
214
233
return fmt .Errorf ("read response body: %w" , err )
215
234
}
235
+
236
+ if strings .Contains (strings .ToLower (string (respBody )), "jwt is expired" ) {
237
+ return & errors.SessionExpiredError {}
238
+ }
239
+
240
+ if strings .Contains (strings .ToLower (string (respBody )), "jwt is missing" ) {
241
+ return & errors.AuthError {}
242
+ }
243
+
244
+ var prettyJSON bytes.Buffer
245
+ if json .Valid (respBody ) {
246
+ if err := json .Indent (& prettyJSON , respBody , "" , " " ); err == nil {
247
+ respBody = prettyJSON .Bytes ()
248
+ } // if indenting fails, fall back to original body
249
+ }
250
+
216
251
output = append (output , respBody ... )
217
252
218
253
if model .OutputFile == nil {
0 commit comments