Skip to content

Commit

Permalink
allow customisation of user info attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
miaoxng committed Oct 15, 2024
1 parent 8308ccf commit f60a8c5
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>org.joget</groupId>
<artifactId>openid-connect-authentication</artifactId>
<packaging>bundle</packaging>
<version>7.0.7</version>
<version>7.0.8</version>
<name>openid-connect-authentication</name>
<url>http://www.joget.org</url>
<build>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/joget/plugin/marketplace/Activator.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

public class Activator implements BundleActivator {

public static final String VERSION = "7.0.8";

protected Collection<ServiceRegistration> registrationList;

public void start(BundleContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public String getDescription() {

@Override
public String getVersion() {
return "7.0.7";
return Activator.VERSION;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.nimbusds.oauth2.sdk.auth.ClientAuthentication;
import com.nimbusds.oauth2.sdk.auth.ClientSecretBasic;
import com.nimbusds.oauth2.sdk.auth.Secret;
import com.nimbusds.oauth2.sdk.http.HTTPRequest;
import com.nimbusds.oauth2.sdk.http.HTTPResponse;
import com.nimbusds.oauth2.sdk.id.ClientID;
import com.nimbusds.oauth2.sdk.id.Issuer;
Expand Down Expand Up @@ -79,6 +80,7 @@
import java.util.List;
import java.net.URI;
import java.net.URLDecoder;
import net.minidev.json.JSONObject;
import net.sf.ehcache.Cache;
import org.joget.directory.dao.UserMetaDataDao;
import org.joget.directory.model.UserMetaData;
Expand All @@ -104,7 +106,7 @@ public String getDescription() {

@Override
public String getVersion() {
return "7.0.7";
return Activator.VERSION;
}

@Override
Expand Down Expand Up @@ -337,27 +339,40 @@ public UserInfo getUserInfo(AccessToken accessTokenContent) throws URISyntaxExce
} else {
UserInfoEndpoint = new URI(dmImpl.getPropertyString("userinfoEndpoint"));
}
HTTPResponse httpResponse = new UserInfoRequest(UserInfoEndpoint, (BearerAccessToken) accessTokenContent).toHTTPRequest().send();
// HTTPResponse httpResponse = new UserInfoRequest(UserInfoEndpoint, (BearerAccessToken) accessTokenContent).toHTTPRequest().send();

HTTPRequest httpRequest = new UserInfoRequest(UserInfoEndpoint, (BearerAccessToken) accessTokenContent).toHTTPRequest();
httpRequest.setAccept("application/json");
HTTPResponse httpResponse = httpRequest.send();

// Parse the response
UserInfoResponse userInfoResponse = null;
try {
userInfoResponse = UserInfoResponse.parse(httpResponse);
JSONObject jsonResponse = httpResponse.getContentAsJSONObject();

if (jsonResponse.containsKey("id")) {
String idValue = jsonResponse.getAsString("id");
jsonResponse.put("sub", idValue);
UserInfo userInfo = new UserInfo(jsonResponse);
return userInfo;
} else {
userInfoResponse = UserInfoResponse.parse(httpResponse);
if (userInfoResponse != null) {
if (!userInfoResponse.indicatesSuccess()) {
LogUtil.error(OpenIDDirectoryManager.class.getName(), null, "The request failed, e.g. due to invalid or expired token");
return null;
}
// Extract the claims
UserInfo userInfo = userInfoResponse.toSuccessResponse().getUserInfo();
return userInfo;
}
}
} catch (ParseException ex) {
LogUtil.error(OpenIDDirectoryManager.class.getName(), ex, "Failed to parse userInfo Response");

return null;
}

if (userInfoResponse != null) {
if (!userInfoResponse.indicatesSuccess()) {
LogUtil.error(OpenIDDirectoryManager.class.getName(), null, "The request failed, e.g. due to invalid or expired token");
return null;
}
// Extract the claims
UserInfo userInfo = userInfoResponse.toSuccessResponse().getUserInfo();
return userInfo;
}
return null;
return null;
}

/**
Expand Down Expand Up @@ -468,11 +483,21 @@ void doLogin(UserInfo userInfo, HttpServletRequest request, HttpServletResponse

//String certificate = dmImpl.getPropertyString("certificate");
boolean userProvisioningEnabled = Boolean.parseBoolean(dmImpl.getPropertyString("userProvisioning"));
String username;
if (userInfo.getPreferredUsername() != null) {
username = userInfo.getPreferredUsername();

// get custom user info from user
String username = "";
String firstName = "";
String lastName = "";
if (dmImpl.getPropertyString("userinfoAttr").equals("userinfoAttrCustom")){
username = userInfo.getClaim(dmImpl.getPropertyString("userinfoAttrUsernameField")).toString();
firstName = userInfo.getClaim(dmImpl.getPropertyString("userinfoAttrFirstNameField")).toString();
lastName = userInfo.getClaim(dmImpl.getPropertyString("userinfoAttrLastNameField")).toString();
} else {
username = userInfo.getEmailAddress();
if (userInfo.getPreferredUsername() != null) {
username = userInfo.getPreferredUsername();
} else {
username = userInfo.getEmailAddress();
}
}

// get user
Expand All @@ -488,13 +513,19 @@ void doLogin(UserInfo userInfo, HttpServletRequest request, HttpServletResponse
user.setEmail(userInfo.getEmailAddress());
}

if (userInfo.getGivenName() != null && !userInfo.getGivenName().isEmpty()) {
user.setFirstName(userInfo.getGivenName());
}

if (userInfo.getFamilyName() != null && !userInfo.getFamilyName().isEmpty()) {
user.setLastName(userInfo.getFamilyName());
if (dmImpl.getPropertyString("userinfoAttr").equals("userinfoAttrCustom")){
user.setFirstName(firstName);
user.setLastName(lastName);
} else {
if (userInfo.getGivenName() != null && !userInfo.getGivenName().isEmpty()) {
user.setFirstName(userInfo.getGivenName());
}

if (userInfo.getFamilyName() != null && !userInfo.getFamilyName().isEmpty()) {
user.setLastName(userInfo.getFamilyName());
}
}


if (userInfo.getLocale() != null && !userInfo.getLocale().isEmpty()) {
user.setLocale(userInfo.getLocale());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public String getName() {

@Override
public String getVersion() {
return "7.0.7";
return Activator.VERSION;
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/messages/open-id-authentication.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ properties.callback.url=Callback URL
properties.issuer=Issuer
properties.issuer.url=Issuer URL
properties.custom=Custom
properties.default=Default
properties.auth.end.point=Authorization Token Endpoint
properties.token.end.point=Token Endpoint
properties.userinfo.end.point=User Info Endpoint
properties.userinfo.attr=User Info Attribute
properties.userinfo.attr.field.username=User Info Username Attribute Field
properties.userinfo.attr.field.firstname=User Info First Name Attribute Field
properties.userinfo.attr.field.lastname=User Info Last Name Attribute Field
properties.web.ket.set=Json Web Key Set
properties.reponse.type=Response Types Supported
properties.client.id=Client ID
Expand Down
47 changes: 47 additions & 0 deletions src/main/resources/properties/app/OpenIDDirectoryManager.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,53 @@
"control_use_regex": "false",
"required": "true"
},
{
"name": "userinfoAttr",
"label": "@@properties.userinfo.attr@@",
"type": "selectbox",
"control_field": "issuer",
"control_value": "custom",
"control_use_regex": "false",
"required": "true",
"options": [{
"value": "userinfoAttrDefault",
"label": "@@properties.default@@"
},
{
"value": "userinfoAttrCustom",
"label": "@@properties.custom@@"
}]
},
{
"name": "userinfoAttrUsernameField",
"label": "@@properties.userinfo.attr.field.username@@",
"type": "textfield",
"value": "",
"control_field": "userinfoAttr",
"control_value": "userinfoAttrCustom",
"control_use_regex": "false",
"required": "true"
},
{
"name": "userinfoAttrFirstNameField",
"label": "@@properties.userinfo.attr.field.firstname@@",
"type": "textfield",
"value": "",
"control_field": "userinfoAttr",
"control_value": "userinfoAttrCustom",
"control_use_regex": "false",
"required": "true"
},
{
"name": "userinfoAttrLastNameField",
"label": "@@properties.userinfo.attr.field.lastname@@",
"type": "textfield",
"value": "",
"control_field": "userinfoAttr",
"control_value": "userinfoAttrCustom",
"control_use_regex": "false",
"required": "true"
},
{
"name": "jsonWebKeySet",
"label": "@@properties.web.ket.set@@",
Expand Down

0 comments on commit f60a8c5

Please sign in to comment.