Utility Packages¶
On this page
JSON Web Tokens (utils.jwt
)¶
The utils.jwt
package provides methods for working with
JSON Web Tokens.
Method | Description |
---|---|
utils.jwt.encode() |
Generates an encoded JSON Web Token string for a given
payload , signingMethod , and secret . |
utils.jwt.decode() |
Decodes the payload of a JSON Web Token string. |
-
utils.jwt.
encode
()¶ Generates an encoded JSON Web Token string for the
payload
based on the specifiedsigningMethod
andsecret
.Parameter Type Description signingMethod
String The cryptographic algorithm to use when encoding the JWT. Stitch supports the following JWT signing methods:
Signing Method Description "HS256"
HMAC using SHA-256 "HS384"
HMAC using SHA-384 "HS512"
HMAC using SHA-512 "RS256"
RSASSA-PKCS1-v1_5 using SHA-256 "RS384"
RSASSA-PKCS1-v1_5 using SHA-384 "RS512"
RSASSA-PKCS1-v1_5 using SHA-512 "ES256"
ECDSA using P-256 and SHA-256 "ES384"
ECDSA using P-384 and SHA-384 "ES512"
ECDSA using P-512 and SHA-512 "PS256"
RSASSA-PSS using SHA-256 and MGF1 with SHA-256 "PS384"
RSASSA-PSS using SHA-384 and MGF1 with SHA-384 "PS512"
RSASSA-PSS using SHA-512 and MGF1 with SHA-512 payload
Object A JSON object that specifies the token’s claims and any additional related data. secret
String A secret string that Stitch uses to sign the token. The value of the string depends on the signing method that you use:
Signing Methods Description "HS256"
"HS384"
"HS512"
A random string. "RS256"
"RS384"
"RS512"
An RSA-SHA private key in PKCS#8 format. "PS256"
"PS384"
"PS512"
An RSA-PSS private key in PKCS#8 format. "ES256"
"ES384"
"ES512"
An ECDSA private key in PKCS#8 format. customHeaderFields
Object A JSON object that specifies additional fields to include in the JWT’s JOSE header. Returns: A JSON Web Token string encoded for the provided payload
.Example
Consider the following JWT claims object:
We can encode the claims object as a JWT string by calling
utils.jwt.encode()
. The following function encodes the JWT using theHS512
signing method and the secret"SuperSecret"
:The function returns the following JWT string:
-
utils.jwt.
decode
()¶ Decodes the
payload
of the provided JSON Web Token string. The value ofkey
must correspond to the secret value that was used to encode the JWT string.Parameter Type Description jwtString
String A JSON Web Token string that encodes a set of claims signed with a secret value. key
String A string that Stitch uses to verify the token signature. The value of the string depends on the signing method that you use:
Signing Methods Description "HS256"
"HS384"
"HS512"
The random string that was used to sign the token. "RS256"
"RS384"
"RS512"
The RSA-SHA public key that corresponds to the private key that was used to sign the token. "PS256"
"PS384"
"PS512"
The RSA-PSS public key that corresponds to the private key that was used to sign the token. "ES256"
"ES384"
"ES512"
The ECDSA public key that corresponds to the private key that was used to sign the token. returnHeader
Boolean If true
, return the JWT’s JOSE header in addition to the decoded payload.Returns: If returnHeader
isfalse
, returns the decoded EJSON payload.If
returnHeader
istrue
, returns an object that contains the JOSE header in theheader
field and the decoded EJSON payload in thepayload
field.Example
Consider the following signed JWT string:
The JWT was signed using the
HS512
signing method with the secret value"SuperSecret"
. We can decode the JWT’s claims objectutils.jwt.decode()
. The following function decodes the JWT string:The function returns the following EJSON representation of the JWT payload:
Cryptography (utils.crypto
)¶
The utils.crypto
package provides methods for working with
cryptographic algorithms.
Method | Description |
---|---|
utils.crypto.encrypt() |
Generates an encrypted text string from a given text string using a specific encryption method and key. |
utils.crypto.decrypt() |
Decrypts a provided text string using a specific encryption method and key. |
utils.crypto.sign() |
Generates a cryptographically unique signature for a given message using a private key. |
utils.crypto.verify() |
Verifies that a signature is valid for a given message and public key. |
utils.crypto.hmac() |
Generates an HMAC signature from a given input and secret. |
utils.crypto.hash() |
Generates a hash value for a given input and hash function. |
-
utils.crypto.
encrypt
()¶ Generates an encrypted text string from the provided text using the specified encryption method and key.
Parameter Type Description encryptionType
String The type of encryption with which to encrypt the message. The following encryption types are supported:
- AES Encryption (
"aes"
)
message
String The text string that you want to encrypt. key
String A cryptographic key used to encrypt the text. The key you should use depends on the encryption method:
Encryption Type Encryption Key AES A 16-byte, 24-byte, or 32-byte random string Returns: A BSON Binary object that contains the text string encrypted with the specified encryption type and key. Example
Assume that we have defined a Value named
aesEncryptionKey
that contains the following 32-byte AES encryption key:We can encrypt a message with this key using the following Stitch function:
If we use the function to encrypt the message
"MongoDB is great!"
, it returns the following encrypted base64 string:- AES Encryption (
-
utils.crypto.
decrypt
()¶ Decrypts the provided text string using the specified encryption type and key. If both the encryption type and key are the same as those used to encrypt, this returns the original, unencrypted text.
Parameter Type Description encryptionType
String The type of encryption that was used to encrypt the provided text. The following encryption types are supported:
- AES Encryption (
"aes"
)
encryptedMessage
BSON.Binary A BSON Binary that encodes the encrypted text string that you want to decrypt. key
String A cryptographic key used to decrypt the text. The key you should use depends on the encryption type:
Encryption Type Encryption Key AES A 16-byte, 24-byte, or 32-byte random string Returns: A BSON Binary object that contains the decrypted message. If the provided encrypted message was encrypted with the specified method and key, then the decrypted message is identical to the original message.
Example
Assume that we have defined a Value named
aesEncryptionKey
that contains the following 32-byte AES encryption key:We can decrypt a message that was encrypted with this key using the following Stitch function:
If we use the function to decrypt the encrypted message, it returns the original, unencrypted string:
- AES Encryption (
-
utils.crypto.
sign
()¶ Generates a cryptographically unique signature for a message using a private key. The signature can be verified with the corresponding public key to ensure that the signer has access to the private key and that the message content has not been altered since it was signed.
Parameter Type Description encryptionType
String The type of encryption that was used to generate the private/public key pair. The following encryption types are supported:
- RSA Encryption (
"rsa"
)
message
String The text string that you want to sign. privateKey
String A private key generated with the specified encryption type.
Key Format
Not all RSA keys use the same format. Stitch can only sign messages with a private key that conforms to the standard PKCS#1 format. Private keys in this format have the header
-----BEGIN RSA PRIVATE KEY-----
.You can use the following shell script to generate a valid RSA private/public key pair and save each key to its own text file:
signatureScheme
String Optional. Default:
"pss"
The padding scheme that the signature should use. Stitch supports signing messages with the following schemes:
- Probabilistic signature scheme (
"pss"
) - PKCS1v1.5 (
"pkcs1v15"
)
Returns: A BSON.Binary cryptographic signature for the message signed using the specified private key. Example
Assume that we have defined a Value named
rsaPrivateKey
that contains the following RSA private key:We can sign a message with this key using the following Stitch function:
If we use the function to sign the message
"MongoDB is great!"
, it returns a BSON.Binary signature that evaluates to the following base64 string:- RSA Encryption (
-
utils.crypto.
verify
()¶ Checks that the provided signature is valid for the specified message and public key.
If the signature is valid, it guarantees that the signer has access to the corresponding private key and that the message content has not been altered since it was signed.
Parameter Type Description encryptionType
String The type of encryption that was used to generate the private/public key pair. The following encryption types are supported:
- RSA Encryption (
"rsa"
)
message
String The text string for which you want to verify the signature. If the signature is valid, this is the exact message that was signed. publicKey
String The public key for which you want to verify the signature. If the signature is valid, this is the corresponding public key of the private key that was used to sign the message.
Key Format
Not all RSA keys use the same format. Stitch can only verify signatures with RSA keys that conform to the standard PKCS#1 format. Public keys in this format have the header
-----BEGIN RSA PUBLIC KEY-----
.You can use the following shell script to generate a valid RSA private/public key pair and save each key to its own text file:
signature
BSON.Binary The signature that you want to verify. signatureScheme
String Optional. Default:
"pss"
The padding scheme that the signature uses. Stitch supports verifying signatures that use the following schemes:
- Probabilistic signature scheme (
"pss"
) - PKCS1v1.5 (
"pkcs1v15"
)
Returns: A boolean that, if true
, indicates whether or not the signature is valid for the provided message and public key.Example
We received a message with a signature in BSON.Binary format and want to verify that the message was signed with the private key that corresponds to the sender’s RSA public key:
We can use the following Stitch function to verify the RSA signature:
- RSA Encryption (
-
utils.crypto.
hmac
()¶ Generates an HMAC signature from the provided input and secret. This is useful when communicating with third-party HTTP services that require signed requests.
Parameter Type Description input
string The input for which you would like to generate a signature. secret
string The secret key to use when generating the signature. hash_function
string The name of the hashing function to use when generating the signature. The following functions are supported: "sha1"
,"sha256"
,"sha512"
.output_format
string The format of the generated signature. Can be either "hex"
for a hex string, or"base64"
for a Base64 string.Returns: The signature of the input, in the format specified by output_format
.
-
utils.crypto.
hash
()¶ Generates a hash value for the provided input using the specified hash function.
Parameter Type Description hash_function
string The name of the hashing function. The following functions are supported: "sha1"
,"sha256"
,"md5"
.input
string or BSON.Binary Required. The input for which you would like to generate a hash value. Returns: A hash value for the provided input generated by the specified hashing function.