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

Commit d92992d

Browse files
authored
fix: parse login command regexp and other regexp (#884)
* fix: parse login command regexp and other regexp Signed-off-by: Stephane Bouchet <[email protected]> * fix: parse login command regexp and other regexp Signed-off-by: Stephane Bouchet <[email protected]> * fix: parse login command regexp and other regexp Signed-off-by: Stephane Bouchet <[email protected]> --------- Signed-off-by: Stephane Bouchet <[email protected]>
1 parent 85ad9db commit d92992d

File tree

10 files changed

+525
-332
lines changed

10 files changed

+525
-332
lines changed

src/main/java/org/jboss/tools/intellij/openshift/telemetry/TelemetrySender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class TelemetrySender implements TelemetryHandler {
2626
private static final Pattern CLUSTER_URL_PATTERN = Pattern.compile("http(.*)/apis", Pattern.CASE_INSENSITIVE);
2727
public static final String ANONYMOUS_CLUSTER_URL = "<CLUSTER_URL>";
2828

29-
private static final Pattern TOKEN_PATTERN = Pattern.compile("token-(.*):([\\d]+)", Pattern.CASE_INSENSITIVE);
29+
private static final Pattern TOKEN_PATTERN = Pattern.compile("token-(.*):(\\d+)", Pattern.CASE_INSENSITIVE);
3030
public static final String ANONYMOUS_TOKEN = "<TOKEN>";
3131

3232
private final TelemetryMessageBuilder.ActionMessage telemetry;

src/main/java/org/jboss/tools/intellij/openshift/ui/cluster/TokenExtractor.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,25 @@
1010
******************************************************************************/
1111
package org.jboss.tools.intellij.openshift.ui.cluster;
1212

13-
import java.util.regex.Matcher;
14-
import java.util.regex.Pattern;
13+
import org.jsoup.Jsoup;
14+
import org.jsoup.nodes.Document;
1515

1616
/**
1717
* Helper class to handle Openshift token authentication response
18-
*
1918
*/
2019
public class TokenExtractor {
21-
/**
22-
* Regular expression used to check if browser page is page that displays the OAuth token
23-
*/
24-
public static final Pattern TOKEN_PAGE_PATTERN = Pattern
25-
.compile(".*<h2>Your API token is<\\/h2>.*<code>(.*)<\\/code>.*", Pattern.DOTALL);
2620

27-
private final Matcher matcher;
21+
private final Document doc;
2822

29-
public TokenExtractor(String content) {
30-
matcher = TOKEN_PAGE_PATTERN.matcher(content);
31-
}
23+
public TokenExtractor(String content) {
24+
doc = Jsoup.parse(content);
25+
}
3226

33-
public boolean isTokenPage() {
34-
return matcher.matches();
35-
}
27+
public boolean isTokenPage() {
28+
return doc.select("h2").text().contains("Your API token is");
29+
}
3630

37-
public String getToken() {
38-
return matcher.group(1);
39-
}
31+
public String getToken() {
32+
return doc.select("h2").next().select("code").text();
33+
}
4034
}

src/main/java/org/jboss/tools/intellij/openshift/ui/sandbox/CountryCodeValidator.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,24 @@
1717
import java.util.regex.Pattern;
1818

1919
public class CountryCodeValidator implements Supplier<ValidationInfo> {
20-
private final JTextComponent component;
20+
private final JTextComponent component;
2121

22-
/*
23-
* see https://github.com/codeready-toolchain/registration-service/blob/master/pkg/assets/landingpage.js
24-
*/
25-
private static final Pattern pattern = Pattern
26-
.compile("^[+]?\\d+$");
22+
private static final Pattern pattern = Pattern
23+
.compile("^[+]?\\d{1,3}$");
2724

28-
public CountryCodeValidator(JTextComponent component) {
29-
this.component = component;
30-
}
25+
public CountryCodeValidator(JTextComponent component) {
26+
this.component = component;
27+
}
3128

32-
@Override
33-
public ValidationInfo get() {
34-
String text = component.getText();
35-
if (text.isEmpty()) {
36-
return new ValidationInfo("Please provide a country code", component);
37-
}
38-
if (pattern.matcher(text).matches()) {
39-
return null;
40-
}
41-
return new ValidationInfo("Country code must be a number", component);
29+
@Override
30+
public ValidationInfo get() {
31+
String text = component.getText();
32+
if (text.isEmpty()) {
33+
return new ValidationInfo("Please provide a country code", component);
34+
}
35+
if (pattern.matcher(text).matches()) {
36+
return null;
4237
}
38+
return new ValidationInfo("Country code must be a number", component);
39+
}
4340
}

src/main/java/org/jboss/tools/intellij/openshift/ui/sandbox/PhoneNumberValidator.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,27 @@
1717
import java.util.regex.Pattern;
1818

1919
public class PhoneNumberValidator implements Supplier<ValidationInfo> {
20-
private final JTextComponent component;
20+
private final JTextComponent component;
2121

22-
/*
23-
* see https://github.com/codeready-toolchain/registration-service/blob/master/pkg/assets/landingpage.js
24-
*/
25-
private static final Pattern pattern = Pattern
26-
.compile("^[(]?[0-9]+[)]?[-\\s\\.]?[0-9]+[-\\s\\.\\/0-9]*$", Pattern.CASE_INSENSITIVE);
22+
private static final Pattern pattern = Pattern
23+
.compile("^[(]?\\d{2,3}[)]?[ \\-./]?\\d{2,3}[ \\-./]?\\d{2,4}[ \\-./]?\\d{0,4}[ \\-./]?\\d{0,4}$", Pattern.CASE_INSENSITIVE);
2724

28-
public PhoneNumberValidator(JTextComponent component) {
29-
this.component = component;
30-
}
25+
public PhoneNumberValidator(JTextComponent component) {
26+
this.component = component;
27+
}
3128

32-
@Override
33-
public ValidationInfo get() {
34-
String text = component.getText();
35-
if (text.isEmpty()) {
36-
return new ValidationInfo("Please provide a phone number", component);
37-
}
38-
if (pattern.matcher(text).matches()) {
39-
return null;
40-
}
41-
return new ValidationInfo("Phone number should be digits optionally separated by - or ' '", component);
29+
@Override
30+
public ValidationInfo get() {
31+
String text = component.getText();
32+
if (text.isEmpty()) {
33+
return new ValidationInfo("Please provide a phone number", component);
34+
}
35+
if (pattern.matcher(text).matches()) {
36+
if (text.chars().allMatch(Character::isDigit) && text.length() < 7) {
37+
return new ValidationInfo("Phone number should be at least 7 digits", component);
38+
}
39+
return null;
4240
}
41+
return new ValidationInfo("Phone number should be digits optionally separated by space or '-' or '.' or '/'", component);
42+
}
4343
}

src/main/java/org/jboss/tools/intellij/openshift/utils/OCCommandUtils.java

Lines changed: 87 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -20,107 +20,106 @@
2020
*/
2121
public final class OCCommandUtils {
2222

23-
private OCCommandUtils() {
24-
//hide constructor
25-
}
23+
private OCCommandUtils() {
24+
//hide constructor
25+
}
2626

27-
/**
28-
* Validates is oc command is in correct format with required fields.
29-
*
30-
* @param ocCommand the oc command
31-
* @return false command has not correct format
32-
*/
33-
public static boolean isValidCommand(String ocCommand) {
34-
ocCommand = ocCommand.trim();
35-
return (ocCommand.startsWith("oc login ") && isValidAuthMethod(ocCommand)
36-
&& getServer(ocCommand) != null);
37-
}
27+
/**
28+
* Validates is oc command is in correct format with required fields.
29+
*
30+
* @param ocCommand the oc command
31+
* @return false command has not correct format
32+
*/
33+
public static boolean isValidCommand(String ocCommand) {
34+
ocCommand = ocCommand.trim();
35+
return (ocCommand.startsWith("oc login ") && isValidAuthMethod(ocCommand)
36+
&& getServer(ocCommand) != null);
37+
}
3838

39-
/**
40-
* Returns true if authorization schema of oc command is valid (basic/OAuth).
41-
*
42-
* @param ocCommand the oc command
43-
* @return false command has not correct authorization schema
44-
*/
45-
public static boolean isValidAuthMethod(String ocCommand) {
46-
ocCommand = ocCommand.trim();
47-
return (ocCommand.contains(" -u") || ocCommand.contains(" --username") || ocCommand.contains("--token="));
48-
}
39+
/**
40+
* Returns true if authorization schema of oc command is valid (basic/OAuth).
41+
*
42+
* @param ocCommand the oc command
43+
* @return false command has not correct authorization schema
44+
*/
45+
private static boolean isValidAuthMethod(String ocCommand) {
46+
return (ocCommand.contains(" -u") || ocCommand.contains(" --username") || ocCommand.contains("--token="));
47+
}
4948

50-
/**
51-
* Parses server address from oc command.
52-
*
53-
* @param ocCommand the oc command
54-
* @return server address
55-
*/
56-
public static String getServer(String ocCommand) {
57-
String server = applyPattern(ocCommand, "(?<=\\s)https[a-zA-Z0-9:/.-]+", 0);
58-
if (server != null) {
59-
return server;
60-
} else {
61-
return applyPattern(ocCommand, "(?<=[\\s=])https[a-zA-Z0-9:/.-]+", 0);
62-
}
49+
/**
50+
* Parses server address from oc command.
51+
*
52+
* @param ocCommand the oc command
53+
* @return server address
54+
*/
55+
public static String getServer(String ocCommand) {
56+
String server = applyPattern(ocCommand, "(?<=\\s)https[a-zA-Z0-9:/.-]+", 0);
57+
if (server != null) {
58+
return server;
59+
} else {
60+
return applyPattern(ocCommand, "(?<=[\\s=])https[a-zA-Z0-9:/.-]+", 0);
6361
}
62+
}
6463

65-
/**
66-
* Parses token from oc command.
67-
*
68-
* @param ocCommand the oc command
69-
* @return token
70-
*/
71-
public static String getToken(String ocCommand) {
72-
return applyPattern(ocCommand, "(?<=--token=)[~a-zA-Z0-9:._-]+", 0);
73-
}
64+
/**
65+
* Parses token from oc command.
66+
*
67+
* @param ocCommand the oc command
68+
* @return token
69+
*/
70+
public static String getToken(String ocCommand) {
71+
return applyPattern(ocCommand, "(?<=--token=)[~a-zA-Z0-9:._-]+", 0);
72+
}
7473

75-
/**
76-
* Parses username from oc command.
77-
*
78-
* @param ocCommand the oc command
79-
* @return username
80-
*/
81-
public static String getUsername(String ocCommand) {
82-
String username = applyPattern(ocCommand, "(?<=-u[\\s=])[a-zA-Z0-9:]+", 0);
83-
if (username != null) {
84-
return username;
85-
} else {
86-
return applyPattern(ocCommand, "(?<=--username[\\s=])[a-zA-Z0-9:]+", 0);
87-
}
74+
/**
75+
* Parses username from oc command.
76+
*
77+
* @param ocCommand the oc command
78+
* @return username
79+
*/
80+
public static String getUsername(String ocCommand) {
81+
String username = applyPattern(ocCommand, "(?<=-u[\\s=])[a-zA-Z0-9:]+", 0);
82+
if (username != null) {
83+
return username;
84+
} else {
85+
return applyPattern(ocCommand, "(?<=--username[\\s=])[a-zA-Z0-9:]+", 0);
8886
}
87+
}
8988

90-
/**
91-
* Parses password from oc command.
92-
*
93-
* @param ocCommand the oc command
94-
* @return password
95-
*/
96-
public static String getPassword(String ocCommand) {
97-
String password = searchInStringForPattern(ocCommand, "(?<=-p[\\s=])(.*)(?=\\b)");
98-
if (password != null) {
99-
return password;
100-
} else {
101-
return searchInStringForPattern(ocCommand, "(?<=--password[\\s=])(.*)(?=\\b)");
102-
}
89+
/**
90+
* Parses password from oc command.
91+
*
92+
* @param ocCommand the oc command
93+
* @return password
94+
*/
95+
public static String getPassword(String ocCommand) {
96+
String password = searchInStringForPattern(ocCommand, "(?<=-p[\\s=])([^\\s ]*)(?=\\b)");
97+
if (password != null) {
98+
return password;
99+
} else {
100+
return searchInStringForPattern(ocCommand, "(?<=--password[\\s=])([^\\s ]*)(?=\\b)");
103101
}
102+
}
104103

105-
private static String searchInStringForPattern(String stringToVerify, String pattern) {
106-
if (stringToVerify.contains("-p")) {
107-
return applyPattern(stringToVerify, pattern);
108-
}
109-
return null;
104+
private static String searchInStringForPattern(String stringToVerify, String pattern) {
105+
if (stringToVerify.contains("-p")) {
106+
return applyPattern(stringToVerify, pattern);
110107
}
108+
return null;
109+
}
111110

112-
private static String applyPattern(String stringToVerify, String pattern) {
113-
return applyPattern(stringToVerify, pattern, 1);
114-
}
111+
private static String applyPattern(String stringToVerify, String pattern) {
112+
return applyPattern(stringToVerify, pattern, 1);
113+
}
115114

116-
private static String applyPattern(String stringToVerify, String pattern, int group) {
117-
stringToVerify = stringToVerify.trim();
118-
Pattern patternToken = Pattern.compile(pattern);
119-
java.util.regex.Matcher matcherToken = patternToken.matcher(stringToVerify);
120-
if (matcherToken.find()) {
121-
return matcherToken.group(group);
122-
}
123-
return null;
115+
private static String applyPattern(String stringToVerify, String pattern, int group) {
116+
stringToVerify = stringToVerify.trim();
117+
Pattern patternToken = Pattern.compile(pattern);
118+
java.util.regex.Matcher matcherToken = patternToken.matcher(stringToVerify);
119+
if (matcherToken.find()) {
120+
return matcherToken.group(group);
124121
}
122+
return null;
123+
}
125124

126125
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Red Hat, Inc.
3+
* Distributed under license by Red Hat, Inc. All rights reserved.
4+
* This program is made available under the terms of the
5+
* Eclipse Public License v2.0 which accompanies this distribution,
6+
* and is available at http://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* Contributors:
9+
* Red Hat, Inc. - initial API and implementation
10+
******************************************************************************/
11+
package org.jboss.tools.intellij.openshift.ui.cluster;
12+
13+
14+
import org.junit.Test;
15+
16+
import static org.junit.Assert.assertEquals;
17+
import static org.junit.Assert.assertFalse;
18+
import static org.junit.Assert.assertTrue;
19+
20+
public class TokenExtractorTest {
21+
22+
@Test
23+
public void checkTokenExtractionOk() {
24+
String contents = "<html><head></head><body><h2>Your API token is</h2>\n <code>sha256~abcd-1234567890ABCDEF</code>\n</body></html>";
25+
TokenExtractor extractor = new TokenExtractor(contents);
26+
assertTrue(extractor.isTokenPage());
27+
assertEquals("sha256~abcd-1234567890ABCDEF", extractor.getToken());
28+
}
29+
30+
@Test
31+
public void checkTokenExtractionEmpty() {
32+
String contents = "<html><head></head><body><h2>Your API token is</h2>\n <code></code>\n</body></html>";
33+
TokenExtractor extractor = new TokenExtractor(contents);
34+
assertTrue(extractor.isTokenPage());
35+
assertEquals("", extractor.getToken());
36+
}
37+
38+
@Test
39+
public void checkTokenExtractionFails() {
40+
String contents = "<html><head></head><body></body></html>";
41+
TokenExtractor extractor = new TokenExtractor(contents);
42+
assertFalse(extractor.isTokenPage());
43+
}
44+
45+
@Test
46+
public void checkTokenExtractionWithMultipleH2() {
47+
String contents = "<html><head></head><body><h2>Your API token is</h2>\n <code>sha256~abcd-1234567890ABCDEF</code>\n<h2>Log in with this token</h2>\n<pre>oc login <span class=\"nowrap\">--token=sha256~abcd-1234567890ABCDEF</span> <span class=\"nowrap\">--server=https://url.com:1234</span></pre></body></html>";
48+
TokenExtractor extractor = new TokenExtractor(contents);
49+
assertTrue(extractor.isTokenPage());
50+
assertEquals("sha256~abcd-1234567890ABCDEF", extractor.getToken());
51+
}
52+
}

0 commit comments

Comments
 (0)