-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add task for invalidating api tokens
It does not do that yet. Currently it only reports last login of all known oauth2 proxy users, but the persistence and scheduling is there.
- Loading branch information
1 parent
9d38a6e
commit 6c6cd93
Showing
6 changed files
with
170 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/github/tumbl3w33d/OAuth2ProxyApiTokenInvalidateQuartz.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.github.tumbl3w33d; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
import static org.sonatype.nexus.common.app.ManagedLifecycle.Phase.TASKS; | ||
|
||
import java.util.Date; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import javax.inject.Singleton; | ||
|
||
import org.sonatype.nexus.common.app.ManagedLifecycle; | ||
import org.sonatype.nexus.common.stateguard.StateGuardLifecycleSupport; | ||
import org.sonatype.nexus.scheduling.TaskConfiguration; | ||
import org.sonatype.nexus.scheduling.TaskScheduler; | ||
import org.sonatype.nexus.scheduling.schedule.Schedule; | ||
|
||
@Named | ||
@ManagedLifecycle(phase = TASKS) | ||
@Singleton | ||
public class OAuth2ProxyApiTokenInvalidateQuartz extends StateGuardLifecycleSupport { | ||
private final TaskScheduler taskScheduler; | ||
|
||
private final String taskCron; | ||
|
||
@Inject | ||
public OAuth2ProxyApiTokenInvalidateQuartz(final TaskScheduler taskScheduler, | ||
@Named("${nexus.tasks.oauth2-proxy.api-token-invalidate.cron:-0 */5 * * * ?}") final String taskCron) { | ||
this.taskScheduler = checkNotNull(taskScheduler); | ||
this.taskCron = checkNotNull(taskCron); | ||
} | ||
|
||
@Override | ||
protected void doStart() throws Exception { | ||
if (!taskScheduler.listsTasks().stream() | ||
.anyMatch((info) -> OAuth2ProxyApiTokenInvalidateTaskDescriptor.TYPE_ID | ||
.equals(info.getConfiguration().getTypeId()))) { | ||
TaskConfiguration configuration = taskScheduler.createTaskConfigurationInstance( | ||
OAuth2ProxyApiTokenInvalidateTaskDescriptor.TYPE_ID); | ||
Schedule schedule = taskScheduler.getScheduleFactory().cron(new Date(), taskCron); | ||
taskScheduler.scheduleTask(configuration, schedule); | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/com/github/tumbl3w33d/OAuth2ProxyApiTokenInvalidateTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.github.tumbl3w33d; | ||
|
||
import static com.github.tumbl3w33d.OAuth2ProxyRealm.CLASS_USER_LOGIN; | ||
import static com.github.tumbl3w33d.OAuth2ProxyRealm.FIELD_LAST_LOGIN; | ||
import static com.github.tumbl3w33d.OAuth2ProxyRealm.FIELD_USER_ID; | ||
import static com.github.tumbl3w33d.OAuth2ProxyRealm.formatDateString; | ||
import static org.sonatype.nexus.logging.task.TaskLogType.NEXUS_LOG_ONLY; | ||
|
||
import java.time.LocalDate; | ||
import java.time.ZoneId; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.sonatype.nexus.logging.task.TaskLogging; | ||
import org.sonatype.nexus.orient.DatabaseInstance; | ||
import org.sonatype.nexus.scheduling.Cancelable; | ||
import org.sonatype.nexus.scheduling.TaskSupport; | ||
|
||
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx; | ||
import com.orientechnologies.orient.core.record.impl.ODocument; | ||
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery; | ||
|
||
@Named | ||
@TaskLogging(NEXUS_LOG_ONLY) | ||
public class OAuth2ProxyApiTokenInvalidateTask | ||
extends TaskSupport | ||
implements Cancelable { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(OAuth2ProxyApiTokenInvalidateTask.class.getName()); | ||
|
||
private final DatabaseInstance databaseInstance; | ||
|
||
@Inject | ||
public OAuth2ProxyApiTokenInvalidateTask(@Named(OAuth2ProxyDatabase.NAME) DatabaseInstance databaseInstance) { | ||
this.databaseInstance = databaseInstance; | ||
|
||
OAuth2ProxyRealm.ensureUserLoginTimestampSchema(databaseInstance, log); | ||
} | ||
|
||
@Override | ||
protected Void execute() throws Exception { | ||
try (ODatabaseDocumentTx db = databaseInstance.acquire()) { | ||
db.begin(); | ||
|
||
List<ODocument> userLogins = db.query(new OSQLSynchQuery<ODocument>( | ||
"select from " + CLASS_USER_LOGIN)); | ||
|
||
if (userLogins.isEmpty()) { | ||
logger.debug("Nothing to do"); | ||
} else { | ||
for (ODocument userLogin : userLogins) { | ||
Date lastLoginDate = userLogin.field(FIELD_LAST_LOGIN); | ||
LocalDate lastLoginLocalDate = lastLoginDate.toInstant().atZone(ZoneId.systemDefault()) | ||
.toLocalDate(); | ||
LocalDate nowLocalDate = LocalDate.now(); | ||
logger.info("Last known login for {} was {}", userLogin.field(FIELD_USER_ID), | ||
formatDateString(lastLoginDate)); | ||
// if > 30d passed, reset user password | ||
/* | ||
* if (!lastLoginLocalDate.equals(nowLocalDate)) { | ||
* // TODO | ||
* } | ||
*/ | ||
} | ||
|
||
} | ||
} catch (Exception e) { | ||
logger.error("Failed to retrieve login timestamps - {}", e); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return "Invalidate OAuth2 Proxy API tokens of users who did not log in for a while"; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/github/tumbl3w33d/OAuth2ProxyApiTokenInvalidateTaskDescriptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.github.tumbl3w33d; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import javax.inject.Singleton; | ||
|
||
import org.sonatype.nexus.scheduling.TaskDescriptorSupport; | ||
|
||
@Named | ||
@Singleton | ||
public class OAuth2ProxyApiTokenInvalidateTaskDescriptor | ||
extends TaskDescriptorSupport { | ||
public static final String TYPE_ID = "oauth2-proxy-api-token.cleanup"; | ||
|
||
@Inject | ||
public OAuth2ProxyApiTokenInvalidateTaskDescriptor() { | ||
super(TYPE_ID, OAuth2ProxyApiTokenInvalidateTask.class, "OAuth2 Proxy API token invalidator", | ||
TaskDescriptorSupport.NOT_VISIBLE, TaskDescriptorSupport.NOT_EXPOSED); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters