Handle expiration and token fields in client package.
Signed-off-by: Matt Moyer <moyerm@vmware.com>
This commit is contained in:
parent
b59604b47c
commit
ec6ec2abe9
@ -132,6 +132,8 @@ func ExchangeToken(ctx context.Context, token, caBundle, apiEndpoint string) (*C
|
|||||||
Kind string `json:"kind"`
|
Kind string `json:"kind"`
|
||||||
Status struct {
|
Status struct {
|
||||||
Credential *struct {
|
Credential *struct {
|
||||||
|
ExpirationTimestamp string `json:"expirationTimestamp"`
|
||||||
|
Token string `json:"token"`
|
||||||
ClientCertificateData string `json:"clientCertificateData"`
|
ClientCertificateData string `json:"clientCertificateData"`
|
||||||
ClientKeyData string `json:"clientKeyData"`
|
ClientKeyData string `json:"clientKeyData"`
|
||||||
}
|
}
|
||||||
@ -146,8 +148,18 @@ func ExchangeToken(ctx context.Context, token, caBundle, apiEndpoint string) (*C
|
|||||||
return nil, fmt.Errorf("%w: %s", ErrLoginFailed, respBody.Status.Message)
|
return nil, fmt.Errorf("%w: %s", ErrLoginFailed, respBody.Status.Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Credential{
|
result := Credential{
|
||||||
|
Token: respBody.Status.Credential.Token,
|
||||||
ClientCertificateData: respBody.Status.Credential.ClientCertificateData,
|
ClientCertificateData: respBody.Status.Credential.ClientCertificateData,
|
||||||
ClientKeyData: respBody.Status.Credential.ClientKeyData,
|
ClientKeyData: respBody.Status.Credential.ClientKeyData,
|
||||||
}, nil
|
}
|
||||||
|
if str := respBody.Status.Credential.ExpirationTimestamp; str != "" {
|
||||||
|
expiration, err := time.Parse(time.RFC3339, str)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid login response: %w", err)
|
||||||
|
}
|
||||||
|
result.ExpirationTimestamp = &expiration
|
||||||
|
}
|
||||||
|
|
||||||
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
@ -151,6 +151,33 @@ func TestExchangeToken(t *testing.T) {
|
|||||||
require.Nil(t, got)
|
require.Nil(t, got)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("invalid timestamp failure", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
// Start a test server that returns success but with an error message
|
||||||
|
caBundle, endpoint := startTestServer(t, func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("content-type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
_, _ = w.Write([]byte(`
|
||||||
|
{
|
||||||
|
"kind": "LoginRequest",
|
||||||
|
"apiVersion": "placeholder.suzerain-io.github.io/v1alpha1",
|
||||||
|
"metadata": {
|
||||||
|
"creationTimestamp": null
|
||||||
|
},
|
||||||
|
"spec": {},
|
||||||
|
"status": {
|
||||||
|
"credential": {
|
||||||
|
"expirationTimestamp": "invalid"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`))
|
||||||
|
})
|
||||||
|
|
||||||
|
got, err := ExchangeToken(ctx, "", caBundle, endpoint)
|
||||||
|
require.EqualError(t, err, `invalid login response: parsing time "invalid" as "2006-01-02T15:04:05Z07:00": cannot parse "invalid" as "2006"`)
|
||||||
|
require.Nil(t, got)
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("success", func(t *testing.T) {
|
t.Run("success", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@ -172,8 +199,8 @@ func TestExchangeToken(t *testing.T) {
|
|||||||
"spec": {
|
"spec": {
|
||||||
"type": "token",
|
"type": "token",
|
||||||
"token": {
|
"token": {
|
||||||
"value": "test-token"
|
"value": "test-token"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"status": {}
|
"status": {}
|
||||||
}`,
|
}`,
|
||||||
@ -192,6 +219,8 @@ func TestExchangeToken(t *testing.T) {
|
|||||||
"spec": {},
|
"spec": {},
|
||||||
"status": {
|
"status": {
|
||||||
"credential": {
|
"credential": {
|
||||||
|
"expirationTimestamp": "2020-07-30T15:52:01Z",
|
||||||
|
"token": "test-token",
|
||||||
"clientCertificateData": "test-certificate",
|
"clientCertificateData": "test-certificate",
|
||||||
"clientKeyData": "test-key"
|
"clientKeyData": "test-key"
|
||||||
}
|
}
|
||||||
@ -201,7 +230,10 @@ func TestExchangeToken(t *testing.T) {
|
|||||||
|
|
||||||
got, err := ExchangeToken(ctx, "test-token", caBundle, endpoint)
|
got, err := ExchangeToken(ctx, "test-token", caBundle, endpoint)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
expires := time.Date(2020, 07, 30, 15, 52, 1, 0, time.UTC)
|
||||||
require.Equal(t, &Credential{
|
require.Equal(t, &Credential{
|
||||||
|
ExpirationTimestamp: &expires,
|
||||||
|
Token: "test-token",
|
||||||
ClientCertificateData: "test-certificate",
|
ClientCertificateData: "test-certificate",
|
||||||
ClientKeyData: "test-key",
|
ClientKeyData: "test-key",
|
||||||
}, got)
|
}, got)
|
||||||
|
Loading…
Reference in New Issue
Block a user