Skip to content

Integrating Hydra and Kratos #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 46 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
edd2a47
Initial steps bringing hydra calls into the Kratos example ui
Jun 20, 2020
dd37af9
refactor: 127.0.01 instead of localhost,
k9ert Jun 29, 2020
9cd346d
refactor: undone package-naming
k9ert Jul 1, 2020
eae1b7d
refactor: renaming hydraauth to hydra
k9ert Jul 1, 2020
91e5436
refactor: removed home-route
k9ert Jul 1, 2020
c51b516
refactor:fix logging and other minor stuff
Jul 1, 2020
4596b92
refactor: migrate consent to hydra-sdk and tidy up
Jul 1, 2020
5a23b10
refactor: remove quickstart, tidy up
Jul 1, 2020
4e15ca4
refactor: remove pure-js-dependencies
Jul 1, 2020
10c2d60
feat: add csrf-protection for consent-endpoint
Jul 2, 2020
634ec3c
fix: gitlab login works only is subject:email
Jul 3, 2020
6abb329
feat: setup winston logging
Jul 7, 2020
af0488b
Merge branch 'master' into master
aeneasr Jul 7, 2020
db986b6
fixes gitlab-email issue
Jul 10, 2020
067cc28
forgot to add winston lib
Jul 10, 2020
1a6bbb7
Merge branch 'master' of github.com:k9ert/kratos-selfservice-ui-node
Jul 10, 2020
85e9b4d
Merge branch 'master' into master
k9ert Jul 10, 2020
52f6e05
Typo: Update package.json
k9ert Jul 10, 2020
8b60ab8
Update src/config.ts: Remove comment
k9ert Jul 10, 2020
0797dcb
Update src/index.ts: rename
k9ert Jul 10, 2020
c27d922
Update src/index.ts
k9ert Jul 10, 2020
478e35d
Update src/index.ts
k9ert Jul 10, 2020
e4bef33
Update src/index.ts
k9ert Jul 10, 2020
0f0581f
npm run format
Jul 10, 2020
9056a21
Update src/routes/auth.ts
k9ert Jul 10, 2020
987f750
Update views/registration.hbs
k9ert Jul 10, 2020
1b96209
refactoring authInfo
Jul 13, 2020
9db1e4b
Merge branch 'master' of github.com:k9ert/kratos-selfservice-ui-node
Jul 13, 2020
864404d
minor improvements and best practices
Jul 13, 2020
1efb08c
minor improvements and best practices
Jul 13, 2020
e3aff58
Update views/login.hbs: empty line
k9ert Jul 13, 2020
be226e2
Update src/routes/consent.ts
k9ert Jul 13, 2020
3c4fda7
Update src/routes/consent.ts
k9ert Jul 13, 2020
3bab29b
tidy-up and beautifying
Jul 13, 2020
1c9119a
refactor: clean up code base and resolve issues
aeneasr Jul 14, 2020
ce1b787
Merge pull request #1 from ory/k9ert
k9ert Jul 17, 2020
2331d92
proxy-fix and consent-endpointadjustment
Jul 17, 2020
026e2b7
Merge remote-tracking branch 'origin/master' into k9ert
aeneasr Jul 21, 2020
ec2b74f
u
aeneasr Jul 21, 2020
3c21e68
u
aeneasr Jul 21, 2020
daf8a7e
u
aeneasr Jul 21, 2020
662a5ae
Merge remote-tracking branch 'origin/master' into k9ert
aeneasr Jul 21, 2020
750f9f6
u
aeneasr Jul 21, 2020
cd8284b
Merge remote-tracking branch 'origin/master' into k9ert
aeneasr Jul 21, 2020
d06b655
u
aeneasr Jul 21, 2020
80915e2
u
aeneasr Jul 21, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
node_modules/
lib
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,21 @@ This application can be configured using two environment variables:
- `BASE_URL` (optional): The base url of this app. If served e.g. behind a proxy or via
GitHub pages this would be the path, e.g. `https://mywebsite.com/kratos-selfservice-ui-node/`.
**Must be absolute!**
- `COOKIE_SECRET` (optional): This secret is used to encrypt cookies which are used as part of the ORY Hydra
integration.
- `TLS_CERT_PATH` (optional): Path to certificate file. Should be set up together with `TLS_KEY_PATH` to enable HTTPS.
- `TLS_KEY_PATH` (optional): Path to key file Should be set up together with `TLS_CERT_PATH` to enable HTTPS.

If you want to also use hydra and connect an app via OAuth2, set these env-variables:
- `HYDRA_ADMIN_URL` should point to hydra's admin port including scheme (e.g. https://hydra.example.com:445)

If you want to test hydra without the use of kratos for user-management, rather have a look at the [hydra-login-consent-node][https://github.com/ory/hydra-login-consent-node].

### Network Setup

This application works in two set ups:

- Standalone with ORY Kratos
- Standalone with ORY Kratos (plus optionally ORY Hydra)
- With the ORY Oathkeeper Reverse Proxy

#### Standalone using cookies
Expand Down
35 changes: 35 additions & 0 deletions contrib/hydra/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# ORY Kratos as Login Provider for ORY Hydra

**Warning: ** this is a preliminary example and will properly be implemented in ORY Kratos directly.

For now, to run this example execute:

```shell script
$ docker-compose up --build
```

Next, create an OAuth2 Client

```shell script
$ docker-compose exec hydra \
hydra clients create \
--endpoint http://127.0.0.1:4445 \
--id auth-code-client \
--secret secret \
--grant-types authorization_code,refresh_token \
--response-types code,id_token \
--scope openid,offline \
--callbacks http://127.0.0.1:5555/callback
```

and perform an OAuth2 Authorize Code Flow

```shell script
$ docker-compose exec hydra \
hydra token user \
--client-id auth-code-client \
--client-secret secret \
--endpoint http://127.0.0.1:4444/ \
--port 5555 \
--scope openid,offline
```
130 changes: 130 additions & 0 deletions contrib/hydra/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# This docker-compose file sets up ORY Kratos, ORY Hydra, and this app in a network and configures
# in such a way that ORY Kratos is the Login Provider for ORY Hydra.

version: '3.7'

services:
postgresd:
image: postgres:9.6
ports:
- "5432:5432"
environment:
- POSTGRES_USER=pguser
- POSTGRES_PASSWORD=secret
- POSTGRES_MULTIPLE_DATABASES=kratos,hydra
volumes:
- ./pg-init:/docker-entrypoint-initdb.d
networks:
- intranet

hydra-migrate:
image: oryd/hydra:v1.6.0-alpine
depends_on:
- postgresd
environment:
- DSN=postgres://pguser:secret@postgresd:5432/hydra?sslmode=disable
command:
migrate sql -e --yes
restart: on-failure
networks:
- intranet

hydra:
image: oryd/hydra:v1.6.0-alpine
depends_on:
- hydra-migrate
ports:
- "4444:4444" # Public port
- "4445:4445" # Admin port
- "5555:5555" # Port for hydra token user
command:
serve all --dangerous-force-http
restart: on-failure # TODO figure out why we need this (incorporate health check into hydra migrate command?)
environment:
- LOG_LEAK_SENSITIVE_VALUES=true
- URLS_SELF_ISSUER=http://127.0.0.1:4444
- URLS_SELF_PUBLIC=http://127.0.0.1:4444
- URLS_CONSENT=http://127.0.0.1:3000/auth/hydra/consent
- URLS_LOGIN=http://127.0.0.1:3000/auth/hydra/login
- URLS_LOGOUT=http://127.0.0.1:3000/logout
- SECRETS_SYSTEM=youReallyNeedToChangeThis
- OIDC_SUBJECT_IDENTIFIERS_SUPPORTED_TYPES=public,pairwise
- OIDC_SUBJECT_IDENTIFIERS_PAIRWISE_SALT=youReallyNeedToChangeThis
- DSN=postgres://pguser:secret@postgresd:5432/hydra?sslmode=disable
networks:
- intranet

kratos-selfservice-ui-node:
build:
context: ../..
dockerfile: Dockerfile
environment:
- HYDRA_ADMIN_URL=http://hydra:4445
- KRATOS_PUBLIC_URL=http://kratos:4433/
- KRATOS_ADMIN_URL=http://kratos:4434/
- SECURITY_MODE=standalone
ports:
- "3000:3000"
networks:
- intranet

kratos-migrate:
image: oryd/kratos:v0.4.6-sqlite
environment:
- DSN=sqlite:///var/lib/sqlite/db.sqlite?_fk=true&mode=rwc
volumes:
-
type: volume
source: kratos-sqlite
target: /var/lib/sqlite
read_only: false
-
type: bind
source: ./kratos
target: /etc/config/kratos
command:
-c /etc/config/kratos/.kratos.yml migrate sql -e --yes
restart: on-failure
networks:
- intranet

kratos:
depends_on:
- kratos-migrate
image: oryd/kratos:v0.4.6-sqlite
ports:
- "4433:4433" # public
- "4434:4434" # admin
restart: unless-stopped
environment:
- DSN=sqlite:///var/lib/sqlite/db.sqlite?_fk=true
command:
serve -c /etc/config/kratos/.kratos.yml --dev
volumes:
-
type: volume
source: kratos-sqlite
target: /var/lib/sqlite
read_only: false
-
type: bind
source: ./kratos
target: /etc/config/kratos
networks:
- intranet

# Sending emails is not part of this demo, so this is commented out:
#
# mailslurper:
# image: oryd/mailslurper:latest-smtps
# ports:
# - "4436:4436"
# - "4437:4437"
# networks:
# - intranet

networks:
intranet:

volumes:
kratos-sqlite:
60 changes: 60 additions & 0 deletions contrib/hydra/kratos/.kratos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
serve:
public:
base_url: http://127.0.0.1:3000/.ory/kratos/public/
admin:
base_url: http://kratos:4434/

selfservice:
default_browser_return_url: http://127.0.0.1:3000/
whitelisted_return_urls:
- http://127.0.0.1:3000/
- http://127.0.0.1:3000/auth/hydra/login

strategies:
password:
enabled: true

flows:
settings:
ui_url: http://127.0.0.1:3000/settings

verification:
ui_url: http://127.0.0.1:3000/verification
enabled: false

recovery:
ui_url: http://127.0.0.1:3000/recovery
enabled: false

logout:
after:
default_browser_return_url: http://127.0.0.1:3000/auth/login

login:
ui_url: http://127.0.0.1:3000/auth/login

registration:
ui_url: http://127.0.0.1:3000/auth/registration
after:
password:
hooks:
-
hook: session

log:
level: debug

hashers:
argon2:
parallelism: 1
memory: 131072
iterations: 2
salt_length: 16
key_length: 16

identity:
default_schema_url: file:///etc/config/kratos/identity.schema.json

courier:
smtp:
connection_uri: smtps://test:test@mailslurper:1025/?skip_ssl_verify=true
31 changes: 31 additions & 0 deletions contrib/hydra/kratos/identity.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$id": "https://schemas.ory.sh/presets/kratos/quickstart/email-password/identity.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Person",
"type": "object",
"properties": {
"traits":{
"type": "object",
"properties": {

"email": {
"type": "string",
"format": "email",
"title": "E-Mail",
"minLength": 3,
"ory.sh/kratos": {
"credentials": {
"password": {
"identifier": true
}
}
}
}
},
"required": [
"email"
]
}
},
"additionalProperties": false
}
22 changes: 22 additions & 0 deletions contrib/hydra/pg-init/pg-init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

set -e
set -u

function create_user_and_database() {
local database=$1
echo " Creating user and database '$database'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE USER $database;
CREATE DATABASE $database;
GRANT ALL PRIVILEGES ON DATABASE $database TO $database;
EOSQL
}

if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES"
for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do
create_user_and_database $db
done
echo "Multiple databases created"
fi
Loading