Skip to content

Commit

Permalink
fix(UNI-160): get ua from session header
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff-Tian committed Aug 26, 2023
1 parent 5a8ed6b commit 753a8fd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-services-social-weixin</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
<name>Keycloak Services Social WeiXin</name>
<description/>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,50 +70,42 @@ public class WeiXinIdentityBrokerService implements IdentityProvider.Authenticat
@Context
private HttpHeaders headers;

public void init(KeycloakSession session, ClientConnection clientConnection, HttpHeaders headers, EventBuilder event, org.keycloak.http.HttpRequest request) {
public void init(KeycloakSession session, ClientConnection clientConnection, EventBuilder event, org.keycloak.http.HttpRequest request) {
if (session != null) {
this.session = session;
}

if (clientConnection != null) {
this.clientConnection = clientConnection;
} else {
this.clientConnection = new ClientConnection() {
@Override
public String getRemoteAddr() {
return null;
}

@Override
public String getRemoteHost() {
return null;
}
this.clientConnection = Objects.requireNonNullElseGet(clientConnection, () -> new ClientConnection() {
@Override
public String getRemoteAddr() {
return null;
}

@Override
public int getRemotePort() {
return 0;
}
@Override
public String getRemoteHost() {
return null;
}

@Override
public String getLocalAddr() {
return null;
}
@Override
public int getRemotePort() {
return 0;
}

@Override
public int getLocalPort() {
return 0;
}
};
}
@Override
public String getLocalAddr() {
return null;
}

if (headers != null) {
this.headers = headers;
}
@Override
public int getLocalPort() {
return 0;
}
});

this.request = Objects.requireNonNullElseGet(request, () -> new HttpRequestImpl(new BaseHttpRequest(new ResteasyUriInfo("/", "/")) {
@Override
public HttpHeaders getHttpHeaders() {
return session.getContext().getRequestHeaders();
return Objects.requireNonNull(session).getContext().getRequestHeaders();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,21 @@ protected boolean supportsExternalExchange() {

@Override
protected BrokeredIdentityContext extractIdentityFromProfile(EventBuilder event, JsonNode profile) {
String uuionid = getJsonProperty(profile, "unionid");
BrokeredIdentityContext user = new BrokeredIdentityContext(
(uuionid != null && uuionid.length() > 0 ? uuionid : getJsonProperty(profile, "openid")));
String unionId = getJsonProperty(profile, "unionid");
var openId = getJsonProperty(profile, "openid");

user.setUsername(getJsonProperty(profile, "openid"));
user.setBrokerUserId(getJsonProperty(profile, "openid"));
user.setModelUsername(getJsonProperty(profile, "openid"));
var externalUserId = unionId != null && !unionId.isEmpty() ? unionId : openId;

BrokeredIdentityContext user = new BrokeredIdentityContext(externalUserId);

user.setUsername(externalUserId);
user.setBrokerUserId(externalUserId);
user.setModelUsername(externalUserId);
user.setName(getJsonProperty(profile, "nickname"));
user.setIdpConfig(getConfig());
user.setIdp(this);
AbstractJsonUserAttributeMapper.storeUserProfileForMapper(user, profile, getConfig().getAlias());

return user;
}

Expand Down Expand Up @@ -290,9 +294,6 @@ protected class Endpoint {
@Context
protected ClientConnection clientConnection;

@Context
protected HttpHeaders headers;

@Context
protected org.keycloak.http.HttpRequest request;

Expand All @@ -308,7 +309,8 @@ public Response authResponse(@QueryParam(AbstractOAuth2IdentityProvider.OAUTH2_P
logger.info(String.format("OAUTH2_PARAMETER_CODE = %s, %s, %s, %s, %s", authorizationCode, error, openid, clientId, tabId));
var wechatLoginType = WechatLoginType.FROM_PC_QR_CODE_SCANNING;

if (headers != null && isWechatBrowser(headers.getHeaderString("user-agent").toLowerCase())) {
String ua = session.getContext().getRequestHeaders().getHeaderString("user-agent").toLowerCase();
if (isWechatBrowser(ua)) {
logger.info("user-agent=wechat");
wechatLoginType = WechatLoginType.FROM_WECHAT_BROWSER;
}
Expand Down Expand Up @@ -365,7 +367,7 @@ public Response authResponse(@QueryParam(AbstractOAuth2IdentityProvider.OAUTH2_P
private Response authenticated(BrokeredIdentityContext federatedIdentity) {
var weiXinIdentityBrokerService =
new WeiXinIdentityBrokerService(realm);
weiXinIdentityBrokerService.init(session, clientConnection, headers, event, request);
weiXinIdentityBrokerService.init(session, clientConnection, event, request);

return weiXinIdentityBrokerService.authenticated(federatedIdentity);
}
Expand Down

0 comments on commit 753a8fd

Please sign in to comment.