Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow no expiration team access token #1665

Merged
merged 6 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,41 @@ public String createToken(int days, int hours, int minutes, String description,
JSONArray groupArray = new JSONArray();
groupArray.add(groupName);

Date expiration = Date.from(Instant.now().plus(days, ChronoUnit.DAYS).plus(hours, ChronoUnit.HOURS).plus(minutes, ChronoUnit.MINUTES));

log.info("Team token will expire: {}", expiration);

jws = Jwts.builder()
.setIssuer(ISSUER)
.setSubject(String.format("%s (Team Token)", groupName))
.setAudience(ISSUER)
.setId(groupToken.getId().toString())
.claim("email", ownerEmail)
.claim("description", description)
.claim("email_verified", true)
.claim("name", String.format("%s (Token)", groupName))
.claim("groups", groupArray)
.setIssuedAt(Date.from(Instant.now()))
.setExpiration(expiration)
.signWith(key)
.compact();
if (days > 0 || hours > 0 || minutes > 0 ) {
Date expiration = Date.from(Instant.now().plus(days, ChronoUnit.DAYS).plus(hours, ChronoUnit.HOURS).plus(minutes, ChronoUnit.MINUTES));
log.info("Team token will expire: {}", expiration);

jws = Jwts.builder()
.setIssuer(ISSUER)
.setSubject(String.format("%s (Team Token)", groupName))
.setAudience(ISSUER)
.setId(groupToken.getId().toString())
.claim("email", ownerEmail)
.claim("description", description)
.claim("email_verified", true)
.claim("name", String.format("%s (Token)", groupName))
.claim("groups", groupArray)
.setIssuedAt(Date.from(Instant.now()))
.setExpiration(expiration)
.signWith(key)
.compact();
} else {
log.info("Team token will not expire:");

jws = Jwts.builder()
.setIssuer(ISSUER)
.setSubject(String.format("%s (Team Token)", groupName))
.setAudience(ISSUER)
.setId(groupToken.getId().toString())
.claim("email", ownerEmail)
.claim("description", description)
.claim("email_verified", true)
.claim("name", String.format("%s (Token)", groupName))
.claim("groups", groupArray)
.setIssuedAt(Date.from(Instant.now()))
.signWith(key)
.compact();
}

} catch (Exception e) {
log.error("Error generating token.", e);
Expand Down
59 changes: 58 additions & 1 deletion api/src/test/java/org/terrakube/api/TokenTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.Map;

import static io.restassured.RestAssured.given;
import static org.hamcrest.core.IsEqual.equalTo;

class TokenTests extends ServerApplicationTests {

Expand Down Expand Up @@ -74,4 +73,62 @@ void createTokenNoExpiration() throws JsonProcessingException {

}

@Test
void createTeamToken() throws JsonProcessingException {
String token = given()
.headers("Authorization", "Bearer " + generatePAT("TERRAKUBE_DEVELOPERS"))
.headers("Content-Type", "application/vnd.api+json")
.when()
.body("{\"description\":\"12345\",\"days\":1,\"minutes\":1,\"hours\":1,\"group\":\"TERRAKUBE_DEVELOPERS\"}")
.post("/access-token/v1/teams")
.then()
.assertThat()
.log()
.all()
.statusCode(HttpStatus.CREATED.value()).extract().path("token");

String[] chunks = token.split("\\.");
Base64.Decoder decoder = Base64.getDecoder();
String payload = new String(decoder.decode(chunks[1]));
Map<String, Object> result = new ObjectMapper().readValue(payload, HashMap.class);
Assert.assertNotNull(result.get("exp"));

given()
.headers("Authorization", "Bearer " + token).when()
.get("/api/v1/organization/d9b58bd3-f3fc-4056-a026-1163297e80a8")
.then()
.log()
.all()
.statusCode(HttpStatus.OK.value());
}

@Test
void createTeamTokenNoExpiration() throws JsonProcessingException {
String token = given()
.headers("Authorization", "Bearer " + generatePAT("TERRAKUBE_DEVELOPERS"))
.headers("Content-Type", "application/vnd.api+json")
.when()
.body("{\"description\":\"12345\",\"days\":0,\"minutes\":0,\"hours\":0,\"group\":\"TERRAKUBE_DEVELOPERS\"}")
.post("/access-token/v1/teams")
.then()
.assertThat()
.log()
.all()
.statusCode(HttpStatus.CREATED.value()).extract().path("token");

String[] chunks = token.split("\\.");
Base64.Decoder decoder = Base64.getDecoder();
String payload = new String(decoder.decode(chunks[1]));
Map<String, Object> result = new ObjectMapper().readValue(payload, HashMap.class);
Assert.assertNull(result.get("exp"));

given()
.headers("Authorization", "Bearer " + token).when()
.get("/api/v1/organization/d9b58bd3-f3fc-4056-a026-1163297e80a8")
.then()
.log()
.all()
.statusCode(HttpStatus.OK.value());
}

}
8 changes: 4 additions & 4 deletions ui/src/domain/Settings/EditTeam.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,10 @@ export const EditTeam = ({ mode, setMode, teamId, loadTeams }) => {
>
{" "}
<b>
Expires{" "}
{DateTime.fromISO(item.createdDate)
.plus({ days: item.days })
.toLocaleString(DateTime.DATETIME_MED)}
Expires{": "}
{(item.days > 0 || item.minutes > 0 || item.hours > 0) ? DateTime.fromISO(item.createdDate)
.plus({ days: item.days, minutes: item.minutes, hours: item.hours })
.toLocaleString(DateTime.DATETIME_MED) : "Token without expiration date"}
</b>
</Tag>
</Col>
Expand Down
Loading