Skip to content

Commit d4e4e66

Browse files
committed
added personal token validity period selection box on settings page
1 parent 4374d41 commit d4e4e66

5 files changed

Lines changed: 22 additions & 4 deletions

File tree

‎src/main/java/com/erudika/scoold/ScooldConfig.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,8 @@ public int jwtExpiresAfterSec() {
512512
value = "604800",
513513
type = Integer.class,
514514
category = "Security",
515-
description = "The validity period of personal API keys (Bearer tokens) for use with API/MCP. Default is 168h.")
515+
description = "The validity period of personal API keys (Bearer tokens) for use with API/MCP. "
516+
+ "Setting this to 0 will show a select combobox in the UI. Default is 168h.")
516517
public int personalTokenExpiresAfterSec() {
517518
return getConfigInt("personal_token_expires_after", 168 * 60 * 60);
518519
}

‎src/main/java/com/erudika/scoold/controllers/AdminController.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,11 @@ public String setTheme(@RequestParam String theme, @RequestParam String css, Htt
504504
public ResponseEntity<Map<String, Object>> generateAPIKey(@RequestParam Integer validityHours,
505505
HttpServletRequest req, Model model) throws ParseException {
506506
Profile authUser = utils.getAuthUser(req);
507+
if (validityHours < 0) {
508+
validityHours = 0;
509+
} else if (validityHours > 2200) {
510+
validityHours = 2200;
511+
}
507512
Map<String, Object> data = utils.generateApiKey(authUser, validityHours, false);
508513
if (!data.isEmpty()) {
509514
return ResponseEntity.ok().body(data);

‎src/main/java/com/erudika/scoold/controllers/SettingsController.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,16 @@ public ResponseEntity<Map<String, Object>> generateAPIKey(@RequestParam Integer
232232
Profile authUser = utils.getAuthUser(req);
233233
if (StringUtils.isBlank(authUser.getPersonalApiToken())) {
234234
Long personalTokenValidityHours = TimeUnit.SECONDS.toHours(CONF.personalTokenExpiresAfterSec());
235-
Map<String, Object> data = utils.generateApiKey(authUser, personalTokenValidityHours.intValue(), true);
235+
if (validityHours < 0) {
236+
validityHours = 0;
237+
} else if (validityHours > 2200) {
238+
validityHours = 2200;
239+
}
240+
// allow users to select validity period from the web UI
241+
if (personalTokenValidityHours != 0) {
242+
validityHours = personalTokenValidityHours.intValue();
243+
}
244+
Map<String, Object> data = utils.generateApiKey(authUser, validityHours, true);
236245
if (!data.isEmpty()) {
237246
return ResponseEntity.ok().body(data);
238247
}

‎src/main/resources/static/scripts/scoold.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ $(function () {
414414
});
415415
table.append(row).removeClass("hide");
416416
row.find(".api-key-show").click();
417+
if (window.location.pathname.match("/settings$")) {
418+
$(form).find("button").attr("disabled", "disabled").prev("div").addClass("hide");
419+
}
417420
});
418421

419422
$(document).on("click", ".api-key-revoke", function () {

‎src/main/resources/templates/macro.vm‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@
308308
</div>
309309
<div class="row mvm">
310310
<div class="col s6 offset-s3 m3 offset-m4 center-align">
311-
<div class="input-field #if($formActionBase != $adminlink)hide#end">
311+
#set($disabled = $formActionBase != $adminlink && $apiKeys.size() >= 1)
312+
<div class="input-field #if($scooldUtils.config.personalTokenExpiresAfterSec != 0 || $disabled)hide#end">
312313
<select id="validity-period" name="validityHours" required>
313314
<option value="1">1 hour</option>
314315
<option value="12" selected="selected">12 hours</option>
@@ -321,7 +322,6 @@
321322
</select>
322323
<label for="validity-period">$!lang.get('admin.validity')</label>
323324
</div>
324-
#set($disabled = $formActionBase != $adminlink && $apiKeys.size() >= 1)
325325
<button type="submit" class="btn waves-effect waves-light" #if($disabled)disabled="disabled"#end>$!lang.get('admin.generate')</button>
326326
</div>
327327
<div class="col s6 m9">

0 commit comments

Comments
 (0)