0

I am working on "Forgot Password". I am trying to create a reset token with email + current_time. email is user login whilst code will check if time >= 5 minutes then this link will not work. Here is my code:

// preparing token email + time
Date now            = new Date();
String prepareToken = "?email="+email+"&tokenTime="+now.getTime();

// encrypt prepareToken value
Encryptor enc = new Encryptor();
resetToken    = enc.encrypt(resetToken);

The token will be sent as for example as http://domainname.com/ForgotPassword?resetToken=adj23498ljj238809802340823

Problem:
When user click it then I got as request parameter and obviously decrypt this parameter but how can I get email in one String + time as another String

Please advise

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
usr169
  • 15
  • 4

1 Answers1

0

If your issue is simply parsing the decoded String to get some sort of Map of your parameters, I'd suggest you to read Parse a URI String into Name-Value Collection .

Hope it helps.

EDIT :

Assuming you have the splitQuery(URL url) method from the previous link and that you successfully decoded the token :

public String getEmailFromToken(String decodedToken) {
        // if you decoded your token it will looks like the prepareToken String
        String stubUrl = "http://localhost"+decodedToken;
        Map<String,String> map = splitQuery(new URL(stubUrl));
        return map.get("timeToken");
    }

I created a properly formed URL to respect the URL syntax. With little tweak, you should be able to implement splitQuery for a String. I hope you can manage that.

Community
  • 1
  • 1
Jeremy Grand
  • 2,300
  • 12
  • 18
  • Can I get specific parameter value for example email from this URL `String prepareToken = "?email="+email+"&tokenTime="+now.getTime();` then Can I get tokenTime value as separately when I need? – usr169 Feb 17 '17 at 15:12
  • Yes, if you convert the queryString into a Map, as following the response in that other question, you can get the tokenTime with map.get("tokenTime") – Jeremy Grand Feb 17 '17 at 15:16
  • I assumed that you already have a Controller method that will receive the token and decrypt it. Isn't that the case ? – Jeremy Grand Feb 17 '17 at 15:37
  • Thanks from the bottom of my heart – usr169 Feb 17 '17 at 16:23