diff --git a/src/main/java/edu/harvard/iq/dataverse/ApiTokenPage.java b/src/main/java/edu/harvard/iq/dataverse/ApiTokenPage.java index 05923b9e13a..c9d7fceb4e5 100644 --- a/src/main/java/edu/harvard/iq/dataverse/ApiTokenPage.java +++ b/src/main/java/edu/harvard/iq/dataverse/ApiTokenPage.java @@ -4,9 +4,9 @@ import edu.harvard.iq.dataverse.authorization.users.ApiToken; import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser; import edu.harvard.iq.dataverse.util.BundleUtil; +import edu.harvard.iq.dataverse.api.Util; import java.sql.Timestamp; import java.util.ArrayList; -import java.util.Calendar; import java.util.List; import java.util.logging.Logger; import javax.ejb.EJB; @@ -75,4 +75,33 @@ public void generate() { } } + + public String getApiTokenExpiration() { + if (session.getUser().isAuthenticated()) { + AuthenticatedUser au = (AuthenticatedUser) session.getUser(); + apiToken = authSvc.findApiTokenByUser(au); + if (apiToken != null) { + return Util.getDateFormat().format(apiToken.getExpireTime()); + } else { + return ""; + } + } else { + // It should be impossible to get here from the UI. + return ""; + } + } + + public Boolean tokenIsExpired(){ + return apiToken.getExpireTime().before(new Timestamp(System.currentTimeMillis())); + } + + public void revoke() { + if (session.getUser().isAuthenticated()) { + AuthenticatedUser au = (AuthenticatedUser) session.getUser(); + apiToken = authSvc.findApiTokenByUser(au); + if (apiToken != null) { + authSvc.removeApiToken(au); + } + } + } } \ No newline at end of file diff --git a/src/main/java/edu/harvard/iq/dataverse/authorization/AuthenticationServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/authorization/AuthenticationServiceBean.java index 65bf466e3ef..44903624c1a 100644 --- a/src/main/java/edu/harvard/iq/dataverse/authorization/AuthenticationServiceBean.java +++ b/src/main/java/edu/harvard/iq/dataverse/authorization/AuthenticationServiceBean.java @@ -437,7 +437,7 @@ public ApiToken findApiTokenByUser(AuthenticatedUser au) { try { return typedQuery.getSingleResult(); } catch (NoResultException | NonUniqueResultException ex) { - logger.log(Level.INFO, "When looking up API token for {0} caught {1}", new Object[]{au, ex}); + logger.log(Level.FINE, "When looking up API token for {0} caught {1}", new Object[]{au, ex}); return null; } } diff --git a/src/main/java/propertyFiles/Bundle.properties b/src/main/java/propertyFiles/Bundle.properties index 38e03b719e6..3f97c897747 100755 --- a/src/main/java/propertyFiles/Bundle.properties +++ b/src/main/java/propertyFiles/Bundle.properties @@ -397,10 +397,14 @@ oauth2.callback.message=Authentication Error - Dataverse could # tab on dataverseuser.xhtml apitoken.title=API Token -apitoken.message=Your API Token is displayed below after it has been created. Check out our {0}API Guide{1} for more information on using your API Token with the Dataverse APIs. +apitoken.message=Your API Token is valid for a year. Check out our {0}API Guide{1} for more information on using your API Token with the Dataverse APIs. apitoken.notFound=API Token for {0} has not been created. +apitoken.expired.warning=This token is about to expire, please generate a new one. +apitoken.expired.error=This token is expired, please generate a new one. apitoken.generateBtn=Create Token apitoken.regenerateBtn=Recreate Token +apitoken.revokeBtn=Revoke Token +apitoken.expirationDate.label=Expiration Date #dashboard.xhtml dashboard.title=Dashboard diff --git a/src/main/webapp/dataset.xhtml b/src/main/webapp/dataset.xhtml index 70d7a71dc4e..a94f6141b31 100644 --- a/src/main/webapp/dataset.xhtml +++ b/src/main/webapp/dataset.xhtml @@ -443,7 +443,7 @@ or !empty DatasetPage.datasetVersionUI.subject.value or !empty DatasetPage.datasetVersionUI.relPublicationCitation or !empty DatasetPage.datasetVersionUI.notes.value) and !empty DatasetPage.datasetSummaryFields}"> -