Skip to content

Commit 1a5f59a

Browse files
samsternbergclaude
andcommitted
feat(auth): Add JSON object context support for Conditional Data Access
- Add typed getContext() (String), getContextAsObject(), getContextAsMap() to Credentials; @SuppressWarnings scoped to Credentials where the invariant is guaranteed by the typed setContext overloads - Remove unchecked cast and @SuppressWarnings from Utils.generateBearerToken by using the new typed accessors - Assert getContextAsObject/getContextAsMap behaviour in CredentialsTests Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
1 parent 5ce9c9e commit 1a5f59a

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

src/main/java/com/skyflow/config/Credentials.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,19 @@ public void setRoles(ArrayList<String> roles) {
3333
this.roles = roles;
3434
}
3535

36-
public Object getContext() {
36+
public String getContext() {
37+
return context instanceof String ? (String) context : null;
38+
}
39+
40+
public Object getContextAsObject() {
3741
return context;
3842
}
3943

44+
@SuppressWarnings("unchecked")
45+
public Map<String, Object> getContextAsMap() {
46+
return context instanceof Map ? (Map<String, Object>) context : null;
47+
}
48+
4049
public void setContext(String context) {
4150
this.context = context;
4251
}

src/main/java/com/skyflow/utils/Utils.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,27 @@ public static String getVaultURL(String clusterId, Env env) {
4747
return sb.toString();
4848
}
4949

50-
@SuppressWarnings("unchecked")
5150
public static String generateBearerToken(Credentials credentials) throws SkyflowException {
5251
if (credentials.getPath() != null) {
5352
BearerToken.BearerTokenBuilder builder = BearerToken.builder()
5453
.setCredentials(new File(credentials.getPath()))
5554
.setRoles(credentials.getRoles());
56-
Object ctx = credentials.getContext();
57-
if (ctx instanceof String) {
58-
builder.setCtx((String) ctx);
59-
} else if (ctx instanceof Map) {
60-
builder.setCtx((Map<String, Object>) ctx);
55+
Map<String, Object> ctxMap = credentials.getContextAsMap();
56+
if (ctxMap != null) {
57+
builder.setCtx(ctxMap);
58+
} else {
59+
builder.setCtx(credentials.getContext());
6160
}
6261
return builder.build().getBearerToken();
6362
} else if (credentials.getCredentialsString() != null) {
6463
BearerToken.BearerTokenBuilder builder = BearerToken.builder()
6564
.setCredentials(credentials.getCredentialsString())
6665
.setRoles(credentials.getRoles());
67-
Object ctx = credentials.getContext();
68-
if (ctx instanceof String) {
69-
builder.setCtx((String) ctx);
70-
} else if (ctx instanceof Map) {
71-
builder.setCtx((Map<String, Object>) ctx);
66+
Map<String, Object> ctxMap = credentials.getContextAsMap();
67+
if (ctxMap != null) {
68+
builder.setCtx(ctxMap);
69+
} else {
70+
builder.setCtx(credentials.getContext());
7271
}
7372
return builder.build().getBearerToken();
7473
} else {

src/test/java/com/skyflow/config/CredentialsTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ public void testValidMapContextInCredentials() {
281281
ctxMap.put("user_id", "user_12345");
282282
credentials.setContext(ctxMap);
283283
Validations.validateCredentials(credentials);
284+
Assert.assertNull(credentials.getContext());
285+
Assert.assertEquals(ctxMap, credentials.getContextAsObject());
286+
Assert.assertEquals(ctxMap, credentials.getContextAsMap());
284287
} catch (SkyflowException e) {
285288
Assert.fail(INVALID_EXCEPTION_THROWN);
286289
}

0 commit comments

Comments
 (0)