sourcegraph/internal/codeintel/uploads/transport/http/auth/github_cache.go
Erik Seliger 169db11ce6
rcache: Explicitly pass redis pool to use (#63644)
Recently, this was refactored to also allow using the redispool.Store.
However, that makes it very implicit to know where something is being
written, so instead we pass down the pool instance at instantiation.

This also gives a slightly better overview of where redispool is
actually required.

Test plan: CI passes.
2024-07-10 01:23:19 +02:00

39 lines
863 B
Go

package auth
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"github.com/sourcegraph/sourcegraph/internal/rcache"
"github.com/sourcegraph/sourcegraph/internal/redispool"
)
type GitHubAuthCache struct {
cache *rcache.Cache
}
var githubAuthCache = &GitHubAuthCache{
cache: rcache.NewWithTTL(redispool.Cache, "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[:])
}