downscope: add some context to returned error

This commit is contained in:
Patrick Jones 2021-06-14 10:55:04 -07:00
parent be467eec8b
commit c4c64d51bc
1 changed files with 8 additions and 3 deletions

View File

@ -14,6 +14,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"time" "time"
@ -129,15 +130,19 @@ func downscopedTokenWithEndpoint(ctx context.Context, config DownscopingConfig,
return nil, fmt.Errorf("unable to generate POST Request %v", err) return nil, fmt.Errorf("unable to generate POST Request %v", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("downscope: unable to exchange token; %v. Failed to read response body: %v", resp.StatusCode, err)
}
return nil, fmt.Errorf("downscope: unable to exchange token; %v. Server responsed: %v", resp.StatusCode, string(b))
}
var tresp downscopedTokenResponse var tresp downscopedTokenResponse
err = json.NewDecoder(resp.Body).Decode(&tresp) err = json.NewDecoder(resp.Body).Decode(&tresp)
if err != nil { if err != nil {
return nil, fmt.Errorf("downscope: unable to unmarshal response body: %v", err) return nil, fmt.Errorf("downscope: unable to unmarshal response body: %v", err)
} }
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("downscope: unable to exchange token; %v", resp.StatusCode)
}
// an exchanged token that is derived from a service account (2LO) has an expired_in value // an exchanged token that is derived from a service account (2LO) has an expired_in value
// a token derived from a users token (3LO) does not. // a token derived from a users token (3LO) does not.