38 lines
777 B
Go
38 lines
777 B
Go
|
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)
|
||
|
}
|
||
|
|
||
|
now := time.Now()
|
||
|
|
||
|
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
|
||
|
}
|