-
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.
Merge pull request #20 from rajumb0232/auth/updates
Refactoring Authentication Module
- Loading branch information
Showing
26 changed files
with
610 additions
and
209 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
// use this template to create the .env file. | ||
DOMAIN= | ||
IS_HTTPS= | ||
MAIL_USERNAME= | ||
MAIL_PASSWORD= | ||
POSTGRES_URL= | ||
|
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
5 changes: 5 additions & 0 deletions
5
E-Stores-API/src/main/java/com/devb/estores/enums/TokenType.java
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,5 @@ | ||
package com.devb.estores.enums; | ||
|
||
public enum TokenType { | ||
ACCESS, REFRESH; | ||
} |
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
10 changes: 10 additions & 0 deletions
10
E-Stores-API/src/main/java/com/devb/estores/exceptions/InvalidJwtException.java
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,10 @@ | ||
package com.devb.estores.exceptions; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public class InvalidJwtException extends RuntimeException { | ||
private final String message; | ||
} |
35 changes: 35 additions & 0 deletions
35
E-Stores-API/src/main/java/com/devb/estores/mapper/UserMapper.java
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,35 @@ | ||
package com.devb.estores.mapper; | ||
|
||
import com.devb.estores.enums.UserRole; | ||
import com.devb.estores.model.User; | ||
import com.devb.estores.requestdto.UserRequest; | ||
import com.devb.estores.responsedto.UserResponse; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Component | ||
public class UserMapper { | ||
|
||
public UserResponse mapToUserResponse(User user) { | ||
return UserResponse.builder() | ||
.userId(user.getUserId()) | ||
.username(user.getUsername()) | ||
.roles(user.getRoles().stream().map(UserRole::name).toList()) | ||
.email(user.getEmail()) | ||
.isEmailVerified(user.isEmailVerified()) | ||
.build(); | ||
} | ||
|
||
public User mapToUserEntity(UserRequest userRequest, UserRole role) { | ||
return User.builder() | ||
.username(userRequest.getEmail().split("@gmail.com")[0]) | ||
.email(userRequest.getEmail()) | ||
.password(userRequest.getPassword()) | ||
.roles(role.equals(UserRole.SELLER) | ||
? Arrays.asList(UserRole.SELLER, UserRole.CUSTOMER) | ||
: List.of(UserRole.CUSTOMER)) | ||
.build(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
E-Stores-API/src/main/java/com/devb/estores/model/TokenIdentification.java
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,28 @@ | ||
package com.devb.estores.model; | ||
|
||
import com.devb.estores.enums.TokenType; | ||
import com.devb.estores.util.IdGenerator; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
import org.hibernate.annotations.GenericGenerator; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Entity | ||
@Table(name = "token_identifications") | ||
@Getter | ||
@Setter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class TokenIdentification { | ||
@Id | ||
@GeneratedValue(generator = "custom") | ||
@GenericGenerator(name = "custom", type = IdGenerator.class) | ||
private String tokenId; | ||
private String username; | ||
private String deviceId; | ||
private TokenType tokenType; | ||
private String jti; | ||
private LocalDateTime expiration; | ||
} |
23 changes: 23 additions & 0 deletions
23
E-Stores-API/src/main/java/com/devb/estores/repository/TokenIdentificationRepo.java
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 @@ | ||
package com.devb.estores.repository; | ||
|
||
import com.devb.estores.enums.TokenType; | ||
import com.devb.estores.model.TokenIdentification; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface TokenIdentificationRepo extends JpaRepository<TokenIdentification, String> { | ||
|
||
Optional<TokenIdentification> findByUsernameAndDeviceIdAndTokenType(String username, String deviceId, TokenType tokenType); | ||
Page<TokenIdentification> findByExpirationBefore(LocalDateTime now, Pageable pageable); | ||
|
||
void deleteByUsernameAndDeviceIdAndTokenType(String username, String deviceId, TokenType tokenType); | ||
|
||
List<TokenIdentification> findAllByUsernameAndDeviceIdNot(String username, String currentDeviceId); | ||
|
||
List<TokenIdentification> findAllByUsername(String username); | ||
} |
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
64 changes: 64 additions & 0 deletions
64
E-Stores-API/src/main/java/com/devb/estores/security/RequestUtils.java
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,64 @@ | ||
package com.devb.estores.security; | ||
|
||
import com.devb.estores.util.SimpleResponseStructure; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.http.HttpStatus; | ||
|
||
import java.io.IOException; | ||
|
||
public class RequestUtils { | ||
|
||
public static final String SEC_CH_UA = "sec-ch-ua"; | ||
public static final String SEC_CH_UA_PLATFORM = "sec-ch-ua-platform"; | ||
public static final String SEC_CH_UA_MOBILE = "sec-ch-ua-mobile"; | ||
public static final String USER_AGENT = "user-agent"; | ||
|
||
public static String extractCookie(String cookieName, Cookie[] cookies) { | ||
String cookieValue = null; | ||
if (cookies != null) | ||
for (Cookie cookie : cookies) { | ||
if (cookie.getName().equals(cookieName)) { | ||
cookieValue = cookie.getValue(); | ||
break; | ||
} | ||
} | ||
return cookieValue; | ||
} | ||
|
||
public static void handleException(HttpServletResponse response, String message) throws IOException { | ||
response.setStatus(HttpStatus.UNAUTHORIZED.value()); | ||
response.setContentType("Application/json"); | ||
response.setHeader("error", message); | ||
SimpleResponseStructure structure = SimpleResponseStructure.builder() | ||
.status(HttpStatus.UNAUTHORIZED.value()) | ||
.message(message) | ||
.build(); | ||
new ObjectMapper().writeValue(response.getOutputStream(), structure); | ||
} | ||
|
||
public static String extractDeviceId(Cookie[] cookies) { | ||
String did = ""; | ||
if (cookies != null) | ||
for (Cookie cookie : cookies) { | ||
if (cookie.getName().equals("did")) { | ||
did = cookie.getValue(); | ||
break; | ||
} | ||
} | ||
return did; | ||
} | ||
|
||
/** | ||
* Helps in extracting the browser name from the given secChUa | ||
*/ | ||
public static String extractBrowserName(String secChUa) { | ||
if (secChUa != null) { | ||
int start = secChUa.indexOf('"') + 1; | ||
int end = secChUa.indexOf("\"", start); | ||
return secChUa.substring(start, end); | ||
} else | ||
return null; | ||
} | ||
} |
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.