Visual Code – Make it possible to Run Program

To configure so that you can run your program without any commands, then you need simply a launch.json file.

Start by creating a folder inside your workspace named .vscode. You can create a workspace by selecting Save Workspace As… in Visual Code under File in the top left corner if you don’t have one already.

Then let’s create a file called launch.json inside .vscode. These configurations will pop up under Run and Debug. This is how my launch.json looks like:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "command": "npm run dev",
            "name": "Front end",
            "request": "launch",
            "type": "node-terminal",
            "cwd": "${workspaceFolder}/client",
        },
        {
            "name": "Backend",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}/backend-server",
            "cwd": "${workspaceFolder}/backend-server",
            "env": {},
        },
    ]
}

Inside my client app package.json:

  "scripts": {
    "dev": "vite",
    "build": "tsc && vite build",
    "preview": "vite preview"
  },

In this case, I got one Frontend application I can run, and one for backend. The front end one is of the type node-terminal, meaning, it can launch node type applications like npm. The important property here is that we have command, which will launch the command npm run dev. This script can be seen from the client package.json above this text. Then the cwd, the path, is specified to the client folder inside the workspace folder where my app is.

The other possible run application is named Backend, and is of the type of Go, where it will launch the program because of the request launch value. Program defined where the program is located.

Create a pem private and public key

This is how to create a private and public pem key. And also an example no how to take use of the key in .NET.

  1. Open up git bash, cd into a folder of your preference.
  2. Enter command to create a private key:
    winpty openssl genrsa -out private.pem 2048
    Note that if you want to create a private key with a keyphrase enter in the parameter des3
  3. Create a public key based from the private key:
    winpty openssl rsa -in private.pem -outform PEM -pubout -out public.pem
  4. Now if we check in our folder we have two files:
    private.pem and public.pem

And if you would want to Create a Token or Validate a token you have methods like these in .NET (C#):

        public string CreateToken(List<Claim> claimList)
        {
            string privateKeyBytes = File.ReadAllText("./Secrets/private.pem");

            using RSA rsa = RSA.Create();
            rsa.ImportFromPem(privateKeyBytes);
            var signingCredentials = new SigningCredentials(new RsaSecurityKey(rsa), SecurityAlgorithms.RsaSha256)
            {
                CryptoProviderFactory = new CryptoProviderFactory { CacheSignatureProviders = false }
            };

            var now = DateTime.Now;
            var unixTimeSeconds = new DateTimeOffset(now).ToUnixTimeSeconds();

            var jwt = new JwtSecurityToken(
                issuer: "my-key",
                claims: claimList,
                notBefore: now,
                expires: now.AddMinutes(30),
                signingCredentials: signingCredentials
            );

            return new JwtSecurityTokenHandler().WriteToken(jwt);
        }

        public bool ValidateToken(string token)
        {
            var publicKey = File.ReadAllText("./Secrets/private.pem");

            using RSA rsa = RSA.Create();
            rsa.ImportFromPem(publicKey);

            var validationParameters = new TokenValidationParameters
            {
                ValidateIssuer = true,
                ValidateAudience = false,
                ValidateLifetime = true,
                ValidateIssuerSigningKey = true,
                ValidIssuer = "my-key",
                IssuerSigningKey = new RsaSecurityKey(rsa),
                CryptoProviderFactory = new CryptoProviderFactory()
                {
                    CacheSignatureProviders = false
                }
            };

            try
            {
                var handler = new JwtSecurityTokenHandler();
                var principal = handler.ValidateToken(token, validationParameters, out var validatedSecurityToken);
                principal.Claims.ToList().ForEach(claim => Debug.WriteLine(claim.Value));
            }
            catch (Exception ex)
            {
                Console.WriteLine("an error... log it some way " + ex.Message);
                return false;
            }

            return true;
        }

In the above example we use the key to set a JwtSecurityToken. This would assume you have your file in a folder called Secrets where you have your public and private key. With .NET we have the System.Security.Cryptography library where we can utulize these RSA algorithm methods which makes it easy, such method is ImportFromPem which does actually what it sounds like, it will read our private key from the pem file and put it into the rsa object, which we then can get retrive a SigningCredentinals object which we then can put into a jwt property for example. The audiance property can be set with an list of Claims coming from an authentication response with claims such as firstname lastname and so forth.

For the validation part, we get to use our public key to match the incoming token with the method ValidateToken from our JwtSecurityTokenHandler object. All we need to validate it is to set some TokenValidationParameters and the most important being the actual public rsa key from our pem file.