Token Signing
Function | Description | Meta |
---|---|---|
io.jwt.encode_sign |
Encodes and optionally signs a JSON Web Token. Inputs are taken as objects, not encoded strings (see Arguments: Returns:headers (object[string: any])JWS Protected Header payload (object[string: any])JWS Payload key (object[string: any])JSON Web Key (RFC7517) output (string)signed JWT | SDK-dependent |
io.jwt.encode_sign_raw |
Encodes and optionally signs a JSON Web Token. Arguments: Returns:headers (string)JWS Protected Header payload (string)JWS Payload key (string)JSON Web Key (RFC7517) output (string)signed JWT | SDK-dependent |
OPA provides two builtins that implement JSON Web Signature RFC7515 functionality.
io.jwt.encode_sign_raw()
takes three JSON Objects (strings) as parameters and returns their JWS Compact Serialization.
This builtin should be used by those that want maximum control over the signing and serialization procedure. It is
important to remember that StringOrURI values are compared as case-sensitive strings with no transformations or
canonicalizations applied. Therefore, line breaks and whitespaces are significant.
io.jwt.encode_sign()
takes three Rego Objects as parameters and returns their JWS Compact Serialization. This builtin
should be used by those that want to use rego objects for signing during policy evaluation.
Note that with io.jwt.encode_sign
the Rego objects are serialized to JSON with standard formatting applied
whereas the io.jwt.encode_sign_raw
built-in will not affect whitespace of the strings passed in.
This will mean that the final encoded token may have different string values, but the decoded and parsed
JSON will match.
The following algorithms are supported:
ES256
: ECDSA using P-256 and SHA-256ES384
: ECDSA using P-384 and SHA-384ES512
: ECDSA using P-521 and SHA-512HS256
: HMAC using SHA-256HS384
: HMAC using SHA-384HS512
: HMAC using SHA-512PS256
: RSASSA-PSS using SHA256 and MGF1-SHA256PS384
: RSASSA-PSS using SHA384 and MGF1-SHA384PS512
: RSASSA-PSS using SHA512 and MGF1-SHA512RS256
: RSASSA-PKCS-v1.5 using SHA-256RS384
: RSASSA-PKCS-v1.5 using SHA-384RS512
: RSASSA-PKCS-v1.5 using SHA-512
Note that the key's provided should be base64 URL encoded (without padding) as per the specification (RFC7517). This differs from the plain text secrets provided with the algorithm specific verify built-ins described below.
Token Signing Examples
Symmetric Key (HMAC with SHA-256)
"{}"
"{}"
package jwt
result := io.jwt.encode_sign({
"typ": "JWT",
"alg": "HS256"
}, {
"iss": "joe",
"exp": 1300819380,
"aud": ["bob", "saul"],
"http://example.com/is_root": true,
"privateParams": {
"private_one": "one",
"private_two": "two"
}
}, {
"kty": "oct",
"k": "AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow"
})
Symmetric Key with empty JSON payload
"{}"
"{}"
package jwt
result := io.jwt.encode_sign({
"typ": "JWT",
"alg": "HS256"},
{}, {
"kty": "oct",
"k": "AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow"
})
RSA Key (RSA Signature with SHA-256)
"{}"
"{}"
package jwt
result := io.jwt.encode_sign({
"alg": "RS256"
}, {
"iss": "joe",
"exp": 1300819380,
"aud": ["bob", "saul"],
"http://example.com/is_root": true,
"privateParams": {
"private_one": "one",
"private_two": "two"
}
},
{
"kty": "RSA",
"n": "ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddxHmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMsD1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSHSXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdVMTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ",
"e": "AQAB",
"d": "Eq5xpGnNCivDflJsRQBXHx1hdR1k6Ulwe2JZD50LpXyWPEAeP88vLNO97IjlA7_GQ5sLKMgvfTeXZx9SE-7YwVol2NXOoAJe46sui395IW_GO-pWJ1O0BkTGoVEn2bKVRUCgu-GjBVaYLU6f3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K0JKSHuLFkuQ3U"
})
Raw Token Signing
If you need to generate the signature for a serialized token you an use the
io.jwt.encode_sign_raw
built-in function which accepts JSON serialized string
parameters.
"{}"
"{}"
package jwt
result := io.jwt.encode_sign_raw(
`{"typ":"JWT","alg":"HS256"}`,
`{"iss":"joe","exp":1300819380,"http://example.com/is_root":true}`,
`{"kty":"oct","k":"AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow"}`
)