ID Token
The ID token is a JSON Web Token (JWT) that contains user profile information (like the user's name, email, and so forth), represented in the form of claims. These claims are statements about the user, which can be trusted if the consumer of the token can verify its signature. An ID token is part of the OpenID Connect specification and is issued as a result of a valid authorization request with an openid
scope. You can find more information about ID Tokens here.
To get the ID token from the iOS SDK, you need to call the UserClient#idToken
variable. There are few prerequisites to be able to obtain a token, otherwise the method will return null
.
- User has to be registered with the
openid
scope. - User has to be authenticated.
Example code
func fetchIdToken() -> String {
let userClient = SharedUserClient.instance
if let idToken = userClient.idToken {
do {
let jwtPayload = try decode(jwtToken: idToken)
let data = try JSONSerialization.data(withJSONObject: jwtPayload,
options: .prettyPrinted)
guard let jsonString = String(data: data, encoding: .utf8) else {
return("Inavlid data")
}
return jsonString
} catch {
return "Some error occured: \(error)"
}
} else {
return "No Id Token set."
}
}