@Test public void updateUserInfoWithNotForbiddenGroup() throws XWikiException, QueryException, OIDCException, ComponentLookupException { this.oldcore.getConfigurationSource().setProperty(OIDCClientConfiguration.PROP_GROUPS_FORBIDDEN, Arrays.asList("pgroup1")); this.oldcore.getConfigurationSource().setProperty(OIDCClientConfiguration.PROP_GROUPS_CLAIM, "groupclaim"); this.oldcore.getConfigurationSource().setProperty(OIDCClientConfiguration.PROP_USERINFOCLAIMS, ListUtils.sum(OIDCClientConfiguration.DEFAULT_USERINFOCLAIMS, Arrays.asList( this.oldcore.getConfigurationSource().<String>getProperty(OIDCClientConfiguration.PROP_GROUPS_CLAIM)))); Issuer issuer = new Issuer("http: Subject subject = new Subject("subject"); IDTokenClaimsSet idToken = new IDTokenClaimsSet(issuer, subject, Collections.emptyList(), new Date(), new Date()); UserInfo userInfo = new UserInfo(subject); userInfo.setClaim("groupclaim", Arrays.asList("pgroup2", "pgroup3")); Principal principal = this.manager.updateUser(idToken, userInfo); assertNotNull(principal); }
public Principal updateUser(IDTokenClaimsSet idToken, UserInfo userInfo) throws XWikiException, QueryException, OIDCException { checkAllowedGroups(userInfo); Map<String, String> formatMap = createFormatMap(idToken, userInfo); StringSubstitutor substitutor = new StringSubstitutor(new OIDCStringLookup(formatMap)); String formattedSubject = formatSubjec(substitutor); XWikiDocument userDocument = this.store.searchDocument(idToken.getIssuer().getValue(), formattedSubject); XWikiDocument modifiableDocument; boolean newUser; if (userDocument == null) { userDocument = getNewUserDocument(substitutor); newUser = true; modifiableDocument = userDocument; } else { newUser = false; modifiableDocument = userDocument.clone(); } XWikiContext xcontext = this.xcontextProvider.get(); BaseClass userClass = xcontext.getWiki().getUserClass(xcontext); BaseObject userObject = modifiableDocument.getXObject(userClass.getDocumentReference(), true, xcontext); userObject.set("active", 1, xcontext); Address address = userInfo.getAddress(); if (address != null) { userObject.set("address", address.getFormatted(), xcontext); } if (userInfo.getEmailAddress() != null) { userObject.set("email", userInfo.getEmailAddress(), xcontext); } if (userInfo.getFamilyName() != null) { userObject.set("last_name", userInfo.getFamilyName(), xcontext); } if (userInfo.getGivenName() != null) { userObject.set("first_name", userInfo.getGivenName(), xcontext); } if (userInfo.getPhoneNumber() != null) { userObject.set("phone", userInfo.getPhoneNumber(), xcontext); } if (userInfo.getLocale() != null) { userObject.set("default_language", Locale.forLanguageTag(userInfo.getLocale()).toString(), xcontext); } if (userInfo.getZoneinfo() != null) { userObject.set("timezone", userInfo.getZoneinfo(), xcontext); } if (userInfo.getWebsite() != null) { userObject.set("blog", userInfo.getWebsite().toString(), xcontext); } if (userInfo.getPicture() != null) { try { String filename = FilenameUtils.getName(userInfo.getPicture().toString()); URLConnection connection = userInfo.getPicture().toURL().openConnection(); connection.setRequestProperty("User-Agent", this.getClass().getPackage().getImplementationTitle() + '/' + this.getClass().getPackage().getImplementationVersion()); try (InputStream content = connection.getInputStream()) { modifiableDocument.setAttachment(filename, content, xcontext); } userObject.set("avatar", filename, xcontext); } catch (IOException e) { this.logger.warn("Failed to get user avatar from URL [{}]: {}", userInfo.getPicture(), ExceptionUtils.getRootCauseMessage(e)); } } updateXWikiClaims(modifiableDocument, userClass, userObject, userInfo, xcontext); this.store.updateOIDCUser(modifiableDocument, idToken.getIssuer().getValue(), formattedSubject); updateUserMapping(modifiableDocument, userClass, userObject, xcontext, substitutor); OIDCUserEventData eventData = new OIDCUserEventData(new NimbusOIDCIdToken(idToken), new NimbusOIDCUserInfo(userInfo)); this.observation.notify(new OIDCUserUpdating(modifiableDocument.getDocumentReference()), modifiableDocument, eventData); boolean userUpdated = false; if (newUser || userDocument.apply(modifiableDocument)) { String comment; if (newUser) { comment = "Create user from OpenID Connect"; } else { comment = "Update user from OpenID Connect"; } xcontext.getWiki().saveDocument(userDocument, comment, xcontext); if (newUser) { xcontext.getWiki().setUserDefaultGroup(userDocument.getFullName(), xcontext); } userUpdated = true; } if (this.configuration.isGroupSync()) { userUpdated = updateGroupMembership(userInfo, userDocument, xcontext); } if (userUpdated) { this.observation.notify(new OIDCUserUpdated(userDocument.getDocumentReference()), userDocument, eventData); } return new SimplePrincipal(userDocument.getPrefixedFullName()); }
OIDCUserManager { public Principal updateUser(IDTokenClaimsSet idToken, UserInfo userInfo) throws XWikiException, QueryException, OIDCException { checkAllowedGroups(userInfo); Map<String, String> formatMap = createFormatMap(idToken, userInfo); StringSubstitutor substitutor = new StringSubstitutor(new OIDCStringLookup(formatMap)); String formattedSubject = formatSubjec(substitutor); XWikiDocument userDocument = this.store.searchDocument(idToken.getIssuer().getValue(), formattedSubject); XWikiDocument modifiableDocument; boolean newUser; if (userDocument == null) { userDocument = getNewUserDocument(substitutor); newUser = true; modifiableDocument = userDocument; } else { newUser = false; modifiableDocument = userDocument.clone(); } XWikiContext xcontext = this.xcontextProvider.get(); BaseClass userClass = xcontext.getWiki().getUserClass(xcontext); BaseObject userObject = modifiableDocument.getXObject(userClass.getDocumentReference(), true, xcontext); userObject.set("active", 1, xcontext); Address address = userInfo.getAddress(); if (address != null) { userObject.set("address", address.getFormatted(), xcontext); } if (userInfo.getEmailAddress() != null) { userObject.set("email", userInfo.getEmailAddress(), xcontext); } if (userInfo.getFamilyName() != null) { userObject.set("last_name", userInfo.getFamilyName(), xcontext); } if (userInfo.getGivenName() != null) { userObject.set("first_name", userInfo.getGivenName(), xcontext); } if (userInfo.getPhoneNumber() != null) { userObject.set("phone", userInfo.getPhoneNumber(), xcontext); } if (userInfo.getLocale() != null) { userObject.set("default_language", Locale.forLanguageTag(userInfo.getLocale()).toString(), xcontext); } if (userInfo.getZoneinfo() != null) { userObject.set("timezone", userInfo.getZoneinfo(), xcontext); } if (userInfo.getWebsite() != null) { userObject.set("blog", userInfo.getWebsite().toString(), xcontext); } if (userInfo.getPicture() != null) { try { String filename = FilenameUtils.getName(userInfo.getPicture().toString()); URLConnection connection = userInfo.getPicture().toURL().openConnection(); connection.setRequestProperty("User-Agent", this.getClass().getPackage().getImplementationTitle() + '/' + this.getClass().getPackage().getImplementationVersion()); try (InputStream content = connection.getInputStream()) { modifiableDocument.setAttachment(filename, content, xcontext); } userObject.set("avatar", filename, xcontext); } catch (IOException e) { this.logger.warn("Failed to get user avatar from URL [{}]: {}", userInfo.getPicture(), ExceptionUtils.getRootCauseMessage(e)); } } updateXWikiClaims(modifiableDocument, userClass, userObject, userInfo, xcontext); this.store.updateOIDCUser(modifiableDocument, idToken.getIssuer().getValue(), formattedSubject); updateUserMapping(modifiableDocument, userClass, userObject, xcontext, substitutor); OIDCUserEventData eventData = new OIDCUserEventData(new NimbusOIDCIdToken(idToken), new NimbusOIDCUserInfo(userInfo)); this.observation.notify(new OIDCUserUpdating(modifiableDocument.getDocumentReference()), modifiableDocument, eventData); boolean userUpdated = false; if (newUser || userDocument.apply(modifiableDocument)) { String comment; if (newUser) { comment = "Create user from OpenID Connect"; } else { comment = "Update user from OpenID Connect"; } xcontext.getWiki().saveDocument(userDocument, comment, xcontext); if (newUser) { xcontext.getWiki().setUserDefaultGroup(userDocument.getFullName(), xcontext); } userUpdated = true; } if (this.configuration.isGroupSync()) { userUpdated = updateGroupMembership(userInfo, userDocument, xcontext); } if (userUpdated) { this.observation.notify(new OIDCUserUpdated(userDocument.getDocumentReference()), userDocument, eventData); } return new SimplePrincipal(userDocument.getPrefixedFullName()); } }
OIDCUserManager { public Principal updateUser(IDTokenClaimsSet idToken, UserInfo userInfo) throws XWikiException, QueryException, OIDCException { checkAllowedGroups(userInfo); Map<String, String> formatMap = createFormatMap(idToken, userInfo); StringSubstitutor substitutor = new StringSubstitutor(new OIDCStringLookup(formatMap)); String formattedSubject = formatSubjec(substitutor); XWikiDocument userDocument = this.store.searchDocument(idToken.getIssuer().getValue(), formattedSubject); XWikiDocument modifiableDocument; boolean newUser; if (userDocument == null) { userDocument = getNewUserDocument(substitutor); newUser = true; modifiableDocument = userDocument; } else { newUser = false; modifiableDocument = userDocument.clone(); } XWikiContext xcontext = this.xcontextProvider.get(); BaseClass userClass = xcontext.getWiki().getUserClass(xcontext); BaseObject userObject = modifiableDocument.getXObject(userClass.getDocumentReference(), true, xcontext); userObject.set("active", 1, xcontext); Address address = userInfo.getAddress(); if (address != null) { userObject.set("address", address.getFormatted(), xcontext); } if (userInfo.getEmailAddress() != null) { userObject.set("email", userInfo.getEmailAddress(), xcontext); } if (userInfo.getFamilyName() != null) { userObject.set("last_name", userInfo.getFamilyName(), xcontext); } if (userInfo.getGivenName() != null) { userObject.set("first_name", userInfo.getGivenName(), xcontext); } if (userInfo.getPhoneNumber() != null) { userObject.set("phone", userInfo.getPhoneNumber(), xcontext); } if (userInfo.getLocale() != null) { userObject.set("default_language", Locale.forLanguageTag(userInfo.getLocale()).toString(), xcontext); } if (userInfo.getZoneinfo() != null) { userObject.set("timezone", userInfo.getZoneinfo(), xcontext); } if (userInfo.getWebsite() != null) { userObject.set("blog", userInfo.getWebsite().toString(), xcontext); } if (userInfo.getPicture() != null) { try { String filename = FilenameUtils.getName(userInfo.getPicture().toString()); URLConnection connection = userInfo.getPicture().toURL().openConnection(); connection.setRequestProperty("User-Agent", this.getClass().getPackage().getImplementationTitle() + '/' + this.getClass().getPackage().getImplementationVersion()); try (InputStream content = connection.getInputStream()) { modifiableDocument.setAttachment(filename, content, xcontext); } userObject.set("avatar", filename, xcontext); } catch (IOException e) { this.logger.warn("Failed to get user avatar from URL [{}]: {}", userInfo.getPicture(), ExceptionUtils.getRootCauseMessage(e)); } } updateXWikiClaims(modifiableDocument, userClass, userObject, userInfo, xcontext); this.store.updateOIDCUser(modifiableDocument, idToken.getIssuer().getValue(), formattedSubject); updateUserMapping(modifiableDocument, userClass, userObject, xcontext, substitutor); OIDCUserEventData eventData = new OIDCUserEventData(new NimbusOIDCIdToken(idToken), new NimbusOIDCUserInfo(userInfo)); this.observation.notify(new OIDCUserUpdating(modifiableDocument.getDocumentReference()), modifiableDocument, eventData); boolean userUpdated = false; if (newUser || userDocument.apply(modifiableDocument)) { String comment; if (newUser) { comment = "Create user from OpenID Connect"; } else { comment = "Update user from OpenID Connect"; } xcontext.getWiki().saveDocument(userDocument, comment, xcontext); if (newUser) { xcontext.getWiki().setUserDefaultGroup(userDocument.getFullName(), xcontext); } userUpdated = true; } if (this.configuration.isGroupSync()) { userUpdated = updateGroupMembership(userInfo, userDocument, xcontext); } if (userUpdated) { this.observation.notify(new OIDCUserUpdated(userDocument.getDocumentReference()), userDocument, eventData); } return new SimplePrincipal(userDocument.getPrefixedFullName()); }  }
OIDCUserManager { public Principal updateUser(IDTokenClaimsSet idToken, UserInfo userInfo) throws XWikiException, QueryException, OIDCException { checkAllowedGroups(userInfo); Map<String, String> formatMap = createFormatMap(idToken, userInfo); StringSubstitutor substitutor = new StringSubstitutor(new OIDCStringLookup(formatMap)); String formattedSubject = formatSubjec(substitutor); XWikiDocument userDocument = this.store.searchDocument(idToken.getIssuer().getValue(), formattedSubject); XWikiDocument modifiableDocument; boolean newUser; if (userDocument == null) { userDocument = getNewUserDocument(substitutor); newUser = true; modifiableDocument = userDocument; } else { newUser = false; modifiableDocument = userDocument.clone(); } XWikiContext xcontext = this.xcontextProvider.get(); BaseClass userClass = xcontext.getWiki().getUserClass(xcontext); BaseObject userObject = modifiableDocument.getXObject(userClass.getDocumentReference(), true, xcontext); userObject.set("active", 1, xcontext); Address address = userInfo.getAddress(); if (address != null) { userObject.set("address", address.getFormatted(), xcontext); } if (userInfo.getEmailAddress() != null) { userObject.set("email", userInfo.getEmailAddress(), xcontext); } if (userInfo.getFamilyName() != null) { userObject.set("last_name", userInfo.getFamilyName(), xcontext); } if (userInfo.getGivenName() != null) { userObject.set("first_name", userInfo.getGivenName(), xcontext); } if (userInfo.getPhoneNumber() != null) { userObject.set("phone", userInfo.getPhoneNumber(), xcontext); } if (userInfo.getLocale() != null) { userObject.set("default_language", Locale.forLanguageTag(userInfo.getLocale()).toString(), xcontext); } if (userInfo.getZoneinfo() != null) { userObject.set("timezone", userInfo.getZoneinfo(), xcontext); } if (userInfo.getWebsite() != null) { userObject.set("blog", userInfo.getWebsite().toString(), xcontext); } if (userInfo.getPicture() != null) { try { String filename = FilenameUtils.getName(userInfo.getPicture().toString()); URLConnection connection = userInfo.getPicture().toURL().openConnection(); connection.setRequestProperty("User-Agent", this.getClass().getPackage().getImplementationTitle() + '/' + this.getClass().getPackage().getImplementationVersion()); try (InputStream content = connection.getInputStream()) { modifiableDocument.setAttachment(filename, content, xcontext); } userObject.set("avatar", filename, xcontext); } catch (IOException e) { this.logger.warn("Failed to get user avatar from URL [{}]: {}", userInfo.getPicture(), ExceptionUtils.getRootCauseMessage(e)); } } updateXWikiClaims(modifiableDocument, userClass, userObject, userInfo, xcontext); this.store.updateOIDCUser(modifiableDocument, idToken.getIssuer().getValue(), formattedSubject); updateUserMapping(modifiableDocument, userClass, userObject, xcontext, substitutor); OIDCUserEventData eventData = new OIDCUserEventData(new NimbusOIDCIdToken(idToken), new NimbusOIDCUserInfo(userInfo)); this.observation.notify(new OIDCUserUpdating(modifiableDocument.getDocumentReference()), modifiableDocument, eventData); boolean userUpdated = false; if (newUser || userDocument.apply(modifiableDocument)) { String comment; if (newUser) { comment = "Create user from OpenID Connect"; } else { comment = "Update user from OpenID Connect"; } xcontext.getWiki().saveDocument(userDocument, comment, xcontext); if (newUser) { xcontext.getWiki().setUserDefaultGroup(userDocument.getFullName(), xcontext); } userUpdated = true; } if (this.configuration.isGroupSync()) { userUpdated = updateGroupMembership(userInfo, userDocument, xcontext); } if (userUpdated) { this.observation.notify(new OIDCUserUpdated(userDocument.getDocumentReference()), userDocument, eventData); } return new SimplePrincipal(userDocument.getPrefixedFullName()); }  void updateUserInfoAsync(); void checkUpdateUserInfo(); Principal updateUserInfo(BearerAccessToken accessToken); Principal updateUserInfo(URI userInfoEndpoint, IDTokenClaimsSet idToken, BearerAccessToken accessToken); Principal updateUser(IDTokenClaimsSet idToken, UserInfo userInfo); Boolean syncXWikiGroupsMembership(String xwikiUserName, List<String> providerGroups, XWikiContext context); void logout(); }
OIDCUserManager { public Principal updateUser(IDTokenClaimsSet idToken, UserInfo userInfo) throws XWikiException, QueryException, OIDCException { checkAllowedGroups(userInfo); Map<String, String> formatMap = createFormatMap(idToken, userInfo); StringSubstitutor substitutor = new StringSubstitutor(new OIDCStringLookup(formatMap)); String formattedSubject = formatSubjec(substitutor); XWikiDocument userDocument = this.store.searchDocument(idToken.getIssuer().getValue(), formattedSubject); XWikiDocument modifiableDocument; boolean newUser; if (userDocument == null) { userDocument = getNewUserDocument(substitutor); newUser = true; modifiableDocument = userDocument; } else { newUser = false; modifiableDocument = userDocument.clone(); } XWikiContext xcontext = this.xcontextProvider.get(); BaseClass userClass = xcontext.getWiki().getUserClass(xcontext); BaseObject userObject = modifiableDocument.getXObject(userClass.getDocumentReference(), true, xcontext); userObject.set("active", 1, xcontext); Address address = userInfo.getAddress(); if (address != null) { userObject.set("address", address.getFormatted(), xcontext); } if (userInfo.getEmailAddress() != null) { userObject.set("email", userInfo.getEmailAddress(), xcontext); } if (userInfo.getFamilyName() != null) { userObject.set("last_name", userInfo.getFamilyName(), xcontext); } if (userInfo.getGivenName() != null) { userObject.set("first_name", userInfo.getGivenName(), xcontext); } if (userInfo.getPhoneNumber() != null) { userObject.set("phone", userInfo.getPhoneNumber(), xcontext); } if (userInfo.getLocale() != null) { userObject.set("default_language", Locale.forLanguageTag(userInfo.getLocale()).toString(), xcontext); } if (userInfo.getZoneinfo() != null) { userObject.set("timezone", userInfo.getZoneinfo(), xcontext); } if (userInfo.getWebsite() != null) { userObject.set("blog", userInfo.getWebsite().toString(), xcontext); } if (userInfo.getPicture() != null) { try { String filename = FilenameUtils.getName(userInfo.getPicture().toString()); URLConnection connection = userInfo.getPicture().toURL().openConnection(); connection.setRequestProperty("User-Agent", this.getClass().getPackage().getImplementationTitle() + '/' + this.getClass().getPackage().getImplementationVersion()); try (InputStream content = connection.getInputStream()) { modifiableDocument.setAttachment(filename, content, xcontext); } userObject.set("avatar", filename, xcontext); } catch (IOException e) { this.logger.warn("Failed to get user avatar from URL [{}]: {}", userInfo.getPicture(), ExceptionUtils.getRootCauseMessage(e)); } } updateXWikiClaims(modifiableDocument, userClass, userObject, userInfo, xcontext); this.store.updateOIDCUser(modifiableDocument, idToken.getIssuer().getValue(), formattedSubject); updateUserMapping(modifiableDocument, userClass, userObject, xcontext, substitutor); OIDCUserEventData eventData = new OIDCUserEventData(new NimbusOIDCIdToken(idToken), new NimbusOIDCUserInfo(userInfo)); this.observation.notify(new OIDCUserUpdating(modifiableDocument.getDocumentReference()), modifiableDocument, eventData); boolean userUpdated = false; if (newUser || userDocument.apply(modifiableDocument)) { String comment; if (newUser) { comment = "Create user from OpenID Connect"; } else { comment = "Update user from OpenID Connect"; } xcontext.getWiki().saveDocument(userDocument, comment, xcontext); if (newUser) { xcontext.getWiki().setUserDefaultGroup(userDocument.getFullName(), xcontext); } userUpdated = true; } if (this.configuration.isGroupSync()) { userUpdated = updateGroupMembership(userInfo, userDocument, xcontext); } if (userUpdated) { this.observation.notify(new OIDCUserUpdated(userDocument.getDocumentReference()), userDocument, eventData); } return new SimplePrincipal(userDocument.getPrefixedFullName()); }  void updateUserInfoAsync(); void checkUpdateUserInfo(); Principal updateUserInfo(BearerAccessToken accessToken); Principal updateUserInfo(URI userInfoEndpoint, IDTokenClaimsSet idToken, BearerAccessToken accessToken); Principal updateUser(IDTokenClaimsSet idToken, UserInfo userInfo); Boolean syncXWikiGroupsMembership(String xwikiUserName, List<String> providerGroups, XWikiContext context); void logout();  }