java - Accessing JWT nested claims with jose4j -
i having trouble accessing nested claims jwt using jose4j. have jwt claim set looks this:
{ "iss": "awesome.issuer", "iat": 1300819370, "exp": 1300819380, "clm": "string claim", "sub": "batman", "context": { "username": "mpdavis", "firstname": "michael", "lastname": "davis } }
i running issues when try access , of nested claims inside context
claim. can access top level claims getclaimvalue
.
private string qsh; qsh = jwtclaims.getclaimvalue("qsh", string.class);
it seems have 2 options if want nested claim.
the first option find way return context
claim map<string,object>
, pull each claim out of object. other option use flattenclaims
flatten of claims map<string,list<object>>
, grab first object off of map nested claims.
neither 1 of these options seem particularly resilient if service granting these jwts alters schema much.
is there better way?
that's right.
you can claim value map , access content (or iterate on it).
@suppresswarnings("unchecked") map<string,string> context = claims.getclaimvalue("context", map.class); string username = context.get("username"); string firstname = context.get("firstname");
using flattenclaims
might this:
map<string,list<object>> flattened = claims.flattenclaims(); string username = (string)flattened.get("context.username").iterator().next(); string firstname = (string)flattened.get("context.firstname").iterator().next();
or iterate whole thing , convert whatever data structure makes sense application.
you make things more resilient changes in claims json things isclaimvalueoftype(...)
, hasclaim(...)
, things on jwtclaims
.
or can use getrawjson()
on jwtclaims
, pass json json processor of choice, if want.
Comments
Post a Comment