From 279c0de75629de5868c3ac1b3272a2850e6b62f7 Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Mon, 28 Nov 2022 13:28:27 +0100 Subject: OAuth: Refactor Token getting and do not save them in the config This commit refactors getting the tokens into receiver methods. This means that functions do not have to call the cryptic "EnsureTokens" method. The receiver getter then already verifier whether or not the tokens could be obtained (and refreshes too). The downside is that some things are now private, so testing for invalid tokens needs to be done somewhere else. This needs another patch such that clients can save the tokens themselves using a keyring. --- internal/oauth/token.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 internal/oauth/token.go (limited to 'internal/oauth/token.go') diff --git a/internal/oauth/token.go b/internal/oauth/token.go new file mode 100644 index 0000000..8ceb9a8 --- /dev/null +++ b/internal/oauth/token.go @@ -0,0 +1,37 @@ +package oauth + +import "time" + +// OAuthTokenResponse defines the OAuth response from the server that includes the tokens. +type OAuthTokenResponse struct { + // Access is the access token returned by the server + Access string `json:"access_token"` + + // Refresh token is the refresh token returned by the server + Refresh string `json:"refresh_token"` + + // Type indicates which type of tokens we have + Type string `json:"token_type"` + + // Expires is the expires time returned by the server + Expires int64 `json:"expires_in"` + +} + +// OAuthToken is a structure that contains our access and refresh tokens and a timestamp when they expire. +type OAuthToken struct { + // Access is the access token returned by the server + access string + + // Refresh token is the refresh token returned by the server + refresh string + + // ExpiredTimestamp is the Expires field but converted to a Go timestamp + expiredTimestamp time.Time +} + +// Expired checks if the access token is expired. +func (tokens *OAuthToken) Expired() bool { + currentTime := time.Now() + return !currentTime.Before(tokens.expiredTimestamp) +} -- cgit v1.2.3