Skip to content

Commit

Permalink
refactor: #184 code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinbator committed Nov 9, 2024
1 parent bf5eefb commit a40b6c4
Show file tree
Hide file tree
Showing 81 changed files with 199 additions and 203 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ Before you can run the project, you will need the following applications:
docker-compose up -d
```

4. Go to local database ([localhost:5432](http://localhost:5432)) with the following credentials and execute the SQL command:
4. Go to local database ([localhost:5432](http://localhost:5432)) with the following credentials and execute the SQL
command:

- **Username:** postgres
- **Password:** postgres
- **Username:** postgres
- **Password:** postgres

```sql
CREATE SCHEMA IF NOT EXISTS "kodemy-auth";
Expand All @@ -39,11 +40,11 @@ Before you can run the project, you will need the following applications:

5. Run individual services in IntelliJ with the following settings for `Active profiles`:

- `KodemyAuthApplication`: `local`
- `KodemyBackendApplication`: `local`
- `KodemyGatewayApplication`: `local`
- ~~`KodemyNotificationApplication`: `local`~~
- `KodemySearchApplication`: `local`
- `KodemyAuthApplication`: `local`
- `KodemyBackendApplication`: `local`
- `KodemyGatewayApplication`: `local`
- ~~`KodemyNotificationApplication`: `local`~~
- `KodemySearchApplication`: `local`

6. Once the project is running, you can access the API documentation (OpenAPI v3) for almost all microservices:

Expand All @@ -54,7 +55,8 @@ Before you can run the project, you will need the following applications:

### RabbitMQ

To access the RabbitMQ management UI, go to [http://localhost:15672](http://localhost:15672) with the following credentials:
To access the RabbitMQ management UI, go to [http://localhost:15672](http://localhost:15672) with the following
credentials:

- **Username:** rabbitmq
- **Password:** rabbitmq
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static RuntimeException throwIfFailure(Throwable throwable) {
return getRuntimeOrDefault(throwable, new InternalError500Exception());
}

public static RuntimeException throwIfFailure(Throwable throwable, String pattern, Object ...args) {
public static RuntimeException throwIfFailure(Throwable throwable, String pattern, Object... args) {
return getRuntimeOrDefault(throwable, new InternalError500Exception(pattern, args));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@

import java.security.Principal;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class AuthFacade {
private static SecurityContext getContext(){
private static SecurityContext getContext() {
return SecurityContextHolder.getContext();
}

Expand Down Expand Up @@ -60,7 +59,7 @@ public static Authentication setAuthentication(Authentication authentication) {
return setAuthentication(authentication, false);
}

public static boolean hasAnyAuthority(String... authorities){
public static boolean hasAnyAuthority(String... authorities) {
List<String> authorityList = Arrays.asList(authorities);
return getAuthentication()
.map(Authentication::getAuthorities)
Expand All @@ -70,7 +69,7 @@ public static boolean hasAnyAuthority(String... authorities){
.isPresent();
}

public static boolean hasAuthority(String authority){
public static boolean hasAuthority(String authority) {
return hasAnyAuthority(authority);
}
}
2 changes: 1 addition & 1 deletion config/opensearch-security/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ config:
#internalProxies: '.*' # trust all internal proxies, regex pattern
#remoteIpHeader: 'x-forwarded-for'
authc:
basic_internal_auth_domain: # <- Settings for Basic authentication
basic_internal_auth_domain: # <- Settings for Basic authentication
description: "Authenticate via HTTP Basic against internal users database"
http_enabled: true
transport_enabled: true
Expand Down
2 changes: 1 addition & 1 deletion config/opensearch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cluster.name: kodemy-os-cluster
node.name: kodemy-os-n1

# Comma-separated list of hostnames or IP addresses of nodes in the cluster
discovery.seed_hosts: ["kodemy-os-n1"]
discovery.seed_hosts: [ "kodemy-os-n1" ]

# Discovery type for single-node configuration
discovery.type: single-node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ app:
cors:
allowed-origins:
- ${network.route.front}

eureka:
client:
service-url:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@Configuration
public class LogbookConfiguration {
@Bean
public Logbook logbook(){
public Logbook logbook() {
return Logbook.create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import pl.sknikod.kodemyauth.infrastructure.database.RefreshToken;
import pl.sknikod.kodemyauth.infrastructure.database.User;
import pl.sknikod.kodemyauth.infrastructure.database.RefreshTokenRepository;
import pl.sknikod.kodemyauth.infrastructure.database.User;
import pl.sknikod.kodemyauth.infrastructure.database.UserRepository;
import pl.sknikod.kodemycommons.exception.InternalError500Exception;
import pl.sknikod.kodemycommons.exception.NotFound404Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import pl.sknikod.kodemyauth.configuration.SecurityConfiguration;
import pl.sknikod.kodemyauth.infrastructure.database.Provider;
import pl.sknikod.kodemyauth.infrastructure.database.Role;
import pl.sknikod.kodemyauth.infrastructure.database.User;
import pl.sknikod.kodemyauth.infrastructure.database.RoleRepository;
import pl.sknikod.kodemyauth.infrastructure.database.UserRepository;
import pl.sknikod.kodemyauth.infrastructure.database.*;
import pl.sknikod.kodemyauth.infrastructure.module.oauth2.provider.OAuth2Provider;
import pl.sknikod.kodemycommons.exception.InternalError500Exception;
import pl.sknikod.kodemycommons.exception.NotFound404Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface PermissionRepository extends JpaRepository<Permission, Long> {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;
import pl.sknikod.kodemyauth.configuration.SecurityConfiguration;
import pl.sknikod.kodemyauth.infrastructure.dao.RefreshTokenDao;
import pl.sknikod.kodemyauth.infrastructure.database.RefreshToken;
import pl.sknikod.kodemyauth.infrastructure.database.Role;
import pl.sknikod.kodemyauth.infrastructure.database.RoleRepository;
import pl.sknikod.kodemyauth.infrastructure.database.User;
import pl.sknikod.kodemyauth.infrastructure.dao.RefreshTokenDao;
import pl.sknikod.kodemyauth.infrastructure.module.auth.model.RefreshTokensResponse;
import pl.sknikod.kodemycommons.exception.InternalError500Exception;
import pl.sknikod.kodemycommons.security.JwtProvider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package pl.sknikod.kodemyauth.infrastructure.module.oauth2;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import pl.sknikod.kodemyauth.infrastructure.module.oauth2.provider.OAuth2Provider;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.web.client.RestTemplate;
import pl.sknikod.kodemyauth.infrastructure.dao.UserDao;
import pl.sknikod.kodemyauth.infrastructure.database.Permission;
import pl.sknikod.kodemyauth.infrastructure.database.Role;
import pl.sknikod.kodemyauth.infrastructure.database.RoleRepository;
import pl.sknikod.kodemyauth.infrastructure.database.User;
import pl.sknikod.kodemyauth.infrastructure.dao.UserDao;
import pl.sknikod.kodemyauth.infrastructure.module.oauth2.provider.OAuth2Provider;
import pl.sknikod.kodemyauth.infrastructure.module.oauth2.util.OAuth2UserPrincipal;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import pl.sknikod.kodemyauth.infrastructure.database.RefreshToken;
import pl.sknikod.kodemyauth.infrastructure.dao.RefreshTokenDao;
import pl.sknikod.kodemyauth.infrastructure.database.RefreshToken;
import pl.sknikod.kodemyauth.util.route.RouteRedirectStrategy;
import pl.sknikod.kodemycommons.exception.InternalError500Exception;
import pl.sknikod.kodemycommons.security.AuthFacade;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import pl.sknikod.kodemyauth.infrastructure.database.User;
import pl.sknikod.kodemyauth.infrastructure.dao.UserDao;
import pl.sknikod.kodemyauth.infrastructure.database.User;
import pl.sknikod.kodemycommons.exception.InternalError500Exception;
import pl.sknikod.kodemycommons.exception.content.ExceptionMsgPattern;
import pl.sknikod.kodemycommons.exception.content.ExceptionUtil;
Expand Down
2 changes: 1 addition & 1 deletion kodemy-auth/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ app:
route:
front: http://localhost:3000
gateway: ${network.route.gateway}

eureka:
client:
serviceUrl:
Expand Down
2 changes: 1 addition & 1 deletion kodemy-auth/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ spring:
clientId: Ov23liqWT50Vtt0A0uIg
clientSecret: 8d3b1c72e34f2077e433c46f0aea4b980c15226b
redirect-uri: ${app.security.oauth2.route.gateway}${app.security.oauth2.endpoints.callback}/github
scope: ['user:email', 'read:user']
scope: [ 'user:email', 'read:user' ]

management:
endpoints:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

<createTable tableName="roles_permissions">
<column name="permission_id" type="BIGINT">
<constraints nullable="false" foreignKeyName="fk_roles_permissions_permission" references="permissions(id)"/>
<constraints nullable="false" foreignKeyName="fk_roles_permissions_permission"
references="permissions(id)"/>
</column>
<column name="role_id" type="BIGINT">
<constraints nullable="false" foreignKeyName="fk_roles_permissions_role" references="roles(id)"/>
Expand Down Expand Up @@ -84,7 +85,7 @@
(2, 'ROLE_MODERATOR'),
(3, 'ROLE_ADMIN'),
(4, 'ROLE_SUPERADMIN');
]]></sql>
]]></sql>

<sql><![CDATA[
INSERT INTO permissions (id, name)
Expand All @@ -107,22 +108,24 @@
<sql><![CDATA[
-- ROLE_MODERATOR
INSERT INTO roles_permissions (role_id, permission_id)
VALUES (2, 1), -- CAN_MODIFY_TAGS
(2, 2), -- CAN_APPROVED_MATERIAL
(2, 3), -- CAN_GET_USER_INFO
(2, 4), -- CAN_VIEW_ALL_MATERIALS
(2, 5); -- CAN_DEPRECATE_MATERIAL
VALUES (2, 1), -- CAN_MODIFY_TAGS
(2, 2), -- CAN_APPROVED_MATERIAL
(2, 3), -- CAN_GET_USER_INFO
(2, 4), -- CAN_VIEW_ALL_MATERIALS
(2, 5);
-- CAN_DEPRECATE_MATERIAL
-- ROLE_ADMIN
INSERT INTO roles_permissions (role_id, permission_id)
VALUES (3, 1), -- CAN_MODIFY_TAGS
(3, 2), -- CAN_APPROVED_MATERIAL
(3, 6), -- CAN_AUTO_APPROVED_MATERIAL
(3, 7), -- CAN_GET_USERS
(3, 8), -- CAN_BANNING_USERS
(3, 3), -- CAN_GET_USER_INFO
(3, 4), -- CAN_VIEW_ALL_MATERIALS
(3, 5); -- CAN_DEPRECATE_MATERIAL
VALUES (3, 1), -- CAN_MODIFY_TAGS
(3, 2), -- CAN_APPROVED_MATERIAL
(3, 6), -- CAN_AUTO_APPROVED_MATERIAL
(3, 7), -- CAN_GET_USERS
(3, 8), -- CAN_BANNING_USERS
(3, 3), -- CAN_GET_USER_INFO
(3, 4), -- CAN_VIEW_ALL_MATERIALS
(3, 5);
-- CAN_DEPRECATE_MATERIAL
-- ROLE_SUPERADMIN
INSERT INTO roles_permissions (role_id, permission_id)
Expand All @@ -140,7 +143,7 @@
(4, 5), -- CAN_DEPRECATE_MATERIAL
(4, 12), -- CAN_BAN_MATERIAL
(4, 13); -- CAN_UNBAN_MATERIAL
]]></sql>
]]></sql>
</changeSet>

<changeSet id="0001-3" author="MarcinBator">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public OAuth2AuthorizationRequestRepository oAuth2SessionAuthRequestRepository()
}

@Bean
public OAuth2LoginSuccessHandler oAuth2SuccessProcessHandler(){
public OAuth2LoginSuccessHandler oAuth2SuccessProcessHandler() {
return Mockito.mock(OAuth2LoginSuccessHandler.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import java.util.Map;

public class OAuth2Factory {
private OAuth2Factory(){
private OAuth2Factory() {

}

public static OAuth2Provider.User oAuth2GithubUser(){
public static OAuth2Provider.User oAuth2GithubUser() {
return new GithubOAuth2Provider.GithubUser(Map.of(
"id", 1L,
"login", "username",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import pl.sknikod.kodemyauth.infrastructure.database.Provider;

public class ProviderFactory {
private ProviderFactory(){
private ProviderFactory() {
}

public static Provider provider(String providerType){
public static Provider provider(String providerType) {
var provider = new Provider();
provider.setId(1L);
provider.setProviderType(providerType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ private RoleFactory() {
public static Role roleUser = role("ROLE_USER");
public static Role roleAdmin = role("ROLE_ADMIN");

public static Role role(String roleName){
public static Role role(String roleName) {
Role role = new Role();
role.setId(1L);
role.setName(roleName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import java.util.UUID;

public class TokenFactory {
private TokenFactory() {}
private TokenFactory() {
}

public static RefreshToken refreshToken = refreshToken();
public static JwtProvider.Token jwtProviderToken = jwtProviderToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.dao.OptimisticLockingFailureException;
import pl.sknikod.kodemyauth.BaseTest;
import pl.sknikod.kodemyauth.factory.TokenFactory;
import pl.sknikod.kodemyauth.factory.UserFactory;
import pl.sknikod.kodemyauth.infrastructure.dao.RefreshTokenDao;
import pl.sknikod.kodemyauth.infrastructure.database.RefreshToken;
import pl.sknikod.kodemyauth.infrastructure.database.RefreshTokenRepository;
import pl.sknikod.kodemyauth.infrastructure.database.UserRepository;
import pl.sknikod.kodemyauth.BaseTest;
import pl.sknikod.kodemyauth.infrastructure.dao.RefreshTokenDao;
import pl.sknikod.kodemycommons.exception.InternalError500Exception;

import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import pl.sknikod.kodemyauth.BaseTest;
import pl.sknikod.kodemyauth.factory.RoleFactory;
import pl.sknikod.kodemyauth.infrastructure.dao.RoleDao;
import pl.sknikod.kodemyauth.infrastructure.database.Role;
import pl.sknikod.kodemyauth.infrastructure.database.RoleRepository;
import pl.sknikod.kodemyauth.BaseTest;
import pl.sknikod.kodemyauth.infrastructure.dao.RoleDao;
import pl.sknikod.kodemycommons.exception.NotFound404Exception;

import java.util.Optional;
Expand Down
Loading

0 comments on commit a40b6c4

Please sign in to comment.