Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

fix: load/save token in a task, avoid slow operation error (#958) #965

Merged
merged 1 commit into from
Feb 6, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
import com.intellij.openapi.components.Service;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import org.jboss.tools.intellij.openshift.Constants;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -48,16 +49,29 @@ private CredentialAttributes getAttributes(IAccount account, String attribute) {
}

private Element account2Node(IAccount account) {
savePasswordSafe(account);
Element node = new Element(ACCOUNT_TAG);
node.setAttribute(ID_ATTRIBUTE, account.getId());
PasswordSafe.getInstance().set(getAttributes(account, ID_TOKEN_ATTRIBUTE), new Credentials(null, account.getIDToken()));
PasswordSafe.getInstance().set(getAttributes(account, ACCESS_TOKEN_ATTRIBUTE), new Credentials(null, account.getAccessToken()));
PasswordSafe.getInstance().set(getAttributes(account, REFRESH_TOKEN_ATTRIBUTE), new Credentials(null, account.getRefreshToken()));
node.setAttribute(ACCESS_TOKEN_EXPIRES_IN_ATTRIBUTE, String.valueOf(account.getAccessTokenExpiryTime()));
node.setAttribute(REFRESH_TOKEN_EXPIRES_IN_ATTRIBUTE, String.valueOf(account.getRefreshTokenExpiryTime()));
node.setAttribute(LAST_REFRESHED_TIME_ATTRIBUTE, String.valueOf(account.getLastRefreshedTime()));
return node;
}

private void savePasswordSafe(IAccount account) {
var task = new Task.Backgroundable(null, "Saving tokens", false) {
@Override
public void run(@NotNull ProgressIndicator progressIndicator) {
var safe = PasswordSafe.getInstance();
safe.set(getAttributes(account, ID_TOKEN_ATTRIBUTE), new Credentials(null, account.getIDToken()));
safe.set(getAttributes(account, ACCESS_TOKEN_ATTRIBUTE), new Credentials(null, account.getAccessToken()));
safe.set(getAttributes(account, REFRESH_TOKEN_ATTRIBUTE), new Credentials(null, account.getRefreshToken()));
}
};

task.queue();
}

private Element server2Node(IAuthorizationServer server) {
Element node = new Element(SERVER_TAG);
node.setAttribute(ID_ATTRIBUTE, server.getId());
Expand All @@ -68,7 +82,7 @@ private Element server2Node(IAuthorizationServer server) {
}

@Override
public @Nullable Element getState() {
public Element getState() {
Element root = new Element(SERVERS_TAG);
for(IAuthorizationServer server : servers) {
root.addContent(server2Node(server));
Expand All @@ -83,9 +97,7 @@ public void loadState(@NotNull Element state) {
IAuthorizationServer server = new AuthorizationServer(serverNode.getAttributeValue(ID_ATTRIBUTE));
for(Element accountNode : serverNode.getChildren(ACCOUNT_TAG)) {
IAccount account = new Account(accountNode.getAttributeValue(ID_ATTRIBUTE), server);
account.setIDToken(PasswordSafe.getInstance().getPassword(getAttributes(account, ID_TOKEN_ATTRIBUTE)));
account.setAccessToken(PasswordSafe.getInstance().getPassword(getAttributes(account, ACCESS_TOKEN_ATTRIBUTE)));
account.setRefreshToken(PasswordSafe.getInstance().getPassword(getAttributes(account, REFRESH_TOKEN_ATTRIBUTE)));
loadPasswordSafe(account);
account.setAccessTokenExpiryTime(Long.parseLong(accountNode.getAttributeValue(ACCESS_TOKEN_EXPIRES_IN_ATTRIBUTE)));
account.setRefreshTokenExpiryTime(Long.parseLong(accountNode.getAttributeValue(REFRESH_TOKEN_EXPIRES_IN_ATTRIBUTE)));
account.setLastRefreshedTime(Long.parseLong(accountNode.getAttributeValue(LAST_REFRESHED_TIME_ATTRIBUTE)));
Expand All @@ -95,6 +107,21 @@ public void loadState(@NotNull Element state) {
}
}

private void loadPasswordSafe(IAccount account) {
var task = new Task.Backgroundable(null, "Loading tokens", false) {
@Override
public void run(@NotNull ProgressIndicator progressIndicator) {
var safe = PasswordSafe.getInstance();
account.setIDToken(safe.getPassword(getAttributes(account, ID_TOKEN_ATTRIBUTE)));
account.setAccessToken(safe.getPassword(getAttributes(account, ACCESS_TOKEN_ATTRIBUTE)));
account.setRefreshToken(safe.getPassword(getAttributes(account, REFRESH_TOKEN_ATTRIBUTE)));
}
};

task.queue();

}

public List<IAuthorizationServer> getServers() {
return servers;
}
Expand Down
Loading