diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-11-24 15:16:06 +0100 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-11-24 15:16:06 +0100 |
| commit | 5d10e7b2e85b89cb087493279d9c5c9420f0b1e2 (patch) | |
| tree | f0a63e1bc4f6b46513324f1ad2833102a285889a | |
| parent | f0ec0be5e833f72fc650ce9d093722838e4f8034 (diff) | |
Util: Get rid of current time helper
Fixes #5
| -rw-r--r-- | internal/discovery/discovery.go | 6 | ||||
| -rw-r--r-- | internal/oauth/oauth.go | 6 | ||||
| -rw-r--r-- | internal/server/common.go | 8 | ||||
| -rw-r--r-- | internal/util/util.go | 5 | ||||
| -rw-r--r-- | internal/util/util_test.go | 12 |
5 files changed, 10 insertions, 27 deletions
diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go index d639406..60f30e3 100644 --- a/internal/discovery/discovery.go +++ b/internal/discovery/discovery.go @@ -155,7 +155,7 @@ func (discovery *Discovery) DetermineServersUpdate() bool { } // 1 hour from the last update should_update_time := discovery.Servers.Timestamp.Add(1 * time.Hour) - now := util.GetCurrentTime() + now := time.Now() return !now.Before(should_update_time) } @@ -173,7 +173,7 @@ func (discovery *Discovery) GetOrganizationsList() (*types.DiscoveryOrganization bodyErr, ) } - discovery.Organizations.Timestamp = util.GetCurrentTime() + discovery.Organizations.Timestamp = time.Now() return &discovery.Organizations, nil } @@ -192,7 +192,7 @@ func (discovery *Discovery) GetServersList() (*types.DiscoveryServers, error) { ) } // Update servers timestamp - discovery.Servers.Timestamp = util.GetCurrentTime() + discovery.Servers.Timestamp = time.Now() return &discovery.Servers, nil } diff --git a/internal/oauth/oauth.go b/internal/oauth/oauth.go index 4bccdf5..9518d79 100644 --- a/internal/oauth/oauth.go +++ b/internal/oauth/oauth.go @@ -161,7 +161,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.GetCurrentTime() + current_time := time.Now() _, body, bodyErr := httpw.HTTPPostWithOpts(reqURL, opts) if bodyErr != nil { return types.NewWrappedError(errorMessage, bodyErr) @@ -187,7 +187,7 @@ func (oauth *OAuth) getTokensWithAuthCode(authCode string) error { func (oauth *OAuth) isTokensExpired() bool { expired_time := oauth.Token.ExpiredTimestamp - current_time := util.GetCurrentTime() + current_time := time.Now() return !current_time.Before(expired_time) } @@ -205,7 +205,7 @@ func (oauth *OAuth) getTokensWithRefresh() error { "content-type": {"application/x-www-form-urlencoded"}, } opts := &httpw.HTTPOptionalParams{Headers: headers, Body: data} - current_time := util.GetCurrentTime() + current_time := time.Now() _, body, bodyErr := httpw.HTTPPostWithOpts(reqURL, opts) if bodyErr != nil { return types.NewWrappedError(errorMessage, bodyErr) diff --git a/internal/server/common.go b/internal/server/common.go index 7d711f0..82a9482 100644 --- a/internal/server/common.go +++ b/internal/server/common.go @@ -228,7 +228,7 @@ func ShouldRenewButton(server Server) bool { } // Get current time - current := util.GetCurrentTime() + current := time.Now() // Session is expired if !current.Before(base.EndTime) { @@ -268,7 +268,7 @@ func GetHeaderToken(server Server) string { } func MarkTokenExpired(server Server) { - server.GetOAuth().Token.ExpiredTimestamp = util.GetCurrentTime() + server.GetOAuth().Token.ExpiredTimestamp = time.Now() } func MarkTokensForRenew(server Server) { @@ -394,7 +394,7 @@ func wireguardGetConfig(server Server, preferTCP bool, supportsOpenVPN bool) (st } // Store start and end time - base.StartTime = util.GetCurrentTime() + base.StartTime = time.Now() base.EndTime = expires if content == "wireguard" { @@ -419,7 +419,7 @@ func openVPNGetConfig(server Server, preferTCP bool) (string, string, error) { configOpenVPN, expires, configErr := APIConnectOpenVPN(server, profile_id, preferTCP) // Store start and end time - base.StartTime = util.GetCurrentTime() + base.StartTime = time.Now() base.EndTime = expires if configErr != nil { diff --git a/internal/util/util.go b/internal/util/util.go index ef52ce2..cbe9c1b 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -7,7 +7,6 @@ import ( "os" "path" "strings" - "time" "github.com/eduvpn/eduvpn-common/types" ) @@ -49,10 +48,6 @@ func MakeRandomByteSlice(size int) ([]byte, error) { return byteSlice, nil } -func GetCurrentTime() time.Time { - return time.Now() -} - func EnsureDirectory(directory string) error { // Create with 700 permissions, read, write, execute only for the owner mkdirErr := os.MkdirAll(directory, 0o700) diff --git a/internal/util/util_test.go b/internal/util/util_test.go index eb1a9f6..dbeec62 100644 --- a/internal/util/util_test.go +++ b/internal/util/util_test.go @@ -58,18 +58,6 @@ func Test_MakeRandomByteSlice(t *testing.T) { } } -func Test_GetCurrentTime(t *testing.T) { - time_now := GetCurrentTime() - - time.Sleep(1 * time.Second) - - time_after_1_second := GetCurrentTime() - - if !time_after_1_second.After(time_now) { - t.Fatal("Time is not after previous time") - } -} - func Test_WAYFEncode(t *testing.T) { // AuthTemplate returnTo := "127.0.0.1:8000/test123bla/#wow " |
