This project demonstrates a simple Go application secured with Keycloak. It uses Echo as the web framework and Gocloak as the Keycloak client.
- Public and secure endpoints
- JWT token validation using Keycloak
- Modular project structure
- Docker
- Docker Compose
git clone https://github.com/yourusername/your_project.git
cd your_project
Create a .env
file in the project root directory and set the necessary environment variables:
Obviously you should use your own variables. the followings are only samples :)
KEYCLOAK_URL=http://localhost:8080
REALM=mani
CLIENT_ID=go
CLIENT_SECRET=aZbUGUvcXxETMznmtsFB1Mm8TEWK4Hpz
Build and run the application using Docker Compose:
docker compose up --build
This will start the Keycloak server and the Go application.
your_project/
|-- config/
| |-- config.go
|-- handlers/
| |-- handlers.go
|-- auth/
| |-- auth.go
|-- main.go
|-- go.mod
|-- go.sum
|-- Dockerfile
|-- docker-compose.yml
- config/: Contains configuration-related code.
- handlers/: Contains HTTP handler functions.
- auth/: Contains authentication middleware and related functions.
- main.go: Entry point of the application.
- Dockerfile: Dockerfile for building the Go application.
- docker-compose.yml: Docker Compose configuration file.
You can access the public endpoint without authentication:
curl http://localhost:4000/public
To access the secure endpoint, you need to obtain a JWT token from Keycloak and include it in the request header.
curl -H "Authorization: Bearer <your_access_token>" http://localhost:4000/secure
- GET /public: Public endpoint accessible without authentication.
- GET /secure: Secure endpoint accessible with a valid JWT token.
Contributions are welcome! Please open an issue or submit a pull request for any bugs, improvements, or new features.
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Make your changes.
- Commit your changes (
git commit -m 'Add new feature'
). - Push to the branch (
git push origin feature-branch
). - Open a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.