Keep context keys private

Instead, offer type safe accessors, as documented here:

https://pkg.go.dev/context#Context
This commit is contained in:
Conrad Hoffmann 2022-03-16 14:33:47 +01:00
parent 07c19a6f6c
commit 78bd2a9b84
3 changed files with 18 additions and 11 deletions

View file

@ -37,14 +37,14 @@ func NewFilesystem(path string) (carddav.Backend, error) {
}
func (b *filesystemBackend) pathForContext(ctx context.Context) (string, error) {
raw := ctx.Value(auth.AuthCtxKey)
if raw == nil {
return "", fmt.Errorf("unauthenticated requests are not supported")
}
authCtx, ok := raw.(*auth.AuthContext)
authCtx, ok := auth.FromContext(ctx)
if !ok {
panic("Invalid data in auth context!")
}
if authCtx == nil {
return "", fmt.Errorf("unauthenticated requests are not supported")
}
userDir := base64.RawStdEncoding.EncodeToString([]byte(authCtx.UserName))
path := filepath.Join(b.path, userDir)