Example #1
0
        public void CreateEncodedJwtToken_FromString_ShouldNotFail()
        {
            // Arrange
            var privateKeySource = new StringPrivateKeySource(File.ReadAllText("envvar.pem"));
            var options          = new GitHubJwtFactoryOptions
            {
                AppIntegrationId  = 6837,
                ExpirationSeconds = 600 // 10 minutes maximum
            };
            var factory = new GitHubJwtFactory(privateKeySource, options);

            // Act
            var token = factory.CreateEncodedJwtToken();

            // Assert
            Assert.IsNotNull(token);
            Console.WriteLine(token);
        }
        private string GetJwtToken()
        {
            GitHubAppOptions settings = _settings.Value;

            var factoryOptions = new GitHubJwtFactoryOptions
            {
                AppIntegrationId  = settings.ApplicationId, // The GitHub App Id
                ExpirationSeconds = 9 * 60                  // 10 minutes is the maximum time allowed, but because exp claim is absolute time, lets have it 9 minutes to cover time skew
            };

            // TODO: refactor into TryXXX(factoryOptions)
            if (!string.IsNullOrEmpty(settings.PrivateKey.KeyString))
            {
                // use `awk '{printf "%s\\n", $0}' private-key.pem` to store the pem data
                var privateKey = settings.PrivateKey.KeyString.Replace("\n", Environment.NewLine, StringComparison.Ordinal);

                var generator = new GitHubJwtFactory(new StringPrivateKeySource(privateKey), factoryOptions);

                return(generator.CreateEncodedJwtToken());
            }
            else if (!string.IsNullOrEmpty(settings.PrivateKey.Base64))
            {
                byte[] data          = Convert.FromBase64String(settings.PrivateKey.Base64);
                string decodedString = Encoding.UTF8.GetString(data);

                var generator = new GitHubJwtFactory(new StringPrivateKeySource(decodedString), factoryOptions);

                return(generator.CreateEncodedJwtToken());
            }
            else if (!string.IsNullOrEmpty(settings.PrivateKey.File))
            {
                var generator = new GitHubJwtFactory(new FilePrivateKeySource(settings.PrivateKey.File), factoryOptions);

                return(generator.CreateEncodedJwtToken());
            }
            else
            {
                throw new InvalidOperationException("Not configured GitHubAppSettings.PrivateKey");
            }
        }
Example #3
0
        public void CreateEncodedJwtToken_FromEnvVar_ShouldNotFail()
        {
            // Arrange
            var privateKeyName   = Guid.NewGuid().ToString("N");
            var privateKeySource = new EnvironmentVariablePrivateKeySource(privateKeyName);
            var options          = new GitHubJwtFactoryOptions
            {
                AppIntegrationId  = 6837,
                ExpirationSeconds = 600 // 10 minutes maximum
            };
            var factory = new GitHubJwtFactory(privateKeySource, options);

            using (new EnvironmentVariableScope(privateKeyName))
            {
                Environment.SetEnvironmentVariable(privateKeyName, File.ReadAllText("envvar.pem"));

                // Act
                var token = factory.CreateEncodedJwtToken();

                // Assert
                Assert.IsNotNull(token);
                Console.WriteLine(token);
            }
        }