import jwt from "jsonwebtoken";
import { permissionsPrivateKey } from "../../constants";
import { PermissionIssuers } from "../../types/PermissionTypes";

/**
 * Sign Permissions
 *
 * This will sign the array of permissions with the private key for permissions, and then return
 * a JWT which will be stored in the databse.
 *
 * @param permissions - All the permissions to be signed
 * @returns {string} - The signed permissions object
 */
export function signPermissions(data: {
  issuer: PermissionIssuers;
  subject: string;
  permissions: string[];
}) {
  return jwt.sign({ permissions: data.permissions }, permissionsPrivateKey, {
    algorithm: "RS256",
    issuer: data.issuer,
    subject: data.subject,
  });
}
