id | title |
---|---|
quickstart |
Quickstart |
import Mermaid from '@theme/Mermaid' import CodeFromRemote from '@theme/CodeFromRemote'
ORY Kratos has several moving parts and getting everything right from the beginning can be challenging. This getting started guide will help you install ORY Kratos and some additional dependencies so that you can see how it works.
Please be aware that this guide is not a replacement for studying the docs. You must understand core concepts and APIs to use ORY Kratos productively. This is merely a guide to get you set up with some examples.
<iframe width="560" height="315" src="https://www.youtube.com/embed/5t1Zr_zJc7E" frameborder="0" allowfullscreen />This section gives you some context on what we want to achieve and what tools we need for that. You will also learn about the network set up we picked for this guide.
This quickstart guide operates on the assumption that we are writing a NodeJS app called SecureApp. This app is using nothing fancy - some ExpressJS and a bit of HTML Templating using Handlebars. We do want to use TypeScript, but only because it's more readable - not because we're doing anything out of the ordinary!
You could pick any technology here, of course. This works with Swift, ReactJS, or Angular (client side) as well as with PHP, Ruby, Python, Java (server side) - you name it! We picked NodeJS + TypeScript because we believe it is the easiest to understand, and because JavaScript and NodeJS are universally understood and easy to install.
We know that SecureApp will need to have some type of dashboard and that it needs users. Therefore, we need:
- Login
- Logout
- Registration
- Profile management ("update first name", "update avatar", etc.)
- Credentials Management ("add a new recovery email", "change password", etc.)
- Account Recovery ("password reset")
- Email Verification
- Two Factor Authentication
- "Sign in with Google" and "Sign in with GitHub"
and of course:
- A dashboard that shows
Hello {{ traits.name.first }} {{ traits.name.last }}}!
which is only visible when the user is signed in.
As you might already know, ORY Kratos is API-only; it does not have a UI or HTML templating engine. We will implement all the user-facing UIs like "dashboard", "login", and "registration" in our NodeJS SecureApp!
To ensure that no one can access the dashboard without prior authentication (login), we can use a small piece of code (here ExpressJS) to do that:
:::info
ORY Kratos is not just an API: it uses cookies, HTTP redirects, anti-CSRF tokens and more so you don't have to.
:::
The SecureApp and ORY Kratos need to share cookies in order for anti-CSRF tokens
and login sessions to work. Because the quickstart runs on different ports on
127.0.0.1
there is nothing we need to do to get this all working. In
environments where you have multiple sub-domains or reverse proxies, the set up
will be a bit more sophisticated. You can find more information about the
different set up possibilities in the
Getting Cookies to Work on Multi-Domains Guide.
ORY Kratos does not ship with an administrative user interface. You must implement that yourself or choose the ORY Cloud offering (to be announced). In this quickstart we will use ORY Kratos' CLI to interact with ORY Kratos' APIs.
The quickstart also comes with MailSlurper, a mock SMTP server the demo uses to show how email verification works.
To get this example working, you will need Git, Docker, and Docker Compose installed on your system. No other dependencies are required. Before you start, make sure that Docker has enough disk space.
:::tip
This tutorial uses Docker-Compose volumes which have reported to run out of disk
space. Check the remaining disk space using docker system df
. If the volumes
are above the 85% threshold,
prune old Docker objects before you
start!
:::
:::tip
If you encounter build errors (e.g. network timeout), make sure that the network
is running correctly and run make docker
again. If the problem persists, feel
free to open an issue.
:::
Let's clone ORY Kratos and run docker-compose
:
git clone https://github.com/ory/kratos.git
cd kratos
git checkout v0.4.6-alpha.1
docker pull oryd/kratos:latest-sqlite
docker pull oryd/kratos-selfservice-ui-node:latest
docker-compose -f quickstart.yml -f quickstart-standalone.yml up --build --force-recreate
This might take a minute or two. Once the output slows down and logs indicate a healthy system you're ready to roll! A healthy system will show something along the lines of (the order of messages might be reversed):
kratos_1 | time="2020-01-20T14:52:13Z" level=info msg="Starting the admin httpd on: 0.0.0.0:4434"
kratos_1 | time="2020-01-20T14:52:13Z" level=info msg="Starting the public httpd on: 0.0.0.0:4433"
:::note
There are two important factors to get a fully functional system:
- You need to make sure that ports
4455
,4433
,4434
, and4436
are free. - Make sure to always use
127.0.0.1
as the hostname; never uselocalhost
! This is important because browsers treat these two as separate domains and will therefore have issues with setting and using cookies correctly.
:::
You might notice that no database is being used in this example. ORY Kratos supports SQLite, PostgreSQL, MySQL, and CockroachDB as database backends. For the quickstart, we're mounting a persistent volume to store the SQLite database in.
Future guides will explain how to set up a production system.
This demo makes use of several services:
- ORY Kratos
- Public ("Browser") API (port 4433)
- Admin API (port 4434) - This is only made public so we can test via the CLI.
- SecureApp
- Public (port 4455) - an example application written in NodeJS that implements the login, registration, logout, dashboard, and other UIs.
- MailSlurper
- Public (port 4436) - a development SMTP server which ORY Kratos will use to send emails.
To better understand the application architecture, let's take a look at the network configuration. This assumes that you have at least some understanding of how Docker networks work:
<Mermaid
chart={graph TD subgraph hn[Host Network] B[Browser] B-->|Can access URLs via 127.0.0.1:4455|OKPHN B-->|Can access URLs via 127.0.0.1:4433|PAPI B-->|Can access UI via 127.0.0.1:4436|SMTPUI OKPHN([SecureApp exposed at :4455]) SMTPUI([MailSlurper UI exposed at :4436]) PAPI([ORY Kratos Public API exposed at :4433]) end subgraph dn["Internal Docker Network (intranet)"] OKPHN-.->SA SMTPUI-.->SMTP PAPI-.->OK SA-->|Talks to and validates login sessions using|OK OK-->|Sends mail via|SMTP OK[ORY Kratos] SA["SecureApp (ORY Kratos SelfService UI Node Example)"] SMTP["SMTP Server (MailSlurper)"] end
}
/>
Enough theory, it's time to get this thing going! Let's start by trying to open the dashboard - go to 127.0.0.1:4455/dashboard.
You should notice that you're ending up at the login endpoint instead of the dashboard:
Looking at the network stack, you can see two redirects happening:
Here's a play-by-play of what happened:
- SecureApp used the ORY Kratos JavaScript language client to guard the
/dashboard
route. The ORY Kratos client checked the cookies from the request and saw you were not logged in. - The route guard redirected you from
/dashboard
to/auth/login
. ORY Kratos' browser API requires a<flow_id>
in order to log you in. It looked for this ID in the URL as a query parameter but couldn't find it. /auth/login
redirected you tohttp://127.0.0.1:4433/self-service/login/browser
, which is one of ORY Kratos' APIs used for logging in browser-based applications.- ORY Kratos performed some security checks, prepared form data, created a
csrf_token
, and redirected the browser to/auth/login?flow=<flow_id>
. - SecureApp handled the
/auth/login
route, found the<flow_id>
in the URL query parameter, and used it to make an HTTP request tohttp://kratos:4434/self-service/login/flows?id=<flow_id>
. Notice the URI iskratos:4434
because SecureApp is making a server-side HTTP request via Docker's private network to ORY Kratos' Admin API. - ORY Kratos responded with data which SecureApp used to render the HTML login form.
:::note
Your rendered login form should be a standard HTML <form>
. AJAX requests will
not work!
:::
We can use curl
to see what ORY Kratos responds with. Try visiting
http://127.0.0.1:4455/auth/login
, copying the generated <flow_id>
, and
making the request yourself:
# curl -s "http://127.0.0.1:4434/self-service/login/flows?id=ee6e1565-d3c3-4f3a-a6ff-0ba6b3a6481b" \
$ curl -s "http://127.0.0.1:4434/self-service/login/flows?id=<flow_id>" \
| jq
{
"id": "ee6e1565-d3c3-4f3a-a6ff-0ba6b3a6481b",
"type": "browser",
"expires_at": "2020-09-13T10:49:54.8295242Z",
"issued_at": "2020-09-13T10:39:54.8295242Z",
"request_url": "http://127.0.0.1:4433/self-service/login/browser",
"methods": {
"password": {
"method": "password",
"config": {
"action": "http://127.0.0.1:4433/self-service/login/methods/password?flow=ee6e1565-d3c3-4f3a-a6ff-0ba6b3a6481b",
"method": "POST",
"fields": [
{
"name": "identifier",
"type": "text",
"required": true,
"value": ""
},
{
"name": "password",
"type": "password",
"required": true
},
{
"name": "csrf_token",
"type": "hidden",
"required": true,
"value": "lNrB8sW2fZY6xnnA91V7ISYrUVcJbmRCOoGHjsnsfI7MsIL5RTbuWFm5TRv1azQW+7IRCfnt2Ch6pC42/45sJQ=="
}
]
}
}
},
"forced": false
}
This flow also works with Single Page Apps (SPA) and frameworks like Angular or ReactJS. For more details about the specific flows (login, registration, logout, etc.), head over to the concept chapter.
Let's move on to the next flow - registration! Click on "Register new account", which initiates a flow similar to the one we just used:
The network trace should look familiar by now:
If we try to sign up using a password like 123456
, Krato's password policy
will complain:
The error message is coming directly from ORY Kratos' Admin API:
# curl -s "http://127.0.0.1:4433/self-service/registration/flows?id=2b1f8c5d-e830-4068-97b8-35f776df9217" \
$ curl -s "http://127.0.0.1:4433/self-service/registration/flows?id=<flow_id>" \
| jq
{
"id": "2b1f8c5d-e830-4068-97b8-35f776df9217",
"type": "browser",
"expires_at": "2020-09-13T10:53:15.1774019Z",
"issued_at": "2020-09-13T10:43:15.1774019Z",
"request_url": "http://127.0.0.1:4433/self-service/registration/browser",
"active": "password",
"messages": null,
"methods": {
"password": {
"method": "password",
"config": {
"action": "http://127.0.0.1:4433/self-service/registration/methods/password?flow=2b1f8c5d-e830-4068-97b8-35f776df9217",
"method": "POST",
"fields": [
{
"name": "csrf_token",
"type": "hidden",
"required": true,
"value": "1IlHWNjkAZxuYhO82WPgNTgujKsUSaW87j6og/20i2uM4wRTWGSSUg0dJ2fbXa8C5bfM9eTKGdauGwE7y9abwA=="
},
{
"name": "password",
"type": "password",
"required": true,
"messages": [
{
"id": 4000005,
"text": "The password can not be used because the password has been found in at least 23597311 data breaches and must no longer be used..",
"type": "error",
"context": {
"reason": "the password has been found in at least 23597311 data breaches and must no longer be used."
}
}
]
},
{
"name": "traits.email",
"type": "text",
"value": "foo@ory.sh"
},
{
"name": "traits.name.first",
"type": "text",
"value": "Ory"
},
{
"name": "traits.name.last",
"type": "text",
"value": "Corp"
}
]
}
}
}
}
Setting a password that doesn't violate these policies, we will be immediately redirected to the dashboard:
By clicking the "logout" icon in the top right, you will be redirected to the login screen again where you will be able to use your credentials to log back in again. Exciting!
Head over to the Self-Service Flows Chapter for an in-depth explanation of how each individual flow works.
As you've signed up, an email was sent to the email address you used. Because the quickstart uses a fake SMTP server, the email did not arrive in your inbox. You can retrieve the email however by opening the MailSlurper UI at 127.0.0.1:4436.
You should see something like this:
If not, hard refresh the tab or click on the home icon in the menu bar.
Next, click the verification link. You will end up at the dashboard, with a
verified email address (check the verified
and verified_at
field in the JSON
response):
To re-request the verification email, fill out the form at 127.0.0.1:4455/verify.
To learn more about verification recovery, head over to the Email and Phone Verification and Account Activation Documentation.
The quickstart has account recovery enabled. To recover an account, log out and click on "Recover account":
The next screen shows a HTML form where you enter your email address:
Hit "submit" and check the emails for the account recovery message:
Click the link, and change your password:
You are now able to sign in with the new password. To learn more about account recovery, head over to the Account Recovery Documentation.
You can find all configuration files used for this quickstart guide in
./contrib/quickstart/kratos
,
./quickstart.yml
,
and
./quickstart-standalone.yml
.
To understand what each of those configuration files does, consult the other
chapters of this documentation.
:::note
To get a minimal version of ORY Kratos running, you need to set configuration
values for
identity.default_schema_url
and
DSN
.
You should also configure
selfservice.flows.*.ui_url
or else Kratos will use fallback URLs.
:::
In the future, this guide will support more use cases such as:
- GitHub to login and registration
To clean everything up, you need to bring down the Docker Compose environment and remove all mounted volumes.
docker-compose -f quickstart.yml down -v
docker-compose -f quickstart.yml rm -fsv
Here is some information if you want to modify the quickstart to test ORY Kratos in with different settings.
Using different databases
If you want to run the quickstart with PostgreSQL, run the following
docker-compose:
docker-compose -f quickstart.yml -f quickstart-standalone.yml -f quickstart-postgres.yml up --build --force-recreate
If you want to run the quickstart with CockroachDB, run the following
docker-compose:
docker-compose -f quickstart.yml -f quickstart-standalone.yml -f quickstart-crdb.yml up --build --force-recreate
If you want to run the quickstart with MySQL, run the following docker-compose:
docker-compose -f quickstart.yml -f quickstart-standalone.yml -f quickstart-mysql.yml up --build --force-recreate
Changing ports
If you want to change ports for the SelfService-UI, you need to change them in
quickstart.yml
as well as in the
contrib/quickstart/kratos/email-password/.kratos.yml
accordingly.
Note that you also need to change the ports for flows (error, settings, recovery, verification, logout, login, registration).
The same procedure applies if you want to change ports for the Public UI/Admin UI or the Mailslurper.
Hooks
If you want to change the redirects happening after registration,login or a settings change, take a look at this document: Hooks.
If you delete the session
hook from kratos.yml
, the user will not be
immediately signed in after registration.
OIDC
If you want to test ORY Kratos integration with different OIDC providers, you will find more information in this document: OIDC