static async System.Threading.Tasks.Task Main(string[] args)
        {
            Chilkat.Global glob = new Chilkat.Global();
            glob.UnlockBundle("Anything for 30-day trial");

            Chilkat.Rsa rsaKey = new Chilkat.Rsa();

            rsaKey.GenerateKey(1024);
            var rsaPrivKey = rsaKey.ExportPrivateKeyObj();

            var rsaPublicKey         = rsaKey.ExportPublicKeyObj();
            var rsaPublicKeyAsString = rsaKey.ExportPublicKey();

            Chilkat.JsonObject jwtHeader = new Chilkat.JsonObject();
            jwtHeader.AppendString("alg", "RS256");
            jwtHeader.AppendString("typ", "JWT");

            Chilkat.JsonObject claims = new Chilkat.JsonObject();
            claims.AppendString("Email", "*****@*****.**");
            claims.AppendString("Test", "test1");

            Chilkat.Jwt jwt = new Chilkat.Jwt();

            string token = jwt.CreateJwtPk(jwtHeader.Emit(), claims.Emit(), rsaPrivKey);

            Console.WriteLine("This is the token generated");
            Console.WriteLine(token);

            // Verifying Token using Public Key
            Console.WriteLine(jwt.VerifyJwtPk(token, rsaPublicKey));
            Console.WriteLine(jwt.GetPayload(token));

            // Importing public key
            Chilkat.Rsa rsaExportedPublicKey = new Chilkat.Rsa();
            Console.WriteLine(rsaExportedPublicKey.ImportPublicKey(rsaPublicKeyAsString));
            Console.WriteLine(jwt.VerifyJwtPk(token, rsaExportedPublicKey.ExportPublicKeyObj()));

            // Store the value in Consul KV
            using (var client = new ConsulClient())
            {
                var putPair = new KVPair("secretkey")
                {
                    Value = Encoding.UTF8.GetBytes(rsaPublicKeyAsString)
                };

                var putAttempt = await client.KV.Put(putPair);

                if (putAttempt.Response)
                {
                    var getPair = await client.KV.Get("secretkey");

                    if (getPair.Response != null)
                    {
                        Console.WriteLine("Getting Back the Stored String");
                        Console.WriteLine(Encoding.UTF8.GetString(getPair.Response.Value, 0, getPair.Response.Value.Length));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        // This method is resposible of generating JWT token
        public async static Task <string> GenerateTokenAsync(string Email)
        {
            Chilkat.Global glob = new Chilkat.Global();
            glob.UnlockBundle("Anything for 30-day trial");

            string token = "";

            //Creating JWT header using chilkat
            Chilkat.JsonObject jwtHeader = new Chilkat.JsonObject();
            jwtHeader.AppendString("alg", "RS256");
            jwtHeader.AppendString("typ", "JWT");

            //Adding Token claims
            Chilkat.JsonObject claims = new Chilkat.JsonObject();
            claims.AppendString("Email", Email);

            //Adding Token Expiration time
            Chilkat.Jwt jwt         = new Chilkat.Jwt();
            int         curDateTime = jwt.GenNumericDate(0);

            claims.AddIntAt(-1, "exp", curDateTime + 720);

            //Ading consul for putting and getting public and private key
            using (var client = new ConsulClient())
            {
                client.Config.Address = new Uri("http://172.23.238.173:8500");

                var getPair = client.KV.Get("myPrivateKey");


                if (getPair.Result.Response != null)
                {
                    string      secret = System.Text.Encoding.UTF8.GetString(getPair.Result.Response.Value);
                    Chilkat.Rsa rsaExportedPrivateKey = new Chilkat.Rsa();
                    rsaExportedPrivateKey.ImportPrivateKey(secret);
                    var rsaPrivKey = rsaExportedPrivateKey.ExportPrivateKeyObj();

                    token = jwt.CreateJwtPk(jwtHeader.Emit(), claims.Emit(), rsaPrivKey);
                }
                else
                {
                    await TokenManager.KeyGeneratorAsync(client);

                    var getPair1 = client.KV.Get("myPrivateKey");

                    string secret = System.Text.Encoding.UTF8.GetString(getPair1.Result.Response.Value);

                    Chilkat.Rsa rsaExportedPrivateKey = new Chilkat.Rsa();
                    rsaExportedPrivateKey.ImportPrivateKey(secret);

                    token = jwt.CreateJwtPk(jwtHeader.Emit(), claims.Emit(), rsaExportedPrivateKey.ExportPrivateKeyObj());
                }
            }

            //jwt.AutoCompact = true;
            //return JsonConvert.SerializeObject(token);
            return(token);
        }
Ejemplo n.º 3
0
        public static string GenjeroTokenin(string user)
        {
            Chilkat.PrivateKey privKey = new Chilkat.PrivateKey();
            bool success = privKey.LoadEncryptedPemFile("C:\\Users\\Lenovo\\Desktop\\Detyra1_DS-Gr-6-master\\ds\\bin\\Debug\\netcoreapp3.0\\keys\\" + user + ".pem", "passwd");

            if (!success)
            {
                return("Nuk ekziston Celsi ose nuk mund te hapet");
            }
            Chilkat.Jwt        jwt  = new Chilkat.Jwt();
            Chilkat.JsonObject jose = new Chilkat.JsonObject();
            //  Use RS256.  Pass the string "RS384" or "RS512" to use RSA with SHA-384 or SHA-512.
            success = jose.AppendString("alg", "RS256");
            success = jose.AppendString("typ", "JWT");
            //  Now build the JWT claims (also known as the payload)
            Chilkat.JsonObject claims = new Chilkat.JsonObject();

            success = claims.AppendString("sub", user);
            DateTime aDay           = DateTime.Now;
            TimeSpan a20min         = new System.TimeSpan(0, 0, 20, 0);
            DateTime after20minutes = aDay.Add(a20min);

            string k = after20minutes.ToString();

            success = claims.AppendString("Valid-till", k);
            //success = claims.AppendString("aud", "http://example.com");
            //  Set the timestamp of when the JWT was created to now.
            int curDateTime = jwt.GenNumericDate(0);

            success = claims.AddIntAt(-1, "iat", curDateTime);

            //  Set the "not process before" timestamp to now.
            success = claims.AddIntAt(-1, "nbf", curDateTime);

            //  Set the timestamp defining an expiration time (end time) for the token
            //  to be now + 20 minutes (1200 seconds)
            success = claims.AddIntAt(-1, "exp", curDateTime + 1200);
            // adding the tme when its not valid


            //  Produce the smallest possible JWT:
            jwt.AutoCompact = true;


            //  Create the JWT token.  This is where the RSA signature is created.
            string token = jwt.CreateJwtPk(jose.Emit(), claims.Emit(), privKey);

            //Console.WriteLine(token);
            return(token);
        }