8x8 / jaas_demo

Embed video meetings in your website, iOS app or Android app.
https://jaas.8x8.vc
MIT License
34 stars 45 forks source link

Update Sample .NET #6

Closed AugustoZanoni closed 3 years ago

AugustoZanoni commented 3 years ago

There is a missing tag that changed and got me 3 days of debuggin to find out it was an error on the sample code.

Side note: I use .NET Framework 4.6.1 and this sample only works in .NET Core 3 or above. So I had to change my sample to work propertly. I will share my code in this description and sugges to the team to publish this sample as optional for who is implementing it on .NET Framework:

private static RSA RsaKeyAsPerContent()
        {
            RSA rSA = RSA.Create();

            string privateKeyFromConfig = ConfigurationManager.AppSettings["JAAS-PrivateKey"];
            privateKeyFromConfig = privateKeyFromConfig.Replace(BEGIN_RSA_PRIVATE_KEY, "");
            privateKeyFromConfig = privateKeyFromConfig.Replace(END_RSA_PRIVATE_KEY, "");
            rSA.ImportParameters(ImportPrivateKey(privateKeyFromConfig));
            return rSA;
        }

        public static RSAParameters ImportPrivateKey(string pem)
        {

            Merged::Org.BouncyCastle.OpenSsl.PemReader pr = new Merged::Org.BouncyCastle.OpenSsl.PemReader(new StringReader(pem));
            Merged::Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters privKey = (Merged::Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters)pr.ReadObject();
            RSAParameters rp = Merged::Org.BouncyCastle.Security.DotNetUtilities.ToRSAParameters(privKey); //new RSAParameters();
            //rp.Modulus = privKey.Modulus.ToByteArrayUnsigned();
            //rp.Exponent = privKey.PublicExponent.ToByteArrayUnsigned();
            //rp.P = privKey.P.ToByteArrayUnsigned();
            //rp.Q = privKey.Q.ToByteArrayUnsigned();
            //rp.D = ConvertRSAParametersField(privKey.Exponent, rp.Modulus.Length);
            //rp.DP = ConvertRSAParametersField(privKey.DP, rp.P.Length);
            //rp.DQ = ConvertRSAParametersField(privKey.DQ, rp.Q.Length);
            //rp.InverseQ = ConvertRSAParametersField(privKey.QInv, rp.Q.Length);

            return rp;
        }

        private static byte[] ConvertRSAParametersField(Merged::Org.BouncyCastle.Math.BigInteger n, int size)
        {
            byte[] bs = n.ToByteArrayUnsigned();
            if (bs.Length == size)
                return bs;
            if (bs.Length > size)
                throw new ArgumentException("Specified size too small", "size");
            byte[] padded = new byte[size];
            Array.Copy(bs, 0, padded, size - bs.Length, bs.Length);
            return padded;
        }
iwaffles commented 3 years ago

Hey @AugustoZanoni, thanks so much for the contribution! 🎉

Someone on the team will review this shortly :)

horymury commented 3 years ago

@AugustoZanoni , thank you for the contribution. Regarding the change in this PR, nbf is the correct payload prop name, so there must be something else wrong in your case. When joining a meeting with JWT, you can see any errors related to JWT in the developer console.

Please note that this sample supports only PCKS#1 keys for now, I did some addings to the sample app in order to support also PCKS#8 unecrypted, will create PR soon. If you used the Key pair generator from the JaaS console, it will generate PCKS#8 key. This needs to be changed for PCKS#8: Replace: rsa.ImportRSAPrivateKey(privateKeyDecoded, out _); with: rsa.ImportPkcs8PrivateKey(privateKeyDecoded, out _); and also replace in the consts : "-----BEGIN RSA PRIVATE KEY-----" with "-----BEGIN PRIVATE KEY-----" "-----END RSA PRIVATE KEY-----" with "-----END PRIVATE KEY-----"

Using the sample app in it's listed state and my own generated Key Pair PCKS#1 I was able to generate a valid JWT with which I successfully joined a meeting by only putting the correct values for the AppId, kid , email, full name in the generator. Please let me know if you still have troubles with it.

Regarding your .NET sample, I'm adding also generating a JWT signed with the rsa key and if all good will create a PR with that as well.

Thank you again.