emissary/internal/auth/agent/jwt.go

38 lines
783 B
Go
Raw Normal View History

package agent
import (
"time"
"forge.cadoles.com/Cadoles/emissary/internal/jwk"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwt"
"github.com/pkg/errors"
)
const keyThumbprint = "thumbprint"
func GenerateToken(key jwk.Key, thumbprint string) (string, error) {
token := jwt.New()
if err := token.Set(keyThumbprint, thumbprint); err != nil {
return "", errors.WithStack(err)
}
2023-03-29 20:49:44 +02:00
now := time.Now().UTC()
if err := token.Set(jwt.NotBeforeKey, now); err != nil {
return "", errors.WithStack(err)
}
if err := token.Set(jwt.IssuedAtKey, now); err != nil {
return "", errors.WithStack(err)
}
rawToken, err := jwt.Sign(token, jwt.WithKey(jwa.RS256, key))
if err != nil {
return "", errors.WithStack(err)
}
return string(rawToken), nil
}