static UserLimits getLimitsFromToken ( HTTPServerConfig config , String jwtToken ) { Objects . requireNonNull ( jwtToken ) ; try { String secretKey = config . getSecretTokenKey ( ) ; if ( secretKey == null ) { throw new RuntimeException ( "You specified a 'token' parameter but this server doesn't accept tokens" ) ; } Algorithm algorithm = Algorithm . HMAC256 ( secretKey ) ; DecodedJWT decodedToken ; try { JWT . require ( algorithm ) . build ( ) . verify ( jwtToken ) ; decodedToken = JWT . decode ( jwtToken ) ; } catch ( JWTDecodeException e ) { throw new AuthException ( "Could not decode token '" + jwtToken + "'" , e ) ; } Claim maxTextLengthClaim = decodedToken . getClaim ( "maxTextLength" ) ; Claim premiumClaim = decodedToken . getClaim ( "premium" ) ; boolean hasPremium = ! premiumClaim . isNull ( ) && premiumClaim . asBoolean ( ) ; Claim uidClaim = decodedToken . getClaim ( "uid" ) ; long uid = uidClaim . isNull ( ) ? - 1 : uidClaim . asLong ( ) ; return new UserLimits ( maxTextLengthClaim . isNull ( ) ? config . maxTextLength : maxTextLengthClaim . asInt ( ) , config . maxCheckTimeMillis , hasPremium ? uid : null ) ; } catch ( UnsupportedEncodingException e ) { throw new RuntimeException ( e ) ; } }
Get limits from the JWT key itself no database access needed .