2023-03-07 23:10:42 +01:00
|
|
|
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()
|
2023-03-07 23:10:42 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|