Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JWTTools #313

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions core/src/main/java/tech/ydb/core/auth/JwtUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,12 @@ public class JwtUtils {
private JwtUtils() { }

private static class JwtClaims {
@SerializedName("iss")
private String issuer;
@SerializedName("sub")
private String subject;
@SerializedName("aud")
private String audience;
@SerializedName("exp")
private Long expiredAt;
@SerializedName("nbf")
private Long notBeforeAt;
@SerializedName("iat")
private Long issuedAt;
@SerializedName("jti")
private String jwtID;

public String getIssuer() {
return this.issuer;
}

public String getSubject() {
return this.subject;
}

public String getAudience() {
return this.audience;
}

public Long getExpiredAt() {
return this.expiredAt;
}

public Long getNotBeforeAt() {
return this.notBeforeAt;
}

public Long getIssuedAt() {
return this.issuedAt;
}

public String getJwtID() {
return this.jwtID;
}
}

public static Instant extractExpireAt(String jwt, Instant defaultValue) {
Expand Down
27 changes: 27 additions & 0 deletions core/src/test/java/tech/ydb/core/auth/JwtUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tech.ydb.core.auth;

import java.time.Instant;

import org.junit.Assert;
import org.junit.Test;

/**
*
* @author Aleksandr Gorshenin
*/
public class JwtUtilsTest {

@Test
public void parseTest() {
// { "alg": "HS256", "typ": "JWT" }.{ "sub": "1234567890", "iat": 1516239022, "exp": 1726544488 }
String jwt1 = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjE3MjY1NDQ0ODh9";
// { "alg": "HS256", "typ": "JWT" }.{ "aud": "Base", "sub": "1234567890", "iat": 1516239022, "exp": 1726544488 }
String jwt2 = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJCYXNlIiwic3ViIjoiMTIzNDU2Nzg5MCIsImlhdCI6MTUxNjIzOTAyMiwiZXhwIjoxNzI2NTQ0NDg4fQ";
// { "alg": "HS256", "typ": "JWT" }.{ "aud": [ "Base" ], "sub": "1234567890", "iat": 1516239022, "exp": 1726544488 }
String jwt3 = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiQmFzZSJdLCJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjE3MjY1NDQ0ODh9";

Assert.assertEquals(Instant.ofEpochSecond(1726544488), JwtUtils.extractExpireAt(jwt1, Instant.EPOCH));
Assert.assertEquals(Instant.ofEpochSecond(1726544488), JwtUtils.extractExpireAt(jwt2, Instant.EPOCH));
Assert.assertEquals(Instant.ofEpochSecond(1726544488), JwtUtils.extractExpireAt(jwt3, Instant.EPOCH));
}
}