diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-08-09 14:18:22 +0200 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-08-09 14:18:22 +0200 |
| commit | 9c7d9958132bcea0aa5ff6ab4aaec67c73087408 (patch) | |
| tree | b3a85b1b340de7b8169542c32972a7f3dd2314aa /internal/oauth | |
| parent | 0c6233ab691973859b6d636e6d9fdddd2a6acd5e (diff) | |
Refactor: Cleanup time calculations and usage
Diffstat (limited to 'internal/oauth')
| -rw-r--r-- | internal/oauth/oauth.go | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/internal/oauth/oauth.go b/internal/oauth/oauth.go index b1569e9..79f4ebf 100644 --- a/internal/oauth/oauth.go +++ b/internal/oauth/oauth.go @@ -8,6 +8,7 @@ import ( "fmt" "net/http" "net/url" + "time" "github.com/jwijenbergh/eduvpn-common/internal/fsm" httpw "github.com/jwijenbergh/eduvpn-common/internal/http" @@ -85,7 +86,7 @@ type OAuthToken struct { Refresh string `json:"refresh_token"` Type string `json:"token_type"` Expires int64 `json:"expires_in"` - ExpiredTimestamp int64 `json:"expires_in_timestamp"` + ExpiredTimestamp time.Time `json:"expires_in_timestamp"` } // Gets an authorized HTTP client by obtaining refresh and access tokens @@ -123,7 +124,7 @@ func (oauth *OAuth) getTokensWithAuthCode(authCode string) error { "content-type": {"application/x-www-form-urlencoded"}, } opts := &httpw.HTTPOptionalParams{Headers: headers, Body: data} - current_time := util.GenerateTimeSeconds() + current_time := util.GetCurrentTime() _, body, bodyErr := httpw.HTTPPostWithOpts(reqURL, opts) if bodyErr != nil { return &types.WrappedErrorMessage{Message: errorMessage, Err: bodyErr} @@ -137,15 +138,15 @@ func (oauth *OAuth) getTokensWithAuthCode(authCode string) error { return &types.WrappedErrorMessage{Message: errorMessage, Err: &httpw.HTTPParseJsonError{URL: reqURL, Body: string(body), Err: jsonErr}} } - tokenStructure.ExpiredTimestamp = current_time + tokenStructure.Expires + tokenStructure.ExpiredTimestamp = current_time.Add(time.Second * time.Duration(tokenStructure.Expires)) oauth.Token = tokenStructure return nil } func (oauth *OAuth) isTokensExpired() bool { expired_time := oauth.Token.ExpiredTimestamp - current_time := util.GenerateTimeSeconds() - return current_time >= expired_time + current_time := util.GetCurrentTime() + return !current_time.Before(expired_time) } // Get the access and refresh tokens with a previously received refresh token @@ -162,7 +163,7 @@ func (oauth *OAuth) getTokensWithRefresh() error { "content-type": {"application/x-www-form-urlencoded"}, } opts := &httpw.HTTPOptionalParams{Headers: headers, Body: data} - current_time := util.GenerateTimeSeconds() + current_time := util.GetCurrentTime() _, body, bodyErr := httpw.HTTPPostWithOpts(reqURL, opts) if bodyErr != nil { return &types.WrappedErrorMessage{Message: errorMessage, Err: bodyErr} @@ -175,7 +176,7 @@ func (oauth *OAuth) getTokensWithRefresh() error { return &types.WrappedErrorMessage{Message: errorMessage, Err: &httpw.HTTPParseJsonError{URL: reqURL, Body: string(body), Err: jsonErr}} } - tokenStructure.ExpiredTimestamp = current_time + tokenStructure.Expires + tokenStructure.ExpiredTimestamp = current_time.Add(time.Second * time.Duration(tokenStructure.Expires)) oauth.Token = tokenStructure return nil } |
