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

Implement Token Expiry Check and Refresh using Refresh Tokens #310

Merged
merged 1 commit into from
Jul 15, 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
86 changes: 86 additions & 0 deletions src/main/java/org/jenkinsci/plugins/oic/OicCredentials.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.jenkinsci.plugins.oic;

Check warning on line 1 in src/main/java/org/jenkinsci/plugins/oic/OicCredentials.java

View check run for this annotation

ci.jenkins.io / Java Compiler

checkstyle:check

ERROR: (misc) NewlineAtEndOfFile: Expected line ending for file is LF(\n), but CRLF(\r\n) is detected.

import hudson.Extension;
import hudson.Util;
import hudson.model.Descriptor;
import hudson.model.User;
import hudson.model.UserProperty;
import hudson.model.UserPropertyDescriptor;
import hudson.util.Secret;
import java.io.Serializable;
import net.sf.json.JSONObject;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.StaplerRequest;

public class OicCredentials extends UserProperty implements Serializable {
static final String PROPERTY_NAME = "oicCredentials";

private static final long serialVersionUID = 1L;

private final Secret accessToken;
private final Secret idToken;
private final Secret refreshToken;
private final Long expiresAtMillis;

@Override
public UserProperty reconfigure(StaplerRequest req, JSONObject form) throws Descriptor.FormException {
req.bindJSON(this, form);
return this;

Check warning on line 28 in src/main/java/org/jenkinsci/plugins/oic/OicCredentials.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/jenkinsci/plugins/oic/OicCredentials.java#L27-L28

Added lines #L27 - L28 were not covered by tests
}

public OicCredentials(Secret accessToken, Secret idToken, Secret refreshToken, Long expiresAtMillis) {
this.accessToken = accessToken;
this.idToken = idToken;
this.refreshToken = refreshToken;
this.expiresAtMillis = expiresAtMillis;
}

Check warning on line 36 in src/main/java/org/jenkinsci/plugins/oic/OicCredentials.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 27-36 are not covered by tests

Check warning on line 36 in src/main/java/org/jenkinsci/plugins/oic/OicCredentials.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/jenkinsci/plugins/oic/OicCredentials.java#L31-L36

Added lines #L31 - L36 were not covered by tests

public OicCredentials(
String accessToken,
String idToken,
String refreshToken,
Long expiresInSeconds,
Long currentTimestamp,
Long allowedClockSkewSeconds) {
this.accessToken = Secret.fromString(accessToken);
this.idToken = Secret.fromString(idToken);
this.refreshToken = Secret.fromString(refreshToken);

if (expiresInSeconds != null && expiresInSeconds > 0) {

Check warning on line 49 in src/main/java/org/jenkinsci/plugins/oic/OicCredentials.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 49 is only partially covered, one branch is missing
long allowedClockSkewFixed = Util.fixNull(allowedClockSkewSeconds, 60L);
this.expiresAtMillis = currentTimestamp + (expiresInSeconds - allowedClockSkewFixed) * 1000;
} else {
this.expiresAtMillis = null;
}
}

public String getAccessToken() {
return Secret.toString(accessToken);
}

public String getIdToken() {
return Secret.toString(idToken);
}

public String getRefreshToken() {
return Secret.toString(refreshToken);
}

public Long getExpiresAtMillis() {
return expiresAtMillis;
}

@Extension
@Symbol(PROPERTY_NAME)
public static final class DescriptorImpl extends UserPropertyDescriptor {
@Override
public boolean isEnabled() {
return false;
}

@Override
public UserProperty newInstance(User user) {
return null;
}
}
}
Loading