Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
OAuth 2.0 Overview¶
Overview of OAuth 2.0 support in Qt
RFC 6749 - The OAuth 2.0 Authorization Framework specifies a protocol for authorization of services by using a third-party application. OAuth 2.0 uses tokens to abstract the authorization from the service and users. This method is safer as the service owner does not need to deal with user credentials. It is a replacement for RFC 5849 OAuth 1.0.
The OAuth 2.0 framework defines two client types, public or confidential, and flows such as the authorization code flow, implicit code grant, and several others for authorization. A typical Qt application is considered a public native application. A public client application is an application that can’t trusted to hold sensitive information, such as passwords, to be embedded within the shipped binary.
RFC 8252 OAuth 2.0 for Native Apps further defines the best practices for native applications. Specifically, RFC 8252 recommends the the authorization flow with the browser. Therefore, the QtNetworkAuth classes provide a concrete implementation of this flow.
New in Qt 6.9, QtNetworkAuth provides support for RFC 8628 - OAuth 2.0 Device Authorization Grant. This device flow is intended for devices that have limited or impractical input capabilities. For this flow, authorization grants use a secondary device such as smartphones instead of the device. Examples of these devices are televisions, media consoles, machine HMIs, and IoT devices. The user may then use an application on their smartphone to authorize the device.
The following table highlights the two OAuth 2.0 flows supported by Qt Network Authorization:
Aspect
Authorization Code Flow
Device Authorization Flow
Network Connection
Yes
Yes
User Interaction
Browser / user-agent on the same device
Browser / user-agent on a different device
Redirect Handling Required
Yes
No
Input Capability On Device
Rich input capabilities
Limited or no input capability
Targets
Desktop and Mobile Apps
TVs, Consoles, HMIs, IoT Devices
OAuth 2.0 requires using a user-agent which is typically a browser. For further information, refer to Qt OAuth2 Browser Support .
OAuth 2.0 classes¶
Qt Network Authorization provides both concrete and abstract OAuth 2.0 classes. The abstract classes are intended for implementing custom flows, while the concrete classes provide a concrete implementation.
For the list of C++ classes, refer to the QtNetworkAuth page.
Qt Network Authorization has two abstract classes for implementing OAuth 2.0 flows:
An OAuth 2.0 flow implementation class provides the main API, and is the orchestrator of the flow. The abstract class is
QAbstractOAuth2, and the concrete implementations areQOAuth2AuthorizationCodeFlowandQOAuth2DeviceAuthorizationFlow.A reply handler class which handles redirects and replies from an authorization server. The reply handler abstract class is
QAbstractOAuthReplyHandler, and the concrete classes areQOAuthHttpServerReplyHandlerandQOAuthUriSchemeReplyHandler. The main difference between the reply handlers is the type of redirects they handle.QOAuth2AuthorizationCodeFlowuses a reply handler to handle the redirects andQOAuth2DeviceAuthorizationFlow, which is not based on redirects, does not use reply handlers.
Refresh tokens¶
Refreshing tokens require that the authorization server provide a refresh token during authorization. Providing a refresh token is up to the authorization server: some servers may choose to always provide it, some may never provide it, and some provide it if a specific scope was present in the authorization request.
The following figure illustrates the token refresh in more detail:
As shown in the figure above, the usual customization points are also available when refreshing tokens.
To refresh the tokens after an application startup, the application needs to persist the refresh token securely, and set it with setRefreshToken . refreshTokens can then be called to request new tokens.
New in Qt 6.9, applications can automatically refresh the tokens - see accessTokenAboutToExpire , autoRefresh , and refreshLeadTime .
The expiration time for a refresh token is generally not indicated by the authorization server (apart from the server’s documentation). Their validity can range from days, months, or longer. Furthermore, as with other tokens, refresh tokens can be revoked by the user and thus invalidated at any time. Therefore, it is important to properly detect a failed refresh attempt with requestFailed or serverReportedErrorOccurred .
OAuth 2.0 flows require many user interaction, which can be intrusive to the user experience. To minimize these interactions, tokens can be silently refreshed for the user. Refer to the RFC 6749 - Refreshing Access Tokens for more information.
Qt OpenID Connect support¶
OpenID Connect (OIDC) is a simple identity layer on top of OAuth 2.0. OIDC can use an authorization server for authenticating the identity of a user. Accessing simple user profile information is also possible with OIDC.
Qt’s support for OIDC is at the moment limited to getting ID tokens. An ID token is a JSON Web Token (JWT) that contains claims about the authentication event.
Note
ID token validation or ID token decryption is currently not implemented. You must use a third-party JWT library for JWT token signing or verification.
Assuming the application is able to validate the received tokens, the token can be used to establish the identity of the user reliably (so long as the OIDC provider itself is trusted).
ID tokens are sensitive information and should be kept as a secret and are not the same as access tokens. ID tokens are not intended for sending out in API calls - the access token is intended for that purpose. Note that some vendors may use the same JWT format for access tokens, but that is not to be confused with actual ID tokens which use the same format. With ID tokens, the client receiving the token is responsible for verifying the token, whereas with access tokens it’s the resource server accepting the token that is responsible for verification.
Getting an ID token¶
Getting an ID token is similar to getting an access token. First, we need to set the appropriate scope. The authorization Server vendor may support additional scope specifiers such as profile and email, but all OIDC requests must include openid scope:
m_oauth.setRequestedScopeTokens({"openid"})
For OIDC, it is strongly recommended to use nonce parameter. This is done by ensuring that appropriate NonceMode is set.
# This is for illustrative purposes, 'Automatic' is the default mode m_oauth.setNonceMode(QAbstractOAuth2.NonceMode.Automatic)
As the last step, we can listen for either granted signal or the idTokenChanged directly:
m_oauth.idTokenChanged.connect(this, [this](const QString &token) { Q_UNUSED(token) # Handle token })
Validating an ID token¶
Validating the received ID Token is a critical part of the authentication flow, and when fully implemented, a somewhat complicated task. Refer to the full at OpenID Connect ID Validation .
As a small summary, validation consists of these steps:
Decrypting the token if needed ( see JWE )
Extracting the token header, payload, and signature
Validating the signature
Validating the fields of the payload (such as
aud, iss, exp, nonce, iat)
Qt currently doesn’t provide support for ID token validation, but there are Third-party JWT libraries , such as jwt-cpp .
ID Token Verification example¶
This section illustrates a simple verification example. As prerequisites, the development environment needs to have OpenSSL libraries, and jwt-cpp in the include folder under the application project’s source directory.
In application project’s CMakeLists.txt file, we first check that the pre-requisities are met:
find_package(OpenSSL 1.0.0 QUIET) set(JWT_CPP_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/include") if(OPENSSL_FOUND AND EXISTS "${JWT_CPP_INCLUDE_DIR}/jwt-cpp/jwt.h")
Then we add necessary includes and libraries:
target_include_directories(networkauth_oauth_snippets PRIVATE "${JWT_CPP_INCLUDE_DIR}") target_link_libraries(networkauth_oauth_snippets PRIVATE OpenSSL::SSL OpenSSL::Crypto) target_compile_definitions(networkauth_oauth_snippets PRIVATE JWT_CPP_AVAILABLE)
In the application source files, include the verification library:
#ifdef JWT_CPP_AVAILABLE from jwt-cpp.jwt import * #endif
Once the application receives an ID token, it’s time to verify it. First we find a matching key from JSON Web Key Sets (JWKS), see OpenID Connect Discovery ).
try { jwt = jwt::decode(m_oauth.idToken().toStdString()) jwks = jwt.parse_jwks(m_jwks.toJson(QJsonDocument.Compact).toStdString()) jwk = jwks.get_jwk(jwt.get_key_id())
And then we do the actual verification:
# Here we use modulus and exponent to derive the key n = jwk.get_jwk_claim("n").as_string() # modulus e = jwk.get_jwk_claim("e").as_string() # exponent if n.empty() or e.empty(): qWarning() << "Modulus or exponent empty" return False if jwt.get_algorithm() != "RS256": # This example only supports RS256 qWarning() << "Unsupported algorithm:" << jwt.get_algorithm() return False if jwk.get_jwk_claim("kty").as_string() != "RSA": qWarning() << "Unsupported key type:" << jwk.get_jwk_claim("kty").as_string() return False if jwk.has_jwk_claim("use") and jwk.get_jwk_claim("use").as_string() != "sig": qWarning() << "Key not for signature" << jwk.get_jwk_claim("use").as_string() return False # Simple minimal verification (omits special cases and eg. 'sub' verification). # jwt-cpp does check also 'exp', 'iat', and 'nbf' if they are present. keyPEM = jwt::helper::create_public_key_from_rsa_components(n, e) verifier = jwt::verify() .allow_algorithm(jwt.algorithm.rs256(keyPEM)) .with_claim("nonce", jwt.claim(m_oauth.nonce().toStdString())) .with_issuer(m_oidcConfig.value("issuer").toString().toStdString()) .with_audience(std.string(clientIdentifier.data())) .leeway(60UL) verifier.verify(jwt) print("ID Token verified successfully") return True } catch(std.exception e) { # Handle error. Alternatively pass error parameter to jwt-cpp calls qWarning() << "ID Token verification failed" << e.what() return False
Reading ID token values¶
The ID token is in JSON Web Token (JWT) format and consists of a header, payload, and signature parts, separated by dots ..
Reading the values of the ID token is straightforward. As an example, assume that there is a struct:
class IDToken(): header = QJsonObject() payload = QJsonObject() signature = QByteArray()
And a function:
std.optional<IDToken> parseIDToken(QString token)
The token can be extracted with:
if token.isEmpty(): return std.nullopt parts = token.toLatin1().split('.') if parts.size() != 3: return std.nullopt parsing = QJsonParseError() header = QJsonDocument.fromJson(() QByteArray.fromBase64(parts.at(0), QByteArray.Base64UrlEncoding), parsing) if parsing.error != QJsonParseError.NoError or !header.isObject(): return std.nullopt payload = QJsonDocument.fromJson(() QByteArray.fromBase64(parts.at(1), QByteArray.Base64UrlEncoding), parsing) if parsing.error != QJsonParseError.NoError or !payload.isObject(): return std.nullopt signature = QByteArray.fromBase64(parts.at(2), QByteArray.Base64UrlEncoding) return IDToken{header.object(), payload.object(), signature}
In some cases the token may be encrypted as JSON Web Encryption (JWE) which internally contains a JWT token. In this case, the token must be decrypted first.
OpenID Connect Discovery¶
OpenID Connect Discovery defines the means to discover needed OpenID provider details, in order to interact with it. This includes information such as authorization_endpoint and token_endpoint URLs.
While these provider details can be statically configured in the application, discovering the details at runtime may provide more flexibility and robustness in interacting with various providers.
Getting the discovery document is a simple HTTP GET request. The document is typically located in https://<domain name>/.well-known/openid_configuration.
m_network.get(request, self, [self](QRestReply reply) { if reply.isSuccess(): if auto doc = reply.readJson(); doc and doc.isObject(): m_oidcConfig = doc.object() # Store the configuration })
Notably, for token validation, the jwks_uri field provides a link for accessing the current (public) security credentials. Using that removes the need to hard-code such credentials in the application directly. This also helps with key rotation; the vendors may change the used keys from time to time, and therefore ensuring an up-to-date key is important.
Getting the keys is similarly a simple HTTP GET request:
m_network.get(request, self, [self](QRestReply reply) { if reply.isSuccess(): if auto doc = reply.readJson(); doc and doc.isObject(): m_jwks = doc # Use the keys later to verify tokens })
The key set typically contains several keys. The correct key is indicated in the JWT header (care must be taken to match the keys properly, just checking the key id, kid, field is not adequate).
OpenID UserInfo Endpoint¶
An alternative way to access user information is to use OpenID Connect UserInfo Endpoint , if the OIDC provider supports it. The URL for the userinfo is in userinfo_endpoint field of the OpenID Connect Discovery document.
The UserInfo endpoint does not use the ID token, but is accessed with the access token. Accessing the UserInfo is similar to accessing any other resource with an access token.
Assuming the access token is received and set for example by:
userInfoApi = QNetworkRequestFactory(url) userInfoApi.setBearerToken(m_oauth.token().toLatin1())
Then accessing the UserInfo is a HTTP GET request:
m_network.get(userInfoApi.createRequest(), self, [self](QRestReply reply) { if reply.isSuccess(): if auto doc = reply.readJson(); doc and doc.isObject(): print(doc.object()) # Use the userinfo })