<@U04UQ68083H> what is the difference between meta...
# ory-copilot
n
@chilly-king-10285 what is the difference between metadata_public and metadata_admin on identities
b
A message from kapa.ai
Based on the provided knowledge sources, I can explain the difference between
metadata_public
and
metadata_admin
for identities in Ory Kratos: 1. `metadata_public`: - Can only be modified using the
/admin/identities
APIs - Visible to anyone having access to the user's sessions (e.g., by calling
toSession()
or
/sessions/whoami
) - Allows access to the metadata in the frontend without calling the admin APIs 2. `metadata_admin`: - Can only be modified and read using the
/admin/identities
APIs - Not visible to the identity owner or in frontend sessions Both types of metadata are attributes defined by the system admin and cannot be updated or modified by the identity owner. They are used to store additional information about the identity, such as the original sign-up date for migrated identities. It's important to note that metadata is not validated using the identity's JSON schema. Administrators must ensure that the stored metadata is valid according to their schema and keep the schema up to date to accommodate changes in the data. To manage both types of metadata, you can use the following APIs: - POST /admin/identities - GET /admin/identities - GET /admin/identities/{id} - PUT /admin/identities/{id} An API Key is required to call these endpoints. [Source: Kratos Identity Metadata & Traits](https://www.ory.sh/docs/kratos/manage-identities/managing-users-identities-metadata)
n
@chilly-king-10285 how to update
metadata_public
value?
b
A message from kapa.ai
To update the
metadata_public
value for an identity in Ory, you need to use the admin API endpoints. Here's how you can do it: 1. Use the
PUT /admin/identities/{id}
endpoint to update an existing identity's metadata_public. 2. In the request body, include the
metadata_public
field with the updated values you want to set. Here's an example of how the request body might look:
Copy code
json {   "metadata_public": {     "key1": "new_value1",     "key2": "new_value2"   } }
It's important to note a few things: 1. This method replaces the entire
metadata_public
object. You can't update only a single value within
metadata_public
or
metadata_admin
. As stated in the [Ory documentation](https://www.ory.sh/docs/guides/integrate-with-ory-cloud-through-webhooks#update-identity-metadata): "This method replaces the entire metadata object. You can't update only a single value in
metadata_public
or
metadata_admin
." 2. You need an API Key to call this endpoint. Make sure you have the necessary authorization to perform this operation. 3. Remember that while
metadata_public
can be modified using the admin APIs, it will be visible to the identity owner when they access their session information. 4. The metadata is not validated against the identity's JSON schema, so ensure that the data you're storing is valid according to your application's requirements. [Ory API Reference](https://www.ory.sh/docs/reference/api) provides more details on the specific API endpoints and their usage.
n
@chilly-king-10285 can we get user id from ory cookie?
b
A message from kapa.ai
Yes, you can get the user ID from the Ory session cookie. The Ory session cookie contains information about the user's session, including their identity details. To retrieve the user ID, you need to use the
/sessions/whoami
endpoint. Here's how you can do it: 1. For browser-based applications: The Ory session cookie is automatically sent with the request when you call the
/sessions/whoami
endpoint. You can use the following cURL command:
Copy code
bash curl -H "Cookie: ory_kratos_session=..." \   "https://$<http://PROJECT_SLUG.projects.oryapis.com/sessions/whoami|PROJECT_SLUG.projects.oryapis.com/sessions/whoami>"
2. For server-side applications: You can forward the cookie received from the frontend to the Ory API:
Copy code
bash curl -H "Cookie: ory_kratos_session=..." \   "https://$<http://PROJECT_SLUG.projects.oryapis.com/sessions/whoami|PROJECT_SLUG.projects.oryapis.com/sessions/whoami>"
3. For client-side (SPA) applications: You can use the Ory SDK to make the request:
Copy code
javascript const ory = new FrontendApi(   new Configuration({     basePath: `{YOUR-CUSTOM-DOMAIN-OR-ORY-TUNNEL}`,     baseOptions: {       withCredentials: true,     },   }), )  const session = await ory.toSession()
The response from the
/sessions/whoami
endpoint will include the session information, including the user's identity. The user ID can be found in the
identity.id
field of the response. Here's an example of what the response might look like:
Copy code
json {   "id": "ede90ce6-2420-435a-a745-3d8ab1a9636c",   "active": true,   "expires_at": "2020-09-04T21:32:47.5642404Z",   "authenticated_at": "2020-09-03T21:32:47.5881038Z",   "issued_at": "2020-09-03T21:32:47.5642688Z",   "identity": {     "id": "d96e86d9-bc33-4aa5-b865-4ade8a3974b3",     "schema_id": "default",     "schema_url": "<https://playground.projects.oryapis.com/schemas/default>",     "traits": {       "email": "<mailto:user@example.com|user@example.com>",       "name": {         "first": "foo",         "last": "user"       }     },     // ... other identity details ...   } }
In this example, the user ID is
d96e86d9-bc33-4aa5-b865-4ade8a3974b3
. Remember to handle the case where the session might be invalid or expired, which would result in a 401 Unauthorized response. [Ory Session Check Documentation](https://www.ory.sh/docs/identities/sign-in/check-session-token-cookie-api) [Ory Session Response Format](https://www.ory.sh/docs/identities/sign-in/check-session-token-cookie-api#session-response)
n
@chilly-king-10285 I mean I received ory cookie, can I deconstruct it so I can get user id?
b
A message from kapa.ai
I apologize for the misunderstanding in my previous response. Let me clarify the situation regarding the Ory session cookie. The Ory session cookie, typically named
ory_kratos_session
, is an encrypted and signed cookie that cannot be directly deconstructed to obtain the user ID or any other information. This is a security measure to prevent tampering and unauthorized access to session data. As stated in the [Ory Session Management Overview](https://www.ory.sh/docs/kratos/session-management/overview): "When a user authenticates, for example by signing in with their username and password, they receive a session. The session is proof that the user is authenticated and allows to interact with the system without the need to re-authenticate for every request." The session cookie is designed to be used securely with Ory's APIs, not to be decoded client-side. To get the user ID or any other session information, you must send the cookie to the Ory backend and use the
/sessions/whoami
endpoint as described in my previous answer. This approach ensures that: 1. The session data remains encrypted and secure. 2. Only the Ory backend can decrypt and validate the session. 3. You always get the most up-to-date session information. So, to answer your question directly: No, you cannot deconstruct the Ory cookie to get the user ID. You must use the proper API endpoints to retrieve this information securely.