public static async Task RunOpenPr( [QueueTrigger("openprmessage")] OpenPrMessage openPrMessage, [Table("installation", "{InstallationId}", "{RepoName}")] Installation installation, TraceWriter log, ExecutionContext context) { if (installation == null) { throw new Exception($"No installation found for InstallationId: {installation.InstallationId}"); } var installationTokenParameters = new InstallationTokenParameters { AccessTokensUrl = installation.AccessTokensUrl, AppId = KnownGitHubs.AppId, }; var installationToken = await InstallationToken.GenerateAsync( installationTokenParameters, File.OpenText(Path.Combine(context.FunctionDirectory, $"..\\{KnownGitHubs.AppPrivateKey}"))); await PullRequest.OpenAsync(new PullRequestParameters { Password = installationToken.Token, RepoName = installation.RepoName, RepoOwner = installation.Owner, }); }
public static async Task RunInstallationMessage( [QueueTrigger("installationmessage")] InstallationMessage installationMessage, [Table("installation")] ICollector <Installation> installations, [Table("installation", "{InstallationId}", "{RepoName}")] Installation installation, [Queue("openprmessage")] ICollector <OpenPrMessage> openPrMessages, TraceWriter log, ExecutionContext context) { // if not already installed if (installation == null) { installations.Add(new Installation(installationMessage.InstallationId, installationMessage.RepoName) { AccessTokensUrl = installationMessage.AccessTokensUrl, CloneUrl = installationMessage.CloneUrl, Owner = installationMessage.Owner, }); } var installationTokenParameters = new InstallationTokenParameters { AccessTokensUrl = installationMessage.AccessTokensUrl, AppId = KnownGitHubs.AppId, }; // good for ~10 minutes var installationToken = await InstallationToken.GenerateAsync( installationTokenParameters, File.OpenText(Path.Combine(context.FunctionDirectory, $"..\\{KnownGitHubs.AppPrivateKey}"))); var compressImagesParameters = new CompressimagesParameters { CloneUrl = installationMessage.CloneUrl, LocalPath = LocalPath.CloneDir(Environment.GetEnvironmentVariable("TMP"), installationMessage.RepoName), Password = installationToken.Token, RepoName = installationMessage.RepoName, RepoOwner = installationMessage.Owner, PgpPrivateKeyStream = File.OpenRead(Path.Combine(context.FunctionDirectory, $"..\\{KnownGitHubs.PGPPrivateKeyFilename}")), PgPPassword = File.ReadAllText(Path.Combine(context.FunctionDirectory, $"..\\{KnownGitHubs.PGPPasswordFilename}")) }; var didCompress = CompressImages.Run(compressImagesParameters); if (didCompress) { openPrMessages.Add(new OpenPrMessage { InstallationId = installationMessage.InstallationId, RepoName = installationMessage.RepoName, }); } }
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)); } }