Check Who the Current HTTP Session Belongs To
Uses the HTTP Headers in the GET request to determine (e.g. by using checking the cookies) who is authenticated.
GET /sessions/whoami
Uses the HTTP Headers in the GET request to determine (e.g. by using checking the cookies) who is authenticated. Returns a session object in the body or 401 if the credentials are invalid or no credentials were sent. When the request it successful it adds the user ID to the 'X-Kratos-Authenticated-Identity-Id' header in the response.
If you call this endpoint from a server-side application, you must forward the HTTP Cookie Header to this endpoint:
pseudo-code example
router.get('/protected-endpoint', async function (req, res) {
const session = await client.toSession(undefined, req.header('cookie'))
console.log(session)
})When calling this endpoint from a non-browser application (e.g. mobile app) you must include the session token:
pseudo-code example
...
const session = await client.toSession("the-session-token")
console.log(session)When using a token template, the token is included in the tokenized field of the session.
pseudo-code example
...
const session = await client.toSession("the-session-token", { tokenize_as: "example-jwt-template" })
console.log(session.tokenized) // The JWTDepending on your configuration this endpoint might return a 403 status code if the session has a lower Authenticator Assurance Level (AAL) than is possible for the identity. This can happen if the identity has password + webauthn credentials (which would result in AAL2) but the session has only AAL1. If this error occurs, ask the user to sign in with the second factor or change the configuration.
This endpoint is useful for:
AJAX calls. Remember to send credentials and set up CORS correctly!
Reverse proxies and API Gateways
Server-side calls - use the X-Session-Token header!
This endpoint authenticates users by checking:
if the Cookie HTTP header was set containing an Ory Kratos Session Cookie;
if the Authorization: bearer <ory-session-token> HTTP header was set with a valid Ory Kratos Session Token;
if the X-Session-Token HTTP header was set with a valid Ory Kratos Session Token.
If none of these headers are set or the cookie or token are invalid, the endpoint returns a HTTP 401 status code.
As explained above, this request may fail due to several reasons. The error.id can be one of:
session_inactive: No active session was found in the request (e.g. no Ory Session Cookie / Ory Session Token).
session_aal2_required: An active session was found but it does not fulfil the Authenticator Assurance Level, implying that the session must (e.g.) authenticate the second factor.
Operation ID: toSession Tag: frontend
Query parameters
| Name | Type | Required | Description |
|---|---|---|---|
tokenize_as | string | no | Returns the session additionally as a token (such as a JWT) The value of this parameter has to be a valid, configured Ory Session token template. For more information head over to the documentation. |
Header parameters
| Name | Type | Required | Description |
|---|---|---|---|
X-Session-Token | string | no | Set the Session Token when calling from non-browser clients. A session token has a format of MP2YWEMeM8MxjkGKpH4dqOQ4Q4DlSPaj. |
Cookie | string | no | Set the Cookie Header. This is especially useful when calling this endpoint from a server-side application. In that scenario you must include the HTTP Cookie Header which originally was included in the request to your server. An example of a session in the HTTP Cookie Header is: ory_kratos_session=a19iOVAbdzdgl70Rq1QZmrKmcjDtdsviCTZx7m9a9yHIUS8Wa9T7hvqyGTsLHi6Qifn2WUfpAKx9DWp0SJGleIn9vh2YF4A16id93kXFTgIgmwIOvbVAScyrx7yVl6bPZnCx27ec4WQDtaTewC1CpgudeDV2jQQnSaCP6ny3xa8qLH-QUgYqdQuoA_LF1phxgRCUfIrCLQOkolX5nv3ze_f==. It is ok if more than one cookie are included here as all other cookies will be ignored. |
Responses
| Status | Description | Body |
|---|---|---|
| 200 | session | application/json, object |
| 401 | errorGeneric | application/json, object |
| 403 | errorGeneric | application/json, object |
| default | errorGeneric | application/json, object |
Generated from /tmp/kratos-api.json at build time.