File: context.go

package info (click to toggle)
golang-github-smallstep-crypto 0.57.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,284 kB
  • sloc: sh: 53; makefile: 36
file content (39 lines) | stat: -rw-r--r-- 946 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package tpm

import "context"

type contextKey struct{}

// NewContext adds TPM `t` to the context.
func NewContext(ctx context.Context, t *TPM) context.Context {
	return context.WithValue(ctx, contextKey{}, t)
}

// FromContext returns a TPM from the context.
//
// It panics when there's no TPM in the context.
func FromContext(ctx context.Context) *TPM {
	return ctx.Value(contextKey{}).(*TPM)
}

type internalCallContextKey struct{}

func internalCall(ctx context.Context) context.Context {
	return context.WithValue(ctx, internalCallContextKey{}, true)
}

func isInternalCall(ctx context.Context) bool {
	v, ok := ctx.Value(internalCallContextKey{}).(bool)
	return ok && v
}

type goTPMCallContextKey struct{}

func goTPMCall(ctx context.Context) context.Context {
	return context.WithValue(ctx, goTPMCallContextKey{}, true)
}

func isGoTPMCall(ctx context.Context) bool {
	v, ok := ctx.Value(goTPMCallContextKey{}).(bool)
	return ok && v
}