Skip to content

Commit

Permalink
feat: token refresh every 12h
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceh121 committed Nov 30, 2023
1 parent ae053e5 commit cf01a92
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/main/java/me/vinceh121/knb/Knb.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class Knb {
private final CommandListener regisListener;
private final Connection dbCon;
private final Table tableKdecoleInstances, tableSkolengoInstances;
private final JobDetail kdecoleJob, skolengoJob;
private final JobDetail kdecoleJob, skolengoJob, skolengoRefreshJob;
private final Map<String, AbstractCommand> cmdMap = new HashMap<>();
private final MetricRegistry metricRegistry = new MetricRegistry();
private final JedisPool redisPool;
Expand Down Expand Up @@ -155,6 +155,9 @@ public Knb() {
.ofType(SkolengoCheckingJob.class)
.withIdentity("skolengo-checker", "jobs")
.build();

this.skolengoRefreshJob
= JobBuilder.newJob().ofType(SkolengoRefreshJob.class).withIdentity("skolengo-refresh", "jobs").build();
}

private void start() {
Expand All @@ -179,9 +182,19 @@ private void start() {

this.skolengoJob.getJobDataMap().put("knb", this);

final Trigger skolengoRefreshTrig = TriggerBuilder.newTrigger()
.forJob(this.skolengoRefreshJob)
.startNow()
.withIdentity("skolengo-refresh-trig")
.withSchedule(SimpleScheduleBuilder.repeatHourlyForever(12))
.build();

this.skolengoRefreshJob.getJobDataMap().put("knb", this);

try {
this.scheduler.scheduleJob(this.kdecoleJob, kdecoleTrig);
this.scheduler.scheduleJob(this.skolengoJob, skolengoTrig);
this.scheduler.scheduleJob(this.skolengoRefreshJob, skolengoRefreshTrig);
} catch (final SchedulerException e) {
Knb.LOG.error("Could not schedule checking job", e);
System.exit(-5);
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/me/vinceh121/knb/SkolengoRefreshJob.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package me.vinceh121.knb;

import java.io.IOException;
import java.net.URI;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.nimbusds.oauth2.sdk.ParseException;
import com.nimbusds.oauth2.sdk.RefreshTokenGrant;
import com.nimbusds.oauth2.sdk.Scope;
import com.nimbusds.oauth2.sdk.TokenRequest;
import com.nimbusds.oauth2.sdk.TokenResponse;
import com.nimbusds.oauth2.sdk.id.ClientID;
import com.nimbusds.oauth2.sdk.token.RefreshToken;
import com.nimbusds.openid.connect.sdk.OIDCTokenResponse;
import com.nimbusds.openid.connect.sdk.OIDCTokenResponseParser;
import com.nimbusds.openid.connect.sdk.token.OIDCTokens;
import com.rethinkdb.model.MapObject;

import me.vinceh121.jskolengo.SkolengoConstants;

public class SkolengoRefreshJob implements Job {
private static final Logger LOG = LogManager.getLogger(SkolengoRefreshJob.class);

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
final Knb knb = (Knb) context.getMergedJobDataMap().get("knb");

knb.getAllValidSkolengoInstances().forEach(ui -> {
try {
TokenRequest req = new TokenRequest(URI.create(ui.getTokenEndpoint()),
new ClientID(SkolengoConstants.OIDC_CLIENT_ID),
new RefreshTokenGrant(new RefreshToken(ui.getTokens().getRefreshToken())), new Scope("openid"),
null, null, null, null);

TokenResponse res = OIDCTokenResponseParser.parse(req.toHTTPRequest().send());

if (!res.indicatesSuccess()) {
throw new IOException("Refresh failed: " + res.toErrorResponse().toJSONObject());
}

OIDCTokenResponse tokensResponse = (OIDCTokenResponse) res.toSuccessResponse();
OIDCTokens tokens = tokensResponse.getOIDCTokens();

OIDCTokenSet tokenSet = new OIDCTokenSet();
tokenSet.setAccessToken(tokens.getAccessToken().toString());
tokenSet.setRefreshToken(tokens.getRefreshToken().toString());
tokenSet.setIdToken(tokens.getIDTokenString());

knb.getTableSkolengoInstances()
.get(ui.getId())
.update(new MapObject<>().with("tokens", tokenSet))
.run(knb.getDbCon());

} catch (ParseException | IOException e) {
LOG.error("Error while refreshing token", e);
}
});
}
}
9 changes: 9 additions & 0 deletions src/main/java/me/vinceh121/knb/SkolengoUserInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class SkolengoUserInstance extends AbstractUserInstance {
private OIDCTokenSet tokens;
private String emsCode;
private String schoolId;
private String tokenEndpoint;

public OIDCTokenSet getTokens() {
return tokens;
Expand All @@ -29,6 +30,14 @@ public void setSchoolId(String schoolId) {
this.schoolId = schoolId;
}

public String getTokenEndpoint() {
return tokenEndpoint;
}

public void setTokenEndpoint(String tokenEndpoint) {
this.tokenEndpoint = tokenEndpoint;
}

@Override
public String toString() {
return "SkolengoUserInstance [tokens="
Expand Down

0 comments on commit cf01a92

Please sign in to comment.