How do I decode the JWT (JSON Web token) token in SWIFT?

I have this JWT token

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ

How do I decode this so that I can get the payload like this

{ “sub”: “1234567890”, “name”: “John Doe ”, “admin”: true }

If you have no problem using the library , I would recommend this https://github.com/auth0/JWTDecode.swift

Then import the library to import JWTDecode and execute it.

 let jwt = try decode(jwt: token)

Since you don’t want to include this library, I have provided the required parts to make it work.

func decode(jwtToken jwt: String) -> [String: Any] {
let segments = jwt.components(separatedBy: ".")
return decodeJWTPart(segments[1]) ?? [: ]
}

func base64UrlDecode(_ value: String) -> Data? {
var base64 = value
.replacingOccurrences(of: "-", with: "+")
.replacingOccurrences(of: "_", with: "/")< br />
let length = Double(base64.lengthOfBytes(using: String.Encoding.utf8))
let requiredLength = 4 * ceil(length / 4.0)
let paddingLength = requiredLength-length
if paddingLength> 0 {
let padding = "".padding(toLength: Int(paddingLength), withPad: "=", startingAt: 0)
base64 = base64 + padding
}
return Data(base64Encoded: base64, options: .ignoreUnknownCharacters)
}

func decodeJWTPart(_ value: String) -> [String: Any]? {
guard let bodyData = base64UrlDecode(value),
let json = try? JSONSerialization.jsonObject(with: bodyData, options: []), let payload = json as? [String: Any] else {
return nil
}

return payload
}

Call it like this:

decode (jwtToken: TOKEN)

I have this kind of JWT token

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTYWiRGiViWiR4WiRg9ViVi .TJVA95OrM7E2cBab30RM HrHDcEfxjoYZgeFONFh7HgQ

How do I decode this so that I can get the payload like this

{ “sub”: “1234567890” , “Name”: “John Doe”, “admin”: true }

If you have no problem using the library, I would suggest This https://github.com/auth0/JWTDecode.swift

Then import the library to import JWTDecode and execute it.

let jwt = try decode(jwt: token)

Since you don’t want to include this library, I have provided the required parts to make it work.

func decode( jwtToken jwt: String) -> [String: Any] {
let segments = jwt.components(separatedBy: ".")
return decodeJWTPart(segments[1]) ?? [:]
}

func base64UrlDecode(_ value: String) -> Data? {
var base64 = value
.replacingOccurrences(of: "-", with: "+")
.replacingOccurrences(of: "_", with: "/")

let length = Double(base64.lengthOfBytes(using: String.Encoding.utf8))
let requiredLength = 4 * ceil(length / 4.0)
let paddingLength = requiredLength-length
if paddingLength> 0 {
let padding = "".padding(toLength: Int(p addingLength), withPad: "=", startingAt: 0)
base64 = base64 + padding
}
return Data(base64Encoded: base64, options: .ignoreUnknownCharacters)
}

func decodeJWTPart(_ value: String) -> [String: Any]? {
guard let bodyData = base64UrlDecode(value),
let json = try? JSONSerialization.jsonObject(with : bodyData, options: []), let payload = json as? [String: Any] else {
return nil
}

return payload
}

Call it like this:

decode(jwtToken: TOKEN)

Leave a Comment

Your email address will not be published.