Skip to content

Commit

Permalink
Merge pull request #35 from DelineaXPM/bh.cache3
Browse files Browse the repository at this point in the history
added simple access token caching
  • Loading branch information
delinea-sagar authored Nov 22, 2024
2 parents 680813d + a7125f7 commit 00beb33
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@
.vscode
.idea
test_config.json
.DS_Store
43 changes: 43 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import (
"io"
"io/ioutil"
"log"
"math"
"mime/multipart"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"time"
)

const (
Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -292,6 +331,10 @@ func (s *Server) getAccessToken() (string, error) {
log.Print("[ERROR] parsing grant response:", err)
return "", err
}
if err = s.setCacheAccessToken(grant.AccessToken, grant.ExpiresIn); err != nil {
log.Print("[ERROR] caching access token:", err)
return "", err
}
return grant.AccessToken, nil
} else {
return response, nil
Expand Down

0 comments on commit 00beb33

Please sign in to comment.