Skip to content

Instantly share code, notes, and snippets.

@vavasthi
Created July 5, 2020 14:04
Show Gist options
  • Save vavasthi/886f37d4003216f6f7036f14c5f0b645 to your computer and use it in GitHub Desktop.
Save vavasthi/886f37d4003216f6f7036f14c5f0b645 to your computer and use it in GitHub Desktop.
package com.avasthi.varahamihir.client.filters;
import com.avasthi.varahamihir.common.constants.VarahamihirConstants;
import com.avasthi.varahamihir.common.exceptions.UnauthorizedException;
import com.avasthi.varahamihir.common.filters.VarahamihirAbstractFilter;
import com.avasthi.varahamihir.common.pojos.AuthorizationHeaderValues;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Base64Utils;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;
public class AuthorizationHeaderFilter extends VarahamihirAbstractFilter implements WebFilter {
@Value("${tutorial.default.tenant.discriminator:default}")
private String defaultDiscriminator;
@Override
public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
String authorizationHeaderValue
= serverWebExchange.getRequest().getHeaders().getFirst(VarahamihirConstants.AUTHORIZATION_HEADER_NAME);
if (authorizationHeaderValue == null) {
return webFilterChain.filter(serverWebExchange);
}
authorizationHeaderValue = authorizationHeaderValue.trim();
String[] headerPieces = authorizationHeaderValue.split(" ");
String authType = headerPieces[0].toLowerCase();
String authToken = headerPieces[1];
if (authType.equals("bearer")) {
return webFilterChain
.filter(serverWebExchange)
.subscriberContext(context -> context.put(VarahamihirConstants.AUTHORIZATION_HEADER_IN_CONTEXT,
AuthorizationHeaderValues.builder()
.authToken(authToken)
.authType(AuthorizationHeaderValues.AuthType.Bearer)
.build()));
} else if (authType.equals("basic")) {
String[] decodedToken = new String(Base64Utils.decode(authToken.getBytes())).split(":");
String username = decodedToken[0];
String password = decodedToken[1];
return webFilterChain
.filter(serverWebExchange)
.subscriberContext(context -> context.put(VarahamihirConstants.AUTHORIZATION_HEADER_IN_CONTEXT,
AuthorizationHeaderValues.builder()
.authToken(authToken)
.username(username)
.password(password)
.clientId(username)
.authType(AuthorizationHeaderValues.AuthType.Basic)
.build()));
} else {
return unauthorizedException(serverWebExchange, "The authorization header is neither of type basic nor value.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment