| 1 | package edu.ucsb.cs156.frontiers.services; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.databind.JsonNode; | |
| 5 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 6 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 7 | import edu.ucsb.cs156.frontiers.errors.NoLinkedOrganizationException; | |
| 8 | import io.jsonwebtoken.Jwts; | |
| 9 | import java.security.KeyFactory; | |
| 10 | import java.security.NoSuchAlgorithmException; | |
| 11 | import java.security.interfaces.RSAPrivateKey; | |
| 12 | import java.security.spec.InvalidKeySpecException; | |
| 13 | import java.security.spec.PKCS8EncodedKeySpec; | |
| 14 | import java.time.Duration; | |
| 15 | import java.time.Instant; | |
| 16 | import java.time.temporal.ChronoUnit; | |
| 17 | import java.util.Base64; | |
| 18 | import java.util.Date; | |
| 19 | import lombok.extern.slf4j.Slf4j; | |
| 20 | import org.springframework.beans.factory.annotation.Value; | |
| 21 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
| 22 | import org.springframework.data.auditing.DateTimeProvider; | |
| 23 | import org.springframework.http.HttpEntity; | |
| 24 | import org.springframework.http.HttpHeaders; | |
| 25 | import org.springframework.http.HttpMethod; | |
| 26 | import org.springframework.http.ResponseEntity; | |
| 27 | import org.springframework.stereotype.Service; | |
| 28 | import org.springframework.web.client.RestTemplate; | |
| 29 | ||
| 30 | @Service | |
| 31 | @Slf4j | |
| 32 | public class JwtService { | |
| 33 | @Value("${app.private.key:no-key-present}") | |
| 34 | private String privateKey; | |
| 35 | ||
| 36 | @Value("${app.client.id:no-client-id}") | |
| 37 | private String clientId; | |
| 38 | ||
| 39 | /* | |
| 40 | * A RestTemplate built with no timeout blocks its calling thread forever on a hung external | |
| 41 | * call. That's a real incident lib-jobs' single-threaded jobsExecutor hit on another app | |
| 42 | * (proj-scaffold): a job stuck this way permanently wedged the executor, with no way to recover | |
| 43 | * short of restarting the app (see lib-jobs DESIGN.md 9 -- cooperative job cancellation only | |
| 44 | * helps a job that reaches another checkpoint, which a truly hung thread never will). Generous | |
| 45 | * but finite: long enough to never trip on legitimate slowness, short enough to guarantee a job | |
| 46 | * can't hang forever. | |
| 47 | */ | |
| 48 | private static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(10); | |
| 49 | private static final Duration READ_TIMEOUT = Duration.ofSeconds(60); | |
| 50 | ||
| 51 | private final RestTemplate restTemplate; | |
| 52 | ||
| 53 | private final ObjectMapper objectMapper; | |
| 54 | ||
| 55 | private final DateTimeProvider dateTimeProvider; | |
| 56 | ||
| 57 | public JwtService( | |
| 58 | RestTemplateBuilder restTemplateBuilder, | |
| 59 | ObjectMapper objectMapper, | |
| 60 | DateTimeProvider dateTimeProvider) { | |
| 61 | this.restTemplate = | |
| 62 | restTemplateBuilder.connectTimeout(CONNECT_TIMEOUT).readTimeout(READ_TIMEOUT).build(); | |
| 63 | this.objectMapper = objectMapper; | |
| 64 | this.dateTimeProvider = dateTimeProvider; | |
| 65 | } | |
| 66 | ||
| 67 | private RSAPrivateKey getPrivateKey() throws NoSuchAlgorithmException, InvalidKeySpecException { | |
| 68 | String key = privateKey; | |
| 69 | key = key.replace("-----BEGIN PRIVATE KEY-----", ""); | |
| 70 | key = key.replace("-----END PRIVATE KEY-----", ""); | |
| 71 | key = key.replaceAll(" ", ""); | |
| 72 | key = key.replaceAll(System.lineSeparator(), ""); | |
| 73 | PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(key.getBytes())); | |
| 74 | KeyFactory kf = KeyFactory.getInstance("RSA"); | |
| 75 |
1
1. getPrivateKey : replaced return value with null for edu/ucsb/cs156/frontiers/services/JwtService::getPrivateKey → KILLED |
return (RSAPrivateKey) kf.generatePrivate(spec); |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * Method to retrieve a signed JWT that a service can use to authenticate with GitHub as an app | |
| 80 | * installation without permissions to a specific organization. | |
| 81 | * | |
| 82 | * @return Signed JWT that expires in 5 minutes in the form of a String | |
| 83 | * @throws InvalidKeySpecException if the key is invalid, the exception will be thrown. | |
| 84 | */ | |
| 85 | public String getJwt() throws NoSuchAlgorithmException, InvalidKeySpecException { | |
| 86 | Instant currentTime = Instant.from(dateTimeProvider.getNow().get()); | |
| 87 | String token = | |
| 88 | Jwts.builder() | |
| 89 | .issuedAt(Date.from(currentTime.minus(30, ChronoUnit.SECONDS))) | |
| 90 | .expiration(Date.from(currentTime.plus(5, ChronoUnit.MINUTES))) | |
| 91 | .issuer(clientId) | |
| 92 | .signWith(getPrivateKey(), Jwts.SIG.RS256) | |
| 93 | .compact(); | |
| 94 |
1
1. getJwt : replaced return value with "" for edu/ucsb/cs156/frontiers/services/JwtService::getJwt → KILLED |
return token; |
| 95 | } | |
| 96 | ||
| 97 | /** | |
| 98 | * Method to retrieve a token to act as a particular app installation in a particular organization | |
| 99 | * | |
| 100 | * @param course ID of the particular app installation to act as | |
| 101 | * @return Token accepted by GitHub to act as a particular installation. | |
| 102 | */ | |
| 103 | public String getInstallationToken(Course course) | |
| 104 | throws JsonProcessingException, | |
| 105 | NoSuchAlgorithmException, | |
| 106 | InvalidKeySpecException, | |
| 107 | NoLinkedOrganizationException { | |
| 108 |
2
1. getInstallationToken : negated conditional → KILLED 2. getInstallationToken : negated conditional → KILLED |
if (course.getOrgName() == null || course.getInstallationId() == null) { |
| 109 | throw new NoLinkedOrganizationException(course.getCourseName()); | |
| 110 | } else { | |
| 111 | String token = getJwt(); | |
| 112 | String ENDPOINT = | |
| 113 | "https://api.github.com/app/installations/" | |
| 114 | + course.getInstallationId() | |
| 115 | + "/access_tokens"; | |
| 116 | HttpHeaders headers = new HttpHeaders(); | |
| 117 |
1
1. getInstallationToken : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 118 |
1
1. getInstallationToken : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 119 |
1
1. getInstallationToken : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 120 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 121 | ResponseEntity<String> response = | |
| 122 | restTemplate.exchange(ENDPOINT, HttpMethod.POST, entity, String.class); | |
| 123 | JsonNode responseJson = objectMapper.readTree(response.getBody()); | |
| 124 | String installationToken = responseJson.get("token").asText(); | |
| 125 |
1
1. getInstallationToken : replaced return value with "" for edu/ucsb/cs156/frontiers/services/JwtService::getInstallationToken → KILLED |
return installationToken; |
| 126 | } | |
| 127 | } | |
| 128 | } | |
Mutations | ||
| 75 |
1.1 |
|
| 94 |
1.1 |
|
| 108 |
1.1 2.2 |
|
| 117 |
1.1 |
|
| 118 |
1.1 |
|
| 119 |
1.1 |
|
| 125 |
1.1 |