Skip to content

Commit

Permalink
Merge pull request #100 from georchestra/oauth2_account_deletion
Browse files Browse the repository at this point in the history
Fixed OAuth2 account deletion
  • Loading branch information
fvanderbiest committed Feb 6, 2024
2 parents fb6e1c0 + cb89b56 commit 30bd221
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public GeorchestraUser getOrCreate(@NonNull GeorchestraUser mappedUser) {
return find(mappedUser).orElseGet(() -> createIfMissing(mappedUser));
}

protected Optional<GeorchestraUser> find(GeorchestraUser mappedUser) {
public Optional<GeorchestraUser> find(GeorchestraUser mappedUser) {
lock.readLock().lock();
try {
return findInternal(mappedUser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,24 @@
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.core.Authentication;

import java.util.Optional;

/**
* @see CreateAccountUserCustomizer
* @see ResolveGeorchestraUserGlobalFilter
*/
public interface AccountManager {

/**
* Finds the stored user that belongs to the {@code mappedUser} if it exists
*
* @param mappedUser the user {@link ResolveGeorchestraUserGlobalFilter}
* resolved by calling
* {@link GeorchestraUserMapper#resolve(Authentication)}
* @return the stored version of the user if it exists, otherwise an empty Optional
*/
Optional<GeorchestraUser> find(GeorchestraUser mappedUser);

/**
* Finds the stored user that belongs to the {@code mappedUser} or creates it if
* it doesn't exist in the users repository.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.georchestra.gateway.accounts.admin;

import java.util.Objects;
import java.util.Optional;
import java.util.WeakHashMap;

import org.georchestra.gateway.security.GeorchestraUserCustomizerExtension;
import org.georchestra.security.model.GeorchestraUser;
Expand All @@ -40,6 +42,8 @@ public class CreateAccountUserCustomizer implements GeorchestraUserCustomizerExt

private final @NonNull AccountManager accounts;

private final WeakHashMap<Authentication, GeorchestraUser> loggedInUsers = new WeakHashMap<>();

/**
* @return {@link Ordered#LOWEST_PRECEDENCE} so it runs after all other
* authentication customizations have been performed, such as setting
Expand Down Expand Up @@ -68,7 +72,17 @@ public class CreateAccountUserCustomizer implements GeorchestraUserCustomizerExt
Objects.requireNonNull(mappedUser.getUsername(), "GeorchestraUser.username is null");
}
if (isOauth2 || isPreAuth) {
return accounts.getOrCreate(mappedUser);
GeorchestraUser user = loggedInUsers.get(auth);
if (user != null) {
Optional<GeorchestraUser> ldapUser = accounts.find(mappedUser);
if (ldapUser.isPresent()) {
user = ldapUser.get();
}
} else {
user = accounts.getOrCreate(mappedUser);
}
loggedInUsers.put(auth, user);
return user;
}
return mappedUser;
}
Expand Down

0 comments on commit 30bd221

Please sign in to comment.