Skip to content

Commit

Permalink
Merge pull request ONDC-Official#67 from pratik-mazumdar/main
Browse files Browse the repository at this point in the history
Java Auth Header Generation
  • Loading branch information
sandeepshahi authored Apr 2, 2024
2 parents d1a6002 + 58dc0cc commit 5d513c2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
11 changes: 10 additions & 1 deletion utilities/on_subscibe-service/java/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,13 @@
}
]
}
}'```
}'
```

### Create Auth Header
To generate the auth header kindly use the following curl request:
```
curl --location 'localhost:8080/create-header' \
--header 'Content-Type: application/json' \
--data '{"value":{"abc":"test"},"private_key":"your_signing_private_key"}'
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
import java.net.http.HttpResponse;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;

import com.fasterxml.jackson.databind.JsonNode;
import org.json.JSONException;
Expand Down Expand Up @@ -42,13 +45,26 @@ public class Routes extends Utils{

@Autowired
private String gatewayUrl;
private Logger logger = LoggerFactory.getLogger(Routes.class);;
private final Logger logger = LoggerFactory.getLogger(Routes.class);;

@GetMapping("/get-keys")
public ResponseEntity<Map<String,byte[]>> getKeys (){
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(keys);
}

@PostMapping("/create-header")
public
String createHeader(@RequestBody JsonNode req) throws Exception {
long created = System.currentTimeMillis() / 1000L;
long expires = created + 300000;
String hashedReq = hashMassage(req.get("value").toString(),created,expires);
String signature = sign(Base64.getDecoder().decode(req.get("private_key").asText()),hashedReq.getBytes());
String subscriberId = "altiux.com";
String uniqueKeyId = "c9aa1b41-04e9-43e2-bd89-9ddcdecbf4cf";

return "Signature keyId=\"" + subscriberId + "|" + uniqueKeyId + "|" + "ed25519\"" + ",algorithm=\"ed25519\"," + "created=\"" + created + "\",expires=\"" + expires + "\",headers=\"(created) (expires)" + " digest\",signature=\"" + signature + "\"";
}

@PostMapping("/subscribe")
public ResponseEntity<String> subscribe(@RequestBody JsonNode subscribeBody) throws NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, JSONException, IOException, InterruptedException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
Expand Down Expand Up @@ -33,17 +34,34 @@ public static CryptoKeyPair generateEncDecKey() throws InvalidKeyException, NoSu
return new CryptoKeyPair(kp.getPublic().getEncoded(),kp.getPrivate().getEncoded());
}

public static String fromBase64(byte[] src){
public static String toBase64(byte[] src){
return Base64.getEncoder().encodeToString(src);
}

public static String hashMassage(String req, long created,long expires) throws Exception {
byte[] digest = generateBlakeHash(req);
return """
(created): %s
(expires): %s
digest: BLAKE-512=%s""".formatted(created,expires, toBase64(digest));
}
public static byte[] generateBlakeHash(String req) throws Exception {
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
Security.addProvider(new BouncyCastleProvider());
}
MessageDigest digest = MessageDigest.getInstance("BLAKE2B-512", BouncyCastleProvider.PROVIDER_NAME);
digest.reset();
digest.update(req.getBytes(StandardCharsets.UTF_8));
return digest.digest();
}

public static String sign(byte[] privateKey,byte[] message) {
// initialise signature variable
byte[] signature = new byte[Ed25519.SIGNATURE_SIZE];

// sign the received message with given private key
Ed25519.sign(privateKey, 0, message, 0, message.length, signature, 0);
return fromBase64(signature);
return toBase64(signature);
}

public static byte[] encryptDecrypt(int mode, byte[] challenge_string,byte[] privateKey, byte[] publicKey) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
Expand Down

0 comments on commit 5d513c2

Please sign in to comment.