const zlib = require('zlib');

const ISSUER = 'https://sp.example.com';
const REQUEST_ID = 'SSO_TOOLS_REQUEST_' + Math.random().toString(36).substr(2, 9);

const loginRequest = `<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="${REQUEST_ID}" Version="2.0" ProviderName="SP test" IssueInstant="2024-05-25T23:15:00Z" Destination="https://idp.example.com" ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST">
  <saml:Issuer>${ISSUER}</saml:Issuer>
  <samlp:NameIDPolicy Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress" AllowCreate="true"/>
  <samlp:RequestedAuthnContext Comparison="exact">
    <saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</saml:AuthnContextClassRef>
  </samlp:RequestedAuthnContext>
</samlp:AuthnRequest>`;

const deflatedRequest = zlib.deflateRawSync(Buffer.from(loginRequest)).toString('base64');
const encodedRequest = encodeURIComponent(deflatedRequest);

console.log(encodedRequest);
