From Implicit to Authorization Code With PKCE, BFF – DZone – Uplaza

This text will assessment the rules behind varied OpenID Join (OIDC) authentication flows, from the only to probably the most trendy, highlighting the vulnerabilities current in every.

We are going to discover every of the next OpenID Join flows intimately:

  • Frontend-only implementations:
    • Implicit circulation
    • Authorization code circulation with Proof Key for Code Alternate (PKCE)
  • Frontend and backend implementations:
    • Authorization code circulation with token-mediating backend
    • Authorization code circulation with token-mediating backend and PKCE
    • Authorization code circulation with PKCE and Backend for Frontend (BFF)

For comfort, some data will likely be repeated in every part. This manner, you may learn concerning the particular flows that curiosity you with no need to learn your complete article.

Vulnerabilities Current in Authentication Processes

These vulnerabilities are essential to grasp when implementing OpenID Join or OAuth 2.0 protocols in your purposes. Trendy internet purposes might be attacked by varied varieties of vectors:

Widespread Assault Vectors

  • XSS (Cross-Website Scripting) assaults contain injecting malicious JavaScript code into an utility by enter fields, URL parameters, or different entry factors. This occurs when enter knowledge is not correctly escaped or sanitized.
  • CSRF (Cross-Website Request Forgery) happens when malicious scripts or browser extensions carry out actions on behalf of the consumer with out their consent. These scripts can use the consumer’s session and credentials to hold out unauthorized actions, emulating respectable consumer conduct.
  • Dependency Compromise happens when trendy internet purposes depend on quite a few exterior libraries and sources. If these dependencies are compromised, they will introduce vulnerabilities into the appliance.
  • Browser extension vulnerabilities come up when browser extensions, which have entry to internet utility code and consumer knowledge, are compromised or malicious. These compromised extensions can introduce vulnerabilities, permitting attackers to steal delicate data or inject malicious code into internet purposes.

These assault vectors enable an attacker to run arbitrary code within the consumer’s utility execution context. This may occasionally result in the next threats:

Particular Threats

  • A Code Interception Assault happens when an attacker intercepts authorization codes uncovered within the URL throughout the redirect, leading to unauthorized entry to the consumer’s sources.
  • Persistent Token Theft is a danger when tokens are saved throughout the browser’s storage, making them inclined to steady theft (equivalent to each 10 seconds) by malicious scripts.
  • Acquisition and Extraction of New Tokens happens when a session is energetic on the OpenID Supplier aspect, and malicious JavaScript initiates a silent authentication course of in a hidden iframe to acquire new entry tokens with out the consumer’s data.
  • Proxying Requests through the Person’s Browser happens when malicious JavaScript exploits an authenticated session by simulating consumer actions throughout the utility, sending unauthorized requests to the Protected Useful resource on behalf of the consumer.

Vulnerabilities Particular to Sure OpenID Join Implementations

  • Token Transmission through URL happens when tokens are seen within the tackle bar, saved in browser historical past, and logs, and handed through HTTP referrers, making them weak to interception and unauthorized use. That is particularly related to Implicit Circulation in frontend-only implementations.
  • Lack of Refresh Token Help happens when there are not any refresh tokens, and frequent requests for brand new tokens are obligatory, rising the probabilities of token leakage and misuse. That is particularly related to Implicit Circulation in frontend-only implementations.

Evolution of OpenID Join Flows

Frontend-Solely Implementations

Frontend and Backend Implementations

Description

The diagrams illustrate the evolution of OpenID Join flows over time, displaying how the authentication strategies have turn out to be more and more safe. They spotlight the shift from the preliminary Implicit Circulation, which had quite a few vulnerabilities, to the safer Authorization Code Circulation with PKCE and BFF. Every development step is depicted to exhibit how these new flows have successfully addressed and decreased varied safety vulnerabilities, making certain a safer authentication course of in trendy internet purposes.

Initially, there have been two various kinds of authorization flows in OAuth 2.0: the Authorization Code Circulation and the Implicit Circulation. The Authorization Code Circulation was designed for eventualities the place a backend server carried out as a shopper, dealing with communication with the authorization server. Alternatively, the Implicit Circulation was a simplified various supposed primarily for front-end purposes.

Nonetheless, the Implicit Circulation had a number of safety vulnerabilities, equivalent to token interception and theft. To boost safety, the Authorization Code Circulation with Proof Key for Code Alternate (PKCE) was developed. PKCE addressed vital vulnerabilities, together with code interception assaults. This circulation marked a substantial enchancment in securing the authentication course of.

To handle extra points, the Authorization Code Circulation with PKCE and a token-mediating backend turned the following attainable transfer. This strategy concerned each the entrance finish and the again finish, thereby lowering some vulnerabilities like token publicity. Nonetheless, it nonetheless had some points, equivalent to Persistent Token Theft.

The newest development proven within the diagram is the Authorization Code Circulation with each PKCE and BFF (Backend for Frontend). This strategy combines the advantages of PKCE with further backend protections, additional mitigating vulnerabilities like persistent token theft.

Now we’ll look at the rules of every circulation intimately.

Implicit Circulation in a Frontend-Solely Implementation

  • In OpenID Join, the Finish-Person is the particular person utilizing an utility to entry companies. The Relying Occasion (RP) is the appliance itself, counting on the OpenID Supplier for consumer authentication. These purposes might be cell apps, internet apps, desktop apps, IoT gadgets, or gaming consoles. They act as purchasers, requesting tokens from the OpenID Supplier to authenticate customers and achieve entry to protected sources.
  • The OpenID Supplier (OP) is an authorization server accountable for verifying the identification of the Finish-Person. It authenticates the consumer and points identification and entry tokens to the Relying Occasion. As an example, within the diagram, the Abblix OIDC Server, a licensed OpenID Basis library with open supply code accessible on GitHub, serves because the OpenID Supplier.  This server ensures that customers are who they declare to be and supplies the required tokens to the purposes that must work together securely with protected sources.
  • The Useful resource Server shops and manages protected data, internet hosting delicate knowledge like private particulars, monetary data, and personal paperwork. In OpenID Join, the Useful resource Server makes use of tokens issued by the OpenID Supplier to determine whether or not to grant or deny entry. The tokens confirm the consumer’s identification and permissions, making certain solely approved customers can entry the delicate knowledge.

Step-By-Step Course of

Step 1: Person Redirects to OpenID Supplier

When a consumer tries to entry a protected useful resource, the appliance checks their authentication standing. If the consumer will not be logged in, the appliance will redirect their browser to the OpenID Supplier to begin the authentication course of. This request consists of parameters equivalent to client_id, redirect_uri, response_type=id_token token (indicating that the shopper expects each an ID token and an entry token within the response, attribute of the Implicit Circulation), scope, and state.

The parameters on this request serve particular functions:

  • client_id: This identifies the shopper utility making the request; for instance, client_id=myapp-12345.
  • redirect_uri: This specifies the URL to which the OpenID Supplier ought to redirect the consumer after authentication; for instance, redirect_uri=https://myapp.com/callback.
  • response_type=id_token token: This means that the shopper expects each an ID token and an entry token within the response.
    • ID token: An ID token is a JSON Internet Token (JWT) that accommodates details about the authenticated consumer. It usually consists of claims such because the consumer’s identification, authentication time, and different related knowledge. For instance, it could include the consumer’s title, electronic mail tackle, and a novel identifier.
    • Entry token: An entry token is a credential used to entry protected sources. It is usually often a JWT and consists of details about the permissions granted to the shopper utility. This token is shipped together with requests to the Useful resource Server to authorize entry to particular sources. An entry token is a bearer token, that means it’s utilized by purchasers as-is, with out parsing its contents or performing validation.
  • scope: This defines the entry privileges requested, equivalent to entry to the consumer’s profile data. For instance, scope=openid profile electronic mail.
  • state: This parameter helps forestall CSRF assaults by sustaining the state between the request and the callback, making certain the response matches the unique request made by the shopper; for instance, state=abc123.

Listed here are examples of what the parameters would possibly appear to be in an actual request:

  • client_id=myapp-12345
  • redirect_uri=https://myapp.com/callback
  • response_type=id_token token
  • scope=openid profile electronic mail
  • state=abc123

By together with these parameters, the shopper utility ensures that the OpenID Supplier can accurately determine the shopper, know the place to ship the response, perceive what varieties of tokens are being requested, outline the entry privileges, and safe communication towards CSRF assaults.

Step 2: Present Login Kind

The OpenID Supplier shows a login type to the consumer. This type prompts the consumer to enter their login credentials (usually a username and password).

Step 3: Person Enters Login and Password and Submits the Kind

The consumer enters their login credentials into the shape, together with their username and password, and submits it. The OpenID Supplier processes these credentials to authenticate the consumer.

Step 4: Entry Token Response

Upon profitable authentication, the OpenID Supplier redirects the consumer’s browser again to the desired redirect uri with an access_token and an ID token included within the URL fragment. This response accommodates the tokens wanted for the shopper (Relying Occasion) to authenticate the consumer and make approved requests to protected sources.

After this step, as soon as the access_token is obtained, a number of varieties of assaults can happen.

Step 5: Protected Useful resource Request

The shopper utility (Relying Occasion) makes use of the entry token to make requests to the Useful resource Server for entry to protected sources. The access_token is shipped as a part of the request to show that the shopper has permission to entry the requested sources.

Step 6: Protected Useful resource Response

The Useful resource Server verifies the access_token and, if legitimate, responds to the shopper’s request, by offering entry to the protected knowledge. This data is shipped again to the shopper utility to be used by the consumer.

Abstract of OpenID Join Implicit Circulation in a Frontend-Solely Implementation

To summarize, the Implicit Circulation in OpenID Join includes redirecting the consumer to the OpenID Supplier for authentication, the consumer submits their login credentials, and the appliance receives an entry token and ID token within the redirect response, which is then used to entry protected sources. Nonetheless, the Implicit Circulation has a number of safety vulnerabilities:

  • Persistent token theft: Tokens are current within the browser, and recent tokens might be stolen constantly, for instance, each 10 seconds.
  • Acquisition and extraction of recent tokens: The consumer is authenticated on the OpenID Supplier aspect, and malicious JavaScript can provoke a silent request to the OpenID Supplier in a hidden iframe and procure its personal distinctive entry token.
  • Proxying requests through the consumer’s browser: Malicious JavaScript can “click buttons in the application” on behalf of the consumer, sending requests to the Protected Useful resource within the consumer’s title.
  • Token transmission through URL: Tokens are seen within the tackle bar, saved in browser historical past, and logs, and handed through HTTP referrers, making them weak to interception and unauthorized use. That is significantly related to Implicit Circulation in frontend-only implementations.
  • Lack of refresh token help: With out refresh tokens, frequent requests for brand new tokens are obligatory, which will increase the probabilities of token leakage and misuse. That is significantly related to Implicit Circulation in frontend-only implementations.

To handle these safety issues, safer flows, such because the Authorization Code Circulation with a backend, are really helpful. Moreover, if the appliance solely includes a frontend element, many of those vulnerabilities might be mitigated by utilizing the OpenID Join Authorization Code Circulation with PKCE. Within the subsequent part, we’ll look at these flows and discover how they mitigate the vulnerabilities current within the Implicit Circulation, offering a safer mechanism for dealing with and transmitting tokens.

Authorization Code Circulation With PKCE in a Frontend-Solely Implementation

Overview

In OpenID Join, the Finish-Person is the particular person utilizing an utility to entry companies. The Relying Occasion (RP) is the appliance itself, counting on the OpenID Supplier for consumer authentication. These purposes might be cell apps, internet apps, desktop apps, IoT gadgets, or gaming consoles. They act as purchasers, requesting tokens from the OpenID Supplier to authenticate customers and achieve entry to protected sources.

The OpenID Supplier (OP) is an authorization server accountable for verifying the identification of the Finish-Person. It authenticates the consumer and points identification and entry tokens to the Relying Occasion. As an example, within the diagram, the Abblix OIDC Server serves because the OpenID Supplier. This server ensures that customers are who they declare to be and supplies the required tokens to the purposes that must work together securely with protected sources.

The Useful resource Server shops and manages protected data, internet hosting delicate knowledge like private particulars, monetary data, and personal paperwork. In OpenID Join, the Useful resource Server makes use of tokens issued by the OpenID Supplier to determine whether or not to grant or deny entry. The tokens confirm the consumer’s identification and permissions, making certain solely approved customers can entry the delicate knowledge.

The Proof Key for Code Alternate (PKCE) is designed to stop authorization code interception and injection assaults. It ensures that solely the shopper that requested the authorization code can use it. PKCE permits public purchasers, equivalent to cell and single-page purposes, to make use of the Authorization Code Circulation securely.

Step-By-Step Course of

Step 1: Person Redirects To OpenID Supplier

When a consumer tries to entry a protected useful resource, the appliance checks their authentication standing. If the consumer will not be logged in, the appliance will redirect their browser to the OpenID Supplier to begin the authentication course of. This request consists of parameters equivalent to client_id, redirect_uri, response_type=code, scope, state, and, particularly for PKCE, code_challenge and code_challenge_method.

Earlier than making the request, the shopper generates a high-entropy cryptographic random string often known as the code_verifier. The code_challenge is then derived by hashing the code_verifier utilizing the desired code_challenge_method. This code_challenge is shipped with the preliminary authorization request, making certain that the code_verifier is used later to show that the true originator of the authorization request is asking for a token in alternate for the authorization code.

The parameters on this request serve particular functions:

  • client_id: This identifies the shopper utility making the request; for instance, client_id=myapp-12345.
  • redirect_uri: This specifies the URL to which the OpenID Supplier ought to redirect the consumer after authentication; for instance, redirect_uri=https://myapp.com/callback.
  • response_type=code: This means that the shopper expects an authorization code within the response.
  • scope: This defines the entry privileges requested, equivalent to entry to the consumer’s profile data; for instance, scope=openid profile electronic mail.
  • state: This parameter helps forestall CSRF assaults by sustaining the state between the request and the callback, making certain the response matches the unique request made by the shopper; for instance, state=abc123.
  • code_challenge: It is a derived worth used to make sure that the token alternate is safe.
  • code_challenge_method: This specifies the tactic used to generate the code problem; for instance, code_challenge_method=S256.

Listed here are examples of what the parameters would possibly appear to be in an actual request:

  • client_id=myapp-12345
  • redirect_uri=https://myapp.com/callback
  • response_type=code
  • scope=openid profile electronic mail
  • state=abc123
  • code_challenge=abc123def456acd789
  • code_challenge_method=S256

By together with these parameters, the shopper utility ensures that the OpenID Supplier can accurately determine the shopper, know the place to ship the response, perceive what varieties of tokens are being requested, outline the entry privileges, and safe the communication towards assaults.

Step 2: Present Login Kind

The OpenID Supplier shows a login type to the consumer. This type prompts the consumer to enter their login credentials (usually a username and password).

Step 3: Person Enters Login and Password and Submits the Kind

The consumer enters their login credentials into the shape, together with their username and password, and submits it. The OpenID Supplier processes these credentials to authenticate the consumer.

Step 4: Authorization Code Response

Upon profitable authentication, the OpenID Supplier redirects the consumer’s browser again to the desired redirect_uri with an authorization_code included within the URL. The authorization_code is a short-lived, one-time code (e.g., authorization_code=SplxlOBeZQQYbYS6WxSbIA) that the shopper exchanges for an entry token. This ensures safe token issuance. This code is utilized by the shopper to request an entry token from the OpenID Supplier.

Step 5: Asynchronous Entry Token Request

The shopper utility (Relying Occasion) sends an asynchronous request to the OpenID Supplier’s token endpoint to alternate the authorization code for an entry token. This request consists of the authorization_code, client_id, redirect_uri, and, particularly for PKCE, the unique worth of code_verifier.

Step 6: Entry Token Response

The OpenID Supplier validates the request and, if profitable, returns an access_token and a refresh_token to the shopper utility. The access_token is a credential used to entry protected sources. It usually has a brief lifespan to attenuate danger. 

  • Instance: access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9– the refresh_token is used to acquire a brand new entry token with out requiring the consumer to re-authenticate, thereby sustaining the session securely over longer durations. 
  • Instance: refresh_token=def502b0b22b3d8f67d1b7c2c1b5a6b2c7d1f7f8c8b9c4e2

After this step, as soon as the tokens are obtained, a number of varieties of assaults can happen.

Step 7: Async Person Information Request

The shopper utility sends an asynchronous request to the OpenID Supplier’s consumer data endpoint to retrieve the consumer’s profile data, utilizing the access_token.

Step 8: Person Information Response

The OpenID Supplier responds with the consumer’s claims (e.g., title, electronic mail).

Step 9: Protected Useful resource Request

The shopper utility (Relying Occasion) makes use of the entry token to make requests to the Useful resource Server to entry protected sources. The access_token is shipped as a part of the request to show that the shopper has permission to entry the requested sources.

Step 10: Protected Useful resource Response

The Useful resource Server verifies the entry token and, if legitimate, responds to the shopper’s request, by offering entry to the protected knowledge. This data is shipped again to the shopper utility to be used by the consumer.

Abstract of OpenID Join Authorization Code Circulation With PKCE in a Frontend-Solely Implementation

To summarize, the Authorization Code Circulation with PKCE in OpenID Join includes redirecting the consumer to the OpenID Supplier for authentication, the consumer submits their login credentials, the appliance receives an authorization code within the redirect response, after which securely exchanges this code for an entry token utilizing PKCE. This circulation enhances safety by making certain that the authorization code can solely be utilized by the shopper that requested it, considerably lowering the chance of token interception.

Utilizing PKCE mitigates a number of vulnerabilities:

  • Code Interception Assault: PKCE ensures that the authorization code can solely be exchanged for tokens by the shopper that requested it, stopping attackers from intercepting the code throughout the redirect.
  • Token transmission through URL: PKCE doesn’t ship tokens within the URL, lowering the chance of tokens being intercepted from the tackle bar, browser historical past, logs, and HTTP referrers.
  • Lack of refresh token help: PKCE helps the usage of refresh tokens, permitting safe, long-term session upkeep with out frequent requests for brand new tokens, thereby lowering the probabilities of token leakage and misuse.

Nonetheless, even with PKCE, some vulnerabilities stay:

  • Persistent token theft: Tokens are current within the browser, and recent tokens might be stolen constantly, for instance, each 10 seconds.
  • Acquisition and extraction of recent tokens: The consumer is authenticated on the OpenID Supplier aspect, and malicious JavaScript can provoke a silent request to the OpenID Supplier in a hidden iframe and procure its personal distinctive entry token.
  • Proxying requests through the consumer’s browser: Malicious JavaScript can “click buttons in the application” on behalf of the consumer, sending requests to the Protected Useful resource within the consumer’s title.

To additional improve safety, the backend element must be used. The Authorization Code Circulation with PKCE, mixed with a backend, addresses many safety issues successfully, offering a safer mechanism for token dealing with and transmission.

Authorization Code Circulation With Token-Mediating Backend

Overview

  • In OpenID Join, the Finish-Person is the particular person utilizing an utility to entry companies. These purposes can embody cell apps, internet apps, desktop apps, IoT gadgets, gaming purposes, and smartwatches. The Finish-Person interacts with these purposes to entry varied companies and sources.
  • The Relying Occasion (RP) is the backend element of the appliance that the Finish-Person interacts with. The RP is determined by the OpenID Supplier for consumer authentication. It requests tokens from the OpenID Supplier to authenticate customers and achieve entry to protected sources. In our scheme, the backend features because the RP, including an additional layer of safety in dealing with authentication and tokens.
  • The OpenID Supplier (OP) is an authorization server accountable for verifying the identification of the Finish-Person. It authenticates the consumer and points identification and entry tokens to the Relying Occasion. As an example, within the diagram, the Abblix OIDC Server serves because the OpenID Supplier. This server ensures that customers are who they declare to be and supplies the required tokens to the purposes that must work together securely with protected sources.
  • The Useful resource Server is the place the protected data is saved and managed. It hosts delicate knowledge like private particulars, monetary data, and personal paperwork. In OpenID Join, the Useful resource Server makes use of the tokens issued by the OpenID Supplier to determine whether or not to grant or deny entry to this data. The tokens confirm the consumer’s identification and permissions, making certain that solely approved customers can entry the delicate knowledge.

Step-By-Step Course of

Step 1: Person Requests a Protected Useful resource

The consumer requests a protected useful resource, prompting the appliance to test the authentication standing. Seeing that the consumer will not be logged in, the appliance redirects the consumer’s browser to the OpenID Supplier to provoke the authentication course of.

Step 2: Redirect to Authentication

The Relying Occasion (RP) initiates a redirect of the consumer’s browser to the OpenID Supplier for authentication, utilizing parameters equivalent to client_id, redirect_uri, response_type=code, scope, and state.

  • client_id: This identifies the shopper utility making the request; for instance, client_id=myapp-12345.
  • redirect_uri: This specifies the URL to which the OpenID Supplier ought to redirect the consumer after authentication; for instance, redirect_uri=https://myapp.com/callback.
  • response_type=code: This means that the shopper expects an authorization code within the response.
  • scope: This defines the entry privileges requested, equivalent to entry to the consumer’s profile data.; for instance, scope=openid profile electronic mail.
  • state: This parameter helps forestall CSRF assaults by sustaining state between the request and the callback, making certain the response matches the unique request made by the shopper; for instance, state=abc123.

Listed here are examples of what the parameters would possibly appear to be in an actual request:

  • client_id=myapp-12345
  • redirect_uri=https://myapp.com/callback
  • response_type=code
  • scope=openid profile electronic mail
  • state=abc123

By together with these parameters, the shopper utility ensures that the OpenID Supplier can accurately determine the shopper, know the place to ship the response, perceive what varieties of tokens are being requested, outline the entry privileges, and safe communication towards CSRF assaults.

Step 3: Present Login Kind

The OpenID Supplier shows a login type to the consumer. This type prompts the consumer to enter their login credentials.

Step 4: Kind Submission

The consumer enters their login credentials into the shape, together with their username and password, and submits it. The OpenID Supplier processes these credentials to authenticate the consumer.

Step 5: Authorization Code Response

Upon profitable authentication, the OpenID Supplier redirects the consumer’s browser again to the desired redirect_uri with an authorization_code included within the URL. The authorization_code is a short-lived, one-time code (e.g., authorization_code=SplxlOBeZQQYbYS6WxSbIA) that the shopper exchanges for an entry token. This ensures safe token issuance. This code is utilized by the shopper to request an entry token from the OpenID Supplier.

After this step, as soon as the authorization_code is obtained within the browser, a number of varieties of assaults can happen.

Step 6: Asynchronous Entry Token Request

The shopper utility (Relying Occasion) sends an asynchronous request to the OpenID Supplier’s token endpoint to alternate the authorization code for an entry token. This request consists of the authorization_code, client_id, client_secret, and redirect_uri.

  • authorization_code: The code obtained within the redirect response
    • Instance: authorization_code=SplxlOBeZQQYbYS6WxSbIA
  • client_id: The identical shopper identifier used within the preliminary request
    • Instance: client_id=myapp-12345
  • client_secret: A secret identified solely to the shopper and the OpenID Supplier, used to authenticate the shopper
    • Instance: client_secret=shhh-its-a-secret
  • redirect_uri: The identical URL used within the preliminary request to make sure consistency
    • Instance: redirect_uri=https://myapp.com/callback

Step 7: Entry Token Response

The OpenID Supplier validates the request and, if profitable, returns an access_token and a refresh_token to the shopper utility.

  • access_token: A credential used to entry protected sources
    • Instance: access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
  • refresh_token: Used to acquire a brand new entry token with out requiring the consumer to re-authenticate
    • Instance: refresh_token=def502b0b22b3d8f67d1b7c2c1b5a6b2c7d1f7f8c8b9c4e2

Step 8: Async Person Information Request

The shopper utility sends an asynchronous request to the OpenID Supplier’s consumer data endpoint to retrieve the consumer’s profile data, utilizing the access_token.

Step 9: Person Information Response

The OpenID Supplier responds with the consumer’s claims (e.g., title, electronic mail).

Step 10: Response from Protected Useful resource

The shopper utility receives the protected useful resource knowledge in response to the access_token and user_claims request.

Step 11: Protected Useful resource Request

The shopper utility sends a request to the Useful resource Server utilizing the access_token to acquire protected knowledge.

Step 12: Protected Useful resource Response

The Useful resource Server verifies the entry token and, if legitimate, responds to the shopper’s request, by offering entry to the protected knowledge. This data is shipped again to the shopper utility to be used by the consumer.

Abstract of OpenID Join Authorization Code Circulation With Token-Mediating Backend

The OpenID Join Authorization Code Circulation with Token-Mediating Backend includes a multi-step course of to make sure safe authentication and authorization. It consists of redirecting the consumer to the OpenID Supplier for authentication, the consumer submitting their login credentials, the OpenID Supplier returns an authorization code within the redirect response, after which the appliance securely exchanges this code for an entry token. This circulation enhances safety by making certain that the authorization code can solely be utilized by the shopper that requested it, considerably lowering the chance of token interception.

Utilizing a backend Relying Occasion (RP) helps tackle a number of vulnerabilities:

  • Acquisition and extraction of recent tokens: By dealing with token requests on the server aspect, a backend RP prevents malicious JavaScript from initiating silent requests to the OpenID Supplier and acquiring new entry tokens.
  • Token transmission through URL: When tokens are managed by a backend RP, they don’t seem to be uncovered within the URL, browser historical past, logs, or HTTP referrers, lowering the chance of interception and unauthorized use.
  • Lack of refresh token help: Backend RPs can securely retailer and use refresh tokens, permitting for long-term session upkeep with out frequent token requests, thus minimizing the probabilities of token leakage and misuse.

Nonetheless, even with this enhanced circulation, some vulnerabilities stay:

  • Code Interception Assault: An attacker can intercept the authorization code that’s uncovered within the URL throughout the redirect, leading to unauthorized entry to the consumer’s sources.
  • Persistent token theft: Tokens are current within the browser, and recent tokens might be stolen constantly, for instance, each 10 seconds.
  • Proxying requests through the consumer’s browser: Malicious JavaScript can “click buttons in the application” on behalf of the consumer, sending requests to the Protected Useful resource within the consumer’s title.

To handle a few of these safety issues, utilizing the OpenID Join Authorization Code Circulation with PKCE in a backend implementation might be efficient. Within the subsequent part, we’ll look at this diagram and discover how PKCE prevents code interception assaults successfully, offering a safer mechanism for token dealing with and transmission.

Authorization Code Circulation With Token-Mediating Backend and PKCE

Overview

  • In OpenID Join, the Finish-Person is the particular person utilizing an utility to entry companies. These purposes can embody cell apps, internet apps, desktop apps, IoT gadgets, gaming purposes, and smartwatches. The Finish-Person interacts with these purposes to entry varied companies and sources.
  • The Relying Occasion (RP) is the backend element of the appliance that the Finish-Person interacts with. The RP is determined by the OpenID Supplier for consumer authentication. It requests tokens from the OpenID Supplier to authenticate customers and achieve entry to protected sources. In our scheme, the backend features because the RP, including an additional layer of safety in dealing with authentication and tokens.
  • The OpenID Supplier (OP) is an authorization server accountable for verifying the identification of the Finish-Person. It authenticates the consumer and points identification and entry tokens to the Relying Occasion. As an example, within the diagram, the Abblix OIDC Server serves because the OpenID Supplier. This server ensures that customers are who they declare to be and supplies the required tokens to the purposes that must work together securely with protected sources.
  • The Useful resource Server is the place the protected data is saved and managed. It hosts delicate knowledge like private particulars, monetary data, and personal paperwork. In OpenID Join, the Useful resource Server makes use of the tokens issued by the OpenID Supplier to determine whether or not to grant or deny entry to this data. The tokens confirm the consumer’s identification and permissions, making certain that solely approved customers can entry the delicate knowledge.
  • The Proof Key for Code Alternate (PKCE) is designed to stop authorization code interception and injection assaults. It ensures that solely the shopper that requested the authorization code can use it. PKCE permits public purchasers, equivalent to cell and single-page purposes, to make use of the Authorization Code Circulation securely.

Step-By-Step Course of

Step 1: Person Requests a Protected Useful resource

The consumer requests a protected useful resource, prompting the appliance to test the authentication standing. Seeing that the consumer will not be logged in, the appliance redirects the consumer’s browser to the OpenID Supplier to provoke the authentication course of.

Step 2: Redirect to Authentication

The Relying Occasion (RP) initiates a redirect of the consumer’s browser to the OpenID Supplier for authentication, utilizing parameters equivalent to client_id, redirect_uri, response_type=code, scope, state, and, particularly for PKCE, code_challenge and code_challenge_method.

Earlier than making the request, the shopper generates a high-entropy cryptographic random string often known as the code_verifier. The code_challenge is then derived by hashing the code_verifier utilizing the desired code_challenge_method. This code_challenge is shipped with the preliminary authorization request, making certain that the code_verifier is used later to show that the true originator of the authorization request is asking for a token in alternate for the authorization code.

  • client_id: This identifies the shopper utility making the request; for instance, client_id=myapp-12345.
  • redirect_uri: This specifies the URL to which the OpenID Supplier ought to redirect the consumer after authentication; for instance, redirect_uri=https://myapp.com/callback.
  • response_type=code: This means that the shopper expects an authorization code within the response.
  • scope: This defines the entry privileges requested, equivalent to entry to the consumer’s profile data; for instance, scope=openid profile electronic mail.
  • state: This parameter helps forestall CSRF assaults by sustaining the state between the request and the callback, making certain the response matches the unique request made by the shopper; for instance, state=abc123.
  • code_challenge: It is a derived worth used to make sure that the token alternate is safe.
    • Instance: code_challenge=abc123def456acd789
  • code_challenge_method: This specifies the tactic used to generate the code problem; for instance, code_challenge_method=S256.

Listed here are examples of what the parameters would possibly appear to be in an actual request:

  • client_id=myapp-12345
  • redirect_uri=https://myapp.com/callback
  • response_type=code
  • scope=openid profile electronic mail
  • state=abc123
  • code_challenge=abc123def456acd789
  • code_challenge_method=S256

By together with these parameters, the shopper utility ensures that the OpenID Supplier can accurately determine the shopper, know the place to ship the response, perceive what varieties of tokens are being requested, outline the entry privileges, and safe communication towards CSRF assaults.

Step 3: Present Login Kind

The OpenID Supplier shows a login type to the consumer. This type prompts the consumer to enter their login credentials.

Step 4: Kind Submission

The consumer enters their login credentials into the shape, together with their username and password, and submits it. The OpenID Supplier processes these credentials to authenticate the consumer.

Step 5: Authorization Code Response

Upon profitable authentication, the OpenID Supplier redirects the consumer’s browser again to the desired redirect_uri with an authorization code included within the URL. The authorization_code is a short-lived, one-time code (e.g., authorization_code=SplxlOBeZQQYbYS6WxSbIA) that the shopper exchanges for an entry token. This ensures safe token issuance. This code is utilized by the shopper to request an entry token from the OpenID Supplier.

Step 6: Asynchronous Entry Token Request

The shopper utility (Relying Occasion) sends an asynchronous request to the OpenID Supplier’s token endpoint to alternate the authorization code for an entry token. This request consists of the authorization_code, client_id, client_secret, redirect_uri, and the unique worth of code_verifier.

  • authorization_code: The code obtained within the redirect response 
    • Instance: authorization_code=SplxlOBeZQQYbYS6WxSbIA
  • client_id: The identical shopper identifier used within the preliminary request
    • Instance: client_id=myapp-12345
  • client_secret: A secret identified solely to the shopper and the OpenID Supplier, used to authenticate the shopper
    • Instance: client_secret=shhh-its-a-secret
  • redirect_uri: The identical URL used within the preliminary request to make sure consistency
    • Instance: redirect_uri=https://myapp.com/callback
  • code_verifier: The unique random string that was used to generate the code problem
    • Instance: code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk

Step 7: Entry Token Response

The OpenID Supplier validates the request and, if profitable, returns an access_token and a refresh_token to the shopper utility.

  • access_token: A credential used to entry protected sources 
    • Instance: access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
  • refresh_token: Used to acquire a brand new entry token with out requiring the consumer to re-authenticate
    • Instance: refresh_token=def502b0b22b3d8f67d1b7c2c1b5a6b2c7d1f7f8c8b9c4e2

Step 8: Async Person Information Request

The shopper utility sends an asynchronous request to the OpenID Supplier’s consumer data endpoint to retrieve the consumer’s profile data, utilizing the access_token.

Step 9: Person Information Response

The OpenID Supplier responds with the consumer’s claims (e.g., title, electronic mail).

Step 10: Response from Protected Useful resource

The shopper utility receives the protected useful resource knowledge in response to the access_token and user_claims request.

After this step, as soon as the access_token is obtained within the browser, a number of varieties of assaults can happen.

Step 11: Protected Useful resource Request

The shopper utility sends a request to the Useful resource Server utilizing the access_token to acquire protected knowledge.

Step 12: Protected Useful resource Response

The Useful resource Server verifies the entry token and, if legitimate, responds to the shopper’s request, by offering entry to the protected knowledge. This data is shipped again to the shopper utility to be used by the consumer.

Abstract of OpenID Join Authorization Code Circulation With Token-Mediating Backend and PKCE

The OpenID Join Authorization Code Circulation with PKCE in a backend implementation includes a multi-step course of to make sure safe authentication and authorization. It consists of redirecting the consumer to the OpenID Supplier for authentication, the consumer submitting their login credentials, the OpenID Supplier returns an authorization code within the redirect response, after which the appliance securely exchanges this code for an entry token. By making certain that solely the shopper who requested the authorization code can use it, this circulation considerably reduces the chance of token interception.

Utilizing a backend Relying Occasion (RP) helps tackle a number of vulnerabilities:

  • Acquisition and extraction of recent tokens: By dealing with token requests on the server aspect, a backend RP prevents malicious JavaScript from initiating silent requests to the OpenID Supplier and acquiring new entry tokens.
  • Token transmission through URL: When tokens are managed by a backend RP, they don’t seem to be uncovered within the URL, browser historical past, logs, or HTTP referrers, lowering the chance of interception and unauthorized use.
  • Lack of refresh token help: Backend RPs can securely retailer and use refresh tokens, permitting for long-term session upkeep with out frequent token requests, thus minimizing the probabilities of token leakage and misuse.

Utilizing PKCE (Proof Key for Code Alternate) additional enhances safety by addressing the Code Interception Assault vulnerability. PKCE ensures that the authorization code can solely be exchanged for tokens by the shopper that requested it, stopping attackers from intercepting the code throughout the redirect.

Nonetheless, even with these enhancements, some vulnerabilities stay:

  • Persistent token theft: Tokens are current within the browser, and recent tokens might be stolen constantly, for instance, each 10 seconds.
  • Proxying requests through the consumer’s browser: Malicious JavaScript can “click buttons in the application” on behalf of the consumer, sending requests to the Protected Useful resource within the consumer’s title.

Utilizing the Backend for Frontend (BFF) sample can additional improve safety by addressing the vulnerability of entry token interception. The BFF sample includes making a backend service that serves as an middleman between the end-user’s shopper utility and the authentication supplier. By dealing with entry tokens on the server aspect, the BFF sample prevents the entry token from being uncovered to the end-user’s machine. This strategy mitigates the chance of entry token interception by malicious JavaScript, making certain that tokens are by no means straight accessible from the consumer’s browser, thereby enhancing general safety.

Authorization Code Circulation With PKCE and BFF

Overview

  • In OpenID Join, the Finish-Person is the particular person utilizing an utility to entry companies. These purposes can embody cell apps, internet apps, desktop apps, IoT gadgets, gaming purposes, and smartwatches. The Finish-Person interacts with these purposes to entry varied companies and sources.
  • The Relying Occasion (RP) on this case is a backend element that makes use of the Backend for Frontend (BFF) sample. The BFF acts as an middleman between the shopper utility and the OpenID Supplier. It handles authentication and token administration, including an additional layer of safety.
  • The OpenID Supplier (OP) is an authorization server accountable for verifying the identification of the Finish-Person. It authenticates the consumer and points identification and entry tokens to the Relying Occasion. As an example, within the diagram, the Abblix OIDC Server serves because the OpenID Supplier. This server ensures that customers are who they declare to be and supplies the required tokens to the purposes that must work together securely with protected sources.
  • The Useful resource Server is the place the protected data is saved and managed. It hosts delicate knowledge like private particulars, monetary data, and personal paperwork. In OpenID Join, the Useful resource Server makes use of the tokens issued by the OpenID Supplier to determine whether or not to grant or deny entry to this data. The tokens confirm the consumer’s identification and permissions, making certain that solely approved customers can entry the delicate knowledge.
  • The Proof Key for Code Alternate (PKCE) is designed to stop authorization code interception and injection assaults. It ensures that the authorization code can solely be utilized by the shopper that requested it. PKCE permits public purchasers, equivalent to cell and single-page purposes, to make use of the Authorization Code Circulation securely.
  • The Backend for Frontend (BFF) sample includes making a backend service that serves as an middleman between the shopper utility and the authentication supplier. The BFF handles authentication and token administration on the server aspect, stopping the entry token from being uncovered to the end-user. This strategy mitigates the chance of entry token interception by malicious JavaScript, making certain that tokens are by no means straight accessible from the consumer’s browser, thereby considerably enhancing general safety.

Step-By-Step Course of

Step 1: Person Requests a Protected Useful resource

The consumer requests a protected useful resource, prompting the appliance to test the authentication standing. Seeing that the consumer will not be logged in, the appliance redirects the consumer’s browser to the OpenID Supplier to provoke the authentication course of.

Step 2: Redirect to Authentication

The Relying Occasion (RP) initiates a redirect of the consumer’s browser to the OpenID Supplier for authentication, utilizing parameters equivalent to client_id, redirect_uri, response_type=code, scope, state, and, particularly for PKCE, code_challenge and code_challenge_method.

Earlier than making the request, the shopper generates a high-entropy cryptographic random string often known as the code_verifier. The code_challenge is then derived by hashing the code_verifier utilizing the desired code_challenge_method. This code_challenge is shipped with the preliminary authorization request, making certain that the code_verifier is used later to show that the true originator of the authorization request is asking for a token in alternate for the authorization code.

  • client_id: This identifies the shopper utility making the request; for instance, client_id=myapp-12345.
  • redirect_uri: This specifies the URL to which the OpenID Supplier ought to redirect the consumer after authentication; for instance, redirect_uri=https://myapp.com/callback.
  • response_type=code: This means that the shopper expects an authorization code within the response.
  • scope: This defines the entry privileges requested, equivalent to entry to the consumer’s profile data; for instance, scope=openid profile electronic mail.
  • state: This parameter helps forestall CSRF assaults by sustaining the state between the request and the callback, making certain the response matches the unique request made by the shopper; for instance, state=abc123.
  • code_challenge: It is a derived worth used to make sure that the token alternate is safe.
    • Instance: code_challenge=abc123def456acd789.
  • code_challenge_method: This specifies the tactic used to generate the code problem; for instance, code_challenge_method=S256.

Listed here are examples of what the parameters would possibly appear to be in an actual request:

  • client_id=myapp-12345
  • redirect_uri=https://myapp.com/callback
  • response_type=code
  • scope=openid profile electronic mail
  • state=abc123
  • code_challenge=abc123def456acd789
  • code_challenge_method=S256

By together with these parameters, the shopper utility ensures that the OpenID Supplier can accurately determine the shopper, know the place to ship the response, perceive what varieties of tokens are being requested, outline the entry privileges, and safe communication towards CSRF assaults.

Step 3: Present Login Kind

The OpenID Supplier shows a login type to the consumer. This type prompts the consumer to enter their login credentials.

Step 4: Kind Submission

The consumer enters their login credentials into the shape, together with their username and password, and submits it. The OpenID Supplier processes these credentials to authenticate the consumer.

Step 5: Authorization Code Response

Upon profitable authentication, the OpenID Supplier redirects the consumer’s browser again to the desired redirect_uri with an authorization code included within the URL. The authorization_code is a short-lived, one-time code (e.g., authorization_code=SplxlOBeZQQYbYS6WxSbIA) that the shopper exchanges for an entry token. This ensures safe token issuance. This code is utilized by the shopper to request an entry token from the OpenID Supplier.

Step 6: Asynchronous Entry Token Request

The shopper utility (Relying Occasion) sends an asynchronous request to the OpenID Supplier’s token endpoint to alternate the authorization code for an entry token. This request consists of the authorization_code, client_id, client_secret, redirect_uri, and, particularly for PKCE, the unique worth of code_verifier.

  • authorization_code: The code obtained within the redirect response
    • Instance: authorization_code=SplxlOBeZQQYbYS6WxSbIA
  • client_id: The identical shopper identifier used within the preliminary request
    • Instance: client_id=myapp-12345
  • redirect_uri: The identical URL used within the preliminary request to make sure consistency
    • Instance: redirect_uri=https://myapp.com/callback
  • code_verifier: The unique random string that was used to generate the code problem
    • Instance: code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk

Step 7: Entry Token Response

The OpenID Supplier validates the request and, if profitable, returns an access_token and a refresh_token to the shopper utility. At this stage, the BFF shops the access_token.

  • access_token: A credential used to entry protected sources
    • Instance: access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
  • refresh_token: Used to acquire a brand new entry token with out requiring the consumer to re-authenticate
    • Instance: refresh_token=def502b0b22b3d8f67d1b7c2c1b5a6b2c7d1f7f8c8b9c4e2

Step 8: Async Person Information Request

The shopper utility sends an asynchronous request to the OpenID Supplier’s consumer data endpoint to retrieve the consumer’s profile data, utilizing the access_token.

Step 9: Person Information Response

The OpenID Supplier responds with the consumer’s claims (e.g., title, electronic mail).

Step 10: Response from Protected Useful resource

The shopper utility receives the protected useful resource knowledge in response to the user_claims request.

Step 11: Protected Useful resource Request

The shopper utility sends a request to the BFF, which then makes use of the access_token to make a request to the Useful resource Server to acquire protected knowledge.

Step 12: Protected Useful resource Response

The Useful resource Server verifies the entry token and, if legitimate, responds to the BFF’s request by offering entry to the protected knowledge. This knowledge is shipped again to the shopper utility through the BFF.

Abstract of OpenID Join Authorization Code Circulation With PKCE and BFF

The OpenID Join Authorization Code Circulation with PKCE and BFF includes a multi-step course of to make sure safe authentication and authorization. It consists of redirecting the consumer to the OpenID Supplier for authentication, the consumer submitting their login credentials, the OpenID Supplier returns an authorization code within the redirect response, after which the appliance securely exchanges this code for an entry token. By making certain that the authorization code can solely be utilized by the shopper that requested it, this circulation considerably reduces the chance of token interception.

Utilizing a backend Relying Occasion (RP) and the BFF sample helps tackle a number of vulnerabilities:

  • Acquisition and extraction of recent tokens: By dealing with token requests on the server aspect, a backend RP prevents malicious JavaScript from initiating silent requests to the OpenID Supplier and acquiring new entry tokens.
  • Token transmission through URL: When tokens are managed by a backend RP, they don’t seem to be uncovered within the URL, browser historical past, logs, or HTTP referrers, lowering the chance of interception and unauthorized use.
  • Lack of refresh token help: Backend RPs can securely retailer and use refresh tokens, permitting for long-term session upkeep with out frequent token requests, thus minimizing the probabilities of token leakage and misuse.

Utilizing Proof Key for Code Alternate (PKCE) additional enhances safety by addressing the Code Interception Assault vulnerability. PKCE ensures that the authorization code can solely be exchanged for tokens by the shopper that requested it, stopping attackers from intercepting the code throughout the redirect.

The BFF sample provides one other layer of safety by stopping entry tokens from being uncovered to the end-user’s machine. This strategy mitigates the chance of Token Theft, making certain that tokens are by no means straight accessible from the consumer’s browser.

Nonetheless, even with these enhancements, one identified vulnerability stays:

  • Proxying requests through the consumer’s browser: Malicious JavaScript can “click buttons in the application” on behalf of the consumer, sending requests to the Protected Useful resource within the consumer’s title.

An internet utility operating in a browser really executes in an surroundings that isn’t managed by the developer, making it unattainable to make sure absolute safety. That is why eliminating tokens from the browser is a trending course for bettering internet utility safety.

To mitigate dangers related to this vulnerability, customers ought to set up solely verified browser extensions, preserve browsers up to date to the newest model, and use antivirus software program to boost general safety.

As of 2024, this authentication scheme is probably the most safe and really helpful to be used, offering strong safety for accessing protected sources.

Conclusion

We explored varied flows, ranging from the Implicit Circulation in a frontend-only implementation, which, whereas easier, is fraught with vulnerabilities. Transferring to safer strategies, the Authorization Code Circulation with PKCE, each in frontend-only and backend-involving implementations, considerably reduces dangers related to token interception and theft.

The mixture of Authorization Code Circulation with PKCE and the Backend for Frontend (BFF) sample represents probably the most safe strategy accessible as of 2024. This setup not solely prevents tokens from being uncovered to the consumer’s browser but in addition mitigates a number of frequent assault vectors like code interception, token theft, and unauthorized token acquisition. Nonetheless, it’s nonetheless important to be vigilant towards browser-based assaults, equivalent to these involving malicious JavaScript.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version