Skip to content

Commit 62dd4da

Browse files
committed
add dex guide
1 parent 4d82913 commit 62dd4da

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

content/guides/dex.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
title: Mocking OAuth services in testing with Dex
3+
description: &desc Mocking OAuth services in testing with Dex
4+
keywords: Dex, container-supported development
5+
linktitle: Mocking OAuth services with Dex
6+
summary: *desc
7+
tags: [app-dev, distributed-systems]
8+
languages: []
9+
params:
10+
time: 10 minutes
11+
---
12+
13+
Dex is an open-source OpenID Connect (OIDC) and OAuth 2.0 identity provider that can be configured to authenticate against various backend identity providers, such as LDAP, SAML, and OAuth. Running Dex in a Docker container allows developers to simulate an OAuth 2.0 server for testing and development purposes. This guide will walk you through setting up Dex as an OAuth mock server using Docker containers.
14+
15+
Nowadays OAuth is the preferred choice to authenticate in web services, the highest part of them give the possibility to access using popular OAuth services like Github, Google or Apple. Using OAuth guarantees a higher level of security and simplification since it is not necessary to create new profiles for each service. This means that, by allowing applications to access resources on behalf of users without sharing passwords, OAuth minimizes the risk of credential exposure.
16+
17+
In this guide, you'll learn how to:
18+
19+
- Use Docker to launch up a Dex container.
20+
- Use mock OAuth in the local development without relying on an external OAuth provider.
21+
22+
## Using Dex with Docker
23+
24+
The official [Docker image for Dex](https://hub.docker.com/r/dexidp/dex/) provides a convenient way to deploy and manage Dex instances. Dex is available for various CPU architectures, including amd64, armv7, and arm64, ensuring compatibility with different devices and platforms. You can learn more about Dex standalone on the [Dex docs site](https://dexidp.io/docs/getting-started/).
25+
26+
### Prerequisites
27+
28+
[Docker Compose](https://docs.docker.com/compose/): Recommended for managing multi-container Docker applications.
29+
30+
### Setting Up Dex with Docker
31+
32+
Begin by creating a directory for your Dex project:
33+
34+
```bash
35+
mkdir dex-mock-server
36+
cd dex-mock-server
37+
```
38+
Organize your project with the following structure:
39+
40+
```bash
41+
dex-mock-server/
42+
├── config.yaml
43+
└── docker-compose.yaml
44+
```
45+
46+
Create the Dex Configuration File:
47+
48+
The config.yaml file defines Dex's settings, including connectors, clients, and storage. For a mock server setup, you can use the following minimal configuration:
49+
50+
```yaml
51+
# config.yaml
52+
issuer: http://localhost:5556/dex
53+
storage:
54+
type: memory
55+
web:
56+
http: 0.0.0.0:5556
57+
staticClients:
58+
- id: example-app
59+
redirectURIs:
60+
- 'http://localhost:5555/callback'
61+
name: 'Example App'
62+
secret: ZXhhbXBsZS1hcHAtc2VjcmV0
63+
enablePasswordDB: true
64+
staticPasswords:
65+
- email: "[email protected]"
66+
hash: "$2a$10$2b2cU8CPhOTaGrs1HRQuAueS7JTT5ZHsHSzYiFPm1leZck7Mc8T4W"
67+
username: "admin"
68+
userID: "1234"
69+
```
70+
71+
Explanation:
72+
73+
- issuer: The public URL for Dex.
74+
75+
- storage: Using in-memory storage for simplicity.
76+
77+
- web: Dex will listen on port 5556.
78+
79+
- staticClients: Defines a client application (example-app) with its redirect URI and secret.
80+
81+
- enablePasswordDB: Enables static password authentication.
82+
83+
- staticPasswords: Defines a static user for authentication. The hash is a bcrypt hash of the password.
84+
85+
> Note: Ensure the hash is a valid bcrypt hash of your desired password. You can generate this using tools like [bcrypt-generator.com](https://bcrypt-generator.com/)
86+
or use CLI tools like [htpasswd](https://httpd.apache.org/docs/2.4/programs/htpasswd.html) like in this following example:
87+
```bash
88+
echo password | htpasswd -BinC 10 admin | cut -d: -f2
89+
```
90+
91+
Running Dex
92+
93+
With Docker Compose configured, start Dex:
94+
95+
```yaml
96+
# docker-compose.yaml
97+
98+
services:
99+
dex:
100+
image: dexidp/dex:latest
101+
container_name: dex
102+
ports:
103+
- "5556:5556"
104+
volumes:
105+
- ./config.yaml:/etc/dex/config.yaml
106+
command: ["dex", "serve", "/etc/dex/config.yaml"]
107+
```
108+
109+
Now it is possible to run the container using `docker compose` command.
110+
```bash
111+
docker compose up -d
112+
```
113+
114+
This command will download the Dex Docker image (if not already available) and start the container in detached mode.
115+
116+
117+
To Verify that Dex is running, check the logs to ensure Dex started successfully:
118+
119+
```bash
120+
docker-compose logs -f dex
121+
```
122+
You should see output indicating that Dex is listening on the specified port.
123+
124+
Testing the OAuth Flow
125+
Prepare a Test Application:
126+
127+
To test the OAuth flow, you'll need a client application configured to authenticate against Dex. Dex provides an example app that you can use for this purpose.
128+
129+
Clone the Dex Repository:
130+

0 commit comments

Comments
 (0)