1

i get token form HTTP request in JWT format. if i print and decode my output as static value it's works fine but when i tried to decode output value, it's doesn't work and show error message

The Token is not yet valid (not before claim).

here is my code

if let token = parseResult["token"] as? String{
    //print(token)
    do {
        let claims = try JWT.decode(token, algorithm: .hs256(Constants.Route.Secret.data(using: .utf8)!))
            print("\(claims)")                       
    } catch {
            print("\(error)")
            return
    }
}

if i use my printed token above for next request, it's works fine. i don't know it's error in JWT decoder or in my JSONSerialization method. but data printed from JSONSerialization is working if i use like this :

if let token = parseResult["token"] as? String{
  let testToken:String = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"
    do {
        let claims = try JWT.decode(testToken, algorithm: .hs256(Constants.Route.Secret.data(using: .utf8)!))
            print("\(claims)")                       
    } catch {
            print("\(error)")
            return
    }
}
user28434'mstep
  • 6,290
  • 2
  • 20
  • 35
Suresh Dhakal
  • 21
  • 1
  • 7
  • Most likely ["nbf" claim](https://tools.ietf.org/html/rfc7519#section-4.1.5) is not valid yet. – user28434'mstep Jan 05 '17 at 09:02
  • Can you add *actual* JWT token you have problem with? First two parts will be enough. – user28434'mstep Jan 05 '17 at 09:06
  • eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ – Suresh Dhakal Jan 05 '17 at 09:20
  • Well, this token has no "nbf", and no date claims at all. So if it's actual token you're having problem with, the error must be in `JWT` library you're using. Can you add info about it to the question (name, repository, cocoapod)? – user28434'mstep Jan 05 '17 at 09:34
  • a) **edit it in question itself**, b) it should be link to the original library, not clone in your project, if i understand correctly lib is [JSONWebToken](https://cocoapods.org/pods/JSONWebToken). – user28434'mstep Jan 05 '17 at 09:53
  • JSON Web Token Swift: https://github.com/kylef/JSONWebToken.swift this is the JWT repository i'm using – Suresh Dhakal Jan 05 '17 at 10:23
  • It is generally not good practice to decode the JWT client side, what do you need to do with the claims client side that you can't do by virtue of passing data back from your server side application? – Tim Ebenezer Jan 05 '17 at 14:21

1 Answers1

0

Finally i figured out that JSONWebToken was not working properly. now i have implemented JWTDecode and its start decoding received JWT properly. i don't know why but JSONWebToken have some serious bug for for SWIFT 3.

Suresh Dhakal
  • 21
  • 1
  • 7