-
Notifications
You must be signed in to change notification settings - Fork 1
[broker-67] Design of authentication mechanism #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
crazyrokr
wants to merge
14
commits into
develop
Choose a base branch
from
feature-broker-67
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4449b92
[broker-67] Add authentications modules and initial class structure
crazyrokr 3c353c4
[broker-67] Remove unnecessary import
crazyrokr bfef50e
[broker-67] Implement file and db credentials sources
crazyrokr 53b38dc
Merge branch 'develop' into feature-broker-67
crazyrokr 3d36a83
[broker-67] Rename CredentialsSourceDatabaseConfig
crazyrokr fd0835b
[broker-67] Add tests
crazyrokr 0de823c
[broker-67] Refactoring
crazyrokr c7c5213
[broker-67] Introduce AuthenticationSpringConfig
crazyrokr 388be51
[broker-67] Revert unnecessary changes
crazyrokr ed00b01
[broker-67] Introduce DatabaseClient from spring-r2dbc
crazyrokr ebbfc47
Merge branch 'develop' into feature-broker-67
crazyrokr ef1f19b
[broker-67] Refactoring
crazyrokr 9239636
[broker-67] Make AuthRequest a record
crazyrokr 63ab62d
Merge branch 'develop' into feature-broker-67
crazyrokr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 0 additions & 1 deletion
1
acl-groovy-dsl/src/main/groovy/javasabr/mqtt/service/acl/AclRulesLoader.groovy
This file contains hidden or 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 hidden or 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 hidden or 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
99 changes: 99 additions & 0 deletions
99
...ion/src/main/java/javasabr/mqtt/broker/application/config/AuthenticationSpringConfig.java
This file contains hidden or 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,99 @@ | ||
| package javasabr.mqtt.broker.application.config; | ||
|
|
||
| import static io.r2dbc.postgresql.PostgresqlConnectionFactoryProvider.OPTIONS; | ||
| import static io.r2dbc.spi.ConnectionFactoryOptions.DATABASE; | ||
| import static io.r2dbc.spi.ConnectionFactoryOptions.DRIVER; | ||
| import static io.r2dbc.spi.ConnectionFactoryOptions.HOST; | ||
| import static io.r2dbc.spi.ConnectionFactoryOptions.PASSWORD; | ||
| import static io.r2dbc.spi.ConnectionFactoryOptions.PORT; | ||
| import static io.r2dbc.spi.ConnectionFactoryOptions.USER; | ||
|
|
||
| import io.r2dbc.pool.ConnectionPool; | ||
| import io.r2dbc.pool.ConnectionPoolConfiguration; | ||
| import io.r2dbc.spi.ConnectionFactories; | ||
| import io.r2dbc.spi.ConnectionFactory; | ||
| import io.r2dbc.spi.ConnectionFactoryOptions; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import javasabr.mqtt.service.auth.AuthenticationService; | ||
| import javasabr.mqtt.service.auth.DefaultAuthenticationService; | ||
| import javasabr.mqtt.service.auth.PasswordBasedAuthenticationProvider; | ||
| import javasabr.mqtt.service.auth.provider.AuthenticationProvider; | ||
| import javasabr.mqtt.service.auth.source.CredentialSource; | ||
| import javasabr.mqtt.service.auth.source.DatabaseProperties; | ||
| import javasabr.mqtt.service.auth.source.FileCredentialsSource; | ||
| import javasabr.mqtt.service.auth.source.R2dbcCredentialsSource; | ||
| import javasabr.rlib.collections.dictionary.DictionaryFactory; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.r2dbc.core.DatabaseClient; | ||
|
|
||
| @Configuration(proxyBeanMethods = false) | ||
| public class AuthenticationSpringConfig { | ||
|
|
||
| private static final String LOCK_TIMEOUT_OPTION = "lock_timeout"; | ||
| private static final String STATEMENT_TIMEOUT_OPTION = "statement_timeout"; | ||
|
|
||
| @Bean | ||
| ConnectionFactory connectionFactory(DatabaseProperties config) { | ||
| Map<String, String> timeoutOptions = Map.of( | ||
| LOCK_TIMEOUT_OPTION, | ||
| config.lockTimeout(), | ||
| STATEMENT_TIMEOUT_OPTION, | ||
| config.statementTimeout()); | ||
| ConnectionFactoryOptions connectionFactoryOptions = ConnectionFactoryOptions | ||
| .builder() | ||
| .option(DRIVER, config.driver()) | ||
| .option(HOST, config.host()) | ||
| .option(PORT, config.port()) | ||
| .option(USER, config.username()) | ||
| .option(PASSWORD, config.password()) | ||
| .option(DATABASE, config.name()) | ||
| .option(OPTIONS, timeoutOptions) | ||
| .build(); | ||
| ConnectionPoolConfiguration configuration = ConnectionPoolConfiguration | ||
| .builder(ConnectionFactories.get(connectionFactoryOptions)) | ||
| .maxIdleTime(config.maxIdleTime()) | ||
| .maxSize(config.maxPoolSize()) | ||
| .initialSize(config.initialPoolSize()) | ||
| .build(); | ||
| return new ConnectionPool(configuration); | ||
| } | ||
|
|
||
| @Bean | ||
| DatabaseClient databaseClient(ConnectionFactory connectionFactory) { | ||
| return DatabaseClient.create(connectionFactory); | ||
| } | ||
|
|
||
| @Bean | ||
| CredentialSource credentialSource(@Value("${credentials.source.file.name:credentials}") String fileName) { | ||
| return new FileCredentialsSource(fileName); | ||
| } | ||
|
|
||
| @Bean | ||
| CredentialSource dbCredentialSource(DatabaseClient connectionFactory, DatabaseProperties databaseProperties) { | ||
| return new R2dbcCredentialsSource(connectionFactory, databaseProperties.credentialsQuery()); | ||
| } | ||
|
|
||
| @Bean | ||
| AuthenticationProvider passwordBasedAuthenticationProvider(CredentialSource credentialSource) { | ||
| return new PasswordBasedAuthenticationProvider(credentialSource); | ||
| } | ||
|
|
||
| @Bean | ||
| AuthenticationService authenticationService( | ||
| List<AuthenticationProvider> credentialSource, | ||
| @Value("${authentication.allow.anonymous:false}") boolean allowAnonymousAuth, | ||
| @Value("${authentication.provider.default:basic}") String defaultProviderName) { | ||
| var authenticationProviders = DictionaryFactory.mutableRefToRefDictionary( | ||
| String.class, | ||
| AuthenticationProvider.class); | ||
| credentialSource.forEach(value -> authenticationProviders.put(value.getAuthMethodName(), value)); | ||
| AuthenticationProvider defaultProvider = authenticationProviders.get(defaultProviderName); | ||
| if (defaultProvider == null) { | ||
| throw new IllegalArgumentException("[%s] authenticator provider not found".formatted(defaultProviderName)); | ||
| } | ||
| return new DefaultAuthenticationService(authenticationProviders.toReadOnly(), defaultProvider, allowAnonymousAuth); | ||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
...ain/java/javasabr/mqtt/broker/application/config/CredentialsSourceDatabaseProperties.java
This file contains hidden or 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,20 @@ | ||
| package javasabr.mqtt.broker.application.config; | ||
|
|
||
| import java.time.Duration; | ||
| import javasabr.mqtt.service.auth.source.DatabaseProperties; | ||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
|
||
| @ConfigurationProperties(prefix = "credentials.source.db") | ||
| public record CredentialsSourceDatabaseProperties( | ||
| String username, | ||
| String password, | ||
| String driver, | ||
| String host, | ||
| int port, | ||
| String name, | ||
| String credentialsQuery, | ||
| Duration maxIdleTime, | ||
| int initialPoolSize, | ||
| int maxPoolSize, | ||
| String lockTimeout, | ||
| String statementTimeout) implements DatabaseProperties {} |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
authenticationProviders or credentialSource?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed