Generating API tokens
- In order to access the endpoints of SecretVault/SecretDataAnalytics, you will require a set of Bearer tokens for authorization - one for each node in your setup.
- Those can be generated programmatically using the information from your
Credentials
andCluster Config
. - Specifically you'll be able to generate them with just the following information using the code below:
- Your private key
- Your DID
- The target node's DID
- We're encouraging generation during runtime with short TTL for these tokens, but it's up to you if you want to manually rotate them on longer intervals.
- Python
- JavaScript (from scratch)
- JavaScript (with wrapper)
# generate.py
# pip install "PyJWT[crypto]" ecdsa
import jwt
import time
from ecdsa import SigningKey, SECP256k1
def create_jwt(secret_key: str = None,
org_did: str = None,
node_ids: list = None,
ttl: int = 3600) -> list:
"""
Create JWTs signed with ES256K for multiple node_ids
"""
# Convert the secret key from hex to bytes
private_key = bytes.fromhex(secret_key)
signer = SigningKey.from_string(private_key, curve=SECP256k1)
tokens = []
for node_id in node_ids:
# Create payload for each node_id
payload = {
"iss": org_did,
"aud": node_id,
"exp": int(time.time()) + ttl
}
# Create and sign the JWT
token = jwt.encode(
payload,
signer.to_pem(),
algorithm="ES256K"
)
tokens.append(token)
print(f"Generated JWT for {node_id}: {token}")
return tokens
# # Replace secret_key with secret Key
# # Replace org_did with DID for organization
# # Replace node_ids with the Node DIDs
if __name__ == "__main__":
secret_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
org_did = "did:nil:testnet:nillionXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
# these are the demo cluster node dids, change them if your config is different
node_ids = [
"did:nil:testnet:nillion1fnhettvcrsfu8zkd5zms4d820l0ct226c3zy8u",
"did:nil:testnet:nillion14x47xx85de0rg9dqunsdxg8jh82nvkax3jrl5g",
"did:nil:testnet:nillion167pglv9k7m4gj05rwj520a46tulkff332vlpjp"
]
create_jwt(secret_key, org_did, node_ids)
nildb/secretvault_nextjs/generate.js
loading...