-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LDAP] Add LDAP Authentication Provider Support. (#269)
Co-authored-by: BilwaST <stbilwa@gmail.com>
- Loading branch information
1 parent
68ffa3f
commit 90d05b0
Showing
28 changed files
with
561 additions
and
33 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
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
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
74 changes: 74 additions & 0 deletions
74
...eatunnel-app/src/main/java/org/apache/seatunnel/app/config/LdapAuthenticationBuilder.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,74 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 org.apache.seatunnel.app.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.ldap.core.support.LdapContextSource; | ||
import org.springframework.ldap.core.support.SimpleDirContextAuthenticationStrategy; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.ldap.DefaultSpringSecurityContextSource; | ||
import org.springframework.security.ldap.authentication.BindAuthenticator; | ||
import org.springframework.security.ldap.authentication.LdapAuthenticationProvider; | ||
import org.springframework.security.ldap.search.FilterBasedLdapUserSearch; | ||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Component | ||
public class LdapAuthenticationBuilder { | ||
|
||
@Value("${spring.ldap.url}") | ||
private String ldapUrl; | ||
|
||
@Value("${spring.ldap.search.base}") | ||
private String ldapSearchBase; | ||
|
||
@Value("${spring.ldap.search.domain}") | ||
private String ldapSearchDomain; | ||
|
||
public LdapAuthenticationProvider buildLdapAuthenticationProvider( | ||
Authentication authentication) { | ||
|
||
LdapContextSource ldapContextSource = new DefaultSpringSecurityContextSource(ldapUrl); | ||
String ldapBindUser = authentication.getName(); | ||
ldapContextSource.setUserDn( | ||
new StringBuilder() | ||
.append(ldapBindUser) | ||
.append("@") | ||
.append(ldapSearchDomain) | ||
.toString()); | ||
ldapContextSource.setPassword(authentication.getCredentials().toString()); | ||
ldapContextSource.setCacheEnvironmentProperties(false); | ||
ldapContextSource.setAuthenticationStrategy(new SimpleDirContextAuthenticationStrategy()); | ||
ldapContextSource.afterPropertiesSet(); | ||
|
||
String ldapAuthID = | ||
ldapBindUser.toLowerCase().endsWith("@" + ldapSearchDomain) | ||
? ldapBindUser | ||
: ldapBindUser + "@" + ldapSearchDomain; | ||
String searchFilter = "(userPrincipalName=" + ldapAuthID + ")"; | ||
FilterBasedLdapUserSearch userSearch = | ||
new FilterBasedLdapUserSearch(ldapSearchBase, searchFilter, ldapContextSource); | ||
userSearch.setSearchSubtree(true); | ||
BindAuthenticator authenticator = new BindAuthenticator(ldapContextSource); | ||
authenticator.setUserSearch(userSearch); | ||
LdapAuthenticationProvider ldapAuthenticationProvider = | ||
new LdapAuthenticationProvider(authenticator); | ||
return ldapAuthenticationProvider; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...src/main/java/org/apache/seatunnel/app/config/SeatunnelAuthenticationProvidersConfig.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,32 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 org.apache.seatunnel.app.config; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import lombok.Data; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Data | ||
@Configuration | ||
@ConfigurationProperties(prefix = "spring.authentication") | ||
public class SeatunnelAuthenticationProvidersConfig { | ||
private List<String> providers = new ArrayList<>(); | ||
} |
64 changes: 64 additions & 0 deletions
64
...pp/src/main/java/org/apache/seatunnel/app/config/SeatunnelLdapAuthenticationProvider.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,64 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 org.apache.seatunnel.app.config; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.AuthenticationProvider; | ||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.ldap.authentication.LdapAuthenticationProvider; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Configuration | ||
@Slf4j | ||
public class SeatunnelLdapAuthenticationProvider implements AuthenticationProvider { | ||
|
||
@Autowired LdapAuthenticationBuilder ldapAuthenticationBuilder; | ||
|
||
@Override | ||
public boolean supports(Class<?> authentication) { | ||
return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication)); | ||
} | ||
|
||
@Override | ||
public Authentication authenticate(Authentication authentication) | ||
throws AuthenticationException { | ||
try { | ||
LdapAuthenticationProvider ldapAuthenticationProvider = | ||
ldapAuthenticationBuilder.buildLdapAuthenticationProvider(authentication); | ||
return ldapAuthenticationProvider.authenticate(authentication); | ||
} catch (BadCredentialsException ex) { | ||
log.error("Invalid credentials for user : {}", authentication.getName()); | ||
throw new BadCredentialsException("Invalid credentials"); | ||
} catch (Exception ex) { | ||
log.error( | ||
"Error while authenticating user : {}, reason :{}", | ||
authentication.getName(), | ||
ex.getMessage()); | ||
throw new AuthenticationException(ex.getMessage().toString()) { | ||
@Override | ||
public String getMessage() { | ||
return super.getMessage(); | ||
} | ||
}; | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,6 @@ public class User { | |
private Date createTime; | ||
|
||
private Date updateTime; | ||
|
||
private String authProvider; | ||
} |
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
24 changes: 24 additions & 0 deletions
24
...va/org/apache/seatunnel/app/security/authentication/strategy/IAuthenticationStrategy.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,24 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 org.apache.seatunnel.app.security.authentication.strategy; | ||
|
||
import org.apache.seatunnel.app.dal.entity.User; | ||
import org.apache.seatunnel.app.domain.request.user.UserLoginReq; | ||
|
||
public interface IAuthenticationStrategy { | ||
User authenticate(UserLoginReq req); | ||
} |
Oops, something went wrong.