Ory Kratos

repository·master·Indexed 11 days ago

https://github.com/ory/kratos

An API-first identity and user management system that centralizes authentication and user lifecycle workflows, including registration, login, and MFA. The system provides a Go API client for managing identities, sessions, and flows (login, registration, recovery, settings, verification), and supports SQL migrations for various database dialects including CockroachDB, MySQL, Postgres, and SQLite.

Tokens
66.3K
Snippets
247
Records
291
Agent score
96%

What's inside Ory Kratos

  1. What is Ory Kratos?

    master

    Ory Kratos is an API-first identity and user management system designed for cloud-native applications. It centralizes core identity workflows, allowing your services to consume them via HTTP APIs instead of implementing them manually.

    Key workflows managed by Kratos include:

    • Self-service login and registration
    • Account verification and recovery
    • Multi-factor authentication (MFA)
    • Profile and account management
    • Identity schemas and traits
    • Admin APIs for identity lifecycle management
  2. Explore Ory Kratos Go Client Models

    master

    The client-go package provides comprehensive Go representations of all Ory Kratos API models. These include identities, sessions, flows (login, registration, recovery, settings, verification), and various authentication credential types (OIDC, Password, WebAuthn, TOTP, etc.).

    Key model categories include:

    • Identity Models: Identity, IdentityPatch, CreateIdentityBody.
    • Flow Models: LoginFlow, RegistrationFlow, RecoveryFlow, SettingsFlow, VerificationFlow and their respective states and update methods.
    • Session Models: Session, ManageSessionsBody.
    • Error Models: GenericError, FlowError, ErrorBrowserLocationChangeRequired.
  3. Understand the structure of JSON Schema path results

    master

    When calling jsonschemax.ListPaths() or ListPathsWithArraysIncluded(), the returned list contains objects representing the metadata for each path.

    Each result object includes fields such as:

    • Name: The dot-delimited path string (e.g., providers.#.id).
    • Type: The type information.
    • TypeHint: A numeric hint for the type.
    • Required: A boolean indicating if the property is required.
    • Format, Pattern, Enum, Minimum, Maximum, etc.: Validation constraints defined in the schema.

    Example path output for a schema with an array of objects:

    [
      {
        "Name": "providers",
        ...
      },
      {
        "Name": "providers.#",
        ...
      },
      {
        "Name": "providers.#.id",
        ...
      }
    ]
  4. Generate even distributions with randx.RuneSequence

    master
    Use randx.RuneSequence to generate sequences of characters that are evenly distributed across a provided character set for a specified length. This ensures that every character in the set has an equal probability of appearing in the resulting sequence.
  5. Migrating from Auth0 or Okta to Ory

    master

    When migrating from identity providers like Auth0 or Okta that use OAuth2/OpenID Connect (OIDC), the recommended architecture is to use Ory Hydra and Ory Kratos together:

    1. Ory Hydra: Acts as the OAuth2 and OpenID Connect provider, replacing the authorization server and token issuing capabilities of your existing IdP.
    2. Ory Kratos: Handles the identity, credentials, and user-facing flows (login, registration, recovery, verification, and profile management).

    This combination serves as a protocol-level replacement. You will need to update client configurations to point to Hydra and migrate your existing identities into Kratos.

  6. Quickstart: Install Ory CLI and create a project

    master

    To try Ory Identities, follow these steps to install the Ory CLI, authenticate, and create a new project.

    1. Install the Ory CLI: Use the following command to download and install the CLI to your local machine.
    2. Authenticate: Run ory auth to sign in or sign up for an Ory account.
    3. Create a Project: Use ory create project to initialize a new project within a workspace. The --use-project flag allows you to immediately start using the newly created project.
    4. Open Project: Use ory open to access your project in the browser.
    # Install the Ory CLI if you do not have it yet:
    bash <(curl https://raw.githubusercontent.com/ory/meta/master/install.sh) -b . ory
    sudo mv ./ory /usr/local/bin/
    
    # Sign in or sign up
    ory auth
    
    # Create a new project
    ory create project --create-workspace "Ory Open Source" --name "GitHub Quickstart"  --use-project
    ory open ax login
  7. Generate SQL migrations using Ory CLI and Soda

    master

    You can use the ory dev command and soda to generate migrations for multiple dialects. This method targets each database individually. After generation, you may need to remove SQLite-specific parts from the files to ensure they remain compatible across all supported databases.

    To generate migrations, set the dialect and name environment variables and run the following commands:

    $ dialect=mysql  # or postgres|cockroach|sqlite
    $ name=my_migration_name
    $ ory dev pop migration create -d=$dialect ./persistence/sql/migrations/templates $name
    $ soda generate sql -e mysql -c ./persistence/sql/.soda.yml -p ./persistence/sql/migrations/templates [name]
    $ soda generate sql -e sqlite -c ./persistence/sql/.soda.yml -p ./persistence/sql/migrations/templates [name]
    $ soda generate sql -e postgres -c ./persistence/sql/.soda.yml -p ./persistence/sql/migrations/templates [name]
    $ soda generate sql -e cockroach -c ./persistence/sql/.soda.yml -p ./persistence/sql/migrations/templates [name]
  8. Authenticate with the Ory Kratos Go Client

    master

    To authenticate requests using an Ory Access Token, you must include the token in the Authorization HTTP header. In the Go client, this is achieved by adding a map of API keys to the request context using the client.ContextAPIKeys key. The key in the map must be exactly oryAccessToken.

    Authentication Scheme Details:

    • Type: API key
    • Parameter Name: Authorization
    • Location: HTTP header
    auth := context.WithValue(
    	context.Background(),
    	client.ContextAPIKeys,
    	map[string]client.APIKey{
    		"oryAccessToken": {Key: "API_KEY_STRING"},
    	},
    )
    r, err := client.Service.Operation(auth, args)
  9. Preview API and Swagger documentation

    master

    To work on and preview the generated API documentation, follow these steps:

    1. Update the SDK and the OpenAPI specification:
    make sdk
    1. Start the preview server for API documentation:
    make docs/api
    1. Or, start the preview server for Swagger documentation:
    make docs/swagger
    make sdk
    make docs/api
    make docs/swagger
  10. Run end-to-end tests with Cypress

    master

    End-to-end (e2e) tests are implemented using Cypress.

    Prerequisites for ARM Macs

    If you are on an ARM-based Mac, you may need to install Rosetta 2 to run Cypress:

    softwareupdate --install-rosetta --agree-to-license

    Running tests

    To run e2e tests in development mode using SQLite:

    ./test/e2e/run.sh --dev sqlite

    To run all e2e tests using full databases:

    make test-e2e

    Filtering tests

    • Single test: Add .only to the test definition in the source (e.g., it.only(...)).
    • Subset of tests: Edit test/e2e/cypress.json and add the testFiles option with a glob pattern, for example: "testFiles": ["profiles/network/*"].