File: method.go

package info (click to toggle)
golang-github-smallstep-certificates 0.20.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,144 kB
  • sloc: sh: 278; makefile: 170
file content (76 lines) | stat: -rw-r--r-- 2,128 bytes parent folder | download
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package provisioner

import (
	"context"
)

// Method indicates the action to action that we will perform, it's used as part
// of the context in the call to authorize. It defaults to Sing.
type Method int

// The key to save the Method in the context.
type methodKey struct{}

const (
	// SignMethod is the method used to sign X.509 certificates.
	SignMethod Method = iota
	// RevokeMethod is the method used to revoke X.509 certificates.
	RevokeMethod
	// RenewMethod is the method used to renew X.509 certificates.
	RenewMethod
	// SSHSignMethod is the method used to sign SSH certificates.
	SSHSignMethod
	// SSHRenewMethod is the method used to renew SSH certificates.
	SSHRenewMethod
	// SSHRevokeMethod is the method used to revoke SSH certificates.
	SSHRevokeMethod
	// SSHRekeyMethod is the method used to rekey SSH certificates.
	SSHRekeyMethod
)

// String returns a string representation of the context method.
func (m Method) String() string {
	switch m {
	case SignMethod:
		return "sign-method"
	case RevokeMethod:
		return "revoke-method"
	case RenewMethod:
		return "renew-method"
	case SSHSignMethod:
		return "ssh-sign-method"
	case SSHRenewMethod:
		return "ssh-renew-method"
	case SSHRevokeMethod:
		return "ssh-revoke-method"
	case SSHRekeyMethod:
		return "ssh-rekey-method"
	default:
		return "unknown"
	}
}

// NewContextWithMethod creates a new context from ctx and attaches method to
// it.
func NewContextWithMethod(ctx context.Context, method Method) context.Context {
	return context.WithValue(ctx, methodKey{}, method)
}

// MethodFromContext returns the Method saved in ctx.
func MethodFromContext(ctx context.Context) Method {
	m, _ := ctx.Value(methodKey{}).(Method)
	return m
}

type tokenKey struct{}

// NewContextWithToken creates a new context with the given token.
func NewContextWithToken(ctx context.Context, token string) context.Context {
	return context.WithValue(ctx, tokenKey{}, token)
}

// TokenFromContext returns the token stored in the given context.
func TokenFromContext(ctx context.Context) (string, bool) {
	token, ok := ctx.Value(tokenKey{}).(string)
	return token, ok
}