MCPcopy Index your code
hub / github.com/coder/coder / Sign

Function Sign

coderd/jwtutils/jws.go:58–92  ·  view source on GitHub ↗

Sign signs a token and returns it as a string.

(ctx context.Context, s SigningKeyProvider, claims Claims)

Source from the content-addressed store, hash-verified

56
57// Sign signs a token and returns it as a string.
58func Sign(ctx context.Context, s SigningKeyProvider, claims Claims) (string, error) {
59 id, key, err := s.SigningKey(ctx)
60 if err != nil {
61 return "", xerrors.Errorf("get signing key: %w", err)
62 }
63
64 signer, err := jose.NewSigner(jose.SigningKey{
65 Algorithm: SigningAlgo,
66 Key: key,
67 }, &jose.SignerOptions{
68 ExtraHeaders: map[jose.HeaderKey]interface{}{
69 keyIDHeaderKey: id,
70 },
71 })
72 if err != nil {
73 return "", xerrors.Errorf("new signer: %w", err)
74 }
75
76 payload, err := json.Marshal(claims)
77 if err != nil {
78 return "", xerrors.Errorf("marshal claims: %w", err)
79 }
80
81 signed, err := signer.Sign(payload)
82 if err != nil {
83 return "", xerrors.Errorf("sign payload: %w", err)
84 }
85
86 compact, err := signed.CompactSerialize()
87 if err != nil {
88 return "", xerrors.Errorf("compact serialize: %w", err)
89 }
90
91 return compact, nil
92}
93
94// VerifyOptions are options for verifying a JWT.
95type VerifyOptions struct {

Callers 7

postConvertLoginTypeMethod · 0.92
TestClaimsFunction · 0.92
TestJWSFunction · 0.92
Test_FromRequestFunction · 0.92
IssueMethod · 0.92
Test_ResolveRequestFunction · 0.92
GenerateResumeTokenMethod · 0.92

Calls 3

SigningKeyMethod · 0.65
ErrorfMethod · 0.45
MarshalMethod · 0.45

Tested by 4

TestClaimsFunction · 0.74
TestJWSFunction · 0.74
Test_FromRequestFunction · 0.74
Test_ResolveRequestFunction · 0.74