paseto-toolkit / jpaseto

A library for creating and parsing Paseto in Java
Apache License 2.0
65 stars 15 forks source link

Cast Exception in String to Paseto #193

Closed anand-mm closed 1 year ago

anand-mm commented 1 year ago

java.lang.ClassCastException: java.lang.String cannot be cast to dev.paseto.jpaseto.Paseto

I tried using instanceof keyword in java it did not work:

    public Integer pasetoclaims(Object clientToken, String param) {
        Integer result = null;
        Paseto token = null;
        try {
            if(clientToken instanceof Paseto)
                token = (Paseto) clientToken;
            if (param.contains("name"))
                result = token.getClaims().get("name", Integer.class);
            else
                result = token.getClaims().get(param, Integer.class);
        } catch (Exception e) {
           e.printStackTrace();
        }
        return result;
    }

I also tried using getclass but it did not work,

    public Integer pasetoclaims(Object clientToken, String param) {
        Integer result = null;
        Paseto token = null;
        try {
            if(clientToken.getClass().equals(Paseto.class))     //This is where i have used getclass
                token = (Paseto) clientToken;
            if (param.contains("name"))
                result = token.getClaims().get("name", Integer.class);
            else
                result = token.getClaims().get(param, Integer.class);
        } catch (Exception e) {
           e.printStackTrace();
        }
        return result;
    }

Can anyone suggest how to cast it in a proper way . Thanks in advance.

bdemers commented 1 year ago

I need clarification on what you are asking. https://stackoverflow.com/help/how-to-ask

What line throws an exception?

anand-mm commented 1 year ago

java.lang.ClassCastException: java.lang.String cannot be cast to dev.paseto.jpaseto.Paseto

I tried using instanceof keyword in java it did not work:

    public Integer pasetoclaims(Object clientToken, String param) {
        Integer result = null;
        Paseto token = null;
        try {
            if(clientToken instanceof Paseto)
                token = (Paseto) clientToken;
            if (param.contains("name"))
                result = token.getClaims().get("name", Integer.class);
            else
                result = token.getClaims().get(param, Integer.class);
        } catch (Exception e) {
           e.printStackTrace();
        }
        return result;
    }

I also tried using getclass but it did not work,

    public Integer pasetoclaims(Object clientToken, String param) {
        Integer result = null;
        Paseto token = null;
        try {
            if(clientToken.getClass().equals(Paseto.class))     //This is where i have used getclass
                token = (Paseto) clientToken;
            if (param.contains("name"))
                result = token.getClaims().get("name", Integer.class);
            else
                result = token.getClaims().get(param, Integer.class);
        } catch (Exception e) {
           e.printStackTrace();
        }
        return result;
    }

Can anyone suggest how to cast it in a proper way . Thanks in advance.

In this method i have getting paseto token in clientToken as a String datatype and i am casting the clientToken to Paseto for getting the Paseto Token claims. when checking in if condition with instanceof that is skipped.without if condition classcastexception in 6 line

token = (Paseto) clientToken

bdemers commented 1 year ago

Sorry @anand-mm, I think I'm missing something.
Are you trying to cast a String to a Paseto object directly? If so please look at: https://github.com/paseto-toolkit/jpaseto#paseto-read

Otherwise, can you create an example project on GitHub that demos the problem and link it here?

anand-mm commented 1 year ago

Yes I am trying to cast String to Paseto Object directly. It is Possible or not??

bdemers commented 1 year ago

No, that is not possible Java.

Take a look at this section of the docs to learn how to parse a [String] PASETO token: https://github.com/paseto-toolkit/jpaseto#paseto-read