Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion backend/auth-service/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ bin/
.vscode/

### Mac OS ###
.DS_Store
.DS_Store

/src/main/resources/
6 changes: 5 additions & 1 deletion backend/auth-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ dependencies {
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5'

// implementation 'org.bouncycastle:bcprov-jdk16:1.45'
implementation 'org.bouncycastle:bcpkix-jdk15on:1.70'
implementation 'org.postgresql:postgresql:42.7.3'
implementation 'redis.clients:jedis:5.1.0'
implementation 'de.mkammerer:argon2-jvm:2.11'
}

tasks.named('test') {
Expand Down
1 change: 1 addition & 0 deletions backend/auth-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>

</dependencies>

This file was deleted.

22 changes: 0 additions & 22 deletions backend/auth-service/src/main/java/com/city/demo/AuthService.java

This file was deleted.

36 changes: 0 additions & 36 deletions backend/auth-service/src/main/java/com/city/demo/JwtTest.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.city.demo.controller;

import com.city.demo.service.dto.RefreshRequest;
import com.city.demo.service.impl.AuthServiceImpl;
import com.city.demo.service.dto.LoginRequest;
import com.city.demo.service.dto.TokenResponse;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/auth")
public class AuthController {

private static final Logger logger = LoggerFactory.getLogger(AuthController.class);

@Autowired
private AuthServiceImpl authService;

@PostMapping("/login")
public ResponseEntity<TokenResponse> login(@RequestBody LoginRequest request, HttpServletResponse response) {
logger.info("Login request: {}", request);

TokenResponse res = authService.login(request.getLogin(), request.getPassword());

Cookie cookie = new Cookie("refreshToken", res.getRefreshToken());
cookie.setPath("/");
// TODO: Change to the refresh token ttl value
cookie.setMaxAge(420);
cookie.setHttpOnly(true);
response.addCookie(cookie);

return ResponseEntity.ok(res);
}

@PostMapping("/refresh")
public ResponseEntity<TokenResponse> refresh(@RequestBody RefreshRequest request, HttpServletResponse response) {
TokenResponse res = authService.refresh(request.getRefreshToken());

Cookie cookie = new Cookie("refreshToken", res.getRefreshToken());
cookie.setPath("/");
// TODO: Change to the refresh token ttl value
cookie.setMaxAge(420);
cookie.setHttpOnly(true);
response.addCookie(cookie);

return ResponseEntity.ok(res);
}
}
47 changes: 47 additions & 0 deletions backend/auth-service/src/main/java/com/city/demo/domain/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.city.demo.domain;

import jakarta.persistence.*;

import java.util.UUID;

@Entity
@Table(name = "users")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.UUID)
private UUID id;

@Column(name = "email", unique = true, nullable = false)
private String email;

@Column(name = "password", nullable = false)
private byte[] password;

public User(){

}
public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public byte[] getPassword() {
return password;
}

public void setPassword(byte[] password) {
this.password = password;
}

public void setId(UUID id) {
this.id = id;
}

public UUID getId() {
return id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.city.demo.repository;

import com.city.demo.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.UUID;

@Repository
public interface UserRepository extends JpaRepository<User, UUID> {
User findByEmail(String email);
// User findById(UUID id);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.city.demo.security;

import de.mkammerer.argon2.Argon2;
import de.mkammerer.argon2.Argon2Factory;


public class HashPassword {
// public static String hashPassword(String password) {
// Argon2 argon2 = Argon2Factory.create(Argon2Factory.Argon2Types.ARGON2id);
//
// int iterations = 3;
// int memory = 65536; // 64MB
// int parallelism = 1;
//
// try {
// return argon2.hash(iterations, memory, parallelism, password.toCharArray());
// } finally {
// argon2.wipeArray(password.toCharArray()); // Optional
// }
// }

public static boolean verifyPassword(String hashedPassword, String password) {
Argon2 argon2 = Argon2Factory.create(Argon2Factory.Argon2Types.ARGON2id);
return argon2.verify(hashedPassword, password.toCharArray());
}

public static void main(String[] args) {
String rawPassword = "1234ddd";
String HashedPassword = "$argon2id$v=19$m=65536,t=3,p=1$5a8tHxwH8HmE6h3IbmNw3A$SU0R8Pnmm8u05GVrec4aTMUgNzSK5txXPJK9NumLurE";
boolean match = verifyPassword(HashedPassword, rawPassword);
System.out.println("Password match: " + match);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.city.demo;
package com.city.demo.security;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.city.demo.security;

import com.city.demo.utils.PemUtils;
import io.jsonwebtoken.Claims;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;
import java.util.Map;

public class JwtTest {

private final static String PRIVATE_KEY_PATH = "private.pem";
private final static String PUBLIC_KEY_PATH = "public.pem";

public static void main(String[] args) {
// 1. Generate a secret key
try {
RSAPublicKey publicKey = PemUtils.loadPublicKey(PUBLIC_KEY_PATH);
RSAPrivateKey privateKey = PemUtils.loadPrivateKey(PRIVATE_KEY_PATH);

// 2. Create an instance of JwtUtil
JwtUtil jwtUtil = new JwtUtil();

// 3. Generate a token
Map<String, String> claims = new HashMap<>();
claims.put("username", "testuser");
claims.put("role", "admin");
String token = jwtUtil.generateAccessToken(claims);
System.out.println("Generated token: " + token);
} catch (Exception e) {
System.out.println(":C");
}

}
}
Loading
Loading