From f9b95395aaa7d51f5dd2ef67684ddc3cb924eb49 Mon Sep 17 00:00:00 2001 From: Bill Hamilton Date: Wed, 6 Nov 2024 10:06:22 -0800 Subject: [PATCH 1/3] added simple caching function for access tokens --- .gitignore | 2 ++ go.mod | 5 +++++ go.sum | 8 ++++++++ server/server.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+) diff --git a/.gitignore b/.gitignore index e2aa8a6..6fa70f0 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,5 @@ .vscode .idea test_config.json +.DS_Store +main.go~ \ No newline at end of file diff --git a/go.mod b/go.mod index eb1117f..2fd078c 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,8 @@ module github.com/DelineaXPM/tss-sdk-go/v2 go 1.13 + +require ( + github.com/patrickmn/go-cache v2.1.0+incompatible // indirect + github.com/tidwall/gjson v1.18.0 // indirect +) diff --git a/go.sum b/go.sum index e69de29..1e0ee1d 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,8 @@ +github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= +github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= +github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY= +github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= diff --git a/server/server.go b/server/server.go index fc21aca..669866a 100644 --- a/server/server.go +++ b/server/server.go @@ -8,11 +8,14 @@ import ( "io" "io/ioutil" "log" + "math" "mime/multipart" "net/http" "net/url" + "os" "regexp" "strings" + "time" ) const ( @@ -20,6 +23,7 @@ const ( defaultAPIPathURI string = "/api/v1" defaultTokenPathURI string = "/oauth2/token" defaultTLD string = "com" + cacheTokenName string = "access_token" ) // UserCredential holds the username and password that the API should use to @@ -40,6 +44,11 @@ type Server struct { Configuration } +type TokenCache struct { + AccessToken string `json:"access_token"` + ExpiresIn int `json:"expires_in"` +} + // New returns an initialized Secrets object func New(config Configuration) (*Server, error) { if config.ServerURL == "" && config.Tenant == "" || config.ServerURL != "" && config.Tenant != "" { @@ -252,12 +261,44 @@ func (s Server) uploadFile(secretId int, fileField SecretField) error { return err } +func (s *Server) setCacheAccessToken(key, value string, expiresIn int) error { + + cache := TokenCache{} + cache.AccessToken = value + cache.ExpiresIn = (int(time.Now().Unix()) + expiresIn) - int(math.Floor(float64(expiresIn)*0.9)) + + data, _ := json.Marshal(cache) + os.Setenv("SS_AT", string(data)) + return nil +} + +func (s *Server) getCacheAccessToken(key string) (string, bool) { + data, ok := os.LookupEnv("SS_AT") + if !ok { + os.Setenv("SS_AT", "") + return "", ok + } + cache := TokenCache{} + if err := json.Unmarshal([]byte(data), &cache); err != nil { + return "", false + } + if time.Now().Unix() < int64(cache.ExpiresIn) { + return cache.AccessToken, true + } + return "", false +} + // getAccessToken gets an OAuth2 Access Grant and returns the token // endpoint and get an accessGrant. func (s *Server) getAccessToken() (string, error) { if s.Credentials.Token != "" { return s.Credentials.Token, nil } + accessToken, found := s.getCacheAccessToken(cacheTokenName) + if found { + return accessToken, nil + } + response, err := s.checkPlatformDetails() if err != nil { log.Print("Error while checking server details:", err) @@ -292,6 +333,7 @@ func (s *Server) getAccessToken() (string, error) { log.Print("[ERROR] parsing grant response:", err) return "", err } + s.setCacheAccessToken(cacheTokenName, grant.AccessToken, grant.ExpiresIn) return grant.AccessToken, nil } else { return response, nil From 18ce924a09732a92c2b0ce7c4aa6aadaab22a668 Mon Sep 17 00:00:00 2001 From: Bill Hamilton Date: Wed, 6 Nov 2024 12:00:15 -0800 Subject: [PATCH 2/3] modified caching functions attributes Signed-off-by: Bill Hamilton --- server/server.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/server/server.go b/server/server.go index 669866a..c99cccb 100644 --- a/server/server.go +++ b/server/server.go @@ -23,7 +23,6 @@ const ( defaultAPIPathURI string = "/api/v1" defaultTokenPathURI string = "/oauth2/token" defaultTLD string = "com" - cacheTokenName string = "access_token" ) // UserCredential holds the username and password that the API should use to @@ -261,7 +260,7 @@ func (s Server) uploadFile(secretId int, fileField SecretField) error { return err } -func (s *Server) setCacheAccessToken(key, value string, expiresIn int) error { +func (s *Server) setCacheAccessToken(value string, expiresIn int) error { cache := TokenCache{} cache.AccessToken = value @@ -272,7 +271,7 @@ func (s *Server) setCacheAccessToken(key, value string, expiresIn int) error { return nil } -func (s *Server) getCacheAccessToken(key string) (string, bool) { +func (s *Server) getCacheAccessToken() (string, bool) { data, ok := os.LookupEnv("SS_AT") if !ok { os.Setenv("SS_AT", "") @@ -294,7 +293,7 @@ func (s *Server) getAccessToken() (string, error) { if s.Credentials.Token != "" { return s.Credentials.Token, nil } - accessToken, found := s.getCacheAccessToken(cacheTokenName) + accessToken, found := s.getCacheAccessToken() if found { return accessToken, nil } @@ -333,7 +332,7 @@ func (s *Server) getAccessToken() (string, error) { log.Print("[ERROR] parsing grant response:", err) return "", err } - s.setCacheAccessToken(cacheTokenName, grant.AccessToken, grant.ExpiresIn) + s.setCacheAccessToken(grant.AccessToken, grant.ExpiresIn) return grant.AccessToken, nil } else { return response, nil From 450ea671ff165f014535b04d74f05a344f5ca68f Mon Sep 17 00:00:00 2001 From: Bill Hamilton Date: Thu, 21 Nov 2024 07:30:46 -0800 Subject: [PATCH 3/3] removed unused dependencies Signed-off-by: Bill Hamilton --- go.mod | 5 +---- go.sum | 2 -- server/server.go | 1 - 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 2fd078c..3983b50 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,4 @@ module github.com/DelineaXPM/tss-sdk-go/v2 go 1.13 -require ( - github.com/patrickmn/go-cache v2.1.0+incompatible // indirect - github.com/tidwall/gjson v1.18.0 // indirect -) +require github.com/tidwall/gjson v1.18.0 diff --git a/go.sum b/go.sum index 1e0ee1d..b8846a1 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= -github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY= github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= diff --git a/server/server.go b/server/server.go index c99cccb..889e9a4 100644 --- a/server/server.go +++ b/server/server.go @@ -261,7 +261,6 @@ func (s Server) uploadFile(secretId int, fileField SecretField) error { } func (s *Server) setCacheAccessToken(value string, expiresIn int) error { - cache := TokenCache{} cache.AccessToken = value cache.ExpiresIn = (int(time.Now().Unix()) + expiresIn) - int(math.Floor(float64(expiresIn)*0.9))