clean-piano-39737
01/22/2025, 11:39 AMbrief-egg-61061
01/22/2025, 11:39 AMbrief-egg-61061
01/22/2025, 11:39 AM/oauth2/introspect
) is part of the administrative API. Make sure to secure this endpoint properly, as it doesn't have built-in access control. 2. Configuring Oathkeeper for OAuth2 Introspection: To enable OAuth2 introspection in Oathkeeper, you need to configure the oauth2_introspection
authenticator. Here's how you can do it: In your Oathkeeper configuration file (e.g., oathkeeper.yml
), add the following: yaml authenticators: oauth2_introspection: enabled: true config: introspection_url: <http://your-hydra-admin-url:4445/oauth2/introspect> scope_strategy: exact required_scope: - your_required_scope
Replace your-hydra-admin-url
with the actual URL of your Hydra admin API, and your_required_scope
with the scope you want to require. 3. Using OAuth2 Introspection: a) Without Oathkeeper: You can use the Ory Hydra CLI to perform token introspection directly: bash docker run --rm -it oryd/hydra:v2.0.1 \ introspect token \ --endpoint <http://your-hydra-admin-url:4445> \ "your-access-token-here"
This command will return the token's metadata if it's valid. b) With Oathkeeper: When using Oathkeeper, you don't need to perform the introspection manually. Oathkeeper will handle it based on your configuration. You just need to send requests to your API with the Bearer token in the Authorization header: Authorization: Bearer your-access-token-here
Oathkeeper will then use the configured oauth2_introspection
authenticator to validate the token against Hydra. For more detailed information, you can refer to the following resources: - [Ory Oathkeeper OAuth2 Introspection](https://www.ory.sh/docs/oathkeeper/pipeline/authn#oauth2_introspection) - [Ory Hydra Introspection Guide](https://www.ory.sh/docs/hydra/5min-tutorial) Remember to adjust the URLs and configurations according to your specific self-hosted environment.clean-piano-39737
01/22/2025, 12:32 PMbrief-egg-61061
01/22/2025, 12:32 PMbrief-egg-61061
01/22/2025, 12:32 PMrefresh_token
grant type: bash docker run --rm -it --network hydraguide oryd/hydra:v2.0.1 \ create client \ --endpoint <http://127.0.0.1:4445> \ --grant-type authorization_code,refresh_token \ --response-type code \ --scope openid,offline_access \ --redirect-uri <http://127.0.0.1:5555/callback>
Note the inclusion of refresh_token
in the --grant-type
flag and offline_access
in the --scope
flag. The offline_access
scope is required to receive a refresh token. 2. When performing the OAuth2 flow, make sure to request the offline_access
scope: bash docker run --rm -it --network hydraguide oryd/hydra:v2.0.1 \ hydra perform authorization-code \ --client-id $code_client_id \ --client-secret $code_client_secret \ --endpoint <http://127.0.0.1:4444/> \ --port 5555 \ --scope openid --scope offline
This will result in receiving both an access token and a refresh token. 3. You can configure the refresh token lifespan using the Ory CLI: bash ory patch oauth2-config --project <project-id> --workspace <workspace-id> \ --replace "/ttl/refresh_token=\"24h\"" \ --format yaml
This example sets the refresh token lifespan to 24 hours. Note that the maximum age of refresh tokens is 6 months. For more detailed information, you can refer to these sources: - [Ory Hydra 5 Minute Tutorial](https://www.ory.sh/docs/hydra/5min-tutorial) - [Ory OAuth2 Refresh Token Grant](https://www.ory.sh/docs/oauth2-oidc/refresh-token-grant#change-refresh-token-lifespan) Remember to adjust the endpoints and configurations according to your specific self-hosted environment.clean-piano-39737
01/22/2025, 12:41 PMbrief-egg-61061
01/22/2025, 12:41 PMbrief-egg-61061
01/22/2025, 12:41 PMPOST /oauth2/token HTTP/1.1 Host: your-hydra-public-url Content-Type: application/x-www-form-urlencoded grant_type=authorization_code &client_id=your-client-id &client_secret=your-client-secret &code=the-authorization-code &redirect_uri=<http://127.0.0.1:5555/callback>
This request should be sent to the token endpoint of your Ory Hydra instance. The response will include both an access token and a refresh token (if the client is configured to receive refresh tokens and the offline_access
scope was requested). Here's an example of how you might make this request using cURL: bash curl -X POST \ <https://your-hydra-public-url/oauth2/token> \ -H 'Content-Type: application/x-www-form-urlencoded' \ -d 'grant_type=authorization_code' \ -d 'client_id=your-client-id' \ -d 'client_secret=your-client-secret' \ -d 'code=the-authorization-code' \ -d 'redirect_uri=<http://127.0.0.1:5555/callback>'
Remember to replace your-hydra-public-url
, your-client-id
, your-client-secret
, the-authorization-code
, and the redirect_uri
with your actual values. The response will look something like this: json { "access_token": "access-token-here", "token_type": "bearer", "expires_in": 3600, "refresh_token": "refresh-token-here", "scope": "openid offline_access" }
Note that to receive a refresh token, you must have configured your client to support the refresh_token
grant type and requested the offline_access
scope during the initial authorization request, as mentioned in the previous answer. For more detailed information, you can refer to the [Ory OAuth2 Refresh Token Grant documentation](https://www.ory.sh/docs/oauth2-oidc/refresh-token-grant).clean-piano-39737
01/22/2025, 12:46 PMbrief-egg-61061
01/22/2025, 12:46 PMbrief-egg-61061
01/22/2025, 12:46 PM<https://your-hydra-public-url/oauth2/auth?response_type=code&client_id=your-client-id&redirect_uri=your-redirect-uri&scope=openid> offline_access&state=some-random-state
Replace your-hydra-public-url
, your-client-id
, and your-redirect-uri
with your actual values. 2. Redirect the user to this URL. The user will be prompted to log in and consent to the requested scopes. 3. After successful authentication and consent, Hydra will redirect the user back to your redirect_uri
with the authorization code as a query parameter. For example, if your redirect_uri
is <http://127.0.0.1:5555/callback>
, the redirect might look like this: <http://127.0.0.1:5555/callback?code=authorization-code-here&state=some-random-state>
You can then extract the code
parameter from this URL to get your authorization code. Here's an example of how you can initiate this flow using the Ory CLI: bash ory perform authorization-code \ --project $project_id \ --client-id $code_client_id \ --client-secret $code_client_secret
This command will start a demo web server and open a browser window to begin the authorization flow. After you complete the flow, the authorization code will be displayed in the terminal. For more detailed information, you can refer to the [Ory OAuth2 Authorization Code Flow documentation](https://www.ory.sh/docs/oauth2-oidc/authorization-code-flow). Remember to adjust the URLs and parameters according to your specific Ory Hydra setup. [Ory OAuth2 Authorization Code Flow](https://www.ory.sh/docs/oauth2-oidc/authorization-code-flow) [Run your own OAuth2 Server](https://www.ory.sh/run-oauth2-server-open-source-api-security/#perform-oauth2-authorization-code-flow)clean-piano-39737
01/22/2025, 2:39 PMbrief-egg-61061
01/22/2025, 2:39 PMbrief-egg-61061
01/22/2025, 2:39 PMbash
ory create oauth2-client \
--project your-project-id \
--name "My Client" \
--grant-type authorization_code,refresh_token \
--response-type code \
--scope openid,offline_access \
--redirect-uri <http://127.0.0.1:5555/callback>
Make sure to replace your-project-id
with your actual project ID.
2. Oathkeeper Configuration:
In your Oathkeeper configuration file (e.g., oathkeeper.yml
), add the following to enable OAuth2 introspection:
yaml
authenticators:
oauth2_introspection:
enabled: true
config:
introspection_url: <http://your-hydra-admin-url:4445/oauth2/introspect>
scope_strategy: exact
required_scope:
- openid
- offline_access
Replace your-hydra-admin-url
with the actual URL of your Hydra admin API.
Now, let's go through the HTTP curl requests needed to obtain and use a refresh token:
3. Initiate Authorization Code Flow:
bash
curl -X GET \
'<https://your-hydra-public-url/oauth2/auth?response_type=code&client_id=your-client-id&redirect_uri=http://127.0.0.1:5555/callback&scope=openid%20offline_access&state=some-random-state>'
This will return a redirect URL. Open this URL in a browser, log in, and authorize the application. You'll be redirected to your callback URL with the authorization code.
4. Exchange Authorization Code for Tokens:
bash
curl -X POST \
<https://your-hydra-public-url/oauth2/token> \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=authorization_code' \
-d 'client_id=your-client-id' \
-d 'client_secret=your-client-secret' \
-d 'code=your-authorization-code' \
-d 'redirect_uri=<http://127.0.0.1:5555/callback>'
This will return an access token and a refresh token.
5. Use Refresh Token to Get New Access Token:
bash
curl -X POST \
<https://your-hydra-public-url/oauth2/token> \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=refresh_token' \
-d 'client_id=your-client-id' \
-d 'client_secret=your-client-secret' \
-d 'refresh_token=your-refresh-token'
This will return a new access token and possibly a new refresh token.
6. Introspect Token (can be done through Oathkeeper):
bash
curl -X POST \
<http://your-hydra-admin-url:4445/oauth2/introspect> \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'token=your-access-token'
Remember to replace your-hydra-public-url
, your-hydra-admin-url
, your-client-id
, your-client-secret
, your-authorization-code
, and your-refresh-token
with your actual values in all these requests.brief-egg-61061
01/22/2025, 2:39 PMbash ory create oauth2-client \ --project your-project-id \ --name "My Client" \ --grant-type authorization_code,refresh_token \ --response-type code \ --scope openid,offline_access \ --redirect-uri <http://127.0.0.1:5555/callback>
Make sure to replace your-project-id
with your actual project ID. 2. Oathkeeper Configuration: In your Oathkeeper configuration file (e.g., oathkeeper.yml
), add the following to enable OAuth2 introspection: yaml authenticators: oauth2_introspection: enabled: true config: introspection_url: <http://your-hydra-admin-url:4445/oauth2/introspect> scope_strategy: exact required_scope: - openid - offline_access
Replace your-hydra-admin-url
with the actual URL of your Hydra admin API. Now, let's go through the HTTP curl requests needed to obtain and use a refresh token: 3. Initiate Authorization Code Flow: bash curl -X GET \ '<https://your-hydra-public-url/oauth2/auth?response_type=code&client_id=your-client-id&redirect_uri=http://127.0.0.1:5555/callback&scope=openid%20offline_access&state=some-random-state>'
This will return a redirect URL. Open this URL in a browser, log in, and authorize the application. You'll be redirected to your callback URL with the authorization code. 4. Exchange Authorization Code for Tokens: bash curl -X POST \ <https://your-hydra-public-url/oauth2/token> \ -H 'Content-Type: application/x-www-form-urlencoded' \ -d 'grant_type=authorization_code' \ -d 'client_id=your-client-id' \ -d 'client_secret=your-client-secret' \ -d 'code=your-authorization-code' \ -d 'redirect_uri=<http://127.0.0.1:5555/callback>'
This will return an access token and a refresh token. 5. Use Refresh Token to Get New Access Token: bash curl -X POST \ <https://your-hydra-public-url/oauth2/token> \ -H 'Content-Type: application/x-www-form-urlencoded' \ -d 'grant_type=refresh_token' \ -d 'client_id=your-client-id' \ -d 'client_secret=your-client-secret' \ -d 'refresh_token=your-refresh-token'
This will return a new access token and possibly a new refresh token. 6. Introspect Token (can be done through Oathkeeper): bash curl -X POST \ <http://your-hydra-admin-url:4445/oauth2/introspect> \ -H 'Content-Type: application/x-www-form-urlencoded' \ -d 'token=your-access-token'
Remember to replace your-hydra-public-url
, your-hydra-admin-url
, your-client-id
, your-client-secret
, your-authorization-code
, and your-refresh-token
with your actual values in all these requests. These configurations and requests will allow you to obtain and use refresh tokens with Ory Hydra and Oathkeeper. For more detailed information, you can refer to the [Ory OAuth2 Refresh Token Grant documentation](https://www.ory.sh/docs/oauth2-oidc/refresh-token-grant) and the [Ory Oathkeeper OAuth2 Introspection documentation](https://www.ory.sh/docs/oathkeeper/pipeline/authn#oauth2_introspection). [Ory OAuth2 Refresh Token Grant](https://www.ory.sh/docs/oauth2-oidc/refresh-token-grant) [Ory Oathkeeper OAuth2 Introspection](https://www.ory.sh/docs/oathkeeper/pipeline/authn#oauth2_introspection)clean-piano-39737
01/22/2025, 3:15 PMbrief-egg-61061
01/22/2025, 3:15 PMbrief-egg-61061
01/22/2025, 3:15 PMbash ory create oauth2-client --project "$PROJECT_ID" \ --name "Client Credentials Demo" \ --grant-type client_credentials
2. Once you've created the client, you'll receive a client ID and client secret. You can then use these to request an access token directly, without any user interaction: bash curl -X POST \ <https://your-project.projects.oryapis.com/oauth2/token> \ -H 'Content-Type: application/x-www-form-urlencoded' \ -d 'grant_type=client_credentials' \ -d 'client_id=your-client-id' \ -d 'client_secret=your-client-secret' \ -d 'scope=read'
Replace <http://your-project.projects.oryapis.com|your-project.projects.oryapis.com>
with your Ory Hydra instance URL, and use the actual client ID and secret you received when creating the client. This request will return an access token that you can use to access protected resources. Note that this flow does not provide refresh tokens, as it's designed for short-lived access tokens in machine-to-machine scenarios. You can then use this access token in your API requests or introspect it as needed: bash curl -X POST \ <http://your-hydra-admin-url:4445/oauth2/introspect> \ -H 'Content-Type: application/x-www-form-urlencoded' \ -d 'token=your-access-token'
This approach allows you to obtain OAuth2 tokens programmatically without any human interaction, which is ideal for automated processes or services. For more detailed information on the Client Credentials flow, you can refer to the [Ory OAuth2 Client Credentials Flow documentation](https://www.ory.sh/docs/oauth2-oidc/client-credentials). [Run your own OAuth2 Server](https://www.ory.sh/run-oauth2-server-open-source-api-security/#oauth2-client-credentials-grant--machine-to-machine) [Ory OAuth2 Client Credentials Flow](https://www.ory.sh/docs/oauth2-oidc/client-credentials)clean-piano-39737
01/22/2025, 3:19 PMbrief-egg-61061
01/22/2025, 3:19 PMbrief-egg-61061
01/22/2025, 3:19 PMbash ory create oauth2-client --project "$PROJECT_ID" \ --name "Client Credentials Demo" \ --grant-type client_credentials
2. Request an access token: bash curl -X POST \ <https://your-project.projects.oryapis.com/oauth2/token> \ -H 'Content-Type: application/x-www-form-urlencoded' \ -d 'grant_type=client_credentials' \ -d 'client_id=your-client-id' \ -d 'client_secret=your-client-secret' \ -d 'scope=read'
3. In your application, implement logic to request a new access token before the current one expires. You can do this by checking the expires_in
value in the token response and scheduling a new token request accordingly. This approach allows you to maintain continuous access without user interaction, effectively mimicking the behavior of refresh tokens in machine-to-machine scenarios. It's important to note that this method is not a standard OAuth2 flow and doesn't provide the same security benefits as using refresh tokens. Always ensure you're following best practices for securing your client credentials and access tokens. For more information on OAuth2 flows and token management in Ory Hydra, you can refer to the [Ory OAuth2 & OpenID Connect documentation](https://www.ory.sh/docs/oauth2-oidc/). [Ory OAuth2 & OpenID Connect](https://www.ory.sh/docs/oauth2-oidc/)