Beispiel #1
0
        public static async Task <InstallationToken> GenerateAsync(InstallationTokenParameters input, StreamReader privateKeyReader)
        {
            var jwtPayload = new
            {
                iat = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds,
                exp = (int)(DateTime.UtcNow.AddMinutes(9) - new DateTime(1970, 1, 1)).TotalSeconds,
                iss = input.AppId,
            };

            var header       = new { alg = "RS256", typ = "JWT" };
            var headerBytes  = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(header, Formatting.None));
            var payloadBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(jwtPayload, Formatting.None));

            var segments = new List <string>
            {
                Base64UrlEncode(headerBytes),
                Base64UrlEncode(payloadBytes),
            };

            var stringToSign = string.Join(".", segments);
            var bytesToSign  = Encoding.UTF8.GetBytes(stringToSign);

            ISigner signer = SignerUtilities.GetSigner("SHA-256withRSA");
            AsymmetricCipherKeyPair keyPair;

            using (privateKeyReader)
            {
                keyPair = (AsymmetricCipherKeyPair) new PemReader(privateKeyReader).ReadObject();
            }

            signer.Init(true, keyPair.Private);
            signer.BlockUpdate(bytesToSign, 0, bytesToSign.Length);
            var sigBytes = signer.GenerateSignature();

            segments.Add(Base64UrlEncode(sigBytes));

            var jwttoken = string.Join(".", segments);

            using (var http = new HttpClient())
            {
                http.DefaultRequestHeaders.Add("Accept", "application/vnd.github.machine-man-preview+json");
                http.DefaultRequestHeaders.Add("User-Agent", "ImgBot");
                http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwttoken);

                var result = await http.PostAsync(input.AccessTokensUrl, null);

                var json = await result.Content.ReadAsStringAsync();

                return(JsonConvert.DeserializeObject <InstallationToken>(json));
            }
        }
Beispiel #2
0
        public async Task <InstallationToken> GenerateAsync(InstallationTokenParameters input, StreamReader privateKeyReader)
        {
            var jwttoken = GenerateJWT(input, privateKeyReader);

            using (var http = new HttpClient())
            {
                http.DefaultRequestHeaders.Add("Accept", "application/vnd.github.machine-man-preview+json");
                http.DefaultRequestHeaders.Add("User-Agent", "ImgBot");
                http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwttoken);

                var result = await http.PostAsync(input.AccessTokensUrl, null);

                var json = await result.Content.ReadAsStringAsync();

                return(JsonConvert.DeserializeObject <InstallationToken>(json));
            }
        }
        public string GenerateJWT(InstallationTokenParameters input, string privateKey)
        {
            var jwtPayload = new
            {
                iat = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds,
                exp = (int)(DateTime.UtcNow.AddMinutes(9) - new DateTime(1970, 1, 1)).TotalSeconds,
                iss = input.AppId,
            };

            var header       = new { alg = "RS256", typ = "JWT" };
            var headerBytes  = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(header, Formatting.None));
            var payloadBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(jwtPayload, Formatting.None));

            var segments = new List <string>
            {
                Base64UrlEncode(headerBytes),
                Base64UrlEncode(payloadBytes),
            };

            var stringToSign = string.Join(".", segments);
            var bytesToSign  = Encoding.UTF8.GetBytes(stringToSign);

            ISigner signer = SignerUtilities.GetSigner("SHA-256withRSA");
            AsymmetricCipherKeyPair keyPair;

            var privateKeyBuilder = new StringBuilder();

            privateKeyBuilder.AppendLine("-----BEGIN RSA PRIVATE KEY-----");
            privateKeyBuilder.AppendLine(privateKey);
            privateKeyBuilder.AppendLine("-----END RSA PRIVATE KEY-----");
            using (TextReader textReader = new StringReader(privateKeyBuilder.ToString()))
            {
                keyPair = (AsymmetricCipherKeyPair) new PemReader(textReader).ReadObject();
            }

            signer.Init(true, keyPair.Private);
            signer.BlockUpdate(bytesToSign, 0, bytesToSign.Length);
            var sigBytes = signer.GenerateSignature();

            segments.Add(Base64UrlEncode(sigBytes));

            return(string.Join(".", segments));
        }