Compare commits
237 Commits
bce775f692
...
group-role
| Author | SHA1 | Date | |
|---|---|---|---|
| d35e5813b5 | |||
| 533e6ea6af | |||
| 0c24ed9382 | |||
| f5c61bb6a0 | |||
| eb05f830fe | |||
| d86a9de388 | |||
| d80caac81b | |||
| 5d49a661ed | |||
| 635a2d6058 | |||
| 1eb96e906d | |||
| 58974d9789 | |||
| 329fac415f | |||
| a83ec61fae | |||
| d5e86acacf | |||
| 221ef192bc | |||
| 18664dbd8b | |||
| d097735965 | |||
| 992776b8a6 | |||
| 3f260b9029 | |||
| 65f40d0897 | |||
| 6d482c784f | |||
| dc07868d15 | |||
| 09a2f05ee5 | |||
| 5cec1cf561 | |||
| 3281764eff | |||
| 868337134d | |||
| 9372673bf1 | |||
| 0eea81b42f | |||
| 7468303e41 | |||
| 8745e7d8bc | |||
| bdc42beb27 | |||
| 72083fa0a4 | |||
| b3ad13e55d | |||
| 0db54e0268 | |||
| b3ef96a0ce | |||
| a773f1f8b4 | |||
| 1a71f50914 | |||
| d17e154e42 | |||
| bad26775eb | |||
| c3fd6637a5 | |||
| 20173ea140 | |||
| 41d439beab | |||
| b36b6e18ca | |||
| 1765485027 | |||
| c5fb5e674a | |||
| 03bf655051 | |||
| f8589fce5d | |||
| ac62158de9 | |||
| 44e1a18e9a | |||
| e0f2c3219f | |||
| d2d52dc041 | |||
| d7d142152c | |||
| 5c321311cd | |||
| ffc8a5f44d | |||
| dbff94e7b3 | |||
| 0b1ef77689 | |||
| b0005c6702 | |||
| c2abf1a5ba | |||
| ac50929e6e | |||
| 32785398ca | |||
| cc497b6016 | |||
| 97ffcbdaa4 | |||
| d09bf8ff02 | |||
| c0814093e5 | |||
| d48519741d | |||
| 213991126d | |||
| c7e88606e3 | |||
| a0d506fb76 | |||
| 0ec7743fca | |||
| a8a0fa55b7 | |||
| 7321448ce7 | |||
| 6d5e0fc9a9 | |||
| ef05d66787 | |||
| b3296c45ad | |||
| 7fd163f957 | |||
| 0f0d50a684 | |||
| 68074e02bc | |||
| 8d38a86f86 | |||
| e0c095c24d | |||
| 4c318b15cd | |||
| 5ea6bc4251 | |||
| 1cbe908489 | |||
| 53ee156e67 | |||
| 07a936acc7 | |||
| f892f0da24 | |||
| 38955ee4e6 | |||
| 7fa7e87e88 | |||
| f085f2e271 | |||
| 08add259a4 | |||
| 5b6142dfa6 | |||
| dc41521a99 | |||
| 299e7eddc4 | |||
| b4699e987c | |||
| be9d4f2a1b | |||
| db99236501 | |||
| e33fb04c99 | |||
| c3d4208e12 | |||
| 3f945fa329 | |||
| 93a5cd7c70 | |||
| 951de989af | |||
| c5cf253a15 | |||
| d70032e36d | |||
| 445ac50537 | |||
| c13e564b01 | |||
| 5d3a77133d | |||
| 44592ebc08 | |||
| 1b941cb0c3 | |||
| 1cb520c2b6 | |||
| b3fdd3bc18 | |||
| 9110db2f08 | |||
| 5972735102 | |||
| 14c69349cc | |||
| 3ceeab04e1 | |||
| b7a67c208f | |||
| cee885a84d | |||
| 4b496ea9bd | |||
| 570ae6ac8c | |||
| f0d3a61e7b | |||
| b09567620f | |||
| 2209846525 | |||
| 108ed61961 | |||
| b73bfd590b | |||
| 19d56159ba | |||
| b9ccf6adac | |||
| 9a0870dbbc | |||
| 70429f69a2 | |||
| ad635008eb | |||
| eacc8fdd89 | |||
| d309fb3f57 | |||
| f4fd993679 | |||
| 016879b53f | |||
| 70bba15cda | |||
| 57daf175ab | |||
| 0817a65272 | |||
| 13f9da1a67 | |||
| 83535acf1c | |||
| 441ce2daca | |||
| f9848d2110 | |||
| 7f0511b0d4 | |||
| a27f2ad593 | |||
| 715a984241 | |||
| 66e1756ade | |||
| 849403a137 | |||
| 8d15c9b8b2 | |||
| 87af1834cf | |||
| 357583f54d | |||
| aa6de76ded | |||
| ab3c2d1eb0 | |||
| 644cf2a358 | |||
| 5b1ed9925d | |||
| 4071a50a37 | |||
| dd7c51efd8 | |||
| 8902f4d187 | |||
| 6666b20464 | |||
| c5f288ba1e | |||
| cc49ab1655 | |||
| 06c60b3491 | |||
| b584a7b07f | |||
| 410e420a46 | |||
| eeb0f6eac1 | |||
| fb622f918a | |||
| a50bad417f | |||
| c395729446 | |||
| eaa92d2fe4 | |||
| a9e382d713 | |||
| 974244025e | |||
| ae41076673 | |||
| cc7f7f40c4 | |||
| e2ae03f2a6 | |||
| 9319564dea | |||
| 83e3e5a2e9 | |||
| 2b40e4e922 | |||
| ed33d03fda | |||
| 34c152a459 | |||
| ad09e98bba | |||
| d3fd5cba16 | |||
| 64dbb4368c | |||
| cb3a6ddc58 | |||
| e774f415d8 | |||
| d5a22895e7 | |||
| 9983c51e3a | |||
| 6a1fc193f4 | |||
| 118877f727 | |||
| 7b8fe6baf2 | |||
| e85b23b3e8 | |||
| 6164b77bee | |||
| 8b5a5744ab | |||
| d9e9c5ab38 | |||
| 0dcef81b59 | |||
| 426b70a1de | |||
| 912973cdb5 | |||
| e4ff799f05 | |||
| 320715f5aa | |||
| a3b04b6243 | |||
| f610d7480f | |||
| 11ac92a026 | |||
| 98ae3e06e9 | |||
| a1146ce371 | |||
| a67ec7e78c | |||
| 9895392b50 | |||
| c6998f33e1 | |||
| 81659181e4 | |||
| 849b5935c2 | |||
| c27d837ab0 | |||
| 92e9b87227 | |||
| 243b7cce33 | |||
| 76d960619f | |||
| 3e59c78287 | |||
| 6cd9da69ab | |||
| 29b97a87b3 | |||
| 8bc4603274 | |||
| cc60a1ba86 | |||
| 89c7dc43e5 | |||
| 95c330568d | |||
| 900d314a95 | |||
| 0d8a3b1b39 | |||
| 4b7396c210 | |||
| d4e2cbdd4f | |||
| 5024ac8151 | |||
| 3bf08c5933 | |||
| b42da50306 | |||
| 0efc90567b | |||
| a5466f1b10 | |||
| 8e946cbee5 | |||
| a3a6b5e4d7 | |||
| ad0a0f5626 | |||
| 2389058ddc | |||
| ce44ef3e62 | |||
| 9ee30d1e23 | |||
| 886d0a7f5c | |||
| cfdf419460 | |||
| 930e069aee | |||
| 1ef261660f | |||
| 5d9e5d27bf | |||
| 3f8a4024ce | |||
| e8a74999c3 | |||
| dc2ce1f349 |
@@ -1,9 +1,12 @@
|
|||||||
|
|
||||||
GUARD_PORT=3001
|
GUARD_PORT=3001
|
||||||
GUARD_HOST="127.0.0.1"
|
GUARD_HOST="127.0.0.1"
|
||||||
|
GUARD_URI="http://localhost:3001"
|
||||||
|
|
||||||
GUARD_DB_URL="postgres://<user>:<user>@<host>:<port>/<db>?sslmode=disable"
|
GUARD_DB_URL="postgres://<user>:<user>@<host>:<port>/<db>?sslmode=disable"
|
||||||
|
|
||||||
|
GUARD_REDIS_URL="redis://guard:guard@localhost:6379/0"
|
||||||
|
|
||||||
GUARD_ADMIN_NAME="admin"
|
GUARD_ADMIN_NAME="admin"
|
||||||
GUARD_ADMIN_EMAIL="admin@test.net"
|
GUARD_ADMIN_EMAIL="admin@test.net"
|
||||||
GUARD_ADMIN_PASSWORD="secret"
|
GUARD_ADMIN_PASSWORD="secret"
|
||||||
@@ -11,7 +14,6 @@ GUARD_ADMIN_PASSWORD="secret"
|
|||||||
GUARD_JWT_PRIVATE="rsa"
|
GUARD_JWT_PRIVATE="rsa"
|
||||||
GUARD_JWT_PUBLIC="rsa"
|
GUARD_JWT_PUBLIC="rsa"
|
||||||
GUARD_JWT_KID="my-rsa-key-1"
|
GUARD_JWT_KID="my-rsa-key-1"
|
||||||
GUARD_JWT_ISSUER="http://localhost:3001"
|
|
||||||
|
|
||||||
GUARD_MINIO_ENDPOINT="localhost:9000"
|
GUARD_MINIO_ENDPOINT="localhost:9000"
|
||||||
GUARD_MINIO_ACCESS_KEY=""
|
GUARD_MINIO_ACCESS_KEY=""
|
||||||
@@ -20,5 +22,3 @@ GUARD_MINIO_SECRET_KEY=""
|
|||||||
GOOSE_DRIVER="postgres"
|
GOOSE_DRIVER="postgres"
|
||||||
GOOSE_DBSTRING=$DATABASE_URL
|
GOOSE_DBSTRING=$DATABASE_URL
|
||||||
GOOSE_MIGRATION_DIR="./migrations"
|
GOOSE_MIGRATION_DIR="./migrations"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -27,6 +27,7 @@ go.work.sum
|
|||||||
|
|
||||||
# env file
|
# env file
|
||||||
.env
|
.env
|
||||||
|
.env.remote
|
||||||
|
|
||||||
# key files
|
# key files
|
||||||
*.pem
|
*.pem
|
||||||
|
|||||||
55
Dockerfile
Normal file
55
Dockerfile
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
# Stage 1: Build frontend
|
||||||
|
FROM node:22 AS frontend-builder
|
||||||
|
WORKDIR /app/web
|
||||||
|
COPY web/ .
|
||||||
|
RUN npm install && npm run build
|
||||||
|
|
||||||
|
# Stage 2: Build backend
|
||||||
|
FROM golang:1.24 AS backend-builder
|
||||||
|
WORKDIR /app
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
RUN go mod download
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
# Copy built frontend into Go embed path (adjust if needed)
|
||||||
|
# COPY --from=frontend-builder /app/web/dist ./web/dist
|
||||||
|
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux make build
|
||||||
|
|
||||||
|
# Stage 3: Final image
|
||||||
|
FROM debian:bookworm-slim
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Install CA certificates for HTTPS
|
||||||
|
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
COPY --from=backend-builder /app/bin/hspguard .
|
||||||
|
COPY --from=frontend-builder /app/dist ./dist
|
||||||
|
|
||||||
|
COPY redis.conf /config/redis.conf
|
||||||
|
|
||||||
|
# Optional: copy default .env file if used
|
||||||
|
# COPY .env .env
|
||||||
|
|
||||||
|
# Set environment variables (can be overridden at runtime)
|
||||||
|
ENV ENV=production \
|
||||||
|
GUARD_PORT=3001 \
|
||||||
|
GUARD_HOST="127.0.0.1" \
|
||||||
|
GUARD_URI="http://localhost:3001" \
|
||||||
|
GUARD_DB_URL="postgres://user:user@localhost:5432/db?sslmode=disable" \
|
||||||
|
GUARD_ADMIN_NAME="admin" \
|
||||||
|
GUARD_ADMIN_EMAIL="admin@test.net" \
|
||||||
|
GUARD_ADMIN_PASSWORD="secret" \
|
||||||
|
GUARD_JWT_PRIVATE="rsa" \
|
||||||
|
GUARD_JWT_PUBLIC="rsa" \
|
||||||
|
GUARD_JWT_KID="my-rsa-key-1" \
|
||||||
|
GUARD_MINIO_ENDPOINT="localhost:9000" \
|
||||||
|
GUARD_MINIO_ACCESS_KEY="" \
|
||||||
|
GUARD_MINIO_SECRET_KEY="" \
|
||||||
|
GOOSE_DRIVER="postgres" \
|
||||||
|
GOOSE_DBSTRING=$GUARD_DB_URL \
|
||||||
|
GOOSE_MIGRATION_DIR="./migrations"
|
||||||
|
|
||||||
|
EXPOSE 3001
|
||||||
|
|
||||||
|
CMD ["./hspguard"]
|
||||||
168
README.md
168
README.md
@@ -1,123 +1,139 @@
|
|||||||
# HSP Guard
|
|
||||||
|
|
||||||
**HSP Guard** is an internal security service for your home lab, designed to manage user access to various home services and tools. It dynamically controls permissions and prevents unauthorized or unexpected users from accessing sensitive services.
|
# 🛡️ HSP Guard
|
||||||
|
|
||||||
|
**HSP Guard** is a modern OpenID Connect (OIDC) identity provider and access management system for home labs. It provides secure authentication and granular authorization for all your self-hosted services, combining ease of use with enterprise-level control — without any vendor lock-in.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📌 Overview
|
## ✨ Features
|
||||||
|
|
||||||
HSP Guard authorizes user requests and provides an efficient way to:
|
- **OIDC Provider**: Central login for your home lab apps
|
||||||
|
- **Admin UI**: Manage apps, users, roles, permissions, and sessions
|
||||||
- Manage permissions for individual services/tools
|
- **API Tokens**: Issue access tokens with embedded roles and permissions
|
||||||
- Define roles for easier access control
|
- **Flexible Authorization**: Support for roles, permissions, and groups (future)
|
||||||
- Validate and authorize users via JWT tokens
|
- **App Registration**: Register OAuth/OIDC clients with custom permissions
|
||||||
- Securely integrate with new services during installation
|
- **Automatic Permission Sync**: Optionally fetch app permissions from `/.well-known/guard-configuration`
|
||||||
|
- **User & Admin Sessions**: See and revoke active user/app sessions
|
||||||
|
- **Pluggable**: Easily integrate new apps and services
|
||||||
|
- **Audit Logging**: Track actions for security and troubleshooting (planned)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📚 Concepts
|
## 🚀 Getting Started
|
||||||
|
|
||||||
### 🔐 Permission
|
### 1. **Run HSP Guard**
|
||||||
|
|
||||||
Permissions define access to specific features or tools.
|
You can run HSP Guard via Docker, Docker Compose, or natively (see below).
|
||||||
By default, HSP Guard includes predefined administrative permissions that allow an admin to log in and configure the system.
|
|
||||||
|
|
||||||
Once logged in, the admin can:
|
### 2. **Register Your First App**
|
||||||
|
|
||||||
- Manually create new permissions for specific applications
|
1. **Login as admin**
|
||||||
- Allow new applications to register their own permissions
|
2. Go to **Apps → Register New App**
|
||||||
- Assign permissions to users, granting them access to corresponding tools
|
3. Enter:
|
||||||
|
- **Name** of your app
|
||||||
|
- **Redirect URIs** (for OIDC/OAuth callbacks)
|
||||||
|
- (Optional) **Permissions** (manual or auto-discovered from the app)
|
||||||
|
4. Save to receive a `client_id` and `client_secret`
|
||||||
|
5. Configure your app to use these for OIDC login
|
||||||
|
|
||||||
|
### 3. **Assign Permissions & Roles**
|
||||||
|
|
||||||
|
- Assign **default roles** to new users automatically (configurable)
|
||||||
|
- Create custom **roles** to bundle permissions (e.g., `FAMILY_MEMBER`)
|
||||||
|
- Assign users to roles and/or groups for flexible access control
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### 🧩 Role
|
## 🏗️ Concepts
|
||||||
|
|
||||||
A **Role** is a named collection of permissions (e.g., `GUEST`, `FRIEND`, `FAMILY_MEMBER`) created by the admin.
|
### 🔑 **Permissions**
|
||||||
Roles simplify user management by allowing bulk assignment of permissions. Instead of assigning multiple permissions individually, a role bundles them under one label.
|
Fine-grained controls for app features (e.g., `music.play`, `dashboard.edit`).
|
||||||
|
Can be manually defined or auto-discovered from an app’s `.well-known/guard-configuration` endpoint.
|
||||||
|
|
||||||
|
### 🧩 **Roles**
|
||||||
|
Named bundles of permissions (e.g., `GUEST`, `FAMILY_MEMBER`, `ADMIN`).
|
||||||
|
Assign to users/groups for easier management.
|
||||||
|
|
||||||
|
### 👥 **Groups**
|
||||||
|
(Planned) Logical user collections (e.g., “Family”, “Guests”, “Admins”) for batch management of roles/permissions.
|
||||||
|
|
||||||
|
### 👤 **Users**
|
||||||
|
Each user has a unique profile, roles, and group memberships.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### 👥 Group *(Coming Soon)*
|
## 🔗 OIDC/OAuth Integration
|
||||||
|
|
||||||
This feature is planned for future releases. Groups will help organize users or services into logical clusters for simplified access control.
|
**HSP Guard** is a standard-compliant OIDC Provider. Any app supporting OIDC/OAuth can integrate.
|
||||||
|
|
||||||
|
- Register app in admin panel to get `client_id` & `client_secret`
|
||||||
|
- Configure your app’s OIDC integration (see your app’s docs)
|
||||||
|
- Token claims include `permissions` and `roles` for easy authorization
|
||||||
|
|
||||||
|
#### **Example Token Claims**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"sub": "123456",
|
||||||
|
"name": "Alex Example",
|
||||||
|
"email": "alex@example.com",
|
||||||
|
"roles": ["GUEST"],
|
||||||
|
"permissions": ["dashboard.view", "music.play"]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📡 API
|
## 📡 **App Permission Discovery**
|
||||||
|
|
||||||
### ✅ User Authorization
|
If your app supports permission discovery:
|
||||||
|
- Expose `/.well-known/guard-configuration` endpoint listing available permissions
|
||||||
|
- When registering in HSP Guard, auto-fetch and display for approval
|
||||||
|
|
||||||
To verify whether a request is made by a valid and authorized user, applications can require a **JWT token** as part of the request.
|
#### **Example guard-configuration JSON**
|
||||||
This token is sent to HSP Guard, which:
|
```json
|
||||||
|
{
|
||||||
- Validates the token
|
"permissions": [
|
||||||
- Returns user details (e.g., ID, name, email) for logging, auditing, or request tracing
|
"dashboard.view",
|
||||||
|
"dashboard.edit",
|
||||||
|
"dashboard.admin"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### 🔑 Permission Checking
|
## 🔄 **User & Admin Sessions**
|
||||||
|
|
||||||
Applications can also verify whether a user holds specific permissions before granting access to certain services or features.
|
- List all active sessions (browser, app, device, timestamp)
|
||||||
|
- Revoke sessions (logout) from user or admin panel
|
||||||
To do this, an app sends:
|
|
||||||
|
|
||||||
- The user's JWT token
|
|
||||||
- A list of required permissions
|
|
||||||
|
|
||||||
HSP Guard checks the user’s assigned permissions and responds with the authorization status.
|
|
||||||
|
|
||||||
> **Best Practice:** Applications should directly integrate with HSP Guard to enforce permission-based access control.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🔄 User Authorization Flow
|
## 📦 **Planned Features & Roadmap**
|
||||||
|
|
||||||
When a user tries to access a home lab service that requires authentication:
|
- [ ] **Group Management** for batch assignments
|
||||||
|
- [ ] **Audit Logging** of all admin/user actions
|
||||||
1. The application will **offer an authorization URL** to the user.
|
- [ ] **Permission Expiry** (time-limited access)
|
||||||
2. The user follows the URL and is taken to the **HSP Guard login page**.
|
- [ ] **Advanced Web UI** (dark mode, mobile)
|
||||||
3. The user selects or signs into an account they wish to use for that service.
|
- [ ] **External Identity Providers** (login with Google, GitHub, etc.)
|
||||||
4. Once authenticated and authorized, the user is redirected to the **application-defined redirect URL**.
|
|
||||||
5. The application can now:
|
|
||||||
- Retrieve a **JWT token** from the redirect callback
|
|
||||||
- **Optionally cache the session/token** to avoid prompting the user every time
|
|
||||||
|
|
||||||
This process is similar to how external identity providers like **Google Sign-In** or **GitHub OAuth** work — providing a seamless and secure authentication experience for the user.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## ⚙️ Integrating New Services & Tools
|
## 🛠️ **Development**
|
||||||
|
|
||||||
When a new service or tool is installed:
|
- See [CONTRIBUTING.md](CONTRIBUTING.md) for how to contribute!
|
||||||
|
- Pull requests and issues are welcome.
|
||||||
1. It provides a configuration file to HSP Guard
|
|
||||||
2. Guard extracts and registers any defined permissions
|
|
||||||
3. These permissions are **isolated** — even if a name overlaps with existing permissions, a prefix is added to avoid conflicts
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 👤 User Registration & Onboarding
|
## 📝 **License**
|
||||||
|
|
||||||
New users (e.g., family, friends, guests) must complete a registration process to access your home lab.
|
MIT — open source, for the home lab community.
|
||||||
|
|
||||||
They can:
|
|
||||||
|
|
||||||
- Visit a user-friendly registration webpage
|
|
||||||
- Fill out a form with basic information (name, email, password, etc.)
|
|
||||||
|
|
||||||
Once registered, the admin can assign roles or individual permissions as needed.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🚧 Roadmap
|
## 💬 **Feedback**
|
||||||
|
|
||||||
- [ ] Group Management
|
Open an [issue](https://github.com/yourusername/hsp-guard/issues) or join the discussion!
|
||||||
- [ ] Web UI Enhancements
|
|
||||||
- [ ] Audit Logging
|
|
||||||
- [ ] Permission Expiry & Time-Based Access
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📬 Feedback & Contribution
|
|
||||||
|
|
||||||
Feel free to open an issue or pull request if you’d like to contribute or report bugs. HSP Guard is a personal home lab project, but feedback is always welcome!
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
"gitea.local/admin/hspguard/internal/admin"
|
"gitea.local/admin/hspguard/internal/admin"
|
||||||
"gitea.local/admin/hspguard/internal/auth"
|
"gitea.local/admin/hspguard/internal/auth"
|
||||||
|
"gitea.local/admin/hspguard/internal/cache"
|
||||||
"gitea.local/admin/hspguard/internal/config"
|
"gitea.local/admin/hspguard/internal/config"
|
||||||
"gitea.local/admin/hspguard/internal/oauth"
|
"gitea.local/admin/hspguard/internal/oauth"
|
||||||
"gitea.local/admin/hspguard/internal/repository"
|
"gitea.local/admin/hspguard/internal/repository"
|
||||||
@@ -21,14 +22,16 @@ type APIServer struct {
|
|||||||
addr string
|
addr string
|
||||||
repo *repository.Queries
|
repo *repository.Queries
|
||||||
storage *storage.FileStorage
|
storage *storage.FileStorage
|
||||||
|
cache *cache.Client
|
||||||
cfg *config.AppConfig
|
cfg *config.AppConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAPIServer(addr string, db *repository.Queries, minio *storage.FileStorage, cfg *config.AppConfig) *APIServer {
|
func NewAPIServer(addr string, db *repository.Queries, minio *storage.FileStorage, cache *cache.Client, cfg *config.AppConfig) *APIServer {
|
||||||
return &APIServer{
|
return &APIServer{
|
||||||
addr: addr,
|
addr: addr,
|
||||||
repo: db,
|
repo: db,
|
||||||
storage: minio,
|
storage: minio,
|
||||||
|
cache: cache,
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,13 +44,13 @@ func (s *APIServer) Run() error {
|
|||||||
// staticDir := http.Dir(filepath.Join(workDir, "static"))
|
// staticDir := http.Dir(filepath.Join(workDir, "static"))
|
||||||
// FileServer(router, "/static", staticDir)
|
// FileServer(router, "/static", staticDir)
|
||||||
|
|
||||||
oauthHandler := oauth.NewOAuthHandler(s.repo, s.cfg)
|
oauthHandler := oauth.NewOAuthHandler(s.repo, s.cache, s.cfg)
|
||||||
|
|
||||||
router.Route("/api/v1", func(r chi.Router) {
|
router.Route("/api/v1", func(r chi.Router) {
|
||||||
userHandler := user.NewUserHandler(s.repo, s.storage, s.cfg)
|
userHandler := user.NewUserHandler(s.repo, s.storage, s.cfg)
|
||||||
userHandler.RegisterRoutes(r)
|
userHandler.RegisterRoutes(r)
|
||||||
|
|
||||||
authHandler := auth.NewAuthHandler(s.repo, s.cfg)
|
authHandler := auth.NewAuthHandler(s.repo, s.cache, s.cfg)
|
||||||
authHandler.RegisterRoutes(r)
|
authHandler.RegisterRoutes(r)
|
||||||
|
|
||||||
oauthHandler.RegisterRoutes(r)
|
oauthHandler.RegisterRoutes(r)
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
"gitea.local/admin/hspguard/cmd/hspguard/api"
|
"gitea.local/admin/hspguard/cmd/hspguard/api"
|
||||||
|
"gitea.local/admin/hspguard/internal/cache"
|
||||||
"gitea.local/admin/hspguard/internal/config"
|
"gitea.local/admin/hspguard/internal/config"
|
||||||
"gitea.local/admin/hspguard/internal/repository"
|
"gitea.local/admin/hspguard/internal/repository"
|
||||||
"gitea.local/admin/hspguard/internal/storage"
|
"gitea.local/admin/hspguard/internal/storage"
|
||||||
@@ -16,9 +18,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := godotenv.Load()
|
err := godotenv.Load()
|
||||||
if err != nil {
|
if err != nil && os.Getenv("ENV") != "production" {
|
||||||
log.Fatalln("ERR: Failed to load environment variables:", err)
|
log.Fatalln("WARNING: .env file not found. Skipping...")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var cfg config.AppConfig
|
var cfg config.AppConfig
|
||||||
@@ -41,9 +42,12 @@ func main() {
|
|||||||
|
|
||||||
fStorage := storage.New(&cfg)
|
fStorage := storage.New(&cfg)
|
||||||
|
|
||||||
user.EnsureAdminUser(ctx, &cfg, repo)
|
cache := cache.NewClient(&cfg)
|
||||||
|
|
||||||
server := api.NewAPIServer(fmt.Sprintf("%s:%s", cfg.Host, cfg.Port), repo, fStorage, &cfg)
|
user.EnsureAdminUser(ctx, &cfg, repo)
|
||||||
|
user.EnsureSystemPermissions(ctx, repo)
|
||||||
|
|
||||||
|
server := api.NewAPIServer(fmt.Sprintf("%s:%s", cfg.Host, cfg.Port), repo, fStorage, cache, &cfg)
|
||||||
if err := server.Run(); err != nil {
|
if err := server.Run(); err != nil {
|
||||||
log.Fatalln("ERR: Failed to start server:", err)
|
log.Fatalln("ERR: Failed to start server:", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
services:
|
services:
|
||||||
db:
|
db:
|
||||||
image: postgres
|
image: postgres
|
||||||
@@ -7,6 +6,24 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: guard
|
POSTGRES_USER: guard
|
||||||
POSTGRES_PASSWORD: guard
|
POSTGRES_PASSWORD: guard
|
||||||
|
volumes:
|
||||||
|
- postgres-data:/var/lib/postgresql/data
|
||||||
ports:
|
ports:
|
||||||
- "5432:5432"
|
- "5432:5432"
|
||||||
|
|
||||||
|
cache:
|
||||||
|
image: redis:7.2 # or newer
|
||||||
|
container_name: guard-redis
|
||||||
|
ports:
|
||||||
|
- "6379:6379"
|
||||||
|
volumes:
|
||||||
|
- redis-data:/data
|
||||||
|
- ./redis.conf:/usr/local/etc/redis/redis.conf
|
||||||
|
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
redis-data:
|
||||||
|
driver: local
|
||||||
|
postgres-data:
|
||||||
|
driver: local
|
||||||
|
|||||||
4
go.mod
4
go.mod
@@ -11,6 +11,9 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/avct/uasurfer v0.0.0-20250506104815-f2613aa2d406 // indirect
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
github.com/go-ini/ini v1.67.0 // indirect
|
github.com/go-ini/ini v1.67.0 // indirect
|
||||||
github.com/goccy/go-json v0.10.5 // indirect
|
github.com/goccy/go-json v0.10.5 // indirect
|
||||||
@@ -23,6 +26,7 @@ require (
|
|||||||
github.com/minio/md5-simd v1.1.2 // indirect
|
github.com/minio/md5-simd v1.1.2 // indirect
|
||||||
github.com/minio/minio-go/v7 v7.0.92 // indirect
|
github.com/minio/minio-go/v7 v7.0.92 // indirect
|
||||||
github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // indirect
|
github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // indirect
|
||||||
|
github.com/redis/go-redis/v9 v9.10.0 // indirect
|
||||||
github.com/rs/xid v1.6.0 // indirect
|
github.com/rs/xid v1.6.0 // indirect
|
||||||
github.com/tinylib/msgp v1.3.0 // indirect
|
github.com/tinylib/msgp v1.3.0 // indirect
|
||||||
golang.org/x/crypto v0.38.0 // indirect
|
golang.org/x/crypto v0.38.0 // indirect
|
||||||
|
|||||||
8
go.sum
8
go.sum
@@ -1,6 +1,12 @@
|
|||||||
|
github.com/avct/uasurfer v0.0.0-20250506104815-f2613aa2d406 h1:5/KfwL9TS8yNtUSunutqifcSC8rdX9PNdvbSsw/X/lQ=
|
||||||
|
github.com/avct/uasurfer v0.0.0-20250506104815-f2613aa2d406/go.mod h1:s+GCtuP4kZNxh1WGoqdWI1+PbluBcycrMMWuKQ9e5Nk=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||||
github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8=
|
github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8=
|
||||||
@@ -38,6 +44,8 @@ github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c h1:dAMKvw0MlJT1Gsh
|
|||||||
github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
|
github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/redis/go-redis/v9 v9.10.0 h1:FxwK3eV8p/CQa0Ch276C7u2d0eNC9kCmAYQ7mCXCzVs=
|
||||||
|
github.com/redis/go-redis/v9 v9.10.0/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
|
||||||
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
||||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
|||||||
@@ -4,44 +4,15 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
"gitea.local/admin/hspguard/internal/repository"
|
"gitea.local/admin/hspguard/internal/repository"
|
||||||
|
"gitea.local/admin/hspguard/internal/types"
|
||||||
"gitea.local/admin/hspguard/internal/util"
|
"gitea.local/admin/hspguard/internal/util"
|
||||||
"gitea.local/admin/hspguard/internal/web"
|
"gitea.local/admin/hspguard/internal/web"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ApiServiceDTO struct {
|
|
||||||
ID uuid.UUID `json:"id"`
|
|
||||||
ClientID string `json:"client_id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
RedirectUris []string `json:"redirect_uris"`
|
|
||||||
Scopes []string `json:"scopes"`
|
|
||||||
GrantTypes []string `json:"grant_types"`
|
|
||||||
CreatedAt time.Time `json:"created_at"`
|
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
|
||||||
IsActive bool `json:"is_active"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewApiServiceDTO(service repository.ApiService) ApiServiceDTO {
|
|
||||||
return ApiServiceDTO{
|
|
||||||
ID: service.ID,
|
|
||||||
ClientID: service.ClientID,
|
|
||||||
Name: service.Name,
|
|
||||||
Description: service.Description.String,
|
|
||||||
RedirectUris: service.RedirectUris,
|
|
||||||
Scopes: service.Scopes,
|
|
||||||
GrantTypes: service.GrantTypes,
|
|
||||||
CreatedAt: service.CreatedAt,
|
|
||||||
UpdatedAt: service.UpdatedAt,
|
|
||||||
IsActive: service.IsActive,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *AdminHandler) GetApiServices(w http.ResponseWriter, r *http.Request) {
|
func (h *AdminHandler) GetApiServices(w http.ResponseWriter, r *http.Request) {
|
||||||
services, err := h.repo.ListApiServices(r.Context())
|
services, err := h.repo.ListApiServices(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -50,19 +21,21 @@ func (h *AdminHandler) GetApiServices(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
apiServices := make([]ApiServiceDTO, 0)
|
apiServices := make([]types.ApiServiceDTO, 0)
|
||||||
|
|
||||||
for _, service := range services {
|
for _, service := range services {
|
||||||
apiServices = append(apiServices, NewApiServiceDTO(service))
|
apiServices = append(apiServices, types.NewApiServiceDTO(service))
|
||||||
}
|
}
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
Items []ApiServiceDTO `json:"items"`
|
Items []types.ApiServiceDTO `json:"items"`
|
||||||
Count int `json:"count"`
|
Count int `json:"count"`
|
||||||
}
|
}
|
||||||
|
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
if err := encoder.Encode(Response{
|
if err := encoder.Encode(Response{
|
||||||
Items: apiServices,
|
Items: apiServices,
|
||||||
Count: len(apiServices),
|
Count: len(apiServices),
|
||||||
@@ -129,10 +102,7 @@ func (h *AdminHandler) AddApiService(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if req.Description != "" {
|
if req.Description != "" {
|
||||||
params.Description = pgtype.Text{
|
params.Description = &req.Description
|
||||||
String: req.Description,
|
|
||||||
Valid: true,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
service, err := h.repo.CreateApiService(r.Context(), params)
|
service, err := h.repo.CreateApiService(r.Context(), params)
|
||||||
@@ -146,13 +116,16 @@ func (h *AdminHandler) AddApiService(w http.ResponseWriter, r *http.Request) {
|
|||||||
service.ClientSecret = clientSecret
|
service.ClientSecret = clientSecret
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
Service ApiServiceDTO `json:"service"`
|
Service types.ApiServiceDTO `json:"service"`
|
||||||
Credentials ApiServiceCredentials `json:"credentials"`
|
Credentials ApiServiceCredentials `json:"credentials"`
|
||||||
}
|
}
|
||||||
|
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
if err := encoder.Encode(Response{
|
if err := encoder.Encode(Response{
|
||||||
Service: NewApiServiceDTO(service),
|
Service: types.NewApiServiceDTO(service),
|
||||||
Credentials: ApiServiceCredentials{
|
Credentials: ApiServiceCredentials{
|
||||||
ClientId: service.ClientID,
|
ClientId: service.ClientID,
|
||||||
ClientSecret: service.ClientSecret,
|
ClientSecret: service.ClientSecret,
|
||||||
@@ -178,7 +151,27 @@ func (h *AdminHandler) GetApiService(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
|
|
||||||
if err := encoder.Encode(NewApiServiceDTO(service)); err != nil {
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
if err := encoder.Encode(types.NewApiServiceDTO(service)); err != nil {
|
||||||
|
web.Error(w, "failed to encode response", http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *AdminHandler) GetApiServiceCID(w http.ResponseWriter, r *http.Request) {
|
||||||
|
clientId := chi.URLParam(r, "client_id")
|
||||||
|
|
||||||
|
service, err := h.repo.GetApiServiceCID(r.Context(), clientId)
|
||||||
|
if err != nil {
|
||||||
|
web.Error(w, "service with provided client id not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
encoder := json.NewEncoder(w)
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
if err := encoder.Encode(types.NewApiServiceDTO(service)); err != nil {
|
||||||
web.Error(w, "failed to encode response", http.StatusInternalServerError)
|
web.Error(w, "failed to encode response", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -213,6 +206,8 @@ func (h *AdminHandler) RegenerateApiServiceSecret(w http.ResponseWriter, r *http
|
|||||||
|
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
if err := encoder.Encode(ApiServiceCredentials{
|
if err := encoder.Encode(ApiServiceCredentials{
|
||||||
ClientId: service.ClientID,
|
ClientId: service.ClientID,
|
||||||
ClientSecret: clientSecret,
|
ClientSecret: clientSecret,
|
||||||
@@ -250,8 +245,8 @@ func (h *AdminHandler) UpdateApiService(w http.ResponseWriter, r *http.Request)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if req.Description == "" {
|
if len(req.Scopes) == 0 {
|
||||||
web.Error(w, "service name is required", http.StatusBadRequest)
|
web.Error(w, "at least 1 scope is required", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,15 +256,57 @@ func (h *AdminHandler) UpdateApiService(w http.ResponseWriter, r *http.Request)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := h.repo.UpdateApiService(r.Context(), repository.UpdateApiServiceParams{
|
updated, err := h.repo.UpdateApiService(r.Context(), repository.UpdateApiServiceParams{
|
||||||
ClientID: service.ClientID,
|
ClientID: service.ClientID,
|
||||||
Name: ,
|
Name: req.Name,
|
||||||
Description: pgtype.Text{},
|
Description: &req.Description,
|
||||||
RedirectUris: []string{},
|
RedirectUris: req.RedirectUris,
|
||||||
Scopes: []string{},
|
Scopes: req.Scopes,
|
||||||
GrantTypes: []string{},
|
GrantTypes: req.GrantTypes,
|
||||||