Skip to content

Commit

Permalink
feat: keycloak application event
Browse files Browse the repository at this point in the history
  • Loading branch information
simonseyock committed Feb 21, 2022
1 parent 0808e1c commit fedb035
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,33 @@
package de.terrestris.shogun.lib.controller;

import de.terrestris.shogun.lib.dto.KeycloakEventDto;
import de.terrestris.shogun.lib.service.GroupService;
import de.terrestris.shogun.lib.service.UserService;
import de.terrestris.shogun.lib.event.KeycloakEvent;
import de.terrestris.shogun.lib.event.KeycloakEventType;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@Log4j2
@RestController
@RequestMapping("/webhooks")
public class WebhookController {

@Autowired
UserService userService;

@Autowired
GroupService groupService;
private ApplicationEventPublisher applicationEventPublisher;

@PostMapping(value = "/keycloak")
public void handleKeyCloakEvent(@RequestBody KeycloakEventDto event) {
Set<String> relevantResourceTypes = new HashSet(Arrays.asList(
Set<String> relevantResourceTypes = Set.of(
"GROUP_MEMBERSHIP",
"GROUP",
"USER"
));
);

log.debug("Keycloak webhook called with event: {}", event);
String resourceType = event.getResourceType();
Expand All @@ -58,18 +53,38 @@ public void handleKeyCloakEvent(@RequestBody KeycloakEventDto event) {
if (StringUtils.isNotEmpty(resourcePath)) {
String[] split = resourcePath.split("/");
if (StringUtils.equals(resourceType, "GROUP_MEMBERSHIP")) {
userService.findOrCreateByKeyCloakId(split[1]);
applicationEventPublisher.publishEvent(new KeycloakEvent(
this,
KeycloakEventType.USER_GROUP_MEMBERSHIP_CHANGED,
split[1]
));
} else if (StringUtils.equals(resourceType, "USER")) {
if (StringUtils.equals(eventType, "CREATE")) {
userService.findOrCreateByKeyCloakId(split[1]);
applicationEventPublisher.publishEvent(new KeycloakEvent(
this,
KeycloakEventType.USER_CREATED,
split[1]
));
} else if (StringUtils.equals(eventType, "DELETE")) {
userService.deleteByKeycloakId(split[1]);
applicationEventPublisher.publishEvent(new KeycloakEvent(
this,
KeycloakEventType.USER_DELETED,
split[1]
));
}
} else if (StringUtils.equals(resourceType, "GROUP")) {
if (StringUtils.equals(eventType, "CREATE")) {
groupService.findOrCreateByKeycloakId(split[1]);
applicationEventPublisher.publishEvent(new KeycloakEvent(
this,
KeycloakEventType.GROUP_CREATED,
split[1]
));
} else if (StringUtils.equals(eventType, "DELETE")) {
groupService.deleteByKeycloakId(split[1]);
applicationEventPublisher.publishEvent(new KeycloakEvent(
this,
KeycloakEventType.GROUP_DELETED,
split[1]
));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SHOGun PROGEMIS, based on https://terrestris.github.io/shogun/
*
* Copyright © 2020-present terrestris GmbH & Co. KG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.terrestris.shogun.lib.event;

import lombok.Getter;
import org.springframework.context.ApplicationEvent;

public class KeycloakEvent extends ApplicationEvent {
@Getter
private final KeycloakEventType eventType;
@Getter
private final String keycloakId;

public KeycloakEvent(Object source, KeycloakEventType eventType, String keycloakId) {
super(source);
this.eventType = eventType;
this.keycloakId = keycloakId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* SHOGun PROGEMIS, based on https://terrestris.github.io/shogun/
*
* Copyright © 2020-present terrestris GmbH & Co. KG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.terrestris.shogun.lib.event;

public enum KeycloakEventType {
GROUP_CREATED,
GROUP_DELETED,
USER_GROUP_MEMBERSHIP_CHANGED,
USER_CREATED,
USER_DELETED
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* SHOGun PROGEMIS, based on https://terrestris.github.io/shogun/
*
* Copyright © 2020-present terrestris GmbH & Co. KG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.terrestris.shogun.lib.listener;

import de.terrestris.shogun.lib.event.KeycloakEvent;
import de.terrestris.shogun.lib.service.GroupService;
import de.terrestris.shogun.lib.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class KeycloakEventListener {
@Autowired
UserService userService;

@Autowired
GroupService groupService;

@EventListener
public void onKeycloakEvent(KeycloakEvent event) {
switch (event.getEventType()) {
case USER_CREATED, USER_GROUP_MEMBERSHIP_CHANGED -> userService.findOrCreateByKeyCloakId(event.getKeycloakId());
case GROUP_CREATED -> groupService.findOrCreateByKeycloakId(event.getKeycloakId());
}
}
}

0 comments on commit fedb035

Please sign in to comment.