Skip to content

Commit

Permalink
Improve token filter performance
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed Jan 20, 2025
1 parent 4278578 commit ef566e9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Objects;

@Extension("token")
public class TokenAuthenticate implements Authenticate {
Expand All @@ -35,9 +36,7 @@ public class TokenAuthenticate implements Authenticate {
public AuthResult authenticate(ServiceRequest request, AuthPolicy policy) {
TokenPolicy tokenPolicy = policy.getTokenPolicy();
if (tokenPolicy != null && tokenPolicy.isValid()) {
String key = tokenPolicy.getKey();
String token = tokenPolicy.getToken();
return new AuthResult(token.equals(decode(request, key)), "Token is not correct.");
return new AuthResult(decodeAndCompare(request, tokenPolicy), "Token is not correct.");
}
return new AuthResult(true, null);
}
Expand All @@ -47,28 +46,24 @@ public void inject(OutboundRequest request, AuthPolicy policy) {
TokenPolicy tokenPolicy = policy.getTokenPolicy();
if (tokenPolicy != null && tokenPolicy.isValid()) {
String key = tokenPolicy.getKey();
String token = tokenPolicy.getToken();
if (request.getHeader(key) == null) {
token = encode(request, key, token);
// add token by transmission
// RequestContext.getOrCreate().addCargo(key, token);
request.setHeader(key, token);
request.setHeader(key, encode(request, tokenPolicy));
}
}
}

/**
* encode a token before injecting it into the specified HTTP outbound request using the given key.
* encode a token before injecting it into the specified HTTP outbound request.
*
* @param request the HTTP outbound request
* @param key the key of the token
* @param token the value of the token
* @param policy the token policy
* @return the decorated token value
*/
protected String encode(OutboundRequest request, String key, String token) {
private String encode(OutboundRequest request, TokenPolicy policy) {
String key = policy.getKey();
String token = policy.getToken();
if (request instanceof HttpOutboundRequest && key.equalsIgnoreCase(KEY_AUTH) && !token.startsWith(BASIC_PREFIX)) {
token = new String(Base64.getEncoder().encode(token.getBytes(StandardCharsets.UTF_8)));
token = BASIC_PREFIX + token;
token = BASIC_PREFIX + policy.getBase64Token();
}
return token;
}
Expand All @@ -77,37 +72,32 @@ protected String encode(OutboundRequest request, String key, String token) {
* Retrieves a token from the specified service request using the given key.
*
* @param request the service request
* @param key the key of the token
* @param policy the token policy
* @return the token value, or null if not found
*/
private String decode(ServiceRequest request, String key) {
private boolean decodeAndCompare(ServiceRequest request, TokenPolicy policy) {
String key = policy.getKey();
String token = request.getHeader(key);
if (token != null
&& request instanceof HttpRequest
&& KEY_AUTH.equalsIgnoreCase(key)
&& token.startsWith(BASIC_PREFIX)) {
token = getBasicPassword(token);
}
return token;
}

/**
* Extracts the password from a basic authentication token.
*
* @param token the basic authentication token
* @return the extracted password
*/
private String getBasicPassword(String token) {
token = token.substring(BASIC_PREFIX.length());
try {
token = new String(Base64.getDecoder().decode(token), StandardCharsets.UTF_8);
int pos = token.indexOf(":");
if (pos != -1) {
token = token.substring(pos + 1);
// basic auth
token = token.substring(BASIC_PREFIX.length());
if (Objects.equals(policy.getBase64Token(), token)) {
// base64 encoded,improve performance.
return true;
} else {
try {
token = new String(Base64.getDecoder().decode(token), StandardCharsets.UTF_8);
int pos = token.indexOf(":");
if (pos != -1) {
token = token.substring(pos + 1);
}
} catch (Exception ignored) {
}
}
} catch (Exception ignored) {
}
return token;
return Objects.equals(policy.getToken(), token);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,51 @@
*/
package com.jd.live.agent.governance.policy.service.auth;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class TokenPolicy {

public static final String KEY_TOKEN = "token";

public static final String KEY_TOKEN_KEY = "token.key";

@Getter
@Setter
private String key;

@Getter
@Setter
private String token;

private volatile String base64Token;

public TokenPolicy() {
}

public TokenPolicy(String key, String token) {
this.key = key;
this.token = token;
}

public boolean isValid() {
return key != null && !key.isEmpty() && token != null && !token.isEmpty();
}

public String getBase64Token() {
if (base64Token == null) {
synchronized (this) {
if (base64Token == null) {
base64Token = token == null || token.isEmpty()
? ""
: new String(Base64.getEncoder().encode(token.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
}
}
}
return base64Token;
}

}

0 comments on commit ef566e9

Please sign in to comment.