36 lines
1.3 KiB
Java
36 lines
1.3 KiB
Java
package vn.sis.appointment;
|
|
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
final class TokenJsonParser {
|
|
private static final Pattern ACCESS_TOKEN = Pattern.compile(
|
|
"\\\"access_token\\\"\\s*:\\s*\\\"([^\\\"]+)\\\"");
|
|
private static final Pattern TOKEN_TYPE = Pattern.compile(
|
|
"\\\"token_type\\\"\\s*:\\s*\\\"([^\\\"]*)\\\"");
|
|
private static final Pattern EXPIRES_IN = Pattern.compile(
|
|
"\\\"expires_in\\\"\\s*:\\s*(\\d+)");
|
|
|
|
private TokenJsonParser() {
|
|
}
|
|
|
|
static ParsedToken parse(String json) {
|
|
String accessToken = group(ACCESS_TOKEN, json);
|
|
if (accessToken == null || accessToken.isBlank()) {
|
|
throw new IllegalArgumentException("API token khong tra ve access_token hop le.");
|
|
}
|
|
String tokenType = group(TOKEN_TYPE, json);
|
|
String expiresText = group(EXPIRES_IN, json);
|
|
long expiresIn = expiresText == null ? 3600 : Long.parseLong(expiresText);
|
|
return new ParsedToken(accessToken, tokenType == null ? "Bearer" : tokenType, expiresIn);
|
|
}
|
|
|
|
private static String group(Pattern pattern, String text) {
|
|
Matcher matcher = pattern.matcher(text == null ? "" : text);
|
|
return matcher.find() ? matcher.group(1) : null;
|
|
}
|
|
|
|
record ParsedToken(String accessToken, String tokenType, long expiresIn) {
|
|
}
|
|
}
|