Skip to content

Commit 0737d5b

Browse files
committed
refactor: change token exchanger to unmarshal expires_in better
Signed-off-by: Chris Privitere <23177737+cprivitere@users.noreply.github.com>
1 parent ae9a1f3 commit 0737d5b

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

internal/emlb/token_exchanger.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ type TokenExchanger struct {
3333
client *http.Client
3434
}
3535

36+
// TokenResponse adds ExpiresIn to the OauthResponse struct.
37+
type TokenResponse struct {
38+
oauth2.Token
39+
ExpiresIn int64 `json:"expires_in,omitempty"`
40+
}
41+
3642
// Token creates a Token object to authenticate with the Load Balancer API.
3743
func (m *TokenExchanger) Token() (*oauth2.Token, error) {
3844
tokenExchangeRequest, err := http.NewRequest(http.MethodPost, m.tokenExchangeURL, http.NoBody) //nolint:noctx // we can't find a way to get the ctx into here yet and just using context.Background adds no value that we can tell
@@ -55,20 +61,17 @@ func (m *TokenExchanger) Token() (*oauth2.Token, error) {
5561
return nil, fmt.Errorf("token exchange request failed with status %v, body %v", resp.StatusCode, string(body))
5662
}
5763

58-
token := oauth2.Token{}
59-
err = json.Unmarshal(body, &token)
64+
var tokenResp TokenResponse
65+
err = json.Unmarshal(body, &tokenResp)
6066
if err != nil {
61-
fmt.Println(len(body))
62-
fmt.Println(token)
63-
fmt.Println(err)
6467
return nil, err
6568
}
6669

67-
expiresIn := token.Extra("expires_in")
68-
if expiresIn != nil {
69-
expiresInSeconds := expiresIn.(int)
70-
token.Expiry = time.Now().Add(time.Second * time.Duration(expiresInSeconds))
70+
fmt.Println(tokenResp.ExpiresIn)
71+
72+
if tokenResp.Expiry.IsZero() && tokenResp.ExpiresIn != 0 {
73+
tokenResp.Expiry = time.Now().Add(time.Second * time.Duration(tokenResp.ExpiresIn))
7174
}
7275

73-
return &token, nil
76+
return &tokenResp.Token, nil
7477
}

0 commit comments

Comments
 (0)