1

Iam trying to get access token using JWT Grant authentication for the embedded signing (docusign e-signature) implemetation.
Provided RSA key pairs (Private&public) keys in .pem file and passing private key in byte[] as to request JWTUserToken. pem object is returning null content.Is this a jar version mismatch? if any kindly suggest.

Code:

public void populateApiClientConfigurations() throws IOException, IllegalArgumentException, ApiException {
    
    ApiClient apiClient = new ApiClient("https://demo.docusign.net"); 
     String integratorKey = "0789a33f-ac9b-4298-b699-18608f8be4f3";
     String UserId = "b945b788-6266-4ac0-b7b6-1aae495186a8";
    List<String> scopes = new ArrayList<>();
    scopes.add(OAuth.Scope_SIGNATURE);
    String privateKeyFilePath="D:\\Demodocs\\privatekey.pem";
    byte[] privateKey = loadKeyFile(privateKeyFilePath);
    Long time = 3600L;
    OAuthToken oauthToken = apiClient.requestJWTUserToken(integratorKey,UserId,scopes,privateKey,time);
    System.out.println((oauthToken.getAccessToken()));
    
}

public static byte[] loadKeyFile(String filePath) {

    File inFile = new File(filePath);
    long fileLen = inFile.length();
    Reader reader = null;
    PemObject pemObject = null;
    try {
        reader = new FileReader(inFile);

        char[] content = new char[(int) fileLen];
        reader.read(content);
        String str = new String(content);
        StringReader stringreader = new StringReader(str);
        PemReader pem = new PemReader(stringreader);
        pemObject = pem.readPemObject();
      
    } catch (Exception e) {
        e.printStackTrace();
    }
    return pemObject.getContent();
}

Error:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.bouncycastle.util.io.pem.PemObject.getContent()" because "pemObject" is null
at Demo.CommonService.loadKeyFile(CommonService.java:165)
at Demo.CommonService.populateApiClientConfigurations(CommonService.java:129)
at Demo.CommonService.main(CommonService.java:56)
  • First, `Throwable::printStackTrace()` does not handle an exception. Second, `pemObject` is `null` either because `pem.readPemObject()` returns `null` or because any of the calls in the `try` block issued an exception that you do not show here. – tquadrat Aug 02 '22 at 11:03
  • Never catch an exception you cannot handle!! – tquadrat Aug 02 '22 at 11:04
  • Try to go to https://developers.docusign.com/docs/esign-rest-api/quickstart/ and select Java and see if that works. This will set everything correctly for you – Inbar Gazit Aug 02 '22 at 22:26
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – vatbub Aug 04 '22 at 18:02

1 Answers1

1

I wanted to share some code from Docusign's Java SDK: https://github.com/docusign/docusign-esign-java-client/blob/master/src/main/java/com/docusign/esign/client/auth/JWTUtils.java#L180. We can just look at lines 180-190. Breaking those lines down, this would be the code for getting the bytes[] from a given filePath:

File pemFile = new File(filepath);
Reader reader = new FileReader(pemFile);
PemReader reader = new PemReader(reader);
PemObject pemObject = reader.readPemObject();
byte[] bytes = pemObject.getContent();

Your code includes StringReader which we do not use. I'm not 100% sure if you are able to, but if your main goal is to just get the bytes, may I suggest a code block like:

File inFile = new File(filePath);
Reader reader = new FileReader(inFile);
PemReader pem = new PemReader(reader);
PemObject pemObject = pem.readPemObject();
byte[] bytes = pemObject.getContent();

I'd also recommend adding validation of the file/file path like on lines 183-186.

Please let me know if you have further issues.