Skip to content

Commit ff49aea

Browse files
committed
add getToken() to obtain string representation of the token
1 parent 292dcbe commit ff49aea

File tree

6 files changed

+31
-6
lines changed

6 files changed

+31
-6
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ script:
1515
after_success:
1616
- bash <(curl -s https://codecov.io/bash)
1717
after_failure:
18-
- cat file:///home/travis/build/auth0/java-jwt/lib/build/reports/tests/index.html
18+
- cat $HOME/travis/build/auth0/java-jwt/lib/build/reports/tests/index.html
1919
branches:
2020
only:
2121
- v3

lib/src/main/java/com/auth0/jwt/JWT.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,9 @@ public String getContentType() {
117117
public String getKeyId() {
118118
return jwt.getKeyId();
119119
}
120+
121+
@Override
122+
public String getToken() {
123+
return jwt.getToken();
124+
}
120125
}

lib/src/main/java/com/auth0/jwt/JWTDecoder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
@SuppressWarnings("WeakerAccess")
1919
final class JWTDecoder implements JWT {
2020

21+
private final String token;
2122
private Header header;
2223
private Payload payload;
2324
private String signature;
2425

2526
private JWTDecoder(String jwt) throws JWTDecodeException {
27+
this.token = jwt;
2628
parseToken(jwt);
2729
}
2830

@@ -124,4 +126,8 @@ public String getSignature() {
124126
return signature;
125127
}
126128

129+
@Override
130+
public String getToken() {
131+
return token;
132+
}
127133
}

lib/src/main/java/com/auth0/jwt/interfaces/JWT.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@
44
* The JWT class represents a Json Web Token.
55
*/
66
public interface JWT extends Payload, Header, Signature {
7+
8+
/**
9+
* Returns the String representation of the token.
10+
*
11+
* @return the String representation of the token.
12+
*/
13+
String getToken();
714
}

lib/src/test/java/com/auth0/jwt/JWTDecoderTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.apache.commons.codec.binary.Base64;
88
import org.hamcrest.collection.IsCollectionWithSize;
99
import org.hamcrest.core.IsCollectionContaining;
10-
import org.junit.Ignore;
1110
import org.junit.Rule;
1211
import org.junit.Test;
1312
import org.junit.rules.ExpectedException;
@@ -61,14 +60,13 @@ public void shouldThrowIfHeaderHasInvalidJSONFormat() throws Exception {
6160
customJWT(invalidJson, validJson, "signature");
6261
}
6362

64-
// toString
63+
// getToken
6564
@Test
66-
@Ignore("Pending implementation")
6765
public void shouldGetStringToken() throws Exception {
6866
JWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ");
6967
assertThat(jwt, is(notNullValue()));
70-
assertThat(jwt.toString(), is(notNullValue()));
71-
assertThat(jwt.toString(), is("eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ"));
68+
assertThat(jwt.getToken(), is(notNullValue()));
69+
assertThat(jwt.getToken(), is("eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ"));
7270
}
7371

7472
// Parts

lib/src/test/java/com/auth0/jwt/JWTTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ public void shouldDecodeAStringToken() throws Exception {
4242
assertThat(jwt, is(notNullValue()));
4343
}
4444

45+
// getToken
46+
@Test
47+
public void shouldGetStringToken() throws Exception {
48+
JWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ");
49+
assertThat(jwt, is(notNullValue()));
50+
assertThat(jwt.getToken(), is(notNullValue()));
51+
assertThat(jwt.getToken(), is("eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ"));
52+
}
53+
4554

4655
// Verify
4756

0 commit comments

Comments
 (0)