melodic-oil-50942
05/09/2024, 4:05 PMredirect_uri
when im exchanging the code for jwt?chilly-king-10285
05/09/2024, 4:05 PMchilly-king-10285
05/09/2024, 4:05 PMredirect_uri
is a crucial part of the OAuth2 and OpenID Connect flows, including the process of exchanging the authorization code for a JWT (JSON Web Token). When your application sends a request to exchange the authorization code for a token, it includes the redirect_uri
as one of the parameters. This is the same redirect_uri
that was used when your application initially requested the authorization code. The Authorization Server uses the redirect_uri
to validate the request. It ensures that the redirect_uri
in the token request matches the redirect_uri
that was used to get the authorization code. This is a security measure to prevent an attacker from intercepting the authorization code and using it to get a token. If the redirect_uri
in the token request does not match the redirect_uri
in the authorization code request, the Authorization Server will not issue a token and will instead return an error. This helps to ensure that only the legitimate client that requested the authorization code can exchange it for a token. For more details, you can refer to the [Ory documentation on the Authorization Code Flow](https://www.ory.sh/docs/oauth2-oidc/authorization-code-flow#step-4-exchange-code-for-token).melodic-oil-50942
05/10/2024, 2:38 PMchilly-king-10285
05/10/2024, 2:38 PMchilly-king-10285
05/10/2024, 2:38 PMredirect_uri
(or return_url
as you referred to it) should be the same as the one used in the initial authorization request. This URI is typically the endpoint in your application where you want the Authorization Server to send the user after they have authorized the app. This redirect_uri
is important for a couple of reasons: 1. It ensures that the tokens are sent to the correct application. The Authorization Server checks that the redirect_uri
in the token request matches the redirect_uri
in the initial authorization request. 2. It helps to prevent what's known as an "Authorization Code Interception Attack". This is where an attacker intercepts the authorization code and tries to exchange it for a token. By checking the redirect_uri
, the Authorization Server can ensure that it's giving the token to the correct application. Here's an example of how you might include the redirect_uri
in a token request: javascript const code = "authorization_code_received_from_auth_server" const tokenUrl = "https://{project.slug}.<http://projects.oryapis.com/oauth2/token|projects.oryapis.com/oauth2/token>" const requestBody = `grant_type=authorization_code&client_id=${clientId}&redirect_uri=${redirectUri}&code=${code}` fetch(tokenUrl, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: requestBody, }) .then((data) => console.log(data)) .catch((error) => console.error(error))
In this example, redirectUri
is the redirect_uri
that you used in the initial authorization request. For more information, you can refer to the [Ory documentation on the Authorization Code Flow](https://www.ory.sh/docs/oauth2-oidc/authorization-code-flow#step-4-exchange-code-for-token).melodic-oil-50942
05/10/2024, 2:41 PMchilly-king-10285
05/10/2024, 2:41 PMchilly-king-10285
05/10/2024, 2:41 PMhttpOnly
and secure
cookies are recommended. For mobile apps, the OS keychain is recommended. Window.localStorage
is also an acceptable storage layer. For more details on secure storage, refer to the [Ory documentation on Refresh Token Rotation and Security Protections](https://www.ory.sh/docs/oauth2-oidc/refresh-token-grant#refresh-token-rotation-and-security-protections).melodic-oil-50942
05/10/2024, 2:43 PMchilly-king-10285
05/10/2024, 2:43 PMchilly-king-10285
05/10/2024, 2:44 PMcode_verifier
, is not sent in the redirect, so it cannot be intercepted. When the client exchanges the authorization code for an access token, it must also send the code_verifier
. The Authorization Server will only issue an access token if the code_verifier
received matches the one associated with the authorization code. Here's an example of how you might include the code_verifier
in a token request: javascript const code = "authorization_code_received_from_auth_server" const tokenUrl = "https://{project.slug}.<http://projects.oryapis.com/oauth2/token|projects.oryapis.com/oauth2/token>" const requestBody = `grant_type=authorization_code&client_id=${clientId}&code_verifier=${codeVerifier}&code=${code}` fetch(tokenUrl, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: requestBody, }) .then((data) => console.log(data)) .catch((error) => console.error(error))
In this example, codeVerifier
is the code_verifier
that was used in the initial authorization request. For more information, you can refer to the [Ory documentation on the Authorization Code Flow](https://www.ory.sh/docs/oauth2-oidc/authorization-code-flow#step-4-exchange-code-for-token).