Skip to content

Commit

Permalink
Fix #39 - key files with line break at end
Browse files Browse the repository at this point in the history
  • Loading branch information
judy2k committed Feb 20, 2017
1 parent d19bd92 commit ee926c3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/main/java/com/nexmo/client/auth/JWTAuthMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
import java.util.regex.Pattern;

public class JWTAuthMethod extends AbstractAuthMethod {
private static final Pattern pemPattern = Pattern.compile("-----BEGIN PRIVATE KEY-----\\n(.*\\n)-----END PRIVATE KEY-----", Pattern.MULTILINE | Pattern.DOTALL);
private static final Pattern pemPattern = Pattern.compile("-----BEGIN PRIVATE KEY-----" +
"(.*\\n)" +
"-----END PRIVATE KEY-----\\n?", Pattern.MULTILINE | Pattern.DOTALL);
public final int SORT_KEY = 10;
private String applicationId;
private JWTSigner signer;
Expand Down Expand Up @@ -114,4 +116,4 @@ protected String constructToken(long iat, String jti) {
public int getSortKey() {
return this.SORT_KEY;
}
}
}
15 changes: 8 additions & 7 deletions src/test/java/com/nexmo/client/auth/JWTAuthMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@


import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.JWTVerifyException;
import com.nexmo.client.TestUtils;
import org.apache.http.client.methods.RequestBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
Expand All @@ -43,17 +41,20 @@ public class JWTAuthMethodTest {
private JWTAuthMethod auth;

@Before
public void setUp() throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, IOException {
public void setUp() throws Exception {
this.testUtils = new TestUtils();

byte[] keyBytes = testUtils.loadKey("test/keys/application_key");
auth = new JWTAuthMethod("application-id", keyBytes);
}

@Test public void testSavedKey() throws Exception {
byte[] keyBytes = testUtils.loadKey("test/keys/application_key2");
auth = new JWTAuthMethod("application-id", keyBytes);
}

@Test
public void testConstructToken()
throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException, IOException,
SignatureException, JWTVerifyException {
public void testConstructToken() throws Exception {
String constructedToken = auth.constructToken(1234, "1111111");

byte[] keyBytes = testUtils.loadKey("test/keys/application_public_key.der");
Expand Down Expand Up @@ -97,4 +98,4 @@ public void testApply() throws Exception {
assertEquals(1, req.getHeaders("Authorization").length);
assertEquals("Bearer ", req.getFirstHeader("Authorization").getValue().substring(0, 7));
}
}
}
28 changes: 28 additions & 0 deletions src/test/resources/com/nexmo/client/test/keys/application_key2
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDqSe/genQY2KUz
xc1iqj2pgeH1k2SKHpIK8L8JLwfEEskhG92Ch9euivu30roKHTVMI4lXRrOL0OyM
uX0EZQGSuDvB8cZpOL+044oV3XInx/LwkEgHIsB7anv6rJ/fyxHTGvlc1c3DmQhl
J5ToFtu2DL8dY/nmYvdpeo74+0iOyTODiGCNrB8Kqc7zrx/KQM732BBrLeRs6TwY
+7lQYIebFQN5C9JvGWAlStw9IJO+dlJojMru9bz//B+rWe350m5WiVSFHnSP5K6o
t1SBuraoJRsdORwVb9dp0gU3UDkGApYgmBFTt0MJoCZWNA3QNkJ8clpjh8JJtIcF
dWFL2mvxAgMBAAECggEAZZd9/rbalNOMfzCsaLYtWs1JL/WjyQiMh1XxYIgWM/15
XXP5z1ocOkFl+UXVCgG0VLmsGj48KMqFaFgeT8OEtRxSPT0brhC/gC6Sd/y4PWvE
em/167I0CNAZxo8IHECwD/xIWOsU+FXpgANz3FfdGcnZLWNmv3H2mrRcPrantdHQ
oMiS2wCF+6Zyo0TTVdddHjvQAutznMEoQLdTvl3MtGY5WWchDzFAmwqDv/+ne9H7
aerpq3tv1Ng0uOfzw39UuUTqrYXrqYDGYtK2D5yoEzN6kt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