| 1 | package edu.ucsb.cs156.frontiers.models; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.frontiers.entities.CourseApiKey; | |
| 4 | import java.time.ZonedDateTime; | |
| 5 | ||
| 6 | /** | |
| 7 | * What an instructor may see about one of their course's API keys: everything except the key | |
| 8 | * itself, which is never stored and so can never be shown again. The last six characters of the key | |
| 9 | * (<code>keySuffix</code>) are enough to tell keys apart without compromising them. | |
| 10 | * | |
| 11 | * @param id the key's id, used to revoke it | |
| 12 | * @param label the name the instructor gave the key, or null | |
| 13 | * @param keySuffix the last six characters of the key | |
| 14 | * @param createdByEmail the email of the user who created the key | |
| 15 | * @param createdAt when the key was created | |
| 16 | * @param expiresAt when the key stops working | |
| 17 | * @param lastUsedAt when the key was last used, or null if never | |
| 18 | * @param usageCount how many requests have been made with the key | |
| 19 | * @param revoked whether the key has been revoked | |
| 20 | * @param status ACTIVE, EXPIRED or REVOKED, as of the time the view was built | |
| 21 | */ | |
| 22 | public record CourseApiKeyView( | |
| 23 | Long id, | |
| 24 | String label, | |
| 25 | String keySuffix, | |
| 26 | String createdByEmail, | |
| 27 | ZonedDateTime createdAt, | |
| 28 | ZonedDateTime expiresAt, | |
| 29 | ZonedDateTime lastUsedAt, | |
| 30 | Long usageCount, | |
| 31 | boolean revoked, | |
| 32 | Status status) { | |
| 33 | ||
| 34 | /** Whether a key can currently be used. A revoked key stays REVOKED even once it expires. */ | |
| 35 | public enum Status { | |
| 36 | ACTIVE, | |
| 37 | EXPIRED, | |
| 38 | REVOKED | |
| 39 | } | |
| 40 | ||
| 41 | /** | |
| 42 | * Builds the view of a key as of the given moment. | |
| 43 | * | |
| 44 | * @param key the key | |
| 45 | * @param now the current time, used to decide whether the key has expired | |
| 46 | * @return the view | |
| 47 | */ | |
| 48 | public static CourseApiKeyView from(CourseApiKey key, ZonedDateTime now) { | |
| 49 | Status status; | |
| 50 |
1
1. from : negated conditional → KILLED |
if (key.getRevoked()) { |
| 51 | status = Status.REVOKED; | |
| 52 |
1
1. from : negated conditional → KILLED |
} else if (key.getExpiresAt().isBefore(now)) { |
| 53 | status = Status.EXPIRED; | |
| 54 | } else { | |
| 55 | status = Status.ACTIVE; | |
| 56 | } | |
| 57 |
1
1. from : replaced return value with null for edu/ucsb/cs156/frontiers/models/CourseApiKeyView::from → KILLED |
return new CourseApiKeyView( |
| 58 | key.getId(), | |
| 59 | key.getLabel(), | |
| 60 | key.getKeySuffix(), | |
| 61 | key.getCreatedBy().getEmail(), | |
| 62 | key.getCreatedAt(), | |
| 63 | key.getExpiresAt(), | |
| 64 | key.getLastUsedAt(), | |
| 65 | key.getUsageCount(), | |
| 66 | key.getRevoked(), | |
| 67 | status); | |
| 68 | } | |
| 69 | } | |
Mutations | ||
| 50 |
1.1 |
|
| 52 |
1.1 |
|
| 57 |
1.1 |