forked from bayang/jelu
-
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.
see Documentation for setup : https://bayang.github.io/jelu-web/
- Loading branch information
Showing
18 changed files
with
303 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export interface ServerSettings { | ||
metadataFetchEnabled: boolean, | ||
metadataFetchCalibreEnabled: boolean, | ||
appVersion: string | ||
appVersion: string, | ||
ldapEnabled: boolean | ||
} |
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
89 changes: 89 additions & 0 deletions
89
src/main/kotlin/io/github/bayang/jelu/config/JeluLdapUserDetailsContextMapper.kt
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,89 @@ | ||
package io.github.bayang.jelu.config | ||
|
||
import io.github.bayang.jelu.dao.Provider | ||
import io.github.bayang.jelu.dao.UserRepository | ||
import io.github.bayang.jelu.dto.CreateUserDto | ||
import io.github.bayang.jelu.dto.JeluUser | ||
import io.github.bayang.jelu.dto.UpdateUserDto | ||
import mu.KotlinLogging | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty | ||
import org.springframework.ldap.core.DirContextAdapter | ||
import org.springframework.ldap.core.DirContextOperations | ||
import org.springframework.security.core.GrantedAuthority | ||
import org.springframework.security.core.userdetails.UserDetails | ||
import org.springframework.security.ldap.userdetails.UserDetailsContextMapper | ||
import org.springframework.stereotype.Component | ||
import org.springframework.transaction.annotation.Transactional | ||
import javax.naming.directory.Attributes | ||
|
||
private val logger = KotlinLogging.logger {} | ||
|
||
@Component | ||
@ConditionalOnProperty(name = ["jelu.auth.ldap.enabled"], havingValue = "true", matchIfMissing = false) | ||
class JeluLdapUserDetailsContextMapper( | ||
private val userRepository: UserRepository, | ||
) : UserDetailsContextMapper { | ||
|
||
@Transactional | ||
override fun mapUserFromContext( | ||
ctx: DirContextOperations?, | ||
username: String?, | ||
authorities: MutableCollection<out GrantedAuthority>? | ||
): UserDetails { | ||
dumpAttributesForDebug(ctx?.attributes) | ||
val isAdmin = findAdminMembership(ctx?.attributes) | ||
val res = userRepository.findByLoginAndProvider(username!!, Provider.LDAP) | ||
if (res.empty()) { | ||
val saved = userRepository.save(CreateUserDto(login = username, password = "ldap", isAdmin = isAdmin, Provider.LDAP)) | ||
return JeluUser(saved) | ||
} | ||
var user = res.first() | ||
if (user.isAdmin != isAdmin) { | ||
user = userRepository.updateUser(user.id.value, UpdateUserDto(password = "ldap", isAdmin = isAdmin, provider = null)) | ||
} | ||
return JeluUser(user) | ||
} | ||
|
||
private fun findAdminMembership(attributes: Attributes?): Boolean { | ||
var isAdmin = false | ||
if (attributes != null) { | ||
val all = attributes.all | ||
while (all.hasMoreElements()) { | ||
val att = all.next() | ||
if (att != null) { | ||
if (att.id.equals("memberOf")) { | ||
var values = "" | ||
if (att.all != null) { | ||
values = att.all.toList().joinToString() | ||
} | ||
if (! values.isNullOrBlank() && values.contains("jelu-admin")) { | ||
isAdmin = true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return isAdmin | ||
} | ||
|
||
override fun mapUserToContext(user: UserDetails?, ctx: DirContextAdapter?) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
fun dumpAttributesForDebug(attributes: Attributes?) { | ||
logger.trace { "ldap attributes : " } | ||
if (attributes != null) { | ||
val all = attributes.all | ||
while (all.hasMoreElements()) { | ||
val att = all.next() | ||
if (att != null) { | ||
var values = "" | ||
if (att.all != null) { | ||
values = att.all.toList().joinToString() | ||
} | ||
logger.trace { "Attribute: ${att.id} -> $values" } | ||
} | ||
} | ||
} | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
src/main/kotlin/io/github/bayang/jelu/config/LdapConfig.kt
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,44 @@ | ||
package io.github.bayang.jelu.config | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.ldap.core.support.BaseLdapPathContextSource | ||
import org.springframework.security.authentication.AuthenticationProvider | ||
import org.springframework.security.ldap.DefaultSpringSecurityContextSource | ||
import org.springframework.security.ldap.authentication.AbstractLdapAuthenticator | ||
import org.springframework.security.ldap.authentication.BindAuthenticator | ||
import org.springframework.security.ldap.authentication.LdapAuthenticationProvider | ||
import org.springframework.security.ldap.search.FilterBasedLdapUserSearch | ||
import org.springframework.security.ldap.userdetails.UserDetailsContextMapper | ||
|
||
@Configuration | ||
@ConditionalOnProperty(name = ["jelu.auth.ldap.enabled"], havingValue = "true", matchIfMissing = false) | ||
class LdapConfig( | ||
private val userDetailsContextMapper: UserDetailsContextMapper, | ||
private val properties: JeluProperties | ||
) { | ||
|
||
@Bean | ||
fun contextSource(): BaseLdapPathContextSource { | ||
return DefaultSpringSecurityContextSource(properties.auth.ldap.url) | ||
} | ||
|
||
@Bean | ||
fun authenticationProvider(contextSource: BaseLdapPathContextSource): AuthenticationProvider { | ||
val authenticator: AbstractLdapAuthenticator = BindAuthenticator(contextSource) | ||
if (properties.auth.ldap.userDnPatterns.isNotEmpty()) { | ||
authenticator.setUserDnPatterns(properties.auth.ldap.userDnPatterns.toTypedArray()) | ||
} | ||
if (! properties.auth.ldap.userSearchFilter.isNullOrBlank()) { | ||
val userSearchBase = if (properties.auth.ldap.userSearchBase.isNullOrBlank()) "" else properties.auth.ldap.userSearchBase | ||
authenticator.setUserSearch( | ||
FilterBasedLdapUserSearch(userSearchBase, properties.auth.ldap.userSearchFilter, contextSource) | ||
) | ||
} | ||
authenticator.afterPropertiesSet() | ||
val provider: LdapAuthenticationProvider = LdapAuthenticationProvider(authenticator) | ||
provider.setUserDetailsContextMapper(userDetailsContextMapper) | ||
return provider | ||
} | ||
} |
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
Oops, something went wrong.