few-oyster-74399
07/31/2024, 12:36 PMchilly-king-10285
07/31/2024, 12:36 PMchilly-king-10285
07/31/2024, 12:36 PMjavascript import { Configuration, OAuth2Api } from "@ory/client" const ory = new OAuth2Api( new Configuration({ basePath: `https://${process.env.ORY_PROJECT_SLUG}.<http://projects.oryapis.com|projects.oryapis.com>`, accessToken: process.env.ORY_API_KEY, }), ) export async function introspectToken(accessToken: string) { const { data } = await ory.introspectOAuth2Token({ token: accessToken }) data.active // true or false }
In this code, introspectToken
is a function that takes an access token as a parameter and uses the introspectOAuth2Token
method from the Ory SDK to check the token's validity. The data.active
property in the response will be true
if the token is valid and false
otherwise. Please note that this method should be used responsibly and securely, considering the sensitivity of access tokens. Sources: - [OAuth 2.0 token introspection](https://www.ory.sh/docs/hydra/guides/oauth2-token-introspection) - [Revoking JSON Web Tokens](https://www.ory.sh/docs/oauth2-oidc/jwt-access-token#revoking-json-web-tokens)few-oyster-74399
07/31/2024, 12:38 PMchilly-king-10285
07/31/2024, 12:38 PMchilly-king-10285
07/31/2024, 12:38 PMjavascript app.get('/verify-token', async (req, res) => { const token = req.headers.authorization; const { data } = await ory.introspectOAuth2Token({ token: token }); res.send({ active: data.active }); });
In this example, a third-party application can make a GET request to the /verify-token
endpoint on your server with the access token in the Authorization
header. Your server then introspects the token and responds with the validity of the token. Remember, the actual implementation may vary based on your application's architecture and security requirements.few-oyster-74399
07/31/2024, 12:39 PMchilly-king-10285
07/31/2024, 12:39 PMchilly-king-10285
07/31/2024, 12:40 PM