-
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
86e6baf
commit 1751749
Showing
17 changed files
with
221 additions
and
121 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
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
23 changes: 23 additions & 0 deletions
23
api/src/main/scala/za/co/absa/loginsvc/rest/service/search/AuthSearchProvider.scala
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,23 @@ | ||
/* | ||
* Copyright 2023 ABSA Group Limited | ||
* | ||
* 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 | ||
* | ||
* 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 za.co.absa.loginsvc.rest.service.search | ||
|
||
import za.co.absa.loginsvc.model.User | ||
|
||
trait AuthSearchProvider { | ||
def searchForUser(username: String): Option[User] | ||
} |
59 changes: 59 additions & 0 deletions
59
api/src/main/scala/za/co/absa/loginsvc/rest/service/search/AuthSearchService.scala
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,59 @@ | ||
/* | ||
* Copyright 2023 ABSA Group Limited | ||
* | ||
* 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 | ||
* | ||
* 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 za.co.absa.loginsvc.rest.service.search | ||
|
||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.stereotype.Service | ||
import za.co.absa.loginsvc.model.User | ||
import za.co.absa.loginsvc.rest.config.auth.{ActiveDirectoryLDAPConfig, DynamicAuthOrder, UsersConfig} | ||
import za.co.absa.loginsvc.rest.config.provider.AuthConfigProvider | ||
import za.co.absa.loginsvc.rest.config.validation.ConfigValidationException | ||
|
||
@Service | ||
class AuthSearchService @Autowired()(authConfigProvider: AuthConfigProvider) { | ||
|
||
private val logger = LoggerFactory.getLogger(classOf[AuthSearchService]) | ||
|
||
private val usersConfig: Option[DynamicAuthOrder] = authConfigProvider.getUsersConfig | ||
private val adLDAPConfig: Option[DynamicAuthOrder] = authConfigProvider.getLdapConfig | ||
|
||
private val configs: Array[DynamicAuthOrder] = Array(usersConfig, adLDAPConfig).flatten.filter(_.order != 0).sortBy(_.order) | ||
private val orderedProviders = createProviders(configs) | ||
|
||
if (orderedProviders.isEmpty) | ||
throw ConfigValidationException("No authentication method enabled in config") | ||
|
||
def searchUser(username: String): User = { | ||
orderedProviders.foreach { provider => | ||
val user = provider.searchForUser(username) | ||
if (user.isDefined) { | ||
return user.get | ||
} | ||
} | ||
throw new NoSuchElementException(s"Value not found in any object.") | ||
} | ||
|
||
private def createProviders(configs: Array[DynamicAuthOrder]): Array[AuthSearchProvider] = { | ||
Array.empty[AuthSearchProvider] ++ configs.filter(_.order > 0).sortBy(_.order) | ||
.map { | ||
case c: UsersConfig => new ConfigSearchProvider(c) | ||
case c: ActiveDirectoryLDAPConfig => new LdapSearchProvider(c) | ||
case other => throw new IllegalStateException(s"unsupported config $other") | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
api/src/main/scala/za/co/absa/loginsvc/rest/service/search/ConfigSearchProvider.scala
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,37 @@ | ||
/* | ||
* Copyright 2023 ABSA Group Limited | ||
* | ||
* 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 | ||
* | ||
* 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 za.co.absa.loginsvc.rest.service.search | ||
|
||
import org.slf4j.LoggerFactory | ||
import za.co.absa.loginsvc.model.User | ||
import za.co.absa.loginsvc.rest.config.auth.UsersConfig | ||
|
||
class ConfigSearchProvider(usersConfig: UsersConfig) | ||
extends AuthSearchProvider { | ||
|
||
private val logger = LoggerFactory.getLogger(classOf[ConfigSearchProvider]) | ||
def searchForUser(username: String): Option[User] = { | ||
logger.info(s"Searching for user in config: $username") | ||
usersConfig.knownUsersMap.get(username).flatMap { userConfig => | ||
val optionalAttributes: Map[String, Option[AnyRef]] = userConfig.attributes.getOrElse(Map.empty).map { | ||
case (k, v) => (k, Some(v)) | ||
} | ||
logger.info(s"User found in config: $username") | ||
Option(User(userConfig.username, userConfig.groups.toList, optionalAttributes)) | ||
} | ||
} | ||
} |
Oops, something went wrong.