<@U04UQ68083H> why do i need the `redirect_uri` wh...
# ory-copilot
m
@chilly-king-10285 why do i need the
redirect_uri
when im exchanging the code for jwt?
c
A message from kapa.ai
The
redirect_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).
m
@chilly-king-10285 so when exchanging for jwt, we should take the origin domain of the request as the return_url to make sure only the authorized client can call it?
c
A message from kapa.ai
Yes, when exchanging the authorization code for a JWT, the
redirect_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:
Copy code
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).
m
@chilly-king-10285 what is the most secure way to send the token back to the client?
c
A message from kapa.ai
The most secure way to send the token back to the client is through HTTPS redirects. After the user has authenticated and authorized the application, the Authorization Server will redirect the user back to the application with the authorization code included in the URL. This is part of the Authorization Code Flow, which is the most secure and widely used OAuth2 flow for web applications. Here's a high-level overview of how it works: 1. The user clicks on a link or button on a web page that requests access to a resource. 2. The user is redirected to the Authorization Server, where they authenticate themselves and grant permission to the requesting application. 3. The Authorization Server generates an authorization code and redirects the user back to the requesting application with the authorization code. The application then exchanges this code, along with its client secret, for an access token using the token endpoint. It's worth noting that in this flow, the client secret cannot be stored securely, since it needs to be included in the request to the token endpoint. For more information, see the [OAuth2 Authorization Code Flow](https://www.ory.sh/docs/oauth2-oidc/authorization-code-flow). Once the token is obtained, it should be stored securely. For web applications,
httpOnly
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).
m
@chilly-king-10285 but can the attacker just steal the token from the url?
c
A message from kapa.ai
Yes, there is a risk that an attacker could intercept the authorization code from the URL during the redirect from the Authorization Server back to the client. This is known as an "Authorization Code Interception Attack". However, OAuth 2.0 provides a mechanism to mitigate this risk through the use of the Proof Key for Code Exchange (PKCE) extension. PKCE is a protocol that prevents the authorization code from being intercepted and used by an attacker. It does this by associating a secret created by the client with the authorization code. This secret, known as the
code_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:
Copy code
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).