-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #218 from /issues/217-useragent
Fix #217: Add User-Agent header parser
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
http-common/src/main/java/com/wultra/core/http/common/headers/UserAgent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright 2023 Wultra s.r.o. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.wultra.core.http.common.headers; | ||
|
||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Utility class for processing our standard user agent strings. | ||
* | ||
* @author Petr Dvorak, [email protected] | ||
*/ | ||
@Slf4j | ||
public final class UserAgent { | ||
|
||
@Data | ||
public static class Device { | ||
private String networkVersion; | ||
private String language; | ||
private String connection; | ||
private String product; | ||
private String version; | ||
private String platform; | ||
private String os; | ||
private String osVersion; | ||
private String model; | ||
} | ||
|
||
private UserAgent() { | ||
} | ||
|
||
private static final Pattern patternPrefix = Pattern.compile("^PowerAuthNetworking/(?<networkVersion>[0-9]+\\.[0-9]+\\.[0-9]+).*"); | ||
private static final Pattern patternV1 = Pattern.compile("^PowerAuthNetworking/(?<networkVersion>[0-9]+\\.[0-9]+\\.[0-9]+) " + | ||
"\\((?<language>[a-zA-Z]{2}); (?<connection>[a-zA-Z0-9]+)\\) " + | ||
"(?<product>[a-zA-Z0-9-_.]+)/(?<version>[0-9.]+) .*" + | ||
"\\((?<platform>[^;]+); (?<os>[^/]+)/(?<osVersion>[^;]+); (?<model>[^)]+)\\)$"); | ||
|
||
public static Device parse(String userAgent) { | ||
// Identify if the user agent is ours and in what version | ||
logger.debug("Parsing user agent value: {}", userAgent); | ||
final Matcher matcherPrefix = patternPrefix.matcher(userAgent); | ||
if (!matcherPrefix.matches()) { | ||
return null; | ||
} | ||
final String networkVersion = matcherPrefix.group("networkVersion"); | ||
logger.debug("Declared networkVersion: {}", networkVersion); | ||
if (!networkVersion.startsWith("1.")) { // simplistic matching for current v1.x clients | ||
return null; | ||
} | ||
|
||
// Parse the device object | ||
return parseUserAgentV1(userAgent); | ||
|
||
} | ||
|
||
/** | ||
* Private method for parsing client user from the v1.x mobile clients. It is added for convenience | ||
* when new versions with another formats will be eventually introduced. | ||
* | ||
* @param userAgent User-Agent Header String | ||
* @return Parsed device info, or null if the user agent header cannot be parsed. | ||
*/ | ||
private static Device parseUserAgentV1(String userAgent) { | ||
final Matcher matcher = patternV1.matcher(userAgent); | ||
if (matcher.matches()) { | ||
final Device device = new Device(); | ||
device.setNetworkVersion(matcher.group("networkVersion")); | ||
device.setLanguage(matcher.group("language")); | ||
device.setConnection(matcher.group("connection")); | ||
device.setProduct(matcher.group("product")); | ||
device.setVersion(matcher.group("version")); | ||
device.setPlatform(matcher.group("platform")); | ||
device.setOs(matcher.group("os")); | ||
device.setOsVersion(matcher.group("osVersion")); | ||
device.setModel(matcher.group("model")); | ||
return device; | ||
} | ||
logger.debug("The user agent value does not match v1 client format"); | ||
return null; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
http-common/src/test/java/com/wultra/core/http/common/headers/UserAgentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2023 Wultra s.r.o. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.wultra.core.http.common.headers; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
/** | ||
* Test for the user agent parser. | ||
* | ||
* @author Petr Dvorak, [email protected] | ||
*/ | ||
class UserAgentTest { | ||
|
||
@Test | ||
void parse() { | ||
final String sample = "PowerAuthNetworking/1.1.7 (en; cellular) com.wultra.app.Mobile-Token.wultra_test/2.0.0 (Apple; iOS/16.6.1; iphone12,3)"; | ||
final UserAgent.Device device = UserAgent.parse(sample); | ||
assertNotNull(device); | ||
assertEquals("1.1.7", device.getNetworkVersion()); | ||
assertEquals("en", device.getLanguage()); | ||
assertEquals("cellular", device.getConnection()); | ||
assertEquals("com.wultra.app.Mobile-Token.wultra_test", device.getProduct()); | ||
assertEquals("2.0.0", device.getVersion()); | ||
assertEquals("Apple", device.getPlatform()); | ||
assertEquals("iOS", device.getOs()); | ||
assertEquals("16.6.1", device.getOsVersion()); | ||
assertEquals("iphone12,3", device.getModel()); | ||
} | ||
} |