-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3ec23c
commit e01c83d
Showing
3 changed files
with
71 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/harmlessprince/ecommerceApi/configs/TenantAwareAuthenticationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.harmlessprince.ecommerceApi.configs; | ||
import com.harmlessprince.ecommerceApi.contexts.TenantContext; | ||
import com.harmlessprince.ecommerceApi.customer.CustomerRepository; | ||
import com.harmlessprince.ecommerceApi.user.UserRepository; | ||
import org.springframework.security.authentication.AuthenticationProvider; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class TenantAwareAuthenticationProvider implements AuthenticationProvider { | ||
private final CustomerRepository customerRepository; | ||
private final UserRepository userRepository; | ||
|
||
public TenantAwareAuthenticationProvider(CustomerRepository customerRepository, UserRepository userRepository) { | ||
this.customerRepository = customerRepository; | ||
this.userRepository = userRepository; | ||
} | ||
|
||
|
||
|
||
@Override | ||
public Authentication authenticate(Authentication authentication) throws AuthenticationException { | ||
String username = authentication.getName(); | ||
String password = authentication.getCredentials().toString(); | ||
|
||
String tenantId = TenantContext.getCurrentTenantID(); // Get tenant dynamically | ||
UserDetails userDetails; | ||
|
||
if (tenantId != null) { | ||
userDetails = customerRepository.findFirstByEmailAndTenantId(username, tenantId) | ||
.orElseThrow(() -> new UsernameNotFoundException("Customer not found")); | ||
} else { | ||
userDetails = userRepository.findFirstByEmail(username) | ||
.orElseThrow(() -> new UsernameNotFoundException("User not found")); | ||
} | ||
|
||
return new UsernamePasswordAuthenticationToken(userDetails, password, userDetails.getAuthorities()); | ||
} | ||
|
||
@Override | ||
public boolean supports(Class<?> authentication) { | ||
return authentication.equals(UsernamePasswordAuthenticationToken.class); | ||
} | ||
} |