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

Problem with JWT signing: "The provided key did not have the expected PEM headers/footers" #114

Open
alfitriv opened this issue May 31, 2023 · 3 comments

Comments

@alfitriv
Copy link

alfitriv commented May 31, 2023

Hello thank you so much for creating this library. I have been working on a problem where I was not able to have success with signing my JWT. The following is my code:

    let teamID = some value
    let keyID = some value
    let authToken = """
                -----BEGIN PRIVATE KEY-----
                MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCoknaik3X9AwXf
                1nb/BfHlR4RBcij+Ri2RzxZfdcTuhcIL4XLrgwaz/Skx3R/UjU3eoxneBjcGeA7X
                QX75aXMS2FKrfQEJ6mp9AVQTowPC5VkAp8L8vk/cBrckZFHQsm9bHnLirJ6LYhWK
                sLbvgpJo+P4OMG4P/GeQVwaWwLxaZNSs0sEjVRuy0vbWCO4jJwnmZpPMxU0sRCRN
                xod3n6DJ0XhwCP/CxhlFVHjoM/nX/HGlPWkwG05BFBH4J9Zy4SNNNg6CDjIsl56R
                2Fu8d/RHtIB/UPhIEoV6t5rWkJx4SP76OwjiXl9IGiDWpb6uu2/OQctZRBezxvCZ
                O4lgzKFXAgMBAAECggEAYK4XsmhmbCTWsqka+GqdcIVS2gIydpsjOZQO3dL6jl5S
                i2PS+DXem04f2URcJBiix4S9qjPgTSqAQH6E52DOKcm9qDL6bIhwaJ9hbB27Y4UM
                Ra7xyukPfj6vvQR4U/xyl0zgURb1mzU266MsWDOH6wKbGuI1zZ9SelsfIUkK/cAV
                s6Ao4kzCCQWZMQ/GkYxtQXg/tdPtI2Ueexon0Xtr4bc50XefEFvpKNi3ZqX7fRHV
                e2bvnzKH6TN6DlEBruIdRwLsfmFXMIXU98D1OYokaaeVeHH5iZ2nXrlGGw72RfcQ
                awrEHvMTTUhzg5LnMw30Smq1ogPanhLtbofPqIQJwQKBgQDfxKZ7lFle+tiMEKmU
                87uiRavAHrmQNipqBcbtadJqqQtvGOCxwSg9phh2YnwxRSyw1oBpg4ogvg1QbhqV
                UNfB8b/M2kpPRpGZpjCvi6En8GzK6K4e/UQJ4i0l7tPT9tt3TynbMwb3xFcEnFEX
                IxfcWnlbC5tm5Sea6b4BzwK84QKBgQDA2nvqHNsqU9HyCX61R5Bo2QLnsI4CgUBk
                z2hhc4bteb//mKCWeVvPNRu9AFhNEJzix0/EEkCVhbpIKE1DUCzBPWI/4CkH1bRc
                RBE7/7I3pcMBbV22OdGoISmUf6IFZSyQuBoShLDBH3CVmqdmto9b5nwYaTgdycvt
                xpyQvdKtNwKBgHFzF2EyXnlcPqwMyp29URU9s41NRpGKFMj6MtgtvcPb/vMNruYQ
                Y2GWM3LaDdNBGh5yMlrMmRxunvt3Rz0K5sjq025+AgzdX3aCHs7xwPwp1k6t15HY
                oEVOictgob8mujBsT3FWFqNJxUCOLELJxRAwQrTZVqm9Zu4Qsgfit6WhAoGBAKWx
                UbOUNU0JlSDBvaacpNsgUFmlnG1UhXHXrVPFAVE5QJeml5qRDCtb8sgQ6szTkCdb
                nRHVqL2Olrz2O2OxF7KzPZ2pxzbfCkYXiUMmbgVXmtK4F0LALHyqeWIHwrml8oMo
                WeY9MOvMSluO83LRORx5S3dht4AIZ/iTouLM5JxDAoGAcAnam49TeCFZuOu5/QCb
                GYyAntOQL/nunSbuHoNvC+bBrcUX2BfDkalkzlm/YRJgmqSQ7Ih3fbp4i5NCVtpM
                1dafoyed5UqY0F7Vou7JJE57tlKieKPhQOMTSl2Q5WMvby+owRb0Sx325xQvoslH
                QM9+y6wy6YMdNweC+JkcZVo=
                -----END PRIVATE KEY-----
                """


    func generateToken() {
        let myHeader = Header(kid: keyID)
        let urlString = "https://www.googleapis.com/oauth2/v4/token"
        let claims = JWTClaims(iss: teamID, iat: Date(), exp: Date() + 3.0, scope: "devstorage.read_write", aud: urlString)
        var jwt = SwiftJWT.JWT(header: myHeader, claims: claims)
        
        
        guard let tokenData = authToken.data(using: .utf8)?.base64EncodedData() else {
            return
        }
        
        var jwtSigner = JWTSigner.rs256(privateKey: tokenData)
        
        do {
            let token = try jwt.sign(using: jwtSigner)
            print(jwt)

            UserDefaults.standard.setValue(token, forKey: "JWT")
        } catch {
            print(error.localizedDescription)
        }
    }
}

struct JWTClaims: Claims {
    let iss: String
    let iat: Date?
    let exp: Date?
    let scope: String
    //let aud: URL
    let aud: String
}

If I po to the debugger try jwt.sign(using: jwtSigner) it gives me the error "The provided key did not have the expected PEM headers/footers".

What am I missing here? I have checked my header and payload seem to be in the correct format.

@dhavalnenaagile
Copy link

I too have the same problem. Can you share your findings if you were able to find the solution? Thanks!

@dhavalnenaagile
Copy link

Make sure you are using proper JWTSigner type while signing the JWT in the final step. I was using wrong JWTSigner and that caused the problem. Thanks.

@alfitriv
Copy link
Author

alfitriv commented Sep 19, 2023 via email

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

2 participants