import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
public class JWTUtils {
public static JSONObject decodeJWT(String token) throws Exception {
String[] split = token.split("\\.");
String base64EncodedHeader = split[0];
String base64EncodedBody = split[1];
String base64EncodedSignature = split[2];
JSONObject header = new JSONObject(getJson(base64EncodedHeader));
JSONObject body = new JSONObject(getJson(base64EncodedBody));
JSONObject jwt = new JSONObject();
try {
jwt.put("header", header);
jwt.put("body", body);
jwt.put("signature", base64EncodedSignature);
} catch (JSONException e) {
e.printStackTrace();
}
return jwt;
}
private static String getJson(String strEncoded) throws UnsupportedEncodingException {
byte[] decodedBytes = Base64.decode(strEncoded, Base64.URL_SAFE);
return new String(decodedBytes, "UTF-8");
}
}
import android.util.Base64
import org.json.JSONException
import org.json.JSONObject
import java.io.UnsupportedEncodingException
object JWTUtils {
fun decodeJWT(token: String): JSONObject {
val split = token.split("\\.".toRegex()).toTypedArray()
val base64EncodedHeader = split[0]
val base64EncodedBody = split[1]
val base64EncodedSignature = split[2]
val header = JSONObject(getJson(base64EncodedHeader))
val body = JSONObject(getJson(base64EncodedBody))
val jwt = JSONObject()
try {
jwt.put("header", header)
jwt.put("body", body)
jwt.put("signature", base64EncodedSignature)
} catch (e: JSONException) {
e.printStackTrace()
}
return jwt
}
@Throws(UnsupportedEncodingException::class)
private fun getJson(strEncoded: String): String {
val decodedBytes = Base64.decode(strEncoded, Base64.URL_SAFE)
return String(decodedBytes, Charsets.UTF_8)
}
}
கருத்துரையிடுக