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..3983b50 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/DelineaXPM/tss-sdk-go/v2 go 1.13 + +require github.com/tidwall/gjson v1.18.0 diff --git a/go.sum b/go.sum index e69de29..b8846a1 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,6 @@ +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..889e9a4 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 ( @@ -40,6 +43,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 +260,43 @@ func (s Server) uploadFile(secretId int, fileField SecretField) error { return err } +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)) + + data, _ := json.Marshal(cache) + os.Setenv("SS_AT", string(data)) + return nil +} + +func (s *Server) getCacheAccessToken() (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() + if found { + return accessToken, nil + } + response, err := s.checkPlatformDetails() if err != nil { log.Print("Error while checking server details:", err) @@ -292,6 +331,7 @@ func (s *Server) getAccessToken() (string, error) { log.Print("[ERROR] parsing grant response:", err) return "", err } + s.setCacheAccessToken(grant.AccessToken, grant.ExpiresIn) return grant.AccessToken, nil } else { return response, nil