sourcegraph/internal/codeintel/uploads/transport/http/auth/github_cache.go
Noah S-C 9333f8f283
codeintel: consolidate enterprise & oss codeintel packages (#54431)
Movin enterprise codeintel stuff (and ~two others that had to be dragged
along) out of `enterprise/internal` and into `internal` as part of the
shift towards enterprise-only

## Test plan

Successfully built frontend with bazel, CI will check the rest
😎 no logic changed, just shufflin things around
2023-07-05 14:58:41 +01:00

38 lines
789 B
Go

package auth
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"github.com/sourcegraph/sourcegraph/internal/rcache"
)
type GitHubAuthCache struct {
cache *rcache.Cache
}
var githubAuthCache = &GitHubAuthCache{
cache: rcache.NewWithTTL("codeintel.github-authz:", 60 /* seconds */),
}
func (c *GitHubAuthCache) Get(key string) (authorized bool, _ bool) {
b, ok := c.cache.Get(key)
if !ok {
return false, false
}
err := json.Unmarshal(b, &authorized)
return authorized, err == nil
}
func (c *GitHubAuthCache) Set(key string, authorized bool) {
b, _ := json.Marshal(authorized)
c.cache.Set(key, b)
}
func makeGitHubAuthCacheKey(githubToken, repoName string) string {
key := sha256.Sum256([]byte(githubToken + ":" + repoName))
return hex.EncodeToString(key[:])
}