17
17
import com .intellij .openapi .components .Service ;
18
18
import com .intellij .openapi .components .State ;
19
19
import com .intellij .openapi .components .Storage ;
20
+ import com .intellij .openapi .progress .ProgressIndicator ;
21
+ import com .intellij .openapi .progress .Task ;
20
22
import org .jboss .tools .intellij .openshift .Constants ;
21
23
import org .jdom .Element ;
22
24
import org .jetbrains .annotations .NotNull ;
23
- import org .jetbrains .annotations .Nullable ;
24
25
25
26
import java .util .ArrayList ;
26
27
import java .util .List ;
@@ -48,16 +49,29 @@ private CredentialAttributes getAttributes(IAccount account, String attribute) {
48
49
}
49
50
50
51
private Element account2Node (IAccount account ) {
52
+ savePasswordSafe (account );
51
53
Element node = new Element (ACCOUNT_TAG );
52
54
node .setAttribute (ID_ATTRIBUTE , account .getId ());
53
- PasswordSafe .getInstance ().set (getAttributes (account , ID_TOKEN_ATTRIBUTE ), new Credentials (null , account .getIDToken ()));
54
- PasswordSafe .getInstance ().set (getAttributes (account , ACCESS_TOKEN_ATTRIBUTE ), new Credentials (null , account .getAccessToken ()));
55
- PasswordSafe .getInstance ().set (getAttributes (account , REFRESH_TOKEN_ATTRIBUTE ), new Credentials (null , account .getRefreshToken ()));
56
55
node .setAttribute (ACCESS_TOKEN_EXPIRES_IN_ATTRIBUTE , String .valueOf (account .getAccessTokenExpiryTime ()));
57
56
node .setAttribute (REFRESH_TOKEN_EXPIRES_IN_ATTRIBUTE , String .valueOf (account .getRefreshTokenExpiryTime ()));
58
57
node .setAttribute (LAST_REFRESHED_TIME_ATTRIBUTE , String .valueOf (account .getLastRefreshedTime ()));
59
58
return node ;
60
59
}
60
+
61
+ private void savePasswordSafe (IAccount account ) {
62
+ var task = new Task .Backgroundable (null , "Saving tokens" , false ) {
63
+ @ Override
64
+ public void run (@ NotNull ProgressIndicator progressIndicator ) {
65
+ var safe = PasswordSafe .getInstance ();
66
+ safe .set (getAttributes (account , ID_TOKEN_ATTRIBUTE ), new Credentials (null , account .getIDToken ()));
67
+ safe .set (getAttributes (account , ACCESS_TOKEN_ATTRIBUTE ), new Credentials (null , account .getAccessToken ()));
68
+ safe .set (getAttributes (account , REFRESH_TOKEN_ATTRIBUTE ), new Credentials (null , account .getRefreshToken ()));
69
+ }
70
+ };
71
+
72
+ task .queue ();
73
+ }
74
+
61
75
private Element server2Node (IAuthorizationServer server ) {
62
76
Element node = new Element (SERVER_TAG );
63
77
node .setAttribute (ID_ATTRIBUTE , server .getId ());
@@ -68,7 +82,7 @@ private Element server2Node(IAuthorizationServer server) {
68
82
}
69
83
70
84
@ Override
71
- public @ Nullable Element getState () {
85
+ public Element getState () {
72
86
Element root = new Element (SERVERS_TAG );
73
87
for (IAuthorizationServer server : servers ) {
74
88
root .addContent (server2Node (server ));
@@ -83,9 +97,7 @@ public void loadState(@NotNull Element state) {
83
97
IAuthorizationServer server = new AuthorizationServer (serverNode .getAttributeValue (ID_ATTRIBUTE ));
84
98
for (Element accountNode : serverNode .getChildren (ACCOUNT_TAG )) {
85
99
IAccount account = new Account (accountNode .getAttributeValue (ID_ATTRIBUTE ), server );
86
- account .setIDToken (PasswordSafe .getInstance ().getPassword (getAttributes (account , ID_TOKEN_ATTRIBUTE )));
87
- account .setAccessToken (PasswordSafe .getInstance ().getPassword (getAttributes (account , ACCESS_TOKEN_ATTRIBUTE )));
88
- account .setRefreshToken (PasswordSafe .getInstance ().getPassword (getAttributes (account , REFRESH_TOKEN_ATTRIBUTE )));
100
+ loadPasswordSafe (account );
89
101
account .setAccessTokenExpiryTime (Long .parseLong (accountNode .getAttributeValue (ACCESS_TOKEN_EXPIRES_IN_ATTRIBUTE )));
90
102
account .setRefreshTokenExpiryTime (Long .parseLong (accountNode .getAttributeValue (REFRESH_TOKEN_EXPIRES_IN_ATTRIBUTE )));
91
103
account .setLastRefreshedTime (Long .parseLong (accountNode .getAttributeValue (LAST_REFRESHED_TIME_ATTRIBUTE )));
@@ -95,6 +107,21 @@ public void loadState(@NotNull Element state) {
95
107
}
96
108
}
97
109
110
+ private void loadPasswordSafe (IAccount account ) {
111
+ var task = new Task .Backgroundable (null , "Loading tokens" , false ) {
112
+ @ Override
113
+ public void run (@ NotNull ProgressIndicator progressIndicator ) {
114
+ var safe = PasswordSafe .getInstance ();
115
+ account .setIDToken (safe .getPassword (getAttributes (account , ID_TOKEN_ATTRIBUTE )));
116
+ account .setAccessToken (safe .getPassword (getAttributes (account , ACCESS_TOKEN_ATTRIBUTE )));
117
+ account .setRefreshToken (safe .getPassword (getAttributes (account , REFRESH_TOKEN_ATTRIBUTE )));
118
+ }
119
+ };
120
+
121
+ task .queue ();
122
+
123
+ }
124
+
98
125
public List <IAuthorizationServer > getServers () {
99
126
return servers ;
100
127
}
0 commit comments