Skip to content

Commit 3f92b42

Browse files
authored
Feature auth update (#89)
* update auth * changes files * updating files * fixed files
1 parent c38674e commit 3f92b42

File tree

14 files changed

+67
-25
lines changed

14 files changed

+67
-25
lines changed

.github/workflows/deploy.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ jobs:
1818
steps:
1919
- uses: actions/checkout@v3
2020

21-
- name: Set up JDK 17
21+
- name: Set up JDK 21
2222
uses: actions/setup-java@v3
2323
with:
24-
java-version: '17'
24+
java-version: '21'
2525
distribution: 'adopt'
2626
architecture: x64
2727
cache: 'maven'

deploy/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ version: '3.8'
22

33
services:
44
postgres_service:
5+
container_name: postgres-chat-db
56
image: postgres:16.3
67
restart: always
78
environment:

pom.xml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>3.1.1</version>
9-
<relativePath/> <!-- lookup parent from repository -->
8+
<version>3.4.1</version>
9+
<relativePath/>
1010
</parent>
1111
<groupId>com.chat</groupId>
1212
<artifactId>chatYourWay</artifactId>
1313
<version>0.0.1-SNAPSHOT</version>
1414
<name>chatYourWay</name>
1515
<description>WebSocket chat</description>
1616
<properties>
17-
<java.version>17</java.version>
17+
<maven.compiler.source>21</maven.compiler.source>
18+
<maven.compiler.target>21</maven.compiler.target>
1819
</properties>
1920
<dependencies>
2021
<dependency>
@@ -67,7 +68,7 @@
6768
<dependency>
6869
<groupId>org.postgresql</groupId>
6970
<artifactId>postgresql</artifactId>
70-
<scope>runtime</scope>
71+
<version>42.7.4</version>
7172
</dependency>
7273

7374
<!--Redis-->
@@ -85,8 +86,10 @@
8586
<dependency>
8687
<groupId>org.flywaydb</groupId>
8788
<artifactId>flyway-core</artifactId>
89+
<version>9.22.1</version>
8890
</dependency>
8991

92+
9093
<!--Others-->
9194
<dependency>
9295
<groupId>org.projectlombok</groupId>

src/main/java/com/chat/yourway/ChatYourWayApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.chat.yourway;
22

3+
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
4+
import io.swagger.v3.oas.annotations.info.Info;
35
import org.springframework.boot.SpringApplication;
46
import org.springframework.boot.autoconfigure.SpringBootApplication;
57
import org.springframework.scheduling.annotation.EnableAsync;

src/main/java/com/chat/yourway/config/redis/RedisConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class RedisConfig {
1919
@Bean
2020
public JedisConnectionFactory jedisConnectionFactory() {
2121
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(
22-
properties.getHost(), properties.getPort());
22+
properties.getHost(), properties.getPort());
2323
if (properties.getProfile().equals("prod")) {
2424
redisStandaloneConfiguration.setPassword(properties.getPassword());
2525
}

src/main/java/com/chat/yourway/controller/rest/AuthenticationController.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import lombok.RequiredArgsConstructor;
1919
import org.springframework.http.HttpHeaders;
2020
import org.springframework.http.HttpStatus;
21+
import org.springframework.http.ResponseEntity;
2122
import org.springframework.security.core.Authentication;
2223
import org.springframework.web.bind.annotation.*;
2324

@@ -83,15 +84,18 @@ public AuthResponseDto refreshToken(HttpServletRequest request) {
8384
}
8485

8586
@Operation(summary = "Activate account", responses = {
86-
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_ACTIVATED_ACCOUNT),
87-
@ApiResponse(responseCode = "404", description = EMAIL_TOKEN_NOT_FOUND,
88-
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class)))
89-
})
90-
@PostMapping(path = ACTIVATE, consumes = APPLICATION_JSON_VALUE)
91-
public void activateAccount() {
92-
authService.activateAccount();
87+
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_ACTIVATED_ACCOUNT),
88+
@ApiResponse(responseCode = "404", description = EMAIL_TOKEN_NOT_FOUND,
89+
content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))),
90+
@ApiResponse(responseCode = "400", description = "Invalid or expired token")
91+
})
92+
@PostMapping(path = ACTIVATE)
93+
public ResponseEntity<String> activateAccount(@RequestParam("token") String token) {
94+
authService.activateAccount(token); // Передаем токен для активации
95+
return ResponseEntity.ok("Account successfully activated");
9396
}
9497

98+
9599
@Operation(summary = "Resend email", responses = {
96100
@ApiResponse(responseCode = "200", description = SUCCESSFULLY_ACTIVATED_ACCOUNT)
97101
})

src/main/java/com/chat/yourway/model/Contact.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class Contact implements UserDetails {
2121
@GeneratedValue(strategy = GenerationType.UUID)
2222
private UUID id;
2323

24-
@Column(nullable = false)
24+
@Column(nullable = false, unique = true)
2525
private String nickname;
2626

2727
@Column(nullable = false)

src/main/java/com/chat/yourway/security/JwtAuthFilter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ public class JwtAuthFilter extends OncePerRequestFilter {
3737
protected void doFilterInternal(@NonNull HttpServletRequest request,
3838
@NonNull HttpServletResponse response,
3939
@NonNull FilterChain filterChain) {
40+
41+
String path = request.getRequestURI();
42+
43+
if (path.startsWith("/auth/activate")) {
44+
filterChain.doFilter(request, response);
45+
return;
46+
}
47+
4048
if (isNotAuthorizationHeader(request) && isNotTokenParameter(request)) {
4149
log.warn("Request without authorization. Header or parameter does not contain {}", AUTHORIZATION);
4250
filterChain.doFilter(request, response);

src/main/java/com/chat/yourway/security/JwtService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public String extractEmail(String token) {
3131
return extractClaim(token, Claims::getSubject);
3232
}
3333

34+
public String extractEmailToken(String token) {
35+
return extractClaim(token, Claims::getSubject);
36+
}
37+
3438
public String generateAccessToken(UserDetails userDetails) {
3539
return generateAccessTokenBuild(new HashMap<>(), userDetails);
3640
}

src/main/java/com/chat/yourway/service/ActivateAccountService.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.chat.yourway.service;
22

33
import com.chat.yourway.dto.common.EmailMessageInfoDto;
4+
import com.chat.yourway.exception.ContactNotFoundException;
5+
import com.chat.yourway.exception.InvalidTokenException;
46
import com.chat.yourway.model.Contact;
7+
import com.chat.yourway.security.JwtService;
58
import lombok.RequiredArgsConstructor;
69
import lombok.extern.slf4j.Slf4j;
710
import org.springframework.stereotype.Service;
@@ -17,16 +20,24 @@ public class ActivateAccountService {
1720
private final EmailSenderService emailSenderService;
1821
private final EmailMessageFactoryService emailMessageFactoryService;
1922
private final ContactService contactService;
23+
private final JwtService jwtService;
2024

2125
@Transactional
22-
public void activateAccount() {
23-
log.trace("Started activateAccount by email");
26+
public void activateAccount(String token) {
27+
log.trace("Started account activation for token: {}", token);
28+
29+
String email = jwtService.extractEmailToken(token);
30+
31+
Contact contact = contactService.findByEmail(email);
32+
if (contact == null) {
33+
log.warn("Contact not found for email: {}", email);
34+
throw new ContactNotFoundException("Contact not found for email: " + email);
35+
}
2436

25-
final var contact = contactService.getCurrentContact();
2637
contact.setActive(true);
2738
contactService.save(contact);
2839

29-
log.info("Account is activate for contact email [{}]", contact.getEmail());
40+
log.info("Account successfully activated for email: {}", email);
3041
}
3142

3243
public void sendVerifyEmail(Contact contact, String clientHost) {

0 commit comments

Comments
 (0)