Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error "Failed to decode JWT: Unsupported algorithm or incorrect key" when I'm trying to decode my token #90

Open
aluco100 opened this issue Oct 5, 2017 · 5 comments

Comments

@aluco100
Copy link

aluco100 commented Oct 5, 2017

I have the following code:

if let user = response.result.value{
                    do {
                        let claims: ClaimSet = try JWT.decode(user.access_token, algorithm: .hs256(self.jwtSecret.data(using: .utf8)!))
                        user.id = claims["sub"] as! Int
                        user.iss = claims["iss"] as! String
                        print(claims)
                     } catch {
                        print("Failed to decode JWT: \(error)")
                        reject(error)
                    }
}

But I have that error. Now when I'm validating on jwt.io I had a valid decodification. There's a picture of that validation:

captura de pantalla 2017-10-05 a la s 13 02 22

So i dont understand what am I doing wrong. Any suggestion?

Best Regards

@kylef
Copy link
Owner

kylef commented Oct 5, 2017

This library only supports the HS* algorithms listed at https://github.com/kylef/JSONWebToken.swift#algorithms. You are using the RS256 algorithm which is not supported by this library.

@aluco100
Copy link
Author

aluco100 commented Oct 5, 2017

Well, What can I do ?

@loukrieg
Copy link

loukrieg commented Oct 6, 2017 via email

@georgeathanasopoulositt

We do need that RS512 pull request to be handled...

@caffieneToCode
Copy link

caffieneToCode commented Feb 19, 2019

Hi @kylef
This error is quite recurring even with HS256 algorithm, while debugging the library, I found that the algorithm passed to decode is being filtered out from the below method.

func verifySignature(_ algorithms: [Algorithm], header: JOSEHeader, signingInput: String, signature: Data) throws {
  guard let alg = header.algorithm else {
    throw InvalidToken.decodeError("Missing Algorithm")
  }

  let verifiedAlgorithms = algorithms
    .filter { algorithm in algorithm.description == alg }
    .filter { algorithm in algorithm.verify(signingInput, signature: signature) }

  if verifiedAlgorithms.isEmpty {
    throw InvalidToken.invalidAlgorithm
  }
}

/// Verify a signature for a message using the algorithm

  func verify(_ message: String, signature: Data) -> Bool {
    return sign(message) == base64encode(signature)        // Because this returns false
  }

Any Idea what went wrong here?
Because this works with other libraries..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants