From 65a67831185110ad765bf2c9bfa72978b5a43b1e Mon Sep 17 00:00:00 2001 From: Asma TEHAR Date: Sun, 5 Apr 2026 16:14:02 +0100 Subject: [PATCH] init --- .gitignore | 38 +++++++++++++ .idea/.gitignore | 3 ++ .idea/encodings.xml | 7 +++ .idea/misc.xml | 14 +++++ .idea/vcs.xml | 6 +++ README.md | 1 + pom.xml | 53 +++++++++++++++++++ src/main/java/com/triztech/Main.java | 17 ++++++ .../com/triztech/security/jwt/JwtUtils.java | 46 ++++++++++++++++ .../com/triztech/security/keys/KeyLoader.java | 44 +++++++++++++++ src/main/java/org/example/Main.java | 17 ++++++ 11 files changed, 246 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 README.md create mode 100644 pom.xml create mode 100644 src/main/java/com/triztech/Main.java create mode 100644 src/main/java/com/triztech/security/jwt/JwtUtils.java create mode 100644 src/main/java/com/triztech/security/keys/KeyLoader.java create mode 100644 src/main/java/org/example/Main.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..eaf91e2 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..a156f52 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e844abc --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..9661ac7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..a99aa6f --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +TRIZ SECURITY COMMON \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..f0ba74b --- /dev/null +++ b/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + com.triztech + triz-security-common + 1.0-SNAPSHOT + jar + + + 17 + + + + + + io.jsonwebtoken + jjwt-api + 0.12.5 + + + io.jsonwebtoken + jjwt-impl + 0.12.5 + runtime + + + io.jsonwebtoken + jjwt-jackson + 0.12.5 + runtime + + + + + com.fasterxml.jackson.core + jackson-core + 2.15.2 + + + com.fasterxml.jackson.core + jackson-databind + 2.15.2 + + + com.fasterxml.jackson.core + jackson-annotations + 2.15.2 + + + diff --git a/src/main/java/com/triztech/Main.java b/src/main/java/com/triztech/Main.java new file mode 100644 index 0000000..ab8fcae --- /dev/null +++ b/src/main/java/com/triztech/Main.java @@ -0,0 +1,17 @@ +package com.triztech; + +//TIP To Run code, press or +// click the icon in the gutter. +public class Main { + public static void main(String[] args) { + //TIP Press with your caret at the highlighted text + // to see how IntelliJ IDEA suggests fixing it. + System.out.printf("Hello and welcome!"); + + for (int i = 1; i <= 5; i++) { + //TIP Press to start debugging your code. We have set one breakpoint + // for you, but you can always add more by pressing . + System.out.println("i = " + i); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/triztech/security/jwt/JwtUtils.java b/src/main/java/com/triztech/security/jwt/JwtUtils.java new file mode 100644 index 0000000..8d708ba --- /dev/null +++ b/src/main/java/com/triztech/security/jwt/JwtUtils.java @@ -0,0 +1,46 @@ +package com.triztech.security.jwt; + +import io.jsonwebtoken.Jwts; +import io.jsonwebtoken.SignatureAlgorithm; +import io.jsonwebtoken.Claims; + +import java.security.PrivateKey; +import java.security.PublicKey; +import java.time.Instant; +import java.util.Date; +import java.util.Map; + +public class JwtUtils { + + // Génération (auth-triz uniquement) + public static String generateToken( + PrivateKey privateKey, + String userId, + String email, + String role, + Map extraClaims) { + + Instant now = Instant.now(); + Instant expiry = now.plusSeconds(3600); + + return Jwts.builder() + .subject(userId) // qui est l’utilisateur + .issuer("triz-auth") // qui a émis le token + .issuedAt(Date.from(now)) // quand + .expiration(Date.from(expiry)) // jusqu’à quand + .claim("email", email) + .claim("role", role) + .claims(extraClaims) + .signWith(privateKey, SignatureAlgorithm.RS256) //un hash cryptographique du contenuet, signé avec la clé privée + .compact(); + } + + // Validation (trizstock / trizdistribution) + public static Claims validateToken(String token, PublicKey publicKey) { + return Jwts.parser() + .verifyWith(publicKey) + .build() + .parseSignedClaims(token) + .getPayload(); + } +} diff --git a/src/main/java/com/triztech/security/keys/KeyLoader.java b/src/main/java/com/triztech/security/keys/KeyLoader.java new file mode 100644 index 0000000..ae64217 --- /dev/null +++ b/src/main/java/com/triztech/security/keys/KeyLoader.java @@ -0,0 +1,44 @@ +package com.triztech.security.keys; + +import java.security.KeyFactory; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.X509EncodedKeySpec; +import java.util.Base64; + +public class KeyLoader { + + public static PrivateKey loadPrivateKey(String pem) { + try { + pem = pem.replace("-----BEGIN PRIVATE KEY-----", "") + .replace("-----END PRIVATE KEY-----", "") + .replaceAll("\\s", ""); + + byte[] decoded = Base64.getDecoder().decode(pem); + PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decoded); + + return KeyFactory.getInstance("RSA").generatePrivate(keySpec); + + } catch (Exception e) { + throw new RuntimeException("Erreur lors du chargement de la clé privée", e); + } + } + + public static PublicKey loadPublicKey(String pem) { + try { + pem = pem.replace("-----BEGIN PUBLIC KEY-----", "") + .replace("-----END PUBLIC KEY-----", "") + .replaceAll("\\s", ""); + + byte[] decoded = Base64.getDecoder().decode(pem); + X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decoded); + + return KeyFactory.getInstance("RSA").generatePublic(keySpec); + + } catch (Exception e) { + throw new RuntimeException("Erreur lors du chargement de la clé publique", e); + } + } +} + diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java new file mode 100644 index 0000000..a02700f --- /dev/null +++ b/src/main/java/org/example/Main.java @@ -0,0 +1,17 @@ +package org.example; + +//TIP To Run code, press or +// click the icon in the gutter. +public class Main { + public static void main(String[] args) { + //TIP Press with your caret at the highlighted text + // to see how IntelliJ IDEA suggests fixing it. + System.out.printf("Hello and welcome!"); + + for (int i = 1; i <= 5; i++) { + //TIP Press to start debugging your code. We have set one breakpoint + // for you, but you can always add more by pressing . + System.out.println("i = " + i); + } + } +} \ No newline at end of file