diff --git a/pom.xml b/pom.xml index f856725..a53c8cc 100644 --- a/pom.xml +++ b/pom.xml @@ -82,6 +82,34 @@ custom-war-packager-lib 1.7 + + io.jsonwebtoken + jjwt-jackson + 0.10.5 + runtime + + + io.jsonwebtoken + jjwt-api + 0.10.5 + + + io.jsonwebtoken + jjwt-impl + 0.10.5 + runtime + + + + com.google.guava + guava + 29.0-jre + + + org.kohsuke + github-api + 1.106 + diff --git a/src/main/java/com/org/jenkins/custom/jenkins/distribution/service/GithubController.java b/src/main/java/com/org/jenkins/custom/jenkins/distribution/service/GithubController.java new file mode 100644 index 0000000..6ab5473 --- /dev/null +++ b/src/main/java/com/org/jenkins/custom/jenkins/distribution/service/GithubController.java @@ -0,0 +1,56 @@ +package com.org.jenkins.custom.jenkins.distribution.service; + +import java.util.List; +import org.kohsuke.github.GHApp; +import org.kohsuke.github.GHAppInstallation; +import org.kohsuke.github.GHAppInstallationToken; +import org.kohsuke.github.GitHub; +import org.kohsuke.github.GitHubBuilder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.env.Environment; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import static com.org.jenkins.custom.jenkins.distribution.service.GithubJwtHelper.createJWT; +import static com.org.jenkins.custom.jenkins.distribution.service.GithubJwtHelper.get; + + +@RestController +@CrossOrigin("*") +public class GithubController { + + @Value("${APP_IDENTIFIER}") + private String getAppIdentifier; + + @PostMapping(path = "/event_handler") + public void handleEvent(@RequestBody String requestBodyString) throws Exception { + System.out.println("Received event handler event"); + String token = authenticateApplication(); + System.out.println(token); + } + + @SuppressWarnings("deprecation") + private String authenticateApplication() { + try { + String APP_IDENTIFIER = getAppIdentifier.replaceAll("^\"+|\"+$", ""); + String jwtToken = createJWT(APP_IDENTIFIER, 600000); + GitHub gitHubApp = new GitHubBuilder().withEndpoint("https://api.github.com").withJwtToken(jwtToken).build(); + GHApp app = gitHubApp.getApp(); + List appInstallations = app.listInstallations().asList(); + if (!appInstallations.isEmpty()) { + GHAppInstallation appInstallation = appInstallations.get(0); + GHAppInstallationToken appInstallationToken = appInstallation + .createToken(appInstallation.getPermissions()) + .create(); + return appInstallationToken.getToken(); + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/src/main/java/com/org/jenkins/custom/jenkins/distribution/service/GithubJwtHelper.java b/src/main/java/com/org/jenkins/custom/jenkins/distribution/service/GithubJwtHelper.java new file mode 100644 index 0000000..248abaf --- /dev/null +++ b/src/main/java/com/org/jenkins/custom/jenkins/distribution/service/GithubJwtHelper.java @@ -0,0 +1,50 @@ +package com.org.jenkins.custom.jenkins.distribution.service; + +import io.jsonwebtoken.JwtBuilder; +import io.jsonwebtoken.SignatureAlgorithm; +import io.jsonwebtoken.Jwts; +import java.io.File; +import java.security.Key; +import java.security.KeyFactory; +import java.security.PrivateKey; +import java.security.spec.PKCS8EncodedKeySpec; +import java.util.Date; +import com.google.common.io.Files; + + +public class GithubJwtHelper { + + static PrivateKey get(String filename) throws Exception { + byte[] keyBytes = Files.toByteArray(new File(filename)); + PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); + KeyFactory kf = KeyFactory.getInstance("RSA"); + return kf.generatePrivate(spec); + } + + static String createJWT(String githubAppId, long ttlMillis) throws Exception { + //The JWT signature algorithm we will be using to sign the token + SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.RS256; + + long nowMillis = System.currentTimeMillis(); + Date now = new Date(nowMillis); + + //We will sign our JWT with our private key + Key signingKey = get("jenkins-custom-distribution-bot.der"); + + //Let's set the JWT Claims + JwtBuilder builder = Jwts.builder() + .setIssuedAt(now) + .setIssuer(githubAppId) + .signWith(signingKey, signatureAlgorithm); + + //if it has been specified, let's add the expiration + if (ttlMillis > 0) { + long expMillis = nowMillis + ttlMillis; + Date exp = new Date(expMillis); + builder.setExpiration(exp); + } + + //Builds the JWT and serializes it to a compact, URL-safe string + return builder.compact(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..2a26bf7 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1 @@ - +APP_IDENTIFIER = "71744" \ No newline at end of file