Skip to content

Commit 0d73f47

Browse files
rawagnergeetikabatra
authored andcommitted
Revamped login workflow
Signed-off-by: Rastislav Wagner <rawagner@redhat.com>
1 parent fbe4d94 commit 0d73f47

22 files changed

Lines changed: 537 additions & 340 deletions

File tree

plugins/com.redhat.fabric8analytics.lsp.eclipse.core/META-INF/MANIFEST.MF

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ Require-Bundle: org.eclipse.lsp4e;bundle-version="0.3.0",
1616
org.apache.commons.httpclient;bundle-version="3.1.0",
1717
org.eclipse.equinox.security,
1818
org.eclipse.m2e.core,
19-
org.eclipse.m2e.maven.runtime
19+
org.eclipse.m2e.maven.runtime,
20+
org.jboss.tools.openshift.io.core
2021
Eclipse-BundleShape: dir
2122
Bundle-Activator: com.redhat.fabric8analytics.lsp.eclipse.core.Fabric8AnalysisLSCoreActivator
2223
Bundle-ActivationPolicy: lazy
2324
Bundle-ClassPath: .
2425
Export-Package: com.redhat.fabric8analytics.lsp.eclipse.core,
25-
com.redhat.fabric8analytics.lsp.eclipse.core.job
26+
com.redhat.fabric8analytics.lsp.eclipse.core.job,
27+
com.redhat.fabric8analytics.lsp.eclipse.core.data,
28+
com.redhat.fabric8analytics.lsp.eclipse.core.internal;x-friends:=com.redhat.fabric8analytics.lsp.eclipse.ui
2629

2730

plugins/com.redhat.fabric8analytics.lsp.eclipse.core/src/com/redhat/fabric8analytics/lsp/eclipse/core/Fabric8AnalysisPreferences.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
1616
import org.eclipse.equinox.security.storage.StorageException;
1717

18+
import com.redhat.fabric8analytics.lsp.eclipse.core.data.ThreeScaleData;
19+
1820
public class Fabric8AnalysisPreferences {
1921

2022
public static final String LSP_SERVER_ENABLED = "fabric8AnalysisPreferences.LSP_ENABLED";
2123
public static final String PROD_URL = "fabric8AnalysisPreferences.PROD_URL";
2224
public static final String STAGE_URL = "fabric8AnalysisPreferences.STAGE_URL";
2325
public static final String USER_KEY = "fabric8AnalysisPreferences.USER_KEY";
24-
public static final String TOKEN = "fabric8AnalysisPreferences.TOKEN";
2526

2627
private ISecurePreferences secureNode;
2728
private static Fabric8AnalysisPreferences instance;
@@ -70,12 +71,11 @@ public void setUserKey(String userKey) throws StorageException {
7071
secureNode.put(USER_KEY, userKey, true);
7172
}
7273

73-
public String getToken() throws StorageException {
74-
return secureNode.get(TOKEN, null);
75-
}
76-
77-
public void setToken(String token) throws StorageException {
78-
secureNode.put(TOKEN, token, true);
74+
public ThreeScaleData getThreeScaleData() throws StorageException {
75+
if(getProdURL() == null || getStageURL() == null || getUserKey() == null) {
76+
return null;
77+
}
78+
return new ThreeScaleData(getProdURL(), getStageURL(), getUserKey());
7979
}
8080

8181
}

plugins/com.redhat.fabric8analytics.lsp.eclipse.core/src/com/redhat/fabric8analytics/lsp/eclipse/core/RecommenderAPIProvider.java

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.json.JSONException;
2828
import org.json.JSONObject;
2929

30+
import com.redhat.fabric8analytics.lsp.eclipse.core.data.AnalyticsAuthData;
3031
import com.redhat.fabric8analytics.lsp.eclipse.core.internal.PomContentBody;
3132

3233
/**
@@ -37,31 +38,25 @@
3738
*/
3839
public class RecommenderAPIProvider {
3940

40-
public String recommender_api_base_url;
41-
4241
private static final String RECOMMENDER_API_URL_POSTFIX = "/api/v1";
4342

44-
private String userScaleKey;
43+
private static final String RECOMMENDER_API_URL_STACK_ANALYSES_POSTFIX = RECOMMENDER_API_URL_POSTFIX
44+
+ "/stack-analyses/";
4545

46-
private static final String RECOMMENDER_API_URL_STACK_ANALYSES_POSTFIX = RECOMMENDER_API_URL_POSTFIX + "/stack-analyses/";
47-
48-
private static final String RECOMMENDER_API_URL_POLL_ANALYSES_POSTFIX = RECOMMENDER_API_URL_POSTFIX + "/stack-analyses/";
46+
private static final String RECOMMENDER_API_URL_POLL_ANALYSES_POSTFIX = RECOMMENDER_API_URL_POSTFIX
47+
+ "/stack-analyses/";
4948

5049
private static final String ANALYSES_REPORT_URL = "https://stack-analytics-report.openshift.io/#/analyze/";
5150

5251
private static final String POST_ANALYSES_REPORT_URL = "?api_data={\"access_token\":\"%s\",\"route_config\":{\"api_url\":\"%s\"},\"user_key\":\"%s\"}";
5352

54-
private String url;
55-
5653
private static final String SERVICE_ID = "2555417754949";
5754

58-
private String token;
55+
private AnalyticsAuthData analyticsAuthData;
5956

60-
public RecommenderAPIProvider(String url, String userScaleKey, String token) {
61-
checkConstructorArguments(url, userScaleKey, token);
62-
this.recommender_api_base_url = url;
63-
this.token = token;
64-
this.userScaleKey = userScaleKey;
57+
public RecommenderAPIProvider(AnalyticsAuthData analyticsAuthData) {
58+
checkAuthData(analyticsAuthData);
59+
this.analyticsAuthData = analyticsAuthData;
6560
}
6661

6762
/**
@@ -73,8 +68,10 @@ public RecommenderAPIProvider(String url, String userScaleKey, String token) {
7368
public String requestAnalyses(Map<String, String> files) throws RecommenderAPIException {
7469

7570
checkFiles(files);// check if this is none
76-
HttpPost post = new HttpPost(recommender_api_base_url + RECOMMENDER_API_URL_STACK_ANALYSES_POSTFIX + String.format("?user_key=%s",userScaleKey));
77-
post.addHeader("Authorization", token);
71+
HttpPost post = new HttpPost(
72+
analyticsAuthData.getThreeScaleData().getProd() + RECOMMENDER_API_URL_STACK_ANALYSES_POSTFIX
73+
+ String.format("?user_key=%s", analyticsAuthData.getThreeScaleData().getUserKey()));
74+
post.addHeader("Authorization", analyticsAuthData.getToken());
7875

7976
MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
8077

@@ -111,13 +108,12 @@ public String requestAnalyses(Map<String, String> files) throws RecommenderAPIEx
111108
public boolean analysesFinished(String jobId) throws RecommenderAPIException {
112109
checkJobID(jobId);
113110
String RECOMMENDER_API_TOKEN = "Bearer ";
114-
if (!RECOMMENDER_API_TOKEN.equals("Bearer " + token)) {
115-
RECOMMENDER_API_TOKEN = "Bearer " + token;
111+
if (!RECOMMENDER_API_TOKEN.equals("Bearer " + analyticsAuthData.getToken())) {
112+
RECOMMENDER_API_TOKEN = "Bearer " + analyticsAuthData.getToken();
116113
}
117-
// HttpGet get = new HttpGet("https://recommender.api.openshift.io/api/v1/stack-analyses/" + jobId);
118-
HttpGet get = new HttpGet(recommender_api_base_url +
119-
RECOMMENDER_API_URL_POLL_ANALYSES_POSTFIX + jobId +
120-
String.format("?user_key=%s", userScaleKey));
114+
HttpGet get = new HttpGet(
115+
analyticsAuthData.getThreeScaleData().getProd() + RECOMMENDER_API_URL_POLL_ANALYSES_POSTFIX + jobId
116+
+ String.format("?user_key=%s", analyticsAuthData.getThreeScaleData().getUserKey()));
121117
get.addHeader("Authorization", RECOMMENDER_API_TOKEN);
122118
CloseableHttpClient client = createClient();
123119

@@ -149,14 +145,8 @@ public boolean analysesFinished(String jobId) throws RecommenderAPIException {
149145
}
150146

151147
public String getAnalysesURL(String jobID) {
152-
// to be used once user key is enabled in analyses url
153-
// String postURLFormat = String.format(POST_ANALYSES_REPORT_URL, token,
154-
// SERVER_URL, USER_KEY);
155-
String temp_server_url = "https://recommender.api.openshift.io/";
156-
157-
// String postURLFormat = String.format(POST_ANALYSES_REPORT_URL, token,
158-
// recommender_api_base_url, userScaleKey);
159-
String postURLFormat = String.format(POST_ANALYSES_REPORT_URL, token, temp_server_url, userScaleKey);
148+
String postURLFormat = String.format(POST_ANALYSES_REPORT_URL, analyticsAuthData.getToken(),
149+
analyticsAuthData.getThreeScaleData().getProd(), analyticsAuthData.getThreeScaleData().getUserKey());
160150

161151
String url = ANALYSES_REPORT_URL + jobID + postURLFormat;
162152
return url;
@@ -166,24 +156,24 @@ protected CloseableHttpClient createClient() {
166156
return HttpClients.createDefault();
167157
}
168158

169-
private void checkConstructorArguments(String url2, String userKey2, String token2) {
170-
if (url2 == null) {
159+
private void checkAuthData(AnalyticsAuthData authData) {
160+
if (authData.getThreeScaleData().getProd() == null) {
171161
throw new IllegalArgumentException("The URL was null");
172162
}
173163

174-
if ("".equals(url2)) {
164+
if (authData.getThreeScaleData().getProd().isEmpty()) {
175165
throw new IllegalArgumentException("The URL was empty");
176166
}
177167

178-
if (userKey2 == null) {
168+
if (authData.getThreeScaleData().getUserKey() == null) {
179169
throw new IllegalArgumentException("The user key was null");
180170
}
181171

182-
if (token2 == null) {
172+
if (authData.getToken() == null) {
183173
throw new IllegalArgumentException("The token was null");
184174
}
185175
}
186-
176+
187177
private void checkFiles(Map<String, String> files) {
188178
if (files == null) {
189179
throw new IllegalArgumentException("Files for analyses were null");
@@ -194,10 +184,6 @@ private void checkFiles(Map<String, String> files) {
194184
}
195185
}
196186

197-
public void setUserKey(String userKey) {
198-
userScaleKey = userKey;
199-
}
200-
201187
private void checkJobID(String jobId) {
202188
if (jobId == null) {
203189
throw new IllegalArgumentException("Job ID was null");

plugins/com.redhat.fabric8analytics.lsp.eclipse.core/src/com/redhat/fabric8analytics/lsp/eclipse/core/ThreeScaleAPIProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.json.JSONException;
2626
import org.json.JSONObject;
2727

28+
import com.redhat.fabric8analytics.lsp.eclipse.core.data.ThreeScaleData;
29+
2830
/**
2931
* Provides access to 3scale service
3032
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 Red Hat Inc..
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Red Hat Incorporated - initial API and implementation
10+
*******************************************************************************/
11+
package com.redhat.fabric8analytics.lsp.eclipse.core.data;
12+
13+
public class AnalyticsAuthData {
14+
15+
private ThreeScaleData threeScaleData;
16+
17+
private String token;
18+
19+
public AnalyticsAuthData() {
20+
// empty constructor
21+
}
22+
23+
public AnalyticsAuthData(ThreeScaleData threeScaleData, String token) {
24+
this.threeScaleData = threeScaleData;
25+
this.token = token;
26+
}
27+
28+
public ThreeScaleData getThreeScaleData() {
29+
return threeScaleData;
30+
}
31+
32+
public void setThreeScaleData(ThreeScaleData threeScaleData) {
33+
this.threeScaleData = threeScaleData;
34+
}
35+
36+
public String getToken() {
37+
return token;
38+
}
39+
40+
public void setToken(String token) {
41+
this.token = token;
42+
}
43+
44+
}

plugins/com.redhat.fabric8analytics.lsp.eclipse.core/src/com/redhat/fabric8analytics/lsp/eclipse/core/ThreeScaleData.java renamed to plugins/com.redhat.fabric8analytics.lsp.eclipse.core/src/com/redhat/fabric8analytics/lsp/eclipse/core/data/ThreeScaleData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.redhat.fabric8analytics.lsp.eclipse.core;
1+
package com.redhat.fabric8analytics.lsp.eclipse.core.data;
22

33
/**
44
* Stores 3scale data.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2017 Red Hat Inc..
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Red Hat Incorporated - initial API and implementation
10+
*******************************************************************************/
11+
12+
package com.redhat.fabric8analytics.lsp.eclipse.core.internal;
13+
14+
import java.util.List;
15+
16+
import org.eclipse.core.runtime.IProgressMonitor;
17+
import org.eclipse.core.runtime.NullProgressMonitor;
18+
import org.eclipse.core.runtime.SubMonitor;
19+
import org.eclipse.equinox.security.storage.StorageException;
20+
import org.jboss.tools.openshift.io.core.AccountService;
21+
import org.jboss.tools.openshift.io.core.AccountStatus;
22+
import org.jboss.tools.openshift.io.core.model.IAccount;
23+
24+
import com.redhat.fabric8analytics.lsp.eclipse.core.Fabric8AnalysisLSCoreActivator;
25+
import com.redhat.fabric8analytics.lsp.eclipse.core.Fabric8AnalysisPreferences;
26+
import com.redhat.fabric8analytics.lsp.eclipse.core.ThreeScaleAPIException;
27+
import com.redhat.fabric8analytics.lsp.eclipse.core.ThreeScaleAPIProvider;
28+
import com.redhat.fabric8analytics.lsp.eclipse.core.data.AnalyticsAuthData;
29+
import com.redhat.fabric8analytics.lsp.eclipse.core.data.ThreeScaleData;
30+
31+
public class AnalyticsAuthService {
32+
33+
private static final AnalyticsAuthService INSTANCE = new AnalyticsAuthService();
34+
35+
public static AnalyticsAuthService getInstance() {
36+
return INSTANCE;
37+
}
38+
39+
private AnalyticsAuthService() {
40+
}
41+
42+
/**
43+
* Returns osio account
44+
* @return osio account or null if there is none
45+
*/
46+
public IAccount getAccount() {
47+
AccountService service = AccountService.getDefault();
48+
List<IAccount> accounts = service.getModel().getClusters().get(0).getAccounts();
49+
if(accounts == null || accounts.isEmpty()) {
50+
return null;
51+
}
52+
return accounts.get(0);
53+
}
54+
55+
/**
56+
* Returns auth data - token and 3scale data.
57+
* Auth token is refreshed if needed.
58+
* If user was not logged before, null is returned.
59+
*
60+
* @return OSIO data (token and 3scale data)
61+
* @throws ThreeScaleAPIException
62+
*/
63+
public AnalyticsAuthData getAnalyticsAuthData(IProgressMonitor progressMonitor) throws StorageException{
64+
SubMonitor monitor = getSubMonitor(progressMonitor);
65+
monitor.setWorkRemaining(2);
66+
monitor.setTaskName("Check Openshift.io accout status");
67+
IAccount account = getAccount();
68+
if(account == null) {
69+
return null;
70+
}
71+
AccountStatus accountStatus = AccountService.getDefault().getStatus(account);
72+
if (accountStatus == AccountStatus.NEEDS_REFRESH) {
73+
return login(progressMonitor);
74+
}
75+
String token = account.getAccessToken();
76+
ThreeScaleData threeScaleData = Fabric8AnalysisPreferences.getInstance().getThreeScaleData();
77+
if(threeScaleData == null) {
78+
try {
79+
threeScaleData = registerThreeScale(monitor, token);
80+
} catch (ThreeScaleAPIException e) {
81+
Fabric8AnalysisLSCoreActivator.getDefault().logError("Unable to get data from 3scale", e);
82+
return null;
83+
}
84+
}
85+
return new AnalyticsAuthData(threeScaleData, token);
86+
87+
}
88+
89+
/**
90+
* Login to OSIO. If anything goes wrong during login process, null is returned
91+
*
92+
* @param progressMonitor progressMonitor to report login progress.
93+
* @return OSIO data (token and 3scale data) or null if login process fails
94+
*/
95+
public AnalyticsAuthData login(IProgressMonitor progressMonitor) throws StorageException{
96+
SubMonitor monitor = getSubMonitor(progressMonitor);
97+
monitor.setWorkRemaining(2);
98+
String token = AccountService.getDefault().getToken(null);
99+
ThreeScaleData threeScaleData = null;
100+
if (token != null) {
101+
try {
102+
threeScaleData = registerThreeScale(monitor, token);
103+
} catch (ThreeScaleAPIException e) {
104+
Fabric8AnalysisLSCoreActivator.getDefault().logError("Unable to get data from 3scale", e);
105+
return null;
106+
}
107+
}
108+
AnalyticsAuthData analyticsAuthData = new AnalyticsAuthData(threeScaleData, token);
109+
return analyticsAuthData;
110+
}
111+
112+
private ThreeScaleData registerThreeScale(SubMonitor monitor, String token) throws ThreeScaleAPIException, StorageException {
113+
monitor.setWorkRemaining(1);
114+
monitor.setTaskName("Get ThreeScale data");
115+
Fabric8AnalysisPreferences preferences = Fabric8AnalysisPreferences.getInstance();
116+
ThreeScaleData threeScaleData = new ThreeScaleAPIProvider(token).register3Scale();
117+
preferences.setProdURL(threeScaleData.getProd());
118+
preferences.setStageURL(threeScaleData.getStage());
119+
preferences.setUserKey(threeScaleData.getUserKey());
120+
return threeScaleData;
121+
}
122+
123+
private SubMonitor getSubMonitor(IProgressMonitor progressMonitor) {
124+
if(progressMonitor == null) {
125+
progressMonitor = new NullProgressMonitor();
126+
}
127+
return SubMonitor.convert(progressMonitor);
128+
}
129+
}

plugins/com.redhat.fabric8analytics.lsp.eclipse.ui/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Require-Bundle: org.eclipse.lsp4e;bundle-version="0.3.0",
2020
org.eclipse.ui.forms,
2121
org.eclipse.ui.ide,
2222
org.eclipse.m2e.maven.runtime,
23+
org.jboss.tools.openshift.io.core,
2324
org.eclipse.equinox.security
2425
Eclipse-BundleShape: dir
2526
Bundle-Activator: com.redhat.fabric8analytics.lsp.eclipse.ui.Fabric8AnalysisLSUIActivator

0 commit comments

Comments
 (0)