mirror of
https://github.com/h44z/wg-portal.git
synced 2026-02-23 19:06:34 +00:00
Compare commits
13 Commits
default_pe
...
fix/chart-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
edfa509828 | ||
|
|
2d18776834 | ||
|
|
ee02916ce5 | ||
|
|
e62db0d62e | ||
|
|
129cd0d408 | ||
|
|
70cc44cc4d | ||
|
|
e53b8c8087 | ||
|
|
df9fdd14fb | ||
|
|
e0f6c1d04b | ||
|
|
d2fe267be7 | ||
|
|
bb516e9115 | ||
|
|
5d58df8a19 | ||
|
|
2200509bc0 |
6
.github/workflows/chart.yml
vendored
6
.github/workflows/chart.yml
vendored
@@ -20,7 +20,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
if: ${{ github.event_name == 'pull_request' }}
|
if: ${{ github.event_name == 'pull_request' }}
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
|
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ jobs:
|
|||||||
# ct lint requires Python 3.x to run following packages:
|
# ct lint requires Python 3.x to run following packages:
|
||||||
# - yamale (https://github.com/23andMe/Yamale)
|
# - yamale (https://github.com/23andMe/Yamale)
|
||||||
# - yamllint (https://github.com/adrienverge/yamllint)
|
# - yamllint (https://github.com/adrienverge/yamllint)
|
||||||
- uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
|
- uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
|
||||||
with:
|
with:
|
||||||
python-version: '3.x'
|
python-version: '3.x'
|
||||||
|
|
||||||
@@ -60,7 +60,7 @@ jobs:
|
|||||||
permissions:
|
permissions:
|
||||||
packages: write
|
packages: write
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
|
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||||
|
|
||||||
- uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.0
|
- uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.0
|
||||||
with:
|
with:
|
||||||
|
|||||||
2
.github/workflows/docker-publish.yml
vendored
2
.github/workflows/docker-publish.yml
vendored
@@ -18,7 +18,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Check out the repo
|
- name: Check out the repo
|
||||||
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
|
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||||
|
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3.7.0
|
uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3.7.0
|
||||||
|
|||||||
4
.github/workflows/pages.yml
vendored
4
.github/workflows/pages.yml
vendored
@@ -15,11 +15,11 @@ jobs:
|
|||||||
deploy:
|
deploy:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
|
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
- uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
|
- uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
|
||||||
with:
|
with:
|
||||||
python-version: 3.x
|
python-version: 3.x
|
||||||
|
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ func main() {
|
|||||||
rawDb, err := adapters.NewDatabase(cfg.Database)
|
rawDb, err := adapters.NewDatabase(cfg.Database)
|
||||||
internal.AssertNoError(err)
|
internal.AssertNoError(err)
|
||||||
|
|
||||||
database, err := adapters.NewSqlRepository(rawDb)
|
database, err := adapters.NewSqlRepository(rawDb, cfg)
|
||||||
internal.AssertNoError(err)
|
internal.AssertNoError(err)
|
||||||
|
|
||||||
wireGuard, err := wireguard.NewControllerManager(cfg)
|
wireGuard, err := wireguard.NewControllerManager(cfg)
|
||||||
@@ -135,6 +135,7 @@ func main() {
|
|||||||
apiV0EndpointPeers := handlersV0.NewPeerEndpoint(cfg, apiV0Auth, validatorManager, apiV0BackendPeers)
|
apiV0EndpointPeers := handlersV0.NewPeerEndpoint(cfg, apiV0Auth, validatorManager, apiV0BackendPeers)
|
||||||
apiV0EndpointConfig := handlersV0.NewConfigEndpoint(cfg, apiV0Auth, wireGuard)
|
apiV0EndpointConfig := handlersV0.NewConfigEndpoint(cfg, apiV0Auth, wireGuard)
|
||||||
apiV0EndpointTest := handlersV0.NewTestEndpoint(apiV0Auth)
|
apiV0EndpointTest := handlersV0.NewTestEndpoint(apiV0Auth)
|
||||||
|
apiV0EndpointWebsocket := handlersV0.NewWebsocketEndpoint(cfg, apiV0Auth, eventBus)
|
||||||
|
|
||||||
apiFrontend := handlersV0.NewRestApi(apiV0Session,
|
apiFrontend := handlersV0.NewRestApi(apiV0Session,
|
||||||
apiV0EndpointAuth,
|
apiV0EndpointAuth,
|
||||||
@@ -144,6 +145,7 @@ func main() {
|
|||||||
apiV0EndpointPeers,
|
apiV0EndpointPeers,
|
||||||
apiV0EndpointConfig,
|
apiV0EndpointConfig,
|
||||||
apiV0EndpointTest,
|
apiV0EndpointTest,
|
||||||
|
apiV0EndpointWebsocket,
|
||||||
)
|
)
|
||||||
|
|
||||||
// endregion API v0 (SPA frontend)
|
// endregion API v0 (SPA frontend)
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ spec:
|
|||||||
{{- with .Values.revisionHistoryLimit }}
|
{{- with .Values.revisionHistoryLimit }}
|
||||||
revisionHistoryLimit: {{ . }}
|
revisionHistoryLimit: {{ . }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
{{- with .Values.replicas }}
|
||||||
|
replicas: {{ . }}
|
||||||
|
{{- end }}
|
||||||
{{- with .Values.strategy }}
|
{{- with .Values.strategy }}
|
||||||
strategy: {{- toYaml . | nindent 4 }}
|
strategy: {{- toYaml . | nindent 4 }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ spec:
|
|||||||
http:
|
http:
|
||||||
paths:
|
paths:
|
||||||
- path: {{ default "/" (urlParse (tpl .Values.config.web.external_url .)).path }}
|
- path: {{ default "/" (urlParse (tpl .Values.config.web.external_url .)).path }}
|
||||||
pathType: {{ default "ImplementationSpecific" .pathType }}
|
pathType: {{ default "ImplementationSpecific" .Values.ingress.pathType }}
|
||||||
backend:
|
backend:
|
||||||
service:
|
service:
|
||||||
name: {{ include "wg-portal.fullname" . }}
|
name: {{ include "wg-portal.fullname" . }}
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ spec:
|
|||||||
{{- with .Values.revisionHistoryLimit }}
|
{{- with .Values.revisionHistoryLimit }}
|
||||||
revisionHistoryLimit: {{ . }}
|
revisionHistoryLimit: {{ . }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
{{- with .Values.replicas }}
|
||||||
|
replicas: {{ . }}
|
||||||
|
{{- end }}
|
||||||
{{- with .Values.strategy }}
|
{{- with .Values.strategy }}
|
||||||
updateStrategy: {{- toYaml . | nindent 4 }}
|
updateStrategy: {{- toYaml . | nindent 4 }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|||||||
@@ -157,12 +157,14 @@ More advanced options are found in the subsequent `Advanced` section.
|
|||||||
### `create_default_peer`
|
### `create_default_peer`
|
||||||
- **Default:** `false`
|
- **Default:** `false`
|
||||||
- **Environment Variable:** `WG_PORTAL_CORE_CREATE_DEFAULT_PEER`
|
- **Environment Variable:** `WG_PORTAL_CORE_CREATE_DEFAULT_PEER`
|
||||||
- **Description:** If a user logs in for the first time with no existing peers, automatically create a new WireGuard peer for **all** server interfaces.
|
- **Description:** If a user logs in for the first time with no existing peers, automatically create a new WireGuard peer for all server interfaces where the "Create default peer" flag is set.
|
||||||
|
- **Important:** This option is only effective for interfaces where the "Create default peer" flag is set (via the UI).
|
||||||
|
|
||||||
### `create_default_peer_on_creation`
|
### `create_default_peer_on_creation`
|
||||||
- **Default:** `false`
|
- **Default:** `false`
|
||||||
- **Environment Variable:** `WG_PORTAL_CORE_CREATE_DEFAULT_PEER_ON_CREATION`
|
- **Environment Variable:** `WG_PORTAL_CORE_CREATE_DEFAULT_PEER_ON_CREATION`
|
||||||
- **Description:** If an LDAP user is created (e.g., through LDAP sync) and has no peers, automatically create a new WireGuard peer for **all** server interfaces.
|
- **Description:** If an LDAP user is created (e.g., through LDAP sync) and has no peers, automatically create a new WireGuard peer for all server interfaces where the "Create default peer" flag is set.
|
||||||
|
- **Important:** This option requires [create_default_peer](#create_default_peer) to be enabled.
|
||||||
|
|
||||||
### `re_enable_peer_after_user_enable`
|
### `re_enable_peer_after_user_enable`
|
||||||
- **Default:** `true`
|
- **Default:** `true`
|
||||||
|
|||||||
@@ -443,6 +443,18 @@ definitions:
|
|||||||
maxLength: 64
|
maxLength: 64
|
||||||
minLength: 32
|
minLength: 32
|
||||||
type: string
|
type: string
|
||||||
|
AuthSources:
|
||||||
|
description: The source of the user. This field is optional.
|
||||||
|
example:
|
||||||
|
- db
|
||||||
|
items:
|
||||||
|
enum:
|
||||||
|
- db
|
||||||
|
- ldap
|
||||||
|
- oauth
|
||||||
|
type: string
|
||||||
|
readOnly: true
|
||||||
|
type: array
|
||||||
Department:
|
Department:
|
||||||
description: The department of the user. This field is optional.
|
description: The department of the user. This field is optional.
|
||||||
example: Software Development
|
example: Software Development
|
||||||
@@ -503,19 +515,6 @@ definitions:
|
|||||||
description: The phone number of the user. This field is optional.
|
description: The phone number of the user. This field is optional.
|
||||||
example: "+1234546789"
|
example: "+1234546789"
|
||||||
type: string
|
type: string
|
||||||
ProviderName:
|
|
||||||
description: The name of the authentication provider. This field is read-only.
|
|
||||||
example: ""
|
|
||||||
readOnly: true
|
|
||||||
type: string
|
|
||||||
Source:
|
|
||||||
description: The source of the user. This field is optional.
|
|
||||||
enum:
|
|
||||||
- db
|
|
||||||
- ldap
|
|
||||||
- oauth
|
|
||||||
example: db
|
|
||||||
type: string
|
|
||||||
required:
|
required:
|
||||||
- Identifier
|
- Identifier
|
||||||
type: object
|
type: object
|
||||||
|
|||||||
152
docs/documentation/usage/authentication.md
Normal file
152
docs/documentation/usage/authentication.md
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
WireGuard Portal supports multiple authentication mechanisms to manage user access. This includes
|
||||||
|
|
||||||
|
- Local user accounts
|
||||||
|
- LDAP authentication
|
||||||
|
- OAuth2 and OIDC authentication
|
||||||
|
- Passkey authentication (WebAuthn)
|
||||||
|
|
||||||
|
Users can have two roles which limit their permissions in WireGuard Portal:
|
||||||
|
|
||||||
|
- **User**: Can manage their own account and peers.
|
||||||
|
- **Admin**: Can manage all users and peers, including the ability to manage WireGuard interfaces.
|
||||||
|
|
||||||
|
In general, each user is identified by a _unique identifier_. If the same user identifier exists across multiple authentication sources, WireGuard Portal automatically merges those accounts into a single user record.
|
||||||
|
When a user is associated with multiple authentication sources, their information in WireGuard Portal is updated based on the most recently logged-in source. For more details, see [User Synchronization](./user-sync.md) documentation.
|
||||||
|
|
||||||
|
## Password Authentication
|
||||||
|
|
||||||
|
WireGuard Portal supports username and password authentication for both local and LDAP-backed accounts.
|
||||||
|
Local users are stored in the database, while LDAP users are authenticated against an external LDAP server.
|
||||||
|
|
||||||
|
On initial startup, WireGuard Portal automatically creates a local admin account with the password `wgportal-default`.
|
||||||
|
> :warning: This password must be changed immediately after the first login.
|
||||||
|
|
||||||
|
The minimum password length for all local users can be configured in the [`auth`](../configuration/overview.md#auth)
|
||||||
|
section of the configuration file. The default value is **16** characters, see [`min_password_length`](../configuration/overview.md#min_password_length).
|
||||||
|
The minimum password length is also enforced for the default admin user.
|
||||||
|
|
||||||
|
|
||||||
|
## Passkey (WebAuthn) Authentication
|
||||||
|
|
||||||
|
Besides the standard authentication mechanisms, WireGuard Portal supports Passkey authentication.
|
||||||
|
This feature is enabled by default and can be configured in the [`webauthn`](../configuration/overview.md#webauthn-passkeys) section of the configuration file.
|
||||||
|
|
||||||
|
Users can register multiple Passkeys to their account. These Passkeys can be used to log in to the web UI as long as the user is not locked.
|
||||||
|
> :warning: Passkey authentication does not disable password authentication. The password can still be used to log in (e.g., as a fallback).
|
||||||
|
|
||||||
|
To register a Passkey, open the settings page *(1)* in the web UI and click on the "Register Passkey" *(2)* button.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
## OAuth2 and OIDC Authentication
|
||||||
|
|
||||||
|
WireGuard Portal supports OAuth2 and OIDC authentication. You can use any OAuth2 or OIDC provider that supports the authorization code flow,
|
||||||
|
such as Google, GitHub, or Keycloak.
|
||||||
|
|
||||||
|
For OAuth2 or OIDC to work, you need to configure the [`external_url`](../configuration/overview.md#external_url) property in the [`web`](../configuration/overview.md#web) section of the configuration file.
|
||||||
|
If you are planning to expose the portal to the internet, make sure that the `external_url` is configured to use HTTPS.
|
||||||
|
|
||||||
|
To add OIDC or OAuth2 authentication to WireGuard Portal, create a Client-ID and Client-Secret in your OAuth2 provider and
|
||||||
|
configure a new authentication provider in the [`auth`](../configuration/overview.md#auth) section of the configuration file.
|
||||||
|
Make sure that each configured provider has a unique `provider_name` property set. Samples can be seen [here](../configuration/examples.md).
|
||||||
|
|
||||||
|
#### Limiting Login to Specific Domains
|
||||||
|
|
||||||
|
You can limit the login to specific domains by setting the `allowed_domains` property for OAuth2 or OIDC providers.
|
||||||
|
This property is a comma-separated list of domains that are allowed to log in. The user's email address is checked against this list.
|
||||||
|
For example, if you want to allow only users with an email address ending in `outlook.com` to log in, set the property as follows:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
auth:
|
||||||
|
oidc:
|
||||||
|
- provider_name: "oidc1"
|
||||||
|
# ... other settings
|
||||||
|
allowed_domains:
|
||||||
|
- "outlook.com"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Limit Login to Existing Users
|
||||||
|
|
||||||
|
You can limit the login to existing users only by setting the `registration_enabled` property to `false` for OAuth2 or OIDC providers.
|
||||||
|
If registration is enabled, new users will be created in the database when they log in for the first time.
|
||||||
|
|
||||||
|
#### Admin Mapping
|
||||||
|
|
||||||
|
You can map users to admin roles based on their attributes in the OAuth2 or OIDC provider. To do this, set the `admin_mapping` property for the provider.
|
||||||
|
Administrative access can either be mapped by a specific attribute or by group membership.
|
||||||
|
|
||||||
|
**Attribute specific mapping** can be achieved by setting the `admin_value_regex` and the `is_admin` property.
|
||||||
|
The `admin_value_regex` property is a regular expression that is matched against the value of the `is_admin` attribute.
|
||||||
|
The user is granted admin access if the regex matches the attribute value.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```yaml
|
||||||
|
auth:
|
||||||
|
oidc:
|
||||||
|
- provider_name: "oidc1"
|
||||||
|
# ... other settings
|
||||||
|
field_map:
|
||||||
|
is_admin: "wg_admin_prop"
|
||||||
|
admin_mapping:
|
||||||
|
admin_value_regex: "^true$"
|
||||||
|
```
|
||||||
|
The example above will grant admin access to users with the `wg_admin_prop` attribute set to `true`.
|
||||||
|
|
||||||
|
**Group membership mapping** can be achieved by setting the `admin_group_regex` and `user_groups` property.
|
||||||
|
The `admin_group_regex` property is a regular expression that is matched against the group names of the user.
|
||||||
|
The user is granted admin access if the regex matches any of the group names.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
```yaml
|
||||||
|
auth:
|
||||||
|
oidc:
|
||||||
|
- provider_name: "oidc1"
|
||||||
|
# ... other settings
|
||||||
|
field_map:
|
||||||
|
user_groups: "groups"
|
||||||
|
admin_mapping:
|
||||||
|
admin_group_regex: "^the-admin-group$"
|
||||||
|
```
|
||||||
|
The example above will grant admin access to users who are members of the `the-admin-group` group.
|
||||||
|
|
||||||
|
|
||||||
|
## LDAP Authentication
|
||||||
|
|
||||||
|
WireGuard Portal supports LDAP authentication. You can use any LDAP server that supports the LDAP protocol, such as Active Directory or OpenLDAP.
|
||||||
|
Multiple LDAP servers can be configured in the [`auth`](../configuration/overview.md#auth) section of the configuration file.
|
||||||
|
WireGuard Portal remembers the authentication provider of the user and therefore avoids conflicts between multiple LDAP providers.
|
||||||
|
|
||||||
|
To configure LDAP authentication, create a new [`ldap`](../configuration/overview.md#ldap) authentication provider in the [`auth`](../configuration/overview.md#auth) section of the configuration file.
|
||||||
|
|
||||||
|
### Limiting Login to Specific Users
|
||||||
|
|
||||||
|
You can limit the login to specific users by setting the `login_filter` property for LDAP provider. This filter uses the LDAP search filter syntax.
|
||||||
|
The username can be inserted into the query by placing the `{{login_identifier}}` placeholder in the filter. This placeholder will then be replaced with the username entered by the user during login.
|
||||||
|
|
||||||
|
For example, if you want to allow only users with the `objectClass` attribute set to `organizationalPerson` to log in, set the property as follows:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
auth:
|
||||||
|
ldap:
|
||||||
|
- provider_name: "ldap1"
|
||||||
|
# ... other settings
|
||||||
|
login_filter: "(&(objectClass=organizationalPerson)(uid={{login_identifier}}))"
|
||||||
|
```
|
||||||
|
|
||||||
|
The `login_filter` should always be designed to return at most one user.
|
||||||
|
|
||||||
|
### Limit Login to Existing Users
|
||||||
|
|
||||||
|
You can limit the login to existing users only by setting the `registration_enabled` property to `false` for LDAP providers.
|
||||||
|
If registration is enabled, new users will be created in the database when they log in for the first time.
|
||||||
|
|
||||||
|
### Admin Mapping
|
||||||
|
|
||||||
|
You can map users to admin roles based on their group membership in the LDAP server. To do this, set the `admin_group` and `memberof` property for the provider.
|
||||||
|
The `admin_group` property defines the distinguished name of the group that is allowed to log in as admin.
|
||||||
|
All groups that are listed in the `memberof` attribute of the user will be checked against this group. If one of the groups matches, the user is granted admin access.
|
||||||
|
|
||||||
|
|
||||||
|
## User Synchronization
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ WireGuard Interfaces can be categorized into three types:
|
|||||||
## Accessing the Web UI
|
## Accessing the Web UI
|
||||||
|
|
||||||
The web UI should be accessed via the URL specified in the `external_url` property of the configuration file.
|
The web UI should be accessed via the URL specified in the `external_url` property of the configuration file.
|
||||||
By default, WireGuard Portal listens on port `8888` for HTTP connections. Check the [Security](security.md) section for more information on securing the web UI.
|
By default, WireGuard Portal listens on port `8888` for HTTP connections. Check the [Security](security.md) or [Authentication](authentication.md) sections for more information on securing the web UI.
|
||||||
|
|
||||||
So the default URL to access the web UI is:
|
So the default URL to access the web UI is:
|
||||||
|
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
WireGuard Portal lets you hook up any LDAP server such as Active Directory or OpenLDAP for both authentication and user sync.
|
|
||||||
You can even register multiple LDAP servers side-by-side. When someone logs in via LDAP, their specific provider is remembered,
|
|
||||||
so there's no risk of cross-provider conflicts. Details on the log-in process can be found in the [Security](security.md#ldap-authentication) documentation.
|
|
||||||
|
|
||||||
If you enable LDAP synchronization, all users within the LDAP directory will be created automatically in the WireGuard Portal database if they do not exist.
|
|
||||||
If a user is disabled or deleted in LDAP, the user will be disabled in WireGuard Portal as well.
|
|
||||||
The synchronization process can be fine-tuned by multiple parameters, which are described below.
|
|
||||||
|
|
||||||
## LDAP Synchronization
|
|
||||||
|
|
||||||
WireGuard Portal can automatically synchronize users from LDAP to the database.
|
|
||||||
To enable this feature, set the `sync_interval` property in the LDAP provider configuration to a value greater than "0".
|
|
||||||
The value is a string representing a duration, such as "15m" for 15 minutes or "1h" for 1 hour (check the [exact format definition](https://pkg.go.dev/time#ParseDuration) for details).
|
|
||||||
The synchronization process will run in the background and synchronize users from LDAP to the database at the specified interval.
|
|
||||||
Also make sure that the `sync_filter` property is a well-formed LDAP filter, or synchronization will fail.
|
|
||||||
|
|
||||||
### Limiting Synchronization to Specific Users
|
|
||||||
|
|
||||||
Use the `sync_filter` property in your LDAP provider block to restrict which users get synchronized.
|
|
||||||
It accepts any valid LDAP search filter, only entries matching that filter will be pulled into the portal's database.
|
|
||||||
|
|
||||||
For example, to import only users with a `mail` attribute:
|
|
||||||
```yaml
|
|
||||||
auth:
|
|
||||||
ldap:
|
|
||||||
- id: ldap
|
|
||||||
# ... other settings
|
|
||||||
sync_filter: (mail=*)
|
|
||||||
```
|
|
||||||
|
|
||||||
### Disable Missing Users
|
|
||||||
|
|
||||||
If you set the `disable_missing` property to `true`, any user that is not found in LDAP during synchronization will be disabled in WireGuard Portal.
|
|
||||||
All peers associated with that user will also be disabled.
|
|
||||||
|
|
||||||
If you want a user and its peers to be automatically re-enabled once they are found in LDAP again, set the `auto_re_enable` property to `true`.
|
|
||||||
This will only re-enable the user if they where disabled by the synchronization process. Manually disabled users will not be re-enabled.
|
|
||||||
@@ -1,153 +1,12 @@
|
|||||||
This section describes the security features available to administrators for hardening WireGuard Portal and protecting its data.
|
This section describes the security features available to administrators for hardening WireGuard Portal and protecting its data.
|
||||||
|
|
||||||
## Authentication
|
## Database Encryption
|
||||||
|
|
||||||
WireGuard Portal supports multiple authentication methods, including:
|
WireGuard Portal supports multiple database backends. To reduce the risk of data exposure, sensitive information stored in the database can be encrypted.
|
||||||
|
To enable encryption, set the [`encryption_passphrase`](../configuration/overview.md#database) in the database configuration section.
|
||||||
- Local user accounts
|
|
||||||
- LDAP authentication
|
|
||||||
- OAuth and OIDC authentication
|
|
||||||
- Passkey authentication (WebAuthn)
|
|
||||||
|
|
||||||
Users can have two roles which limit their permissions in WireGuard Portal:
|
|
||||||
|
|
||||||
- **User**: Can manage their own account and peers.
|
|
||||||
- **Admin**: Can manage all users and peers, including the ability to manage WireGuard interfaces.
|
|
||||||
|
|
||||||
### Password Security
|
|
||||||
|
|
||||||
WireGuard Portal supports username and password authentication for both local and LDAP-backed accounts.
|
|
||||||
Local users are stored in the database, while LDAP users are authenticated against an external LDAP server.
|
|
||||||
|
|
||||||
On initial startup, WireGuard Portal automatically creates a local admin account with the password `wgportal-default`.
|
|
||||||
> :warning: This password must be changed immediately after the first login.
|
|
||||||
|
|
||||||
The minimum password length for all local users can be configured in the [`auth`](../configuration/overview.md#auth)
|
|
||||||
section of the configuration file. The default value is **16** characters, see [`min_password_length`](../configuration/overview.md#min_password_length).
|
|
||||||
The minimum password length is also enforced for the default admin user.
|
|
||||||
|
|
||||||
|
|
||||||
### Passkey (WebAuthn) Authentication
|
|
||||||
|
|
||||||
Besides the standard authentication mechanisms, WireGuard Portal supports Passkey authentication.
|
|
||||||
This feature is enabled by default and can be configured in the [`webauthn`](../configuration/overview.md#webauthn-passkeys) section of the configuration file.
|
|
||||||
|
|
||||||
Users can register multiple Passkeys to their account. These Passkeys can be used to log in to the web UI as long as the user is not locked.
|
|
||||||
> :warning: Passkey authentication does not disable password authentication. The password can still be used to log in (e.g., as a fallback).
|
|
||||||
|
|
||||||
To register a Passkey, open the settings page *(1)* in the web UI and click on the "Register Passkey" *(2)* button.
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
|
|
||||||
### OAuth and OIDC Authentication
|
|
||||||
|
|
||||||
WireGuard Portal supports OAuth and OIDC authentication. You can use any OAuth or OIDC provider that supports the authorization code flow,
|
|
||||||
such as Google, GitHub, or Keycloak.
|
|
||||||
|
|
||||||
For OAuth or OIDC to work, you need to configure the [`external_url`](../configuration/overview.md#external_url) property in the [`web`](../configuration/overview.md#web) section of the configuration file.
|
|
||||||
If you are planning to expose the portal to the internet, make sure that the `external_url` is configured to use HTTPS.
|
|
||||||
|
|
||||||
To add OIDC or OAuth authentication to WireGuard Portal, create a Client-ID and Client-Secret in your OAuth provider and
|
|
||||||
configure a new authentication provider in the [`auth`](../configuration/overview.md#auth) section of the configuration file.
|
|
||||||
Make sure that each configured provider has a unique `provider_name` property set. Samples can be seen [here](../configuration/examples.md).
|
|
||||||
|
|
||||||
#### Limiting Login to Specific Domains
|
|
||||||
|
|
||||||
You can limit the login to specific domains by setting the `allowed_domains` property for OAuth or OIDC providers.
|
|
||||||
This property is a comma-separated list of domains that are allowed to log in. The user's email address is checked against this list.
|
|
||||||
For example, if you want to allow only users with an email address ending in `outlook.com` to log in, set the property as follows:
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
auth:
|
|
||||||
oidc:
|
|
||||||
- provider_name: "oidc1"
|
|
||||||
# ... other settings
|
|
||||||
allowed_domains:
|
|
||||||
- "outlook.com"
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Limit Login to Existing Users
|
|
||||||
|
|
||||||
You can limit the login to existing users only by setting the `registration_enabled` property to `false` for OAuth or OIDC providers.
|
|
||||||
If registration is enabled, new users will be created in the database when they log in for the first time.
|
|
||||||
|
|
||||||
#### Admin Mapping
|
|
||||||
|
|
||||||
You can map users to admin roles based on their attributes in the OAuth or OIDC provider. To do this, set the `admin_mapping` property for the provider.
|
|
||||||
Administrative access can either be mapped by a specific attribute or by group membership.
|
|
||||||
|
|
||||||
**Attribute specific mapping** can be achieved by setting the `admin_value_regex` and the `is_admin` property.
|
|
||||||
The `admin_value_regex` property is a regular expression that is matched against the value of the `is_admin` attribute.
|
|
||||||
The user is granted admin access if the regex matches the attribute value.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
```yaml
|
|
||||||
auth:
|
|
||||||
oidc:
|
|
||||||
- provider_name: "oidc1"
|
|
||||||
# ... other settings
|
|
||||||
field_map:
|
|
||||||
is_admin: "wg_admin_prop"
|
|
||||||
admin_mapping:
|
|
||||||
admin_value_regex: "^true$"
|
|
||||||
```
|
|
||||||
The example above will grant admin access to users with the `wg_admin_prop` attribute set to `true`.
|
|
||||||
|
|
||||||
**Group membership mapping** can be achieved by setting the `admin_group_regex` and `user_groups` property.
|
|
||||||
The `admin_group_regex` property is a regular expression that is matched against the group names of the user.
|
|
||||||
The user is granted admin access if the regex matches any of the group names.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
```yaml
|
|
||||||
auth:
|
|
||||||
oidc:
|
|
||||||
- provider_name: "oidc1"
|
|
||||||
# ... other settings
|
|
||||||
field_map:
|
|
||||||
user_groups: "groups"
|
|
||||||
admin_mapping:
|
|
||||||
admin_group_regex: "^the-admin-group$"
|
|
||||||
```
|
|
||||||
The example above will grant admin access to users who are members of the `the-admin-group` group.
|
|
||||||
|
|
||||||
|
|
||||||
### LDAP Authentication
|
|
||||||
|
|
||||||
WireGuard Portal supports LDAP authentication. You can use any LDAP server that supports the LDAP protocol, such as Active Directory or OpenLDAP.
|
|
||||||
Multiple LDAP servers can be configured in the [`auth`](../configuration/overview.md#auth) section of the configuration file.
|
|
||||||
WireGuard Portal remembers the authentication provider of the user and therefore avoids conflicts between multiple LDAP providers.
|
|
||||||
|
|
||||||
To configure LDAP authentication, create a new [`ldap`](../configuration/overview.md#ldap) authentication provider in the [`auth`](../configuration/overview.md#auth) section of the configuration file.
|
|
||||||
|
|
||||||
#### Limiting Login to Specific Users
|
|
||||||
|
|
||||||
You can limit the login to specific users by setting the `login_filter` property for LDAP provider. This filter uses the LDAP search filter syntax.
|
|
||||||
The username can be inserted into the query by placing the `{{login_identifier}}` placeholder in the filter. This placeholder will then be replaced with the username entered by the user during login.
|
|
||||||
|
|
||||||
For example, if you want to allow only users with the `objectClass` attribute set to `organizationalPerson` to log in, set the property as follows:
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
auth:
|
|
||||||
ldap:
|
|
||||||
- provider_name: "ldap1"
|
|
||||||
# ... other settings
|
|
||||||
login_filter: "(&(objectClass=organizationalPerson)(uid={{login_identifier}}))"
|
|
||||||
```
|
|
||||||
|
|
||||||
The `login_filter` should always be designed to return at most one user.
|
|
||||||
|
|
||||||
#### Limit Login to Existing Users
|
|
||||||
|
|
||||||
You can limit the login to existing users only by setting the `registration_enabled` property to `false` for LDAP providers.
|
|
||||||
If registration is enabled, new users will be created in the database when they log in for the first time.
|
|
||||||
|
|
||||||
#### Admin Mapping
|
|
||||||
|
|
||||||
You can map users to admin roles based on their group membership in the LDAP server. To do this, set the `admin_group` and `memberof` property for the provider.
|
|
||||||
The `admin_group` property defines the distinguished name of the group that is allowed to log in as admin.
|
|
||||||
All groups that are listed in the `memberof` attribute of the user will be checked against this group. If one of the groups matches, the user is granted admin access.
|
|
||||||
|
|
||||||
|
> :warning: Important: Once encryption is enabled, it cannot be disabled, and the passphrase cannot be changed!
|
||||||
|
> Only new or updated records will be encrypted; existing data remains in plaintext until it’s next modified.
|
||||||
|
|
||||||
## UI and API Access
|
## UI and API Access
|
||||||
|
|
||||||
@@ -158,3 +17,8 @@ It is recommended to use HTTPS for all communication with the portal to prevent
|
|||||||
|
|
||||||
Event though, WireGuard Portal supports HTTPS out of the box, it is recommended to use a reverse proxy like Nginx or Traefik to handle SSL termination and other security features.
|
Event though, WireGuard Portal supports HTTPS out of the box, it is recommended to use a reverse proxy like Nginx or Traefik to handle SSL termination and other security features.
|
||||||
A detailed explanation is available in the [Reverse Proxy](../getting-started/reverse-proxy.md) section.
|
A detailed explanation is available in the [Reverse Proxy](../getting-started/reverse-proxy.md) section.
|
||||||
|
|
||||||
|
### Secure Authentication
|
||||||
|
To prevent unauthorized access, WireGuard Portal supports integrating with secure authentication providers such as LDAP, OAuth2, or Passkeys, see [Authentication](./authentication.md) for more details.
|
||||||
|
When possible, use centralized authentication and enforce multi-factor authentication (MFA) at the provider level for enhanced account security.
|
||||||
|
For local accounts, administrators should enforce strong password requirements.
|
||||||
46
docs/documentation/usage/user-sync.md
Normal file
46
docs/documentation/usage/user-sync.md
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
For all external authentication providers (LDAP, OIDC, OAuth2), WireGuard Portal can automatically create a local user record upon the user's first successful login.
|
||||||
|
This behavior is controlled by the `registration_enabled` setting in each authentication provider's configuration.
|
||||||
|
|
||||||
|
User information from external authentication sources is merged into the corresponding local WireGuard Portal user record whenever the user logs in.
|
||||||
|
Additionally, WireGuard Portal supports periodic synchronization of user data from an LDAP directory.
|
||||||
|
|
||||||
|
To prevent overwriting local changes, WireGuard Portal allows you to set a per-user flag that disables synchronization of external attributes.
|
||||||
|
When this flag is set, the user in WireGuard Portal will not be updated automatically during log-ins or LDAP synchronization.
|
||||||
|
|
||||||
|
### LDAP Synchronization
|
||||||
|
|
||||||
|
WireGuard Portal lets you hook up any LDAP server such as Active Directory or OpenLDAP for both authentication and user sync.
|
||||||
|
You can even register multiple LDAP servers side-by-side. Details on the log-in process can be found in the [LDAP Authentication](./authentication.md#ldap-authentication) section.
|
||||||
|
|
||||||
|
If you enable LDAP synchronization, all users within the LDAP directory will be created automatically in the WireGuard Portal database if they do not exist.
|
||||||
|
If a user is disabled or deleted in LDAP, the user will be disabled in WireGuard Portal as well.
|
||||||
|
The synchronization process can be fine-tuned by multiple parameters, which are described below.
|
||||||
|
|
||||||
|
#### Synchronization Parameters
|
||||||
|
|
||||||
|
To enable the LDAP sycnhronization this feature, set the `sync_interval` property in the LDAP provider configuration to a value greater than "0".
|
||||||
|
The value is a string representing a duration, such as "15m" for 15 minutes or "1h" for 1 hour (check the [exact format definition](https://pkg.go.dev/time#ParseDuration) for details).
|
||||||
|
The synchronization process will run in the background and synchronize users from LDAP to the database at the specified interval.
|
||||||
|
Also make sure that the `sync_filter` property is a well-formed LDAP filter, or synchronization will fail.
|
||||||
|
|
||||||
|
##### Limiting Synchronization to Specific Users
|
||||||
|
|
||||||
|
Use the `sync_filter` property in your LDAP provider block to restrict which users get synchronized.
|
||||||
|
It accepts any valid LDAP search filter, only entries matching that filter will be pulled into the portal's database.
|
||||||
|
|
||||||
|
For example, to import only users with a `mail` attribute:
|
||||||
|
```yaml
|
||||||
|
auth:
|
||||||
|
ldap:
|
||||||
|
- id: ldap
|
||||||
|
# ... other settings
|
||||||
|
sync_filter: (mail=*)
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Disable Missing Users
|
||||||
|
|
||||||
|
If you set the `disable_missing` property to `true`, any user that is not found in LDAP during synchronization will be disabled in WireGuard Portal.
|
||||||
|
All peers associated with that user will also be disabled.
|
||||||
|
|
||||||
|
If you want a user and its peers to be automatically re-enabled once they are found in LDAP again, set the `auto_re_enable` property to `true`.
|
||||||
|
This will only re-enable the user if they were disabled by the synchronization process. Manually disabled users will not be re-enabled.
|
||||||
@@ -68,26 +68,32 @@ All payload models are encoded as JSON objects. Fields with empty values might b
|
|||||||
|
|
||||||
#### User Payload (entity: `user`)
|
#### User Payload (entity: `user`)
|
||||||
|
|
||||||
| JSON Field | Type | Description |
|
| JSON Field | Type | Description |
|
||||||
|----------------|-------------|-----------------------------------|
|
|----------------|---------------|-----------------------------------|
|
||||||
| CreatedBy | string | Creator identifier |
|
| CreatedBy | string | Creator identifier |
|
||||||
| UpdatedBy | string | Last updater identifier |
|
| UpdatedBy | string | Last updater identifier |
|
||||||
| CreatedAt | time.Time | Time of creation |
|
| CreatedAt | time.Time | Time of creation |
|
||||||
| UpdatedAt | time.Time | Time of last update |
|
| UpdatedAt | time.Time | Time of last update |
|
||||||
| Identifier | string | Unique user identifier |
|
| Identifier | string | Unique user identifier |
|
||||||
| Email | string | User email |
|
| Email | string | User email |
|
||||||
| Source | string | Authentication source |
|
| AuthSources | []AuthSource | Authentication sources |
|
||||||
| ProviderName | string | Name of auth provider |
|
| IsAdmin | bool | Whether user has admin privileges |
|
||||||
| IsAdmin | bool | Whether user has admin privileges |
|
| Firstname | string | User's first name (optional) |
|
||||||
| Firstname | string | User's first name (optional) |
|
| Lastname | string | User's last name (optional) |
|
||||||
| Lastname | string | User's last name (optional) |
|
| Phone | string | Contact phone number (optional) |
|
||||||
| Phone | string | Contact phone number (optional) |
|
| Department | string | User's department (optional) |
|
||||||
| Department | string | User's department (optional) |
|
| Notes | string | Additional notes (optional) |
|
||||||
| Notes | string | Additional notes (optional) |
|
| Disabled | *time.Time | When user was disabled |
|
||||||
| Disabled | *time.Time | When user was disabled |
|
| DisabledReason | string | Reason for deactivation |
|
||||||
| DisabledReason | string | Reason for deactivation |
|
| Locked | *time.Time | When user account was locked |
|
||||||
| Locked | *time.Time | When user account was locked |
|
| LockedReason | string | Reason for being locked |
|
||||||
| LockedReason | string | Reason for being locked |
|
|
||||||
|
`AuthSource`:
|
||||||
|
|
||||||
|
| JSON Field | Type | Description |
|
||||||
|
|--------------|---------------|-----------------------------------------------------|
|
||||||
|
| Source | string | The authentication source (e.g. LDAP, OAuth, or DB) |
|
||||||
|
| ProviderName | string | The identifier of the authentication provider |
|
||||||
|
|
||||||
|
|
||||||
#### Peer Payload (entity: `peer`)
|
#### Peer Payload (entity: `peer`)
|
||||||
|
|||||||
@@ -83,6 +83,7 @@ watch(() => props.visible, async (newValue, oldValue) => {
|
|||||||
formData.value.Identifier = interfaces.Prepared.Identifier
|
formData.value.Identifier = interfaces.Prepared.Identifier
|
||||||
formData.value.DisplayName = interfaces.Prepared.DisplayName
|
formData.value.DisplayName = interfaces.Prepared.DisplayName
|
||||||
formData.value.Mode = interfaces.Prepared.Mode
|
formData.value.Mode = interfaces.Prepared.Mode
|
||||||
|
formData.value.CreateDefaultPeer = interfaces.Prepared.CreateDefaultPeer
|
||||||
formData.value.Backend = interfaces.Prepared.Backend
|
formData.value.Backend = interfaces.Prepared.Backend
|
||||||
|
|
||||||
formData.value.PublicKey = interfaces.Prepared.PublicKey
|
formData.value.PublicKey = interfaces.Prepared.PublicKey
|
||||||
@@ -122,6 +123,7 @@ watch(() => props.visible, async (newValue, oldValue) => {
|
|||||||
formData.value.Identifier = selectedInterface.value.Identifier
|
formData.value.Identifier = selectedInterface.value.Identifier
|
||||||
formData.value.DisplayName = selectedInterface.value.DisplayName
|
formData.value.DisplayName = selectedInterface.value.DisplayName
|
||||||
formData.value.Mode = selectedInterface.value.Mode
|
formData.value.Mode = selectedInterface.value.Mode
|
||||||
|
formData.value.CreateDefaultPeer = selectedInterface.value.CreateDefaultPeer
|
||||||
formData.value.Backend = selectedInterface.value.Backend
|
formData.value.Backend = selectedInterface.value.Backend
|
||||||
|
|
||||||
formData.value.PublicKey = selectedInterface.value.PublicKey
|
formData.value.PublicKey = selectedInterface.value.PublicKey
|
||||||
@@ -487,6 +489,10 @@ async function del() {
|
|||||||
<input v-model="formData.Disabled" class="form-check-input" type="checkbox">
|
<input v-model="formData.Disabled" class="form-check-input" type="checkbox">
|
||||||
<label class="form-check-label">{{ $t('modals.interface-edit.disabled.label') }}</label>
|
<label class="form-check-label">{{ $t('modals.interface-edit.disabled.label') }}</label>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="form-check form-switch" v-if="formData.Mode==='server' && settings.Setting('CreateDefaultPeer')">
|
||||||
|
<input v-model="formData.CreateDefaultPeer" class="form-check-input" type="checkbox">
|
||||||
|
<label class="form-check-label">{{ $t('modals.interface-edit.create-default-peer.label') }}</label>
|
||||||
|
</div>
|
||||||
<div class="form-check form-switch" v-if="formData.Backend==='local'">
|
<div class="form-check form-switch" v-if="formData.Backend==='local'">
|
||||||
<input v-model="formData.SaveConfig" checked="" class="form-check-input" type="checkbox">
|
<input v-model="formData.SaveConfig" checked="" class="form-check-input" type="checkbox">
|
||||||
<label class="form-check-label">{{ $t('modals.interface-edit.save-config.label') }}</label>
|
<label class="form-check-label">{{ $t('modals.interface-edit.save-config.label') }}</label>
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ const passwordWeak = computed(() => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const formValid = computed(() => {
|
const formValid = computed(() => {
|
||||||
if (formData.value.Source !== 'db') {
|
if (!formData.value.AuthSources.some(s => s === 'db')) {
|
||||||
return true // nothing to validate
|
return true // nothing to validate
|
||||||
}
|
}
|
||||||
if (props.userId !== '#NEW#' && passwordWeak.value) {
|
if (props.userId !== '#NEW#' && passwordWeak.value) {
|
||||||
@@ -70,7 +70,7 @@ watch(() => props.visible, async (newValue, oldValue) => {
|
|||||||
} else { // fill existing userdata
|
} else { // fill existing userdata
|
||||||
formData.value.Identifier = selectedUser.value.Identifier
|
formData.value.Identifier = selectedUser.value.Identifier
|
||||||
formData.value.Email = selectedUser.value.Email
|
formData.value.Email = selectedUser.value.Email
|
||||||
formData.value.Source = selectedUser.value.Source
|
formData.value.AuthSources = selectedUser.value.AuthSources
|
||||||
formData.value.IsAdmin = selectedUser.value.IsAdmin
|
formData.value.IsAdmin = selectedUser.value.IsAdmin
|
||||||
formData.value.Firstname = selectedUser.value.Firstname
|
formData.value.Firstname = selectedUser.value.Firstname
|
||||||
formData.value.Lastname = selectedUser.value.Lastname
|
formData.value.Lastname = selectedUser.value.Lastname
|
||||||
@@ -80,6 +80,7 @@ watch(() => props.visible, async (newValue, oldValue) => {
|
|||||||
formData.value.Password = ""
|
formData.value.Password = ""
|
||||||
formData.value.Disabled = selectedUser.value.Disabled
|
formData.value.Disabled = selectedUser.value.Disabled
|
||||||
formData.value.Locked = selectedUser.value.Locked
|
formData.value.Locked = selectedUser.value.Locked
|
||||||
|
formData.value.PersistLocalChanges = selectedUser.value.PersistLocalChanges
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -133,7 +134,7 @@ async function del() {
|
|||||||
<template>
|
<template>
|
||||||
<Modal :title="title" :visible="visible" @close="close">
|
<Modal :title="title" :visible="visible" @close="close">
|
||||||
<template #default>
|
<template #default>
|
||||||
<fieldset v-if="formData.Source==='db'">
|
<fieldset>
|
||||||
<legend class="mt-4">{{ $t('modals.user-edit.header-general') }}</legend>
|
<legend class="mt-4">{{ $t('modals.user-edit.header-general') }}</legend>
|
||||||
<div v-if="props.userId==='#NEW#'" class="form-group">
|
<div v-if="props.userId==='#NEW#'" class="form-group">
|
||||||
<label class="form-label mt-4">{{ $t('modals.user-edit.identifier.label') }}</label>
|
<label class="form-label mt-4">{{ $t('modals.user-edit.identifier.label') }}</label>
|
||||||
@@ -141,16 +142,22 @@ async function del() {
|
|||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label class="form-label mt-4">{{ $t('modals.user-edit.source.label') }}</label>
|
<label class="form-label mt-4">{{ $t('modals.user-edit.source.label') }}</label>
|
||||||
<input v-model="formData.Source" class="form-control" disabled="disabled" :placeholder="$t('modals.user-edit.source.placeholder')" type="text">
|
<input v-model="formData.AuthSources" class="form-control" disabled="disabled" :placeholder="$t('modals.user-edit.source.placeholder')" type="text">
|
||||||
</div>
|
</div>
|
||||||
<div v-if="formData.Source==='db'" class="form-group">
|
<div class="form-group" v-if="formData.AuthSources.some(s => s ==='db')">
|
||||||
<label class="form-label mt-4">{{ $t('modals.user-edit.password.label') }}</label>
|
<label class="form-label mt-4">{{ $t('modals.user-edit.password.label') }}</label>
|
||||||
<input v-model="formData.Password" aria-describedby="passwordHelp" class="form-control" :class="{ 'is-invalid': passwordWeak, 'is-valid': formData.Password !== '' && !passwordWeak }" :placeholder="$t('modals.user-edit.password.placeholder')" type="password">
|
<input v-model="formData.Password" aria-describedby="passwordHelp" class="form-control" :class="{ 'is-invalid': passwordWeak, 'is-valid': formData.Password !== '' && !passwordWeak }" :placeholder="$t('modals.user-edit.password.placeholder')" type="password">
|
||||||
<div class="invalid-feedback">{{ $t('modals.user-edit.password.too-weak') }}</div>
|
<div class="invalid-feedback">{{ $t('modals.user-edit.password.too-weak') }}</div>
|
||||||
<small v-if="props.userId!=='#NEW#'" id="passwordHelp" class="form-text text-muted">{{ $t('modals.user-edit.password.description') }}</small>
|
<small v-if="props.userId!=='#NEW#'" id="passwordHelp" class="form-text text-muted">{{ $t('modals.user-edit.password.description') }}</small>
|
||||||
</div>
|
</div>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
<fieldset v-if="formData.Source==='db'">
|
<fieldset v-if="formData.AuthSources.some(s => s !=='db') && !formData.PersistLocalChanges">
|
||||||
|
<legend class="mt-4">{{ $t('modals.user-edit.header-personal') }}</legend>
|
||||||
|
<div class="alert alert-warning mt-3">
|
||||||
|
{{ $t('modals.user-edit.sync-warning') }}
|
||||||
|
</div>
|
||||||
|
</fieldset>
|
||||||
|
<fieldset v-if="!formData.AuthSources.some(s => s !=='db') || formData.PersistLocalChanges">
|
||||||
<legend class="mt-4">{{ $t('modals.user-edit.header-personal') }}</legend>
|
<legend class="mt-4">{{ $t('modals.user-edit.header-personal') }}</legend>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label class="form-label mt-4">{{ $t('modals.user-edit.email.label') }}</label>
|
<label class="form-label mt-4">{{ $t('modals.user-edit.email.label') }}</label>
|
||||||
@@ -194,10 +201,14 @@ async function del() {
|
|||||||
<input v-model="formData.Locked" class="form-check-input" type="checkbox">
|
<input v-model="formData.Locked" class="form-check-input" type="checkbox">
|
||||||
<label class="form-check-label" >{{ $t('modals.user-edit.locked.label') }}</label>
|
<label class="form-check-label" >{{ $t('modals.user-edit.locked.label') }}</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-check form-switch" v-if="formData.Source==='db'">
|
<div class="form-check form-switch" v-if="!formData.AuthSources.some(s => s !=='db') || formData.PersistLocalChanges">
|
||||||
<input v-model="formData.IsAdmin" checked="" class="form-check-input" type="checkbox">
|
<input v-model="formData.IsAdmin" checked="" class="form-check-input" type="checkbox">
|
||||||
<label class="form-check-label">{{ $t('modals.user-edit.admin.label') }}</label>
|
<label class="form-check-label">{{ $t('modals.user-edit.admin.label') }}</label>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="form-check form-switch" v-if="formData.AuthSources.some(s => s !=='db')">
|
||||||
|
<input v-model="formData.PersistLocalChanges" class="form-check-input" type="checkbox">
|
||||||
|
<label class="form-check-label" >{{ $t('modals.user-edit.persist-local-changes.label') }}</label>
|
||||||
|
</div>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
export function base64_url_encode(input) {
|
export function base64_url_encode(input) {
|
||||||
let output = btoa(input)
|
let output = btoa(input)
|
||||||
output = output.replace('+', '.')
|
output = output.replaceAll('+', '.')
|
||||||
output = output.replace('/', '_')
|
output = output.replaceAll('/', '_')
|
||||||
output = output.replace('=', '-')
|
output = output.replaceAll('=', '-')
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,7 @@ export function freshInterface() {
|
|||||||
Disabled: false,
|
Disabled: false,
|
||||||
DisplayName: "",
|
DisplayName: "",
|
||||||
Identifier: "",
|
Identifier: "",
|
||||||
|
CreateDefaultPeer: false,
|
||||||
Mode: "server",
|
Mode: "server",
|
||||||
Backend: "local",
|
Backend: "local",
|
||||||
|
|
||||||
@@ -136,7 +137,7 @@ export function freshUser() {
|
|||||||
Identifier: "",
|
Identifier: "",
|
||||||
|
|
||||||
Email: "",
|
Email: "",
|
||||||
Source: "db",
|
AuthSources: ["db"],
|
||||||
IsAdmin: false,
|
IsAdmin: false,
|
||||||
|
|
||||||
Firstname: "",
|
Firstname: "",
|
||||||
@@ -154,6 +155,8 @@ export function freshUser() {
|
|||||||
|
|
||||||
ApiEnabled: false,
|
ApiEnabled: false,
|
||||||
|
|
||||||
|
PersistLocalChanges: false,
|
||||||
|
|
||||||
PeerCount: 0,
|
PeerCount: 0,
|
||||||
|
|
||||||
// Internal values
|
// Internal values
|
||||||
|
|||||||
86
frontend/src/helpers/websocket-wrapper.js
Normal file
86
frontend/src/helpers/websocket-wrapper.js
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
import { peerStore } from '@/stores/peers';
|
||||||
|
import { interfaceStore } from '@/stores/interfaces';
|
||||||
|
import { authStore } from '@/stores/auth';
|
||||||
|
|
||||||
|
let socket = null;
|
||||||
|
let reconnectTimer = null;
|
||||||
|
let failureCount = 0;
|
||||||
|
|
||||||
|
export const websocketWrapper = {
|
||||||
|
connect() {
|
||||||
|
if (socket) {
|
||||||
|
console.log('WebSocket already connected, re-using existing connection.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const protocol = WGPORTAL_BACKEND_BASE_URL.startsWith('https://') ? 'wss://' : 'ws://';
|
||||||
|
const baseUrl = WGPORTAL_BACKEND_BASE_URL.replace(/^https?:\/\//, '');
|
||||||
|
const url = `${protocol}${baseUrl}/ws`;
|
||||||
|
|
||||||
|
socket = new WebSocket(url);
|
||||||
|
|
||||||
|
socket.onopen = () => {
|
||||||
|
console.log('WebSocket connected');
|
||||||
|
failureCount = 0;
|
||||||
|
if (reconnectTimer) {
|
||||||
|
clearInterval(reconnectTimer);
|
||||||
|
reconnectTimer = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
socket.onclose = () => {
|
||||||
|
console.log('WebSocket disconnected');
|
||||||
|
failureCount++;
|
||||||
|
socket = null;
|
||||||
|
this.scheduleReconnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
socket.onerror = (error) => {
|
||||||
|
console.error('WebSocket error:', error);
|
||||||
|
failureCount++;
|
||||||
|
socket.close();
|
||||||
|
socket = null;
|
||||||
|
};
|
||||||
|
|
||||||
|
socket.onmessage = (event) => {
|
||||||
|
const message = JSON.parse(event.data);
|
||||||
|
switch (message.type) {
|
||||||
|
case 'peer_stats':
|
||||||
|
peerStore().updatePeerTrafficStats(message.data);
|
||||||
|
break;
|
||||||
|
case 'interface_stats':
|
||||||
|
interfaceStore().updateInterfaceTrafficStats(message.data);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
disconnect() {
|
||||||
|
if (socket) {
|
||||||
|
socket.close();
|
||||||
|
socket = null;
|
||||||
|
}
|
||||||
|
if (reconnectTimer) {
|
||||||
|
clearInterval(reconnectTimer);
|
||||||
|
reconnectTimer = null;
|
||||||
|
failureCount = 0;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
scheduleReconnect() {
|
||||||
|
if (reconnectTimer) return;
|
||||||
|
if (!authStore().IsAuthenticated) return; // Don't reconnect if not logged in
|
||||||
|
|
||||||
|
reconnectTimer = setInterval(() => {
|
||||||
|
if (failureCount > 2) {
|
||||||
|
console.log('WebSocket connection unavailable, giving up.');
|
||||||
|
clearInterval(reconnectTimer);
|
||||||
|
reconnectTimer = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Attempting to reconnect WebSocket...');
|
||||||
|
this.connect();
|
||||||
|
}, 5000);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -147,7 +147,7 @@
|
|||||||
"email": "E-Mail",
|
"email": "E-Mail",
|
||||||
"firstname": "Vorname",
|
"firstname": "Vorname",
|
||||||
"lastname": "Nachname",
|
"lastname": "Nachname",
|
||||||
"source": "Quelle",
|
"sources": "Quellen",
|
||||||
"peers": "Peers",
|
"peers": "Peers",
|
||||||
"admin": "Admin"
|
"admin": "Admin"
|
||||||
},
|
},
|
||||||
@@ -378,7 +378,11 @@
|
|||||||
},
|
},
|
||||||
"admin": {
|
"admin": {
|
||||||
"label": "Ist Administrator"
|
"label": "Ist Administrator"
|
||||||
}
|
},
|
||||||
|
"persist-local-changes": {
|
||||||
|
"label": "Lokale Änderungen speichern"
|
||||||
|
},
|
||||||
|
"sync-warning": "Um diesen synchronisierten Benutzer zu bearbeiten, aktivieren Sie die lokale Änderungsspeicherung. Andernfalls werden Ihre Änderungen bei der nächsten Synchronisierung überschrieben."
|
||||||
},
|
},
|
||||||
"interface-view": {
|
"interface-view": {
|
||||||
"headline": "Konfiguration für Schnittstelle:"
|
"headline": "Konfiguration für Schnittstelle:"
|
||||||
@@ -469,6 +473,9 @@
|
|||||||
"disabled": {
|
"disabled": {
|
||||||
"label": "Schnittstelle deaktiviert"
|
"label": "Schnittstelle deaktiviert"
|
||||||
},
|
},
|
||||||
|
"create-default-peer": {
|
||||||
|
"label": "Peer für neue Benutzer automatisch erstellen"
|
||||||
|
},
|
||||||
"save-config": {
|
"save-config": {
|
||||||
"label": "wg-quick Konfiguration automatisch speichern"
|
"label": "wg-quick Konfiguration automatisch speichern"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -147,7 +147,7 @@
|
|||||||
"email": "E-Mail",
|
"email": "E-Mail",
|
||||||
"firstname": "Firstname",
|
"firstname": "Firstname",
|
||||||
"lastname": "Lastname",
|
"lastname": "Lastname",
|
||||||
"source": "Source",
|
"sources": "Sources",
|
||||||
"peers": "Peers",
|
"peers": "Peers",
|
||||||
"admin": "Admin"
|
"admin": "Admin"
|
||||||
},
|
},
|
||||||
@@ -378,7 +378,11 @@
|
|||||||
},
|
},
|
||||||
"admin": {
|
"admin": {
|
||||||
"label": "Is Admin"
|
"label": "Is Admin"
|
||||||
}
|
},
|
||||||
|
"persist-local-changes": {
|
||||||
|
"label": "Persist local changes"
|
||||||
|
},
|
||||||
|
"sync-warning": "To modify this synchronized user, enable local change persistence. Otherwise, your changes will be overwritten during the next synchronization."
|
||||||
},
|
},
|
||||||
"interface-view": {
|
"interface-view": {
|
||||||
"headline": "Config for Interface:"
|
"headline": "Config for Interface:"
|
||||||
@@ -469,6 +473,9 @@
|
|||||||
"disabled": {
|
"disabled": {
|
||||||
"label": "Interface Disabled"
|
"label": "Interface Disabled"
|
||||||
},
|
},
|
||||||
|
"create-default-peer": {
|
||||||
|
"label": "Create default peer for new users"
|
||||||
|
},
|
||||||
"save-config": {
|
"save-config": {
|
||||||
"label": "Automatically save wg-quick config"
|
"label": "Automatically save wg-quick config"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -162,7 +162,7 @@
|
|||||||
"email": "Correo electrónico",
|
"email": "Correo electrónico",
|
||||||
"firstname": "Nombre",
|
"firstname": "Nombre",
|
||||||
"lastname": "Apellido",
|
"lastname": "Apellido",
|
||||||
"source": "Origen",
|
"sources": "Origen",
|
||||||
"peers": "Peers",
|
"peers": "Peers",
|
||||||
"admin": "Administrador"
|
"admin": "Administrador"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -137,7 +137,7 @@
|
|||||||
"email": "E-mail",
|
"email": "E-mail",
|
||||||
"firstname": "Prénom",
|
"firstname": "Prénom",
|
||||||
"lastname": "Nom",
|
"lastname": "Nom",
|
||||||
"source": "Source",
|
"sources": "Sources",
|
||||||
"peers": "Pairs",
|
"peers": "Pairs",
|
||||||
"admin": "Admin"
|
"admin": "Admin"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -136,7 +136,7 @@
|
|||||||
"email": "이메일",
|
"email": "이메일",
|
||||||
"firstname": "이름",
|
"firstname": "이름",
|
||||||
"lastname": "성",
|
"lastname": "성",
|
||||||
"source": "소스",
|
"sources": "소스",
|
||||||
"peers": "피어",
|
"peers": "피어",
|
||||||
"admin": "관리자"
|
"admin": "관리자"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -137,7 +137,7 @@
|
|||||||
"email": "E-Mail",
|
"email": "E-Mail",
|
||||||
"firstname": "Primeiro Nome",
|
"firstname": "Primeiro Nome",
|
||||||
"lastname": "Último Nome",
|
"lastname": "Último Nome",
|
||||||
"source": "Fonte",
|
"sources": "Fonte",
|
||||||
"peers": "Peers",
|
"peers": "Peers",
|
||||||
"admin": "Administrador"
|
"admin": "Administrador"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -143,7 +143,7 @@
|
|||||||
"email": "Электронная почта",
|
"email": "Электронная почта",
|
||||||
"firstname": "Имя",
|
"firstname": "Имя",
|
||||||
"lastname": "Фамилия",
|
"lastname": "Фамилия",
|
||||||
"source": "Источник",
|
"sources": "Источник",
|
||||||
"peers": "Пиры",
|
"peers": "Пиры",
|
||||||
"admin": "Админ"
|
"admin": "Админ"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -135,7 +135,7 @@
|
|||||||
"email": "E-Mail",
|
"email": "E-Mail",
|
||||||
"firstname": "Ім'я",
|
"firstname": "Ім'я",
|
||||||
"lastname": "Прізвище",
|
"lastname": "Прізвище",
|
||||||
"source": "Джерело",
|
"sources": "Джерело",
|
||||||
"peers": "Піри",
|
"peers": "Піри",
|
||||||
"admin": "Адміністратор"
|
"admin": "Адміністратор"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -134,7 +134,7 @@
|
|||||||
"email": "E-Mail",
|
"email": "E-Mail",
|
||||||
"firstname": "Tên",
|
"firstname": "Tên",
|
||||||
"lastname": "Họ",
|
"lastname": "Họ",
|
||||||
"source": "Nguồn",
|
"sources": "Nguồn",
|
||||||
"peers": "Peers",
|
"peers": "Peers",
|
||||||
"admin": "Quản trị viên"
|
"admin": "Quản trị viên"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -134,7 +134,7 @@
|
|||||||
"email": "电子邮件",
|
"email": "电子邮件",
|
||||||
"firstname": "名",
|
"firstname": "名",
|
||||||
"lastname": "姓",
|
"lastname": "姓",
|
||||||
"source": "来源",
|
"sources": "来源",
|
||||||
"peers": "节点",
|
"peers": "节点",
|
||||||
"admin": "管理员"
|
"admin": "管理员"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { defineStore } from 'pinia'
|
|||||||
|
|
||||||
import { notify } from "@kyvg/vue3-notification";
|
import { notify } from "@kyvg/vue3-notification";
|
||||||
import { apiWrapper } from '@/helpers/fetch-wrapper'
|
import { apiWrapper } from '@/helpers/fetch-wrapper'
|
||||||
|
import { websocketWrapper } from '@/helpers/websocket-wrapper'
|
||||||
import router from '../router'
|
import router from '../router'
|
||||||
import { browserSupportsWebAuthn,startRegistration,startAuthentication } from '@simplewebauthn/browser';
|
import { browserSupportsWebAuthn,startRegistration,startAuthentication } from '@simplewebauthn/browser';
|
||||||
import {base64_url_encode} from "@/helpers/encoding";
|
import {base64_url_encode} from "@/helpers/encoding";
|
||||||
@@ -295,9 +296,11 @@ export const authStore = defineStore('auth',{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
localStorage.setItem('user', JSON.stringify(this.user))
|
localStorage.setItem('user', JSON.stringify(this.user))
|
||||||
|
websocketWrapper.connect()
|
||||||
} else {
|
} else {
|
||||||
this.user = null
|
this.user = null
|
||||||
localStorage.removeItem('user')
|
localStorage.removeItem('user')
|
||||||
|
websocketWrapper.disconnect()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
setWebAuthnCredentials(credentials) {
|
setWebAuthnCredentials(credentials) {
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ export const interfaceStore = defineStore('interfaces', {
|
|||||||
configuration: "",
|
configuration: "",
|
||||||
selected: "",
|
selected: "",
|
||||||
fetching: false,
|
fetching: false,
|
||||||
|
trafficStats: {},
|
||||||
}),
|
}),
|
||||||
getters: {
|
getters: {
|
||||||
Count: (state) => state.interfaces.length,
|
Count: (state) => state.interfaces.length,
|
||||||
@@ -24,6 +25,9 @@ export const interfaceStore = defineStore('interfaces', {
|
|||||||
},
|
},
|
||||||
GetSelected: (state) => state.interfaces.find((i) => i.Identifier === state.selected) || state.interfaces[0],
|
GetSelected: (state) => state.interfaces.find((i) => i.Identifier === state.selected) || state.interfaces[0],
|
||||||
isFetching: (state) => state.fetching,
|
isFetching: (state) => state.fetching,
|
||||||
|
TrafficStats: (state) => {
|
||||||
|
return (state.selected in state.trafficStats) ? state.trafficStats[state.selected] : { Received: 0, Transmitted: 0 }
|
||||||
|
},
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
setInterfaces(interfaces) {
|
setInterfaces(interfaces) {
|
||||||
@@ -34,6 +38,14 @@ export const interfaceStore = defineStore('interfaces', {
|
|||||||
this.selected = ""
|
this.selected = ""
|
||||||
}
|
}
|
||||||
this.fetching = false
|
this.fetching = false
|
||||||
|
this.trafficStats = {}
|
||||||
|
},
|
||||||
|
updateInterfaceTrafficStats(interfaceStats) {
|
||||||
|
const id = interfaceStats.EntityId;
|
||||||
|
this.trafficStats[id] = {
|
||||||
|
Received: interfaceStats.BytesReceived,
|
||||||
|
Transmitted: interfaceStats.BytesTransmitted,
|
||||||
|
};
|
||||||
},
|
},
|
||||||
async LoadInterfaces() {
|
async LoadInterfaces() {
|
||||||
this.fetching = true
|
this.fetching = true
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ export const peerStore = defineStore('peers', {
|
|||||||
fetching: false,
|
fetching: false,
|
||||||
sortKey: 'IsConnected', // Default sort key
|
sortKey: 'IsConnected', // Default sort key
|
||||||
sortOrder: -1, // 1 for ascending, -1 for descending
|
sortOrder: -1, // 1 for ascending, -1 for descending
|
||||||
|
trafficStats: {},
|
||||||
}),
|
}),
|
||||||
getters: {
|
getters: {
|
||||||
Find: (state) => {
|
Find: (state) => {
|
||||||
@@ -76,6 +77,9 @@ export const peerStore = defineStore('peers', {
|
|||||||
Statistics: (state) => {
|
Statistics: (state) => {
|
||||||
return (id) => state.statsEnabled && (id in state.stats) ? state.stats[id] : freshStats()
|
return (id) => state.statsEnabled && (id in state.stats) ? state.stats[id] : freshStats()
|
||||||
},
|
},
|
||||||
|
TrafficStats: (state) => {
|
||||||
|
return (id) => (id in state.trafficStats) ? state.trafficStats[id] : { Received: 0, Transmitted: 0 }
|
||||||
|
},
|
||||||
hasStatistics: (state) => state.statsEnabled,
|
hasStatistics: (state) => state.statsEnabled,
|
||||||
|
|
||||||
},
|
},
|
||||||
@@ -111,6 +115,7 @@ export const peerStore = defineStore('peers', {
|
|||||||
this.peers = peers
|
this.peers = peers
|
||||||
this.calculatePages()
|
this.calculatePages()
|
||||||
this.fetching = false
|
this.fetching = false
|
||||||
|
this.trafficStats = {}
|
||||||
},
|
},
|
||||||
setPeer(peer) {
|
setPeer(peer) {
|
||||||
this.peer = peer
|
this.peer = peer
|
||||||
@@ -126,11 +131,19 @@ export const peerStore = defineStore('peers', {
|
|||||||
if (!statsResponse) {
|
if (!statsResponse) {
|
||||||
this.stats = {}
|
this.stats = {}
|
||||||
this.statsEnabled = false
|
this.statsEnabled = false
|
||||||
|
this.trafficStats = {}
|
||||||
} else {
|
} else {
|
||||||
this.stats = statsResponse.Stats
|
this.stats = statsResponse.Stats
|
||||||
this.statsEnabled = statsResponse.Enabled
|
this.statsEnabled = statsResponse.Enabled
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
updatePeerTrafficStats(peerStats) {
|
||||||
|
const id = peerStats.EntityId;
|
||||||
|
this.trafficStats[id] = {
|
||||||
|
Received: peerStats.BytesReceived,
|
||||||
|
Transmitted: peerStats.BytesTransmitted,
|
||||||
|
};
|
||||||
|
},
|
||||||
async Reset() {
|
async Reset() {
|
||||||
this.setPeers([])
|
this.setPeers([])
|
||||||
this.setStats(undefined)
|
this.setStats(undefined)
|
||||||
|
|||||||
@@ -210,6 +210,12 @@ onMounted(async () => {
|
|||||||
<div class="col-12 col-lg-8">
|
<div class="col-12 col-lg-8">
|
||||||
{{ $t('interfaces.interface.headline') }} <strong>{{interfaces.GetSelected.Identifier}}</strong> ({{ $t('modals.interface-edit.mode.' + interfaces.GetSelected.Mode )}} | {{ $t('interfaces.interface.backend') + ": " + calculateBackendName }}<span v-if="!isBackendValid" :title="t('interfaces.interface.wrong-backend')" class="ms-1 me-1"><i class="fa-solid fa-triangle-exclamation"></i></span>)
|
{{ $t('interfaces.interface.headline') }} <strong>{{interfaces.GetSelected.Identifier}}</strong> ({{ $t('modals.interface-edit.mode.' + interfaces.GetSelected.Mode )}} | {{ $t('interfaces.interface.backend') + ": " + calculateBackendName }}<span v-if="!isBackendValid" :title="t('interfaces.interface.wrong-backend')" class="ms-1 me-1"><i class="fa-solid fa-triangle-exclamation"></i></span>)
|
||||||
<span v-if="interfaces.GetSelected.Disabled" class="text-danger"><i class="fa fa-circle-xmark" :title="interfaces.GetSelected.DisabledReason"></i></span>
|
<span v-if="interfaces.GetSelected.Disabled" class="text-danger"><i class="fa fa-circle-xmark" :title="interfaces.GetSelected.DisabledReason"></i></span>
|
||||||
|
<div v-if="interfaces.GetSelected && (interfaces.TrafficStats.Received > 0 || interfaces.TrafficStats.Transmitted > 0)" class="mt-2">
|
||||||
|
<small class="text-muted">
|
||||||
|
Traffic: <i class="fa-solid fa-arrow-down me-1"></i>{{ humanFileSize(interfaces.TrafficStats.Received) }}/s
|
||||||
|
<i class="fa-solid fa-arrow-up ms-1 me-1"></i>{{ humanFileSize(interfaces.TrafficStats.Transmitted) }}/s
|
||||||
|
</small>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-12 col-lg-4 text-lg-end">
|
<div class="col-12 col-lg-4 text-lg-end">
|
||||||
<a class="btn-link" href="#" :title="$t('interfaces.interface.button-show-config')" @click.prevent="viewedInterfaceId=interfaces.GetSelected.Identifier"><i class="fas fa-eye"></i></a>
|
<a class="btn-link" href="#" :title="$t('interfaces.interface.button-show-config')" @click.prevent="viewedInterfaceId=interfaces.GetSelected.Identifier"><i class="fas fa-eye"></i></a>
|
||||||
@@ -451,14 +457,19 @@ onMounted(async () => {
|
|||||||
<td v-if="interfaces.GetSelected.Mode==='client'">{{peer.Endpoint.Value}}</td>
|
<td v-if="interfaces.GetSelected.Mode==='client'">{{peer.Endpoint.Value}}</td>
|
||||||
<td v-if="peers.hasStatistics">
|
<td v-if="peers.hasStatistics">
|
||||||
<div v-if="peers.Statistics(peer.Identifier).IsConnected">
|
<div v-if="peers.Statistics(peer.Identifier).IsConnected">
|
||||||
<span class="badge rounded-pill bg-success" :title="$t('interfaces.peer-connected')"><i class="fa-solid fa-link"></i></span> <span :title="$t('interfaces.peer-handshake') + ' ' + peers.Statistics(peer.Identifier).LastHandshake">{{ $t('interfaces.peer-connected') }}</span>
|
<span class="badge rounded-pill bg-success" :title="$t('interfaces.peer-connected')"><i class="fa-solid fa-link"></i></span> <small class="text-muted" :title="$t('interfaces.peer-handshake') + ' ' + peers.Statistics(peer.Identifier).LastHandshake"><i class="fa-solid fa-circle-info"></i></small>
|
||||||
</div>
|
</div>
|
||||||
<div v-else>
|
<div v-else>
|
||||||
<span class="badge rounded-pill bg-light" :title="$t('interfaces.peer-not-connected')"><i class="fa-solid fa-link-slash"></i></span>
|
<span class="badge rounded-pill bg-light" :title="$t('interfaces.peer-not-connected')"><i class="fa-solid fa-link-slash"></i></span>
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
<td v-if="peers.hasStatistics" >
|
<td v-if="peers.hasStatistics" >
|
||||||
<span class="text-center" >{{ humanFileSize(peers.Statistics(peer.Identifier).BytesReceived) }} / {{ humanFileSize(peers.Statistics(peer.Identifier).BytesTransmitted) }}</span>
|
<div class="d-flex flex-column">
|
||||||
|
<span :title="humanFileSize(peers.Statistics(peer.Identifier).BytesReceived) + ' / ' + humanFileSize(peers.Statistics(peer.Identifier).BytesTransmitted)">
|
||||||
|
<i class="fa-solid fa-arrow-down me-1"></i>{{ humanFileSize(peers.TrafficStats(peer.Identifier).Received) }}/s
|
||||||
|
<i class="fa-solid fa-arrow-up ms-1 me-1"></i>{{ humanFileSize(peers.TrafficStats(peer.Identifier).Transmitted) }}/s
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
</td>
|
</td>
|
||||||
<td class="text-center">
|
<td class="text-center">
|
||||||
<a href="#" :title="$t('interfaces.button-show-peer')" @click.prevent="viewedPeerId=peer.Identifier"><i class="fas fa-eye me-2"></i></a>
|
<a href="#" :title="$t('interfaces.button-show-peer')" @click.prevent="viewedPeerId=peer.Identifier"><i class="fas fa-eye me-2"></i></a>
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ onMounted(() => {
|
|||||||
<th scope="col">{{ $t('users.table-heading.email') }}</th>
|
<th scope="col">{{ $t('users.table-heading.email') }}</th>
|
||||||
<th scope="col">{{ $t('users.table-heading.firstname') }}</th>
|
<th scope="col">{{ $t('users.table-heading.firstname') }}</th>
|
||||||
<th scope="col">{{ $t('users.table-heading.lastname') }}</th>
|
<th scope="col">{{ $t('users.table-heading.lastname') }}</th>
|
||||||
<th class="text-center" scope="col">{{ $t('users.table-heading.source') }}</th>
|
<th class="text-center" scope="col">{{ $t('users.table-heading.sources') }}</th>
|
||||||
<th class="text-center" scope="col">{{ $t('users.table-heading.peers') }}</th>
|
<th class="text-center" scope="col">{{ $t('users.table-heading.peers') }}</th>
|
||||||
<th class="text-center" scope="col">{{ $t('users.table-heading.admin') }}</th>
|
<th class="text-center" scope="col">{{ $t('users.table-heading.admin') }}</th>
|
||||||
<th scope="col"></th><!-- Actions -->
|
<th scope="col"></th><!-- Actions -->
|
||||||
@@ -150,7 +150,7 @@ onMounted(() => {
|
|||||||
<td>{{user.Email}}</td>
|
<td>{{user.Email}}</td>
|
||||||
<td>{{user.Firstname}}</td>
|
<td>{{user.Firstname}}</td>
|
||||||
<td>{{user.Lastname}}</td>
|
<td>{{user.Lastname}}</td>
|
||||||
<td class="text-center"><span class="badge rounded-pill bg-light">{{user.Source}}</span></td>
|
<td><span class="badge bg-light me-1" v-for="src in user.AuthSources" :key="src">{{src}}</span></td>
|
||||||
<td class="text-center">{{user.PeerCount}}</td>
|
<td class="text-center">{{user.PeerCount}}</td>
|
||||||
<td class="text-center">
|
<td class="text-center">
|
||||||
<span v-if="user.IsAdmin" class="text-danger" :title="$t('users.admin')"><i class="fa fa-check-circle"></i></span>
|
<span v-if="user.IsAdmin" class="text-danger" :title="$t('users.admin')"><i class="fa fa-check-circle"></i></span>
|
||||||
|
|||||||
7
go.mod
7
go.mod
@@ -12,6 +12,7 @@ require (
|
|||||||
github.com/go-playground/validator/v10 v10.30.1
|
github.com/go-playground/validator/v10 v10.30.1
|
||||||
github.com/go-webauthn/webauthn v0.15.0
|
github.com/go-webauthn/webauthn v0.15.0
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
|
github.com/gorilla/websocket v1.5.3
|
||||||
github.com/prometheus-community/pro-bing v0.7.0
|
github.com/prometheus-community/pro-bing v0.7.0
|
||||||
github.com/prometheus/client_golang v1.23.2
|
github.com/prometheus/client_golang v1.23.2
|
||||||
github.com/stretchr/testify v1.11.1
|
github.com/stretchr/testify v1.11.1
|
||||||
@@ -21,9 +22,9 @@ require (
|
|||||||
github.com/xhit/go-simple-mail/v2 v2.16.0
|
github.com/xhit/go-simple-mail/v2 v2.16.0
|
||||||
github.com/yeqown/go-qrcode/v2 v2.2.5
|
github.com/yeqown/go-qrcode/v2 v2.2.5
|
||||||
github.com/yeqown/go-qrcode/writer/compressed v1.0.1
|
github.com/yeqown/go-qrcode/writer/compressed v1.0.1
|
||||||
golang.org/x/crypto v0.46.0
|
golang.org/x/crypto v0.47.0
|
||||||
golang.org/x/oauth2 v0.34.0
|
golang.org/x/oauth2 v0.34.0
|
||||||
golang.org/x/sys v0.39.0
|
golang.org/x/sys v0.40.0
|
||||||
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20241231184526-a9ab2273dd10
|
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20241231184526-a9ab2273dd10
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
gorm.io/driver/mysql v1.6.0
|
gorm.io/driver/mysql v1.6.0
|
||||||
@@ -96,7 +97,7 @@ require (
|
|||||||
golang.org/x/mod v0.31.0 // indirect
|
golang.org/x/mod v0.31.0 // indirect
|
||||||
golang.org/x/net v0.48.0 // indirect
|
golang.org/x/net v0.48.0 // indirect
|
||||||
golang.org/x/sync v0.19.0 // indirect
|
golang.org/x/sync v0.19.0 // indirect
|
||||||
golang.org/x/text v0.32.0 // indirect
|
golang.org/x/text v0.33.0 // indirect
|
||||||
golang.org/x/tools v0.40.0 // indirect
|
golang.org/x/tools v0.40.0 // indirect
|
||||||
golang.zx2c4.com/wireguard v0.0.0-20250521234502-f333402bd9cb // indirect
|
golang.zx2c4.com/wireguard v0.0.0-20250521234502-f333402bd9cb // indirect
|
||||||
google.golang.org/protobuf v1.36.10 // indirect
|
google.golang.org/protobuf v1.36.10 // indirect
|
||||||
|
|||||||
14
go.sum
14
go.sum
@@ -130,6 +130,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
|||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
|
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
|
||||||
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
|
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
|
||||||
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
|
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
|
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
|
||||||
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
|
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
|
||||||
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
|
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
|
||||||
@@ -270,8 +272,8 @@ golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOM
|
|||||||
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
|
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
|
||||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||||
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
|
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
|
||||||
golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU=
|
golang.org/x/crypto v0.47.0 h1:V6e3FRj+n4dbpw86FJ8Fv7XVOql7TEwpHapKoMJ/GO8=
|
||||||
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
|
golang.org/x/crypto v0.47.0/go.mod h1:ff3Y9VzzKbwSSEzWqJsJVBnWmRwRSHt/6Op5n9bQc4A=
|
||||||
golang.org/x/exp v0.0.0-20251209150349-8475f28825e9 h1:MDfG8Cvcqlt9XXrmEiD4epKn7VJHZO84hejP9Jmp0MM=
|
golang.org/x/exp v0.0.0-20251209150349-8475f28825e9 h1:MDfG8Cvcqlt9XXrmEiD4epKn7VJHZO84hejP9Jmp0MM=
|
||||||
golang.org/x/exp v0.0.0-20251209150349-8475f28825e9/go.mod h1:EPRbTFwzwjXj9NpYyyrvenVh9Y+GFeEvMNh7Xuz7xgU=
|
golang.org/x/exp v0.0.0-20251209150349-8475f28825e9/go.mod h1:EPRbTFwzwjXj9NpYyyrvenVh9Y+GFeEvMNh7Xuz7xgU=
|
||||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
@@ -332,8 +334,8 @@ golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
|||||||
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
|
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
|
||||||
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
|
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
@@ -362,8 +364,8 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
|||||||
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
||||||
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
|
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
|
||||||
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
|
golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE=
|
||||||
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
|
golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||||
|
|||||||
@@ -23,9 +23,6 @@ import (
|
|||||||
"github.com/h44z/wg-portal/internal/domain"
|
"github.com/h44z/wg-portal/internal/domain"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SchemaVersion describes the current database schema version. It must be incremented if a manual migration is needed.
|
|
||||||
var SchemaVersion uint64 = 1
|
|
||||||
|
|
||||||
// SysStat stores the current database schema version and the timestamp when it was applied.
|
// SysStat stores the current database schema version and the timestamp when it was applied.
|
||||||
type SysStat struct {
|
type SysStat struct {
|
||||||
MigratedAt time.Time `gorm:"column:migrated_at"`
|
MigratedAt time.Time `gorm:"column:migrated_at"`
|
||||||
@@ -179,13 +176,15 @@ func NewDatabase(cfg config.DatabaseConfig) (*gorm.DB, error) {
|
|||||||
// SqlRepo is a SQL database repository implementation.
|
// SqlRepo is a SQL database repository implementation.
|
||||||
// Currently, it supports MySQL, SQLite, Microsoft SQL and Postgresql database systems.
|
// Currently, it supports MySQL, SQLite, Microsoft SQL and Postgresql database systems.
|
||||||
type SqlRepo struct {
|
type SqlRepo struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
|
cfg *config.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSqlRepository creates a new SqlRepo instance.
|
// NewSqlRepository creates a new SqlRepo instance.
|
||||||
func NewSqlRepository(db *gorm.DB) (*SqlRepo, error) {
|
func NewSqlRepository(db *gorm.DB, cfg *config.Config) (*SqlRepo, error) {
|
||||||
repo := &SqlRepo{
|
repo := &SqlRepo{
|
||||||
db: db,
|
db: db,
|
||||||
|
cfg: cfg,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := repo.preCheck(); err != nil {
|
if err := repo.preCheck(); err != nil {
|
||||||
@@ -223,6 +222,8 @@ func (r *SqlRepo) preCheck() error {
|
|||||||
func (r *SqlRepo) migrate() error {
|
func (r *SqlRepo) migrate() error {
|
||||||
slog.Debug("running migration: sys-stat", "result", r.db.AutoMigrate(&SysStat{}))
|
slog.Debug("running migration: sys-stat", "result", r.db.AutoMigrate(&SysStat{}))
|
||||||
slog.Debug("running migration: user", "result", r.db.AutoMigrate(&domain.User{}))
|
slog.Debug("running migration: user", "result", r.db.AutoMigrate(&domain.User{}))
|
||||||
|
slog.Debug("running migration: user authentications", "result",
|
||||||
|
r.db.AutoMigrate(&domain.UserAuthentication{}))
|
||||||
slog.Debug("running migration: user webauthn credentials", "result",
|
slog.Debug("running migration: user webauthn credentials", "result",
|
||||||
r.db.AutoMigrate(&domain.UserWebauthnCredential{}))
|
r.db.AutoMigrate(&domain.UserWebauthnCredential{}))
|
||||||
slog.Debug("running migration: interface", "result", r.db.AutoMigrate(&domain.Interface{}))
|
slog.Debug("running migration: interface", "result", r.db.AutoMigrate(&domain.Interface{}))
|
||||||
@@ -232,16 +233,88 @@ func (r *SqlRepo) migrate() error {
|
|||||||
slog.Debug("running migration: audit data", "result", r.db.AutoMigrate(&domain.AuditEntry{}))
|
slog.Debug("running migration: audit data", "result", r.db.AutoMigrate(&domain.AuditEntry{}))
|
||||||
|
|
||||||
existingSysStat := SysStat{}
|
existingSysStat := SysStat{}
|
||||||
r.db.Where("schema_version = ?", SchemaVersion).First(&existingSysStat)
|
r.db.Order("schema_version desc").First(&existingSysStat) // get latest version
|
||||||
|
|
||||||
|
// Migration: 0 --> 1
|
||||||
if existingSysStat.SchemaVersion == 0 {
|
if existingSysStat.SchemaVersion == 0 {
|
||||||
|
const schemaVersion = 1
|
||||||
sysStat := SysStat{
|
sysStat := SysStat{
|
||||||
MigratedAt: time.Now(),
|
MigratedAt: time.Now(),
|
||||||
SchemaVersion: SchemaVersion,
|
SchemaVersion: schemaVersion,
|
||||||
}
|
}
|
||||||
if err := r.db.Create(&sysStat).Error; err != nil {
|
if err := r.db.Create(&sysStat).Error; err != nil {
|
||||||
return fmt.Errorf("failed to write sysstat entry for schema version %d: %w", SchemaVersion, err)
|
return fmt.Errorf("failed to write sysstat entry for schema version %d: %w", schemaVersion, err)
|
||||||
}
|
}
|
||||||
slog.Debug("sys-stat entry written", "schema_version", SchemaVersion)
|
slog.Debug("sys-stat entry written", "schema_version", schemaVersion)
|
||||||
|
existingSysStat = sysStat // ensure that follow-up checks test against the latest version
|
||||||
|
}
|
||||||
|
|
||||||
|
// Migration: 1 --> 2
|
||||||
|
if existingSysStat.SchemaVersion == 1 {
|
||||||
|
const schemaVersion = 2
|
||||||
|
// Preserve existing behavior for installations that had default-peer-creation enabled.
|
||||||
|
if r.cfg.Core.CreateDefaultPeer {
|
||||||
|
err := r.db.Model(&domain.Interface{}).
|
||||||
|
Where("type = ?", domain.InterfaceTypeServer).
|
||||||
|
Update("create_default_peer", true).Error
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to migrate interface flags for schema version %d: %w", schemaVersion, err)
|
||||||
|
}
|
||||||
|
slog.Debug("migrated interface create_default_peer flags", "schema_version", schemaVersion)
|
||||||
|
}
|
||||||
|
sysStat := SysStat{
|
||||||
|
MigratedAt: time.Now(),
|
||||||
|
SchemaVersion: schemaVersion,
|
||||||
|
}
|
||||||
|
if err := r.db.Create(&sysStat).Error; err != nil {
|
||||||
|
return fmt.Errorf("failed to write sysstat entry for schema version %d: %w", schemaVersion, err)
|
||||||
|
}
|
||||||
|
existingSysStat = sysStat // ensure that follow-up checks test against the latest version
|
||||||
|
}
|
||||||
|
|
||||||
|
// Migration: 2 --> 3
|
||||||
|
if existingSysStat.SchemaVersion == 2 {
|
||||||
|
const schemaVersion = 3
|
||||||
|
// Migration to multi-auth
|
||||||
|
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||||
|
var users []domain.User
|
||||||
|
if err := tx.Find(&users).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
for _, user := range users {
|
||||||
|
auth := domain.UserAuthentication{
|
||||||
|
BaseModel: domain.BaseModel{
|
||||||
|
CreatedBy: domain.CtxSystemDBMigrator,
|
||||||
|
UpdatedBy: domain.CtxSystemDBMigrator,
|
||||||
|
CreatedAt: now,
|
||||||
|
UpdatedAt: now,
|
||||||
|
},
|
||||||
|
UserIdentifier: user.Identifier,
|
||||||
|
Source: user.Source,
|
||||||
|
ProviderName: user.ProviderName,
|
||||||
|
}
|
||||||
|
if err := tx.Create(&auth).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Debug("migrated users to multi-auth model", "schema_version", schemaVersion)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to migrate to multi-auth: %w", err)
|
||||||
|
}
|
||||||
|
sysStat := SysStat{
|
||||||
|
MigratedAt: time.Now(),
|
||||||
|
SchemaVersion: schemaVersion,
|
||||||
|
}
|
||||||
|
if err := r.db.Create(&sysStat).Error; err != nil {
|
||||||
|
return fmt.Errorf("failed to write sysstat entry for schema version %d: %w", schemaVersion, err)
|
||||||
|
}
|
||||||
|
existingSysStat = sysStat // ensure that follow-up checks test against the latest version
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -751,7 +824,7 @@ func (r *SqlRepo) GetUsedIpsPerSubnet(ctx context.Context, subnets []domain.Cidr
|
|||||||
func (r *SqlRepo) GetUser(ctx context.Context, id domain.UserIdentifier) (*domain.User, error) {
|
func (r *SqlRepo) GetUser(ctx context.Context, id domain.UserIdentifier) (*domain.User, error) {
|
||||||
var user domain.User
|
var user domain.User
|
||||||
|
|
||||||
err := r.db.WithContext(ctx).Preload("WebAuthnCredentialList").First(&user, id).Error
|
err := r.db.WithContext(ctx).Preload("WebAuthnCredentialList").Preload("Authentications").First(&user, id).Error
|
||||||
|
|
||||||
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return nil, domain.ErrNotFound
|
return nil, domain.ErrNotFound
|
||||||
@@ -769,7 +842,8 @@ func (r *SqlRepo) GetUser(ctx context.Context, id domain.UserIdentifier) (*domai
|
|||||||
func (r *SqlRepo) GetUserByEmail(ctx context.Context, email string) (*domain.User, error) {
|
func (r *SqlRepo) GetUserByEmail(ctx context.Context, email string) (*domain.User, error) {
|
||||||
var users []domain.User
|
var users []domain.User
|
||||||
|
|
||||||
err := r.db.WithContext(ctx).Where("email = ?", email).Preload("WebAuthnCredentialList").Find(&users).Error
|
err := r.db.WithContext(ctx).Where("email = ?",
|
||||||
|
email).Preload("WebAuthnCredentialList").Preload("Authentications").Find(&users).Error
|
||||||
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return nil, domain.ErrNotFound
|
return nil, domain.ErrNotFound
|
||||||
}
|
}
|
||||||
@@ -809,7 +883,7 @@ func (r *SqlRepo) GetUserByWebAuthnCredential(ctx context.Context, credentialIdB
|
|||||||
func (r *SqlRepo) GetAllUsers(ctx context.Context) ([]domain.User, error) {
|
func (r *SqlRepo) GetAllUsers(ctx context.Context) ([]domain.User, error) {
|
||||||
var users []domain.User
|
var users []domain.User
|
||||||
|
|
||||||
err := r.db.WithContext(ctx).Preload("WebAuthnCredentialList").Find(&users).Error
|
err := r.db.WithContext(ctx).Preload("WebAuthnCredentialList").Preload("Authentications").Find(&users).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -829,6 +903,7 @@ func (r *SqlRepo) FindUsers(ctx context.Context, search string) ([]domain.User,
|
|||||||
Or("lastname LIKE ?", searchValue).
|
Or("lastname LIKE ?", searchValue).
|
||||||
Or("email LIKE ?", searchValue).
|
Or("email LIKE ?", searchValue).
|
||||||
Preload("WebAuthnCredentialList").
|
Preload("WebAuthnCredentialList").
|
||||||
|
Preload("Authentications").
|
||||||
Find(&users).Error
|
Find(&users).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -888,7 +963,17 @@ func (r *SqlRepo) getOrCreateUser(ui *domain.ContextUserInfo, tx *gorm.DB, id do
|
|||||||
) {
|
) {
|
||||||
var user domain.User
|
var user domain.User
|
||||||
|
|
||||||
// userDefaults will be applied to newly created user records
|
result := tx.Model(&user).Preload("WebAuthnCredentialList").Preload("Authentications").Find(&user, id)
|
||||||
|
if result.Error != nil {
|
||||||
|
if !errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, result.Error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if result.Error == nil && result.RowsAffected > 0 {
|
||||||
|
return &user, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// create a new user record if no user record exists yet
|
||||||
userDefaults := domain.User{
|
userDefaults := domain.User{
|
||||||
BaseModel: domain.BaseModel{
|
BaseModel: domain.BaseModel{
|
||||||
CreatedBy: ui.UserId(),
|
CreatedBy: ui.UserId(),
|
||||||
@@ -897,16 +982,15 @@ func (r *SqlRepo) getOrCreateUser(ui *domain.ContextUserInfo, tx *gorm.DB, id do
|
|||||||
UpdatedAt: time.Now(),
|
UpdatedAt: time.Now(),
|
||||||
},
|
},
|
||||||
Identifier: id,
|
Identifier: id,
|
||||||
Source: domain.UserSourceDatabase,
|
|
||||||
IsAdmin: false,
|
IsAdmin: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
err := tx.Attrs(userDefaults).FirstOrCreate(&user, id).Error
|
err := tx.Create(&userDefaults).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &user, nil
|
return &userDefaults, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *SqlRepo) upsertUser(ui *domain.ContextUserInfo, tx *gorm.DB, user *domain.User) error {
|
func (r *SqlRepo) upsertUser(ui *domain.ContextUserInfo, tx *gorm.DB, user *domain.User) error {
|
||||||
@@ -923,6 +1007,11 @@ func (r *SqlRepo) upsertUser(ui *domain.ContextUserInfo, tx *gorm.DB, user *doma
|
|||||||
return fmt.Errorf("failed to update users webauthn credentials: %w", err)
|
return fmt.Errorf("failed to update users webauthn credentials: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = tx.Session(&gorm.Session{FullSaveAssociations: true}).Unscoped().Model(user).Association("Authentications").Unscoped().Replace(user.Authentications)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to update users authentications: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -626,7 +626,7 @@ func (c LocalController) exec(command string, interfaceId domain.InterfaceIdenti
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Warn("failed to executed shell command",
|
slog.Warn("failed to executed shell command",
|
||||||
"command", commandWithInterfaceName, "stdin", stdin, "output", string(out), "error", err)
|
"command", commandWithInterfaceName, "stdin", stdin, "output", string(out), "error", err)
|
||||||
return fmt.Errorf("failed to exexute shell command %s: %w", commandWithInterfaceName, err)
|
return fmt.Errorf("failed to execute shell command %s: %w", commandWithInterfaceName, err)
|
||||||
}
|
}
|
||||||
slog.Debug("executed shell command",
|
slog.Debug("executed shell command",
|
||||||
"command", commandWithInterfaceName,
|
"command", commandWithInterfaceName,
|
||||||
|
|||||||
@@ -2676,6 +2676,12 @@
|
|||||||
"ApiTokenCreated": {
|
"ApiTokenCreated": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
|
"AuthSources": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
"Department": {
|
"Department": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
@@ -2719,14 +2725,11 @@
|
|||||||
"PeerCount": {
|
"PeerCount": {
|
||||||
"type": "integer"
|
"type": "integer"
|
||||||
},
|
},
|
||||||
|
"PersistLocalChanges": {
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
"Phone": {
|
"Phone": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
|
||||||
"ProviderName": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"Source": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -431,6 +431,10 @@ definitions:
|
|||||||
type: string
|
type: string
|
||||||
ApiTokenCreated:
|
ApiTokenCreated:
|
||||||
type: string
|
type: string
|
||||||
|
AuthSources:
|
||||||
|
items:
|
||||||
|
type: string
|
||||||
|
type: array
|
||||||
Department:
|
Department:
|
||||||
type: string
|
type: string
|
||||||
Disabled:
|
Disabled:
|
||||||
@@ -461,12 +465,10 @@ definitions:
|
|||||||
type: string
|
type: string
|
||||||
PeerCount:
|
PeerCount:
|
||||||
type: integer
|
type: integer
|
||||||
|
PersistLocalChanges:
|
||||||
|
type: boolean
|
||||||
Phone:
|
Phone:
|
||||||
type: string
|
type: string
|
||||||
ProviderName:
|
|
||||||
type: string
|
|
||||||
Source:
|
|
||||||
type: string
|
|
||||||
type: object
|
type: object
|
||||||
model.WebAuthnCredentialRequest:
|
model.WebAuthnCredentialRequest:
|
||||||
properties:
|
properties:
|
||||||
|
|||||||
@@ -2132,6 +2132,22 @@
|
|||||||
"minLength": 32,
|
"minLength": 32,
|
||||||
"example": ""
|
"example": ""
|
||||||
},
|
},
|
||||||
|
"AuthSources": {
|
||||||
|
"description": "The source of the user. This field is optional.",
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": [
|
||||||
|
"db",
|
||||||
|
"ldap",
|
||||||
|
"oauth"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"readOnly": true,
|
||||||
|
"example": [
|
||||||
|
"db"
|
||||||
|
]
|
||||||
|
},
|
||||||
"Department": {
|
"Department": {
|
||||||
"description": "The department of the user. This field is optional.",
|
"description": "The department of the user. This field is optional.",
|
||||||
"type": "string",
|
"type": "string",
|
||||||
@@ -2205,22 +2221,6 @@
|
|||||||
"description": "The phone number of the user. This field is optional.",
|
"description": "The phone number of the user. This field is optional.",
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"example": "+1234546789"
|
"example": "+1234546789"
|
||||||
},
|
|
||||||
"ProviderName": {
|
|
||||||
"description": "The name of the authentication provider. This field is read-only.",
|
|
||||||
"type": "string",
|
|
||||||
"readOnly": true,
|
|
||||||
"example": ""
|
|
||||||
},
|
|
||||||
"Source": {
|
|
||||||
"description": "The source of the user. This field is optional.",
|
|
||||||
"type": "string",
|
|
||||||
"enum": [
|
|
||||||
"db",
|
|
||||||
"ldap",
|
|
||||||
"oauth"
|
|
||||||
],
|
|
||||||
"example": "db"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -490,6 +490,18 @@ definitions:
|
|||||||
maxLength: 64
|
maxLength: 64
|
||||||
minLength: 32
|
minLength: 32
|
||||||
type: string
|
type: string
|
||||||
|
AuthSources:
|
||||||
|
description: The source of the user. This field is optional.
|
||||||
|
example:
|
||||||
|
- db
|
||||||
|
items:
|
||||||
|
enum:
|
||||||
|
- db
|
||||||
|
- ldap
|
||||||
|
- oauth
|
||||||
|
type: string
|
||||||
|
readOnly: true
|
||||||
|
type: array
|
||||||
Department:
|
Department:
|
||||||
description: The department of the user. This field is optional.
|
description: The department of the user. This field is optional.
|
||||||
example: Software Development
|
example: Software Development
|
||||||
@@ -552,19 +564,6 @@ definitions:
|
|||||||
description: The phone number of the user. This field is optional.
|
description: The phone number of the user. This field is optional.
|
||||||
example: "+1234546789"
|
example: "+1234546789"
|
||||||
type: string
|
type: string
|
||||||
ProviderName:
|
|
||||||
description: The name of the authentication provider. This field is read-only.
|
|
||||||
example: ""
|
|
||||||
readOnly: true
|
|
||||||
type: string
|
|
||||||
Source:
|
|
||||||
description: The source of the user. This field is optional.
|
|
||||||
enum:
|
|
||||||
- db
|
|
||||||
- ldap
|
|
||||||
- oauth
|
|
||||||
example: db
|
|
||||||
type: string
|
|
||||||
required:
|
required:
|
||||||
- Identifier
|
- Identifier
|
||||||
type: object
|
type: object
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package logging
|
package logging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -38,6 +40,12 @@ func (w *writerWrapper) Write(data []byte) (int, error) {
|
|||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hijack wraps the Hijack method of the ResponseWriter and returns the hijacked connection.
|
||||||
|
// This is required for websockets to work.
|
||||||
|
func (w *writerWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||||
|
return http.NewResponseController(w.ResponseWriter).Hijack()
|
||||||
|
}
|
||||||
|
|
||||||
// newWriterWrapper returns a new writerWrapper that wraps the given http.ResponseWriter.
|
// newWriterWrapper returns a new writerWrapper that wraps the given http.ResponseWriter.
|
||||||
// It initializes the StatusCode to http.StatusOK.
|
// It initializes the StatusCode to http.StatusOK.
|
||||||
func newWriterWrapper(w http.ResponseWriter) *writerWrapper {
|
func newWriterWrapper(w http.ResponseWriter) *writerWrapper {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package backend
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/h44z/wg-portal/internal/config"
|
"github.com/h44z/wg-portal/internal/config"
|
||||||
@@ -95,8 +96,10 @@ func (u UserService) ChangePassword(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ensure that the user uses the database backend; otherwise we can't change the password
|
// ensure that the user uses the database backend; otherwise we can't change the password
|
||||||
if user.Source != domain.UserSourceDatabase {
|
if !slices.ContainsFunc(user.Authentications, func(authentication domain.UserAuthentication) bool {
|
||||||
return nil, fmt.Errorf("user source %s does not support password changes", user.Source)
|
return authentication.Source == domain.UserSourceDatabase
|
||||||
|
}) {
|
||||||
|
return nil, fmt.Errorf("user has no linked authentication source that does support password changes")
|
||||||
}
|
}
|
||||||
|
|
||||||
// validate old password
|
// validate old password
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-pkgz/routegroup"
|
"github.com/go-pkgz/routegroup"
|
||||||
@@ -449,7 +448,17 @@ func (e AuthEndpoint) handleLogoutPost() http.HandlerFunc {
|
|||||||
|
|
||||||
// isValidReturnUrl checks if the given return URL matches the configured external URL of the application.
|
// isValidReturnUrl checks if the given return URL matches the configured external URL of the application.
|
||||||
func (e AuthEndpoint) isValidReturnUrl(returnUrl string) bool {
|
func (e AuthEndpoint) isValidReturnUrl(returnUrl string) bool {
|
||||||
if !strings.HasPrefix(returnUrl, e.cfg.Web.ExternalUrl) {
|
expectedUrl, err := url.Parse(e.cfg.Web.ExternalUrl)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
returnUrlParsed, err := url.Parse(returnUrl)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if returnUrlParsed.Scheme != expectedUrl.Scheme || returnUrlParsed.Host != expectedUrl.Host {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -145,6 +145,7 @@ func (e ConfigEndpoint) handleSettingsGet() http.HandlerFunc {
|
|||||||
MinPasswordLength: e.cfg.Auth.MinPasswordLength,
|
MinPasswordLength: e.cfg.Auth.MinPasswordLength,
|
||||||
AvailableBackends: controllerFn(),
|
AvailableBackends: controllerFn(),
|
||||||
LoginFormVisible: !e.cfg.Auth.HideLoginForm || !hasSocialLogin,
|
LoginFormVisible: !e.cfg.Auth.HideLoginForm || !hasSocialLogin,
|
||||||
|
CreateDefaultPeer: e.cfg.Core.CreateDefaultPeer,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
100
internal/app/api/v0/handlers/endpoint_websocket.go
Normal file
100
internal/app/api/v0/handlers/endpoint_websocket.go
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/go-pkgz/routegroup"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
|
||||||
|
"github.com/h44z/wg-portal/internal/app"
|
||||||
|
"github.com/h44z/wg-portal/internal/config"
|
||||||
|
"github.com/h44z/wg-portal/internal/domain"
|
||||||
|
)
|
||||||
|
|
||||||
|
type WebsocketEventBus interface {
|
||||||
|
Subscribe(topic string, fn any) error
|
||||||
|
Unsubscribe(topic string, fn any) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type WebsocketEndpoint struct {
|
||||||
|
authenticator Authenticator
|
||||||
|
bus WebsocketEventBus
|
||||||
|
|
||||||
|
upgrader websocket.Upgrader
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWebsocketEndpoint(cfg *config.Config, auth Authenticator, bus WebsocketEventBus) *WebsocketEndpoint {
|
||||||
|
return &WebsocketEndpoint{
|
||||||
|
authenticator: auth,
|
||||||
|
bus: bus,
|
||||||
|
upgrader: websocket.Upgrader{
|
||||||
|
ReadBufferSize: 1024,
|
||||||
|
WriteBufferSize: 1024,
|
||||||
|
CheckOrigin: func(r *http.Request) bool {
|
||||||
|
origin := r.Header.Get("Origin")
|
||||||
|
return strings.HasPrefix(origin, cfg.Web.ExternalUrl)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e WebsocketEndpoint) GetName() string {
|
||||||
|
return "WebsocketEndpoint"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e WebsocketEndpoint) RegisterRoutes(g *routegroup.Bundle) {
|
||||||
|
g.With(e.authenticator.LoggedIn()).HandleFunc("GET /ws", e.handleWebsocket())
|
||||||
|
}
|
||||||
|
|
||||||
|
// wsMessage represents a message sent over websocket to the frontend
|
||||||
|
type wsMessage struct {
|
||||||
|
Type string `json:"type"` // either "peer_stats" or "interface_stats"
|
||||||
|
Data any `json:"data"` // domain.TrafficDelta
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e WebsocketEndpoint) handleWebsocket() http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
conn, err := e.upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(r.Context())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
writeMutex := sync.Mutex{}
|
||||||
|
writeJSON := func(msg wsMessage) error {
|
||||||
|
writeMutex.Lock()
|
||||||
|
defer writeMutex.Unlock()
|
||||||
|
return conn.WriteJSON(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
peerStatsHandler := func(status domain.TrafficDelta) {
|
||||||
|
_ = writeJSON(wsMessage{Type: "peer_stats", Data: status})
|
||||||
|
}
|
||||||
|
interfaceStatsHandler := func(status domain.TrafficDelta) {
|
||||||
|
_ = writeJSON(wsMessage{Type: "interface_stats", Data: status})
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = e.bus.Subscribe(app.TopicPeerStatsUpdated, peerStatsHandler)
|
||||||
|
defer e.bus.Unsubscribe(app.TopicPeerStatsUpdated, peerStatsHandler)
|
||||||
|
_ = e.bus.Subscribe(app.TopicInterfaceStatsUpdated, interfaceStatsHandler)
|
||||||
|
defer e.bus.Unsubscribe(app.TopicInterfaceStatsUpdated, interfaceStatsHandler)
|
||||||
|
|
||||||
|
// Keep connection open until client disconnects or context is cancelled
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
if _, _, err := conn.ReadMessage(); err != nil {
|
||||||
|
cancel()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
<-ctx.Done()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,6 +14,7 @@ type Settings struct {
|
|||||||
MinPasswordLength int `json:"MinPasswordLength"`
|
MinPasswordLength int `json:"MinPasswordLength"`
|
||||||
AvailableBackends []SettingsBackendNames `json:"AvailableBackends"`
|
AvailableBackends []SettingsBackendNames `json:"AvailableBackends"`
|
||||||
LoginFormVisible bool `json:"LoginFormVisible"`
|
LoginFormVisible bool `json:"LoginFormVisible"`
|
||||||
|
CreateDefaultPeer bool `json:"CreateDefaultPeer"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SettingsBackendNames struct {
|
type SettingsBackendNames struct {
|
||||||
|
|||||||
@@ -9,15 +9,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Interface struct {
|
type Interface struct {
|
||||||
Identifier string `json:"Identifier" example:"wg0"` // device name, for example: wg0
|
Identifier string `json:"Identifier" example:"wg0"` // device name, for example: wg0
|
||||||
DisplayName string `json:"DisplayName"` // a nice display name/ description for the interface
|
DisplayName string `json:"DisplayName"` // a nice display name/ description for the interface
|
||||||
Mode string `json:"Mode" example:"server"` // the interface type, either 'server', 'client' or 'any'
|
Mode string `json:"Mode" example:"server"` // the interface type, either 'server', 'client' or 'any'
|
||||||
Backend string `json:"Backend" example:"local"` // the backend used for this interface e.g., local, mikrotik, ...
|
Backend string `json:"Backend" example:"local"` // the backend used for this interface e.g., local, mikrotik, ...
|
||||||
PrivateKey string `json:"PrivateKey" example:"abcdef=="` // private Key of the server interface
|
PrivateKey string `json:"PrivateKey" example:"abcdef=="` // private Key of the server interface
|
||||||
PublicKey string `json:"PublicKey" example:"abcdef=="` // public Key of the server interface
|
PublicKey string `json:"PublicKey" example:"abcdef=="` // public Key of the server interface
|
||||||
Disabled bool `json:"Disabled"` // flag that specifies if the interface is enabled (up) or not (down)
|
Disabled bool `json:"Disabled"` // flag that specifies if the interface is enabled (up) or not (down)
|
||||||
DisabledReason string `json:"DisabledReason"` // the reason why the interface has been disabled
|
DisabledReason string `json:"DisabledReason"` // the reason why the interface has been disabled
|
||||||
SaveConfig bool `json:"SaveConfig"` // automatically persist config changes to the wgX.conf file
|
SaveConfig bool `json:"SaveConfig"` // automatically persist config changes to the wgX.conf file
|
||||||
|
CreateDefaultPeer bool `json:"CreateDefaultPeer"` // if true, default peers will be created for this interface
|
||||||
|
|
||||||
ListenPort int `json:"ListenPort"` // the listening port, for example: 51820
|
ListenPort int `json:"ListenPort"` // the listening port, for example: 51820
|
||||||
Addresses []string `json:"Addresses"` // the interface ip addresses
|
Addresses []string `json:"Addresses"` // the interface ip addresses
|
||||||
@@ -65,6 +66,7 @@ func NewInterface(src *domain.Interface, peers []domain.Peer) *Interface {
|
|||||||
Disabled: src.IsDisabled(),
|
Disabled: src.IsDisabled(),
|
||||||
DisabledReason: src.DisabledReason,
|
DisabledReason: src.DisabledReason,
|
||||||
SaveConfig: src.SaveConfig,
|
SaveConfig: src.SaveConfig,
|
||||||
|
CreateDefaultPeer: src.CreateDefaultPeer,
|
||||||
ListenPort: src.ListenPort,
|
ListenPort: src.ListenPort,
|
||||||
Addresses: domain.CidrsToStringSlice(src.Addresses),
|
Addresses: domain.CidrsToStringSlice(src.Addresses),
|
||||||
Dns: internal.SliceString(src.DnsStr),
|
Dns: internal.SliceString(src.DnsStr),
|
||||||
@@ -151,6 +153,7 @@ func NewDomainInterface(src *Interface) *domain.Interface {
|
|||||||
PreDown: src.PreDown,
|
PreDown: src.PreDown,
|
||||||
PostDown: src.PostDown,
|
PostDown: src.PostDown,
|
||||||
SaveConfig: src.SaveConfig,
|
SaveConfig: src.SaveConfig,
|
||||||
|
CreateDefaultPeer: src.CreateDefaultPeer,
|
||||||
DisplayName: src.DisplayName,
|
DisplayName: src.DisplayName,
|
||||||
Type: domain.InterfaceType(src.Mode),
|
Type: domain.InterfaceType(src.Mode),
|
||||||
Backend: domain.InterfaceBackend(src.Backend),
|
Backend: domain.InterfaceBackend(src.Backend),
|
||||||
|
|||||||
@@ -3,15 +3,15 @@ package model
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/h44z/wg-portal/internal"
|
||||||
"github.com/h44z/wg-portal/internal/domain"
|
"github.com/h44z/wg-portal/internal/domain"
|
||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
Identifier string `json:"Identifier"`
|
Identifier string `json:"Identifier"`
|
||||||
Email string `json:"Email"`
|
Email string `json:"Email"`
|
||||||
Source string `json:"Source"`
|
AuthSources []string `json:"AuthSources"`
|
||||||
ProviderName string `json:"ProviderName"`
|
IsAdmin bool `json:"IsAdmin"`
|
||||||
IsAdmin bool `json:"IsAdmin"`
|
|
||||||
|
|
||||||
Firstname string `json:"Firstname"`
|
Firstname string `json:"Firstname"`
|
||||||
Lastname string `json:"Lastname"`
|
Lastname string `json:"Lastname"`
|
||||||
@@ -29,6 +29,8 @@ type User struct {
|
|||||||
ApiTokenCreated *time.Time `json:"ApiTokenCreated,omitempty"`
|
ApiTokenCreated *time.Time `json:"ApiTokenCreated,omitempty"`
|
||||||
ApiEnabled bool `json:"ApiEnabled"`
|
ApiEnabled bool `json:"ApiEnabled"`
|
||||||
|
|
||||||
|
PersistLocalChanges bool `json:"PersistLocalChanges"`
|
||||||
|
|
||||||
// Calculated
|
// Calculated
|
||||||
|
|
||||||
PeerCount int `json:"PeerCount"`
|
PeerCount int `json:"PeerCount"`
|
||||||
@@ -36,24 +38,26 @@ type User struct {
|
|||||||
|
|
||||||
func NewUser(src *domain.User, exposeCreds bool) *User {
|
func NewUser(src *domain.User, exposeCreds bool) *User {
|
||||||
u := &User{
|
u := &User{
|
||||||
Identifier: string(src.Identifier),
|
Identifier: string(src.Identifier),
|
||||||
Email: src.Email,
|
Email: src.Email,
|
||||||
Source: string(src.Source),
|
AuthSources: internal.Map(src.Authentications, func(authentication domain.UserAuthentication) string {
|
||||||
ProviderName: src.ProviderName,
|
return string(authentication.Source)
|
||||||
IsAdmin: src.IsAdmin,
|
}),
|
||||||
Firstname: src.Firstname,
|
IsAdmin: src.IsAdmin,
|
||||||
Lastname: src.Lastname,
|
Firstname: src.Firstname,
|
||||||
Phone: src.Phone,
|
Lastname: src.Lastname,
|
||||||
Department: src.Department,
|
Phone: src.Phone,
|
||||||
Notes: src.Notes,
|
Department: src.Department,
|
||||||
Password: "", // never fill password
|
Notes: src.Notes,
|
||||||
Disabled: src.IsDisabled(),
|
Password: "", // never fill password
|
||||||
DisabledReason: src.DisabledReason,
|
Disabled: src.IsDisabled(),
|
||||||
Locked: src.IsLocked(),
|
DisabledReason: src.DisabledReason,
|
||||||
LockedReason: src.LockedReason,
|
Locked: src.IsLocked(),
|
||||||
ApiToken: "", // by default, do not expose API token
|
LockedReason: src.LockedReason,
|
||||||
ApiTokenCreated: src.ApiTokenCreated,
|
ApiToken: "", // by default, do not expose API token
|
||||||
ApiEnabled: src.IsApiEnabled(),
|
ApiTokenCreated: src.ApiTokenCreated,
|
||||||
|
ApiEnabled: src.IsApiEnabled(),
|
||||||
|
PersistLocalChanges: src.PersistLocalChanges,
|
||||||
|
|
||||||
PeerCount: src.LinkedPeerCount,
|
PeerCount: src.LinkedPeerCount,
|
||||||
}
|
}
|
||||||
@@ -77,22 +81,21 @@ func NewUsers(src []domain.User) []User {
|
|||||||
func NewDomainUser(src *User) *domain.User {
|
func NewDomainUser(src *User) *domain.User {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
res := &domain.User{
|
res := &domain.User{
|
||||||
Identifier: domain.UserIdentifier(src.Identifier),
|
Identifier: domain.UserIdentifier(src.Identifier),
|
||||||
Email: src.Email,
|
Email: src.Email,
|
||||||
Source: domain.UserSource(src.Source),
|
IsAdmin: src.IsAdmin,
|
||||||
ProviderName: src.ProviderName,
|
Firstname: src.Firstname,
|
||||||
IsAdmin: src.IsAdmin,
|
Lastname: src.Lastname,
|
||||||
Firstname: src.Firstname,
|
Phone: src.Phone,
|
||||||
Lastname: src.Lastname,
|
Department: src.Department,
|
||||||
Phone: src.Phone,
|
Notes: src.Notes,
|
||||||
Department: src.Department,
|
Password: domain.PrivateString(src.Password),
|
||||||
Notes: src.Notes,
|
Disabled: nil, // set below
|
||||||
Password: domain.PrivateString(src.Password),
|
DisabledReason: src.DisabledReason,
|
||||||
Disabled: nil, // set below
|
Locked: nil, // set below
|
||||||
DisabledReason: src.DisabledReason,
|
LockedReason: src.LockedReason,
|
||||||
Locked: nil, // set below
|
LinkedPeerCount: src.PeerCount,
|
||||||
LockedReason: src.LockedReason,
|
PersistLocalChanges: src.PersistLocalChanges,
|
||||||
LinkedPeerCount: src.PeerCount,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if src.Disabled {
|
if src.Disabled {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/h44z/wg-portal/internal"
|
||||||
"github.com/h44z/wg-portal/internal/domain"
|
"github.com/h44z/wg-portal/internal/domain"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,9 +14,7 @@ type User struct {
|
|||||||
// The email address of the user. This field is optional.
|
// The email address of the user. This field is optional.
|
||||||
Email string `json:"Email" binding:"omitempty,email" example:"test@test.com"`
|
Email string `json:"Email" binding:"omitempty,email" example:"test@test.com"`
|
||||||
// The source of the user. This field is optional.
|
// The source of the user. This field is optional.
|
||||||
Source string `json:"Source" binding:"oneof=db ldap oauth" example:"db"`
|
AuthSources []string `json:"AuthSources" readonly:"true" binding:"oneof=db ldap oauth" example:"db"`
|
||||||
// The name of the authentication provider. This field is read-only.
|
|
||||||
ProviderName string `json:"ProviderName,omitempty" readonly:"true" example:""`
|
|
||||||
// If this field is set, the user is an admin.
|
// If this field is set, the user is an admin.
|
||||||
IsAdmin bool `json:"IsAdmin" example:"false"`
|
IsAdmin bool `json:"IsAdmin" example:"false"`
|
||||||
|
|
||||||
@@ -52,10 +51,11 @@ type User struct {
|
|||||||
|
|
||||||
func NewUser(src *domain.User, exposeCredentials bool) *User {
|
func NewUser(src *domain.User, exposeCredentials bool) *User {
|
||||||
u := &User{
|
u := &User{
|
||||||
Identifier: string(src.Identifier),
|
Identifier: string(src.Identifier),
|
||||||
Email: src.Email,
|
Email: src.Email,
|
||||||
Source: string(src.Source),
|
AuthSources: internal.Map(src.Authentications, func(authentication domain.UserAuthentication) string {
|
||||||
ProviderName: src.ProviderName,
|
return string(authentication.Source)
|
||||||
|
}),
|
||||||
IsAdmin: src.IsAdmin,
|
IsAdmin: src.IsAdmin,
|
||||||
Firstname: src.Firstname,
|
Firstname: src.Firstname,
|
||||||
Lastname: src.Lastname,
|
Lastname: src.Lastname,
|
||||||
@@ -93,8 +93,6 @@ func NewDomainUser(src *User) *domain.User {
|
|||||||
res := &domain.User{
|
res := &domain.User{
|
||||||
Identifier: domain.UserIdentifier(src.Identifier),
|
Identifier: domain.UserIdentifier(src.Identifier),
|
||||||
Email: src.Email,
|
Email: src.Email,
|
||||||
Source: domain.UserSource(src.Source),
|
|
||||||
ProviderName: src.ProviderName,
|
|
||||||
IsAdmin: src.IsAdmin,
|
IsAdmin: src.IsAdmin,
|
||||||
Firstname: src.Firstname,
|
Firstname: src.Firstname,
|
||||||
Lastname: src.Lastname,
|
Lastname: src.Lastname,
|
||||||
|
|||||||
@@ -129,8 +129,6 @@ func (a *App) createDefaultUser(ctx context.Context) error {
|
|||||||
},
|
},
|
||||||
Identifier: adminUserId,
|
Identifier: adminUserId,
|
||||||
Email: "admin@wgportal.local",
|
Email: "admin@wgportal.local",
|
||||||
Source: domain.UserSourceDatabase,
|
|
||||||
ProviderName: "",
|
|
||||||
IsAdmin: true,
|
IsAdmin: true,
|
||||||
Firstname: "WireGuard Portal",
|
Firstname: "WireGuard Portal",
|
||||||
Lastname: "Admin",
|
Lastname: "Admin",
|
||||||
|
|||||||
@@ -29,8 +29,8 @@ type UserManager interface {
|
|||||||
GetUser(context.Context, domain.UserIdentifier) (*domain.User, error)
|
GetUser(context.Context, domain.UserIdentifier) (*domain.User, error)
|
||||||
// RegisterUser creates a new user in the database.
|
// RegisterUser creates a new user in the database.
|
||||||
RegisterUser(ctx context.Context, user *domain.User) error
|
RegisterUser(ctx context.Context, user *domain.User) error
|
||||||
// UpdateUser updates an existing user in the database.
|
// UpdateUserInternal updates an existing user in the database.
|
||||||
UpdateUser(ctx context.Context, user *domain.User) (*domain.User, error)
|
UpdateUserInternal(ctx context.Context, user *domain.User) (*domain.User, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventBus interface {
|
type EventBus interface {
|
||||||
@@ -232,7 +232,7 @@ func (a *Authenticator) setupExternalAuthProviders(
|
|||||||
}
|
}
|
||||||
for i := range ldap { // LDAP
|
for i := range ldap { // LDAP
|
||||||
providerCfg := &ldap[i]
|
providerCfg := &ldap[i]
|
||||||
providerId := strings.ToLower(providerCfg.URL)
|
providerId := strings.ToLower(providerCfg.ProviderName)
|
||||||
|
|
||||||
if _, exists := a.ldapAuthenticators[providerId]; exists {
|
if _, exists := a.ldapAuthenticators[providerId]; exists {
|
||||||
// this is an unrecoverable error, we cannot register the same provider twice
|
// this is an unrecoverable error, we cannot register the same provider twice
|
||||||
@@ -354,21 +354,45 @@ func (a *Authenticator) passwordAuthentication(
|
|||||||
var ldapProvider AuthenticatorLdap
|
var ldapProvider AuthenticatorLdap
|
||||||
|
|
||||||
var userInDatabase = false
|
var userInDatabase = false
|
||||||
var userSource domain.UserSource
|
|
||||||
existingUser, err := a.users.GetUser(ctx, identifier)
|
existingUser, err := a.users.GetUser(ctx, identifier)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
userInDatabase = true
|
userInDatabase = true
|
||||||
userSource = existingUser.Source
|
|
||||||
}
|
}
|
||||||
if userInDatabase && (existingUser.IsLocked() || existingUser.IsDisabled()) {
|
if userInDatabase && (existingUser.IsLocked() || existingUser.IsDisabled()) {
|
||||||
return nil, errors.New("user is locked")
|
return nil, errors.New("user is locked")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !userInDatabase || userSource == domain.UserSourceLdap {
|
authOK := false
|
||||||
// search user in ldap if registration is enabled
|
if userInDatabase {
|
||||||
|
// User is already in db, search for authentication sources which support password authentication and
|
||||||
|
// validate the password.
|
||||||
|
for _, authentication := range existingUser.Authentications {
|
||||||
|
if authentication.Source == domain.UserSourceDatabase {
|
||||||
|
err := existingUser.CheckPassword(password)
|
||||||
|
if err == nil {
|
||||||
|
authOK = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if authentication.Source == domain.UserSourceLdap {
|
||||||
|
ldapProvider, ok := a.ldapAuthenticators[strings.ToLower(authentication.ProviderName)]
|
||||||
|
if !ok {
|
||||||
|
continue // ldap provider not found, skip further checks
|
||||||
|
}
|
||||||
|
err := ldapProvider.PlaintextAuthentication(identifier, password)
|
||||||
|
if err == nil {
|
||||||
|
authOK = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// User is not yet in the db, check ldap providers which have registration enabled.
|
||||||
|
// If the user is found, check the password - on success, sync it to the db.
|
||||||
for _, ldapAuth := range a.ldapAuthenticators {
|
for _, ldapAuth := range a.ldapAuthenticators {
|
||||||
if !userInDatabase && !ldapAuth.RegistrationEnabled() {
|
if !ldapAuth.RegistrationEnabled() {
|
||||||
continue
|
continue // ldap provider does not support registration, skip further checks
|
||||||
}
|
}
|
||||||
|
|
||||||
rawUserInfo, err := ldapAuth.GetUserInfo(context.Background(), identifier)
|
rawUserInfo, err := ldapAuth.GetUserInfo(context.Background(), identifier)
|
||||||
@@ -379,55 +403,39 @@ func (a *Authenticator) passwordAuthentication(
|
|||||||
}
|
}
|
||||||
continue // user not found / other ldap error
|
continue // user not found / other ldap error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// user found, check if the password is correct
|
||||||
|
err = ldapAuth.PlaintextAuthentication(identifier, password)
|
||||||
|
if err != nil {
|
||||||
|
continue // password is incorrect, skip further checks
|
||||||
|
}
|
||||||
|
|
||||||
|
// create a new user in the db
|
||||||
ldapUserInfo, err = ldapAuth.ParseUserInfo(rawUserInfo)
|
ldapUserInfo, err = ldapAuth.ParseUserInfo(rawUserInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("failed to parse ldap user info",
|
slog.Error("failed to parse ldap user info",
|
||||||
"source", ldapAuth.GetName(), "identifier", identifier, "error", err)
|
"source", ldapAuth.GetName(), "identifier", identifier, "error", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
user, err := a.processUserInfo(ctx, ldapUserInfo, domain.UserSourceLdap, ldapProvider.GetName(), true)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to process user information: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
// ldap user found
|
existingUser = user
|
||||||
userSource = domain.UserSourceLdap
|
slog.Debug("created new LDAP user in db",
|
||||||
ldapProvider = ldapAuth
|
"identifier", user.Identifier, "provider", ldapProvider.GetName())
|
||||||
|
|
||||||
|
authOK = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if userSource == "" {
|
if !authOK {
|
||||||
slog.Warn("no user source found for user",
|
return nil, errors.New("failed to authenticate user")
|
||||||
"identifier", identifier, "ldapProviderCount", len(a.ldapAuthenticators), "inDb", userInDatabase)
|
|
||||||
return nil, errors.New("user not found")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if userSource == domain.UserSourceLdap && ldapProvider == nil {
|
return existingUser, nil
|
||||||
slog.Warn("no ldap provider found for user",
|
|
||||||
"identifier", identifier, "ldapProviderCount", len(a.ldapAuthenticators), "inDb", userInDatabase)
|
|
||||||
return nil, errors.New("ldap provider not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
switch userSource {
|
|
||||||
case domain.UserSourceDatabase:
|
|
||||||
err = existingUser.CheckPassword(password)
|
|
||||||
case domain.UserSourceLdap:
|
|
||||||
err = ldapProvider.PlaintextAuthentication(identifier, password)
|
|
||||||
default:
|
|
||||||
err = errors.New("no authentication backend available")
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to authenticate: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !userInDatabase {
|
|
||||||
user, err := a.processUserInfo(ctx, ldapUserInfo, domain.UserSourceLdap, ldapProvider.GetName(),
|
|
||||||
ldapProvider.RegistrationEnabled())
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("unable to process user information: %w", err)
|
|
||||||
}
|
|
||||||
return user, nil
|
|
||||||
} else {
|
|
||||||
return existingUser, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// endregion password authentication
|
// endregion password authentication
|
||||||
@@ -590,17 +598,34 @@ func (a *Authenticator) registerNewUser(
|
|||||||
source domain.UserSource,
|
source domain.UserSource,
|
||||||
provider string,
|
provider string,
|
||||||
) (*domain.User, error) {
|
) (*domain.User, error) {
|
||||||
|
ctxUserInfo := domain.GetUserInfo(ctx)
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
// convert user info to domain.User
|
// convert user info to domain.User
|
||||||
user := &domain.User{
|
user := &domain.User{
|
||||||
Identifier: userInfo.Identifier,
|
Identifier: userInfo.Identifier,
|
||||||
Email: userInfo.Email,
|
Email: userInfo.Email,
|
||||||
Source: source,
|
IsAdmin: false,
|
||||||
ProviderName: provider,
|
Firstname: userInfo.Firstname,
|
||||||
IsAdmin: userInfo.IsAdmin,
|
Lastname: userInfo.Lastname,
|
||||||
Firstname: userInfo.Firstname,
|
Phone: userInfo.Phone,
|
||||||
Lastname: userInfo.Lastname,
|
Department: userInfo.Department,
|
||||||
Phone: userInfo.Phone,
|
Authentications: []domain.UserAuthentication{
|
||||||
Department: userInfo.Department,
|
{
|
||||||
|
BaseModel: domain.BaseModel{
|
||||||
|
CreatedBy: ctxUserInfo.UserId(),
|
||||||
|
UpdatedBy: ctxUserInfo.UserId(),
|
||||||
|
CreatedAt: now,
|
||||||
|
UpdatedAt: now,
|
||||||
|
},
|
||||||
|
UserIdentifier: userInfo.Identifier,
|
||||||
|
Source: source,
|
||||||
|
ProviderName: provider,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if userInfo.AdminInfoAvailable && userInfo.IsAdmin {
|
||||||
|
user.IsAdmin = true
|
||||||
}
|
}
|
||||||
|
|
||||||
err := a.users.RegisterUser(ctx, user)
|
err := a.users.RegisterUser(ctx, user)
|
||||||
@@ -610,6 +635,7 @@ func (a *Authenticator) registerNewUser(
|
|||||||
|
|
||||||
slog.Debug("registered user from external authentication provider",
|
slog.Debug("registered user from external authentication provider",
|
||||||
"user", user.Identifier,
|
"user", user.Identifier,
|
||||||
|
"adminInfoAvailable", userInfo.AdminInfoAvailable,
|
||||||
"isAdmin", user.IsAdmin,
|
"isAdmin", user.IsAdmin,
|
||||||
"provider", source)
|
"provider", source)
|
||||||
|
|
||||||
@@ -643,6 +669,39 @@ func (a *Authenticator) updateExternalUser(
|
|||||||
return nil // user is locked or disabled, do not update
|
return nil // user is locked or disabled, do not update
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update authentication sources
|
||||||
|
foundAuthSource := false
|
||||||
|
for _, auth := range existingUser.Authentications {
|
||||||
|
if auth.Source == source && auth.ProviderName == provider {
|
||||||
|
foundAuthSource = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !foundAuthSource {
|
||||||
|
ctxUserInfo := domain.GetUserInfo(ctx)
|
||||||
|
now := time.Now()
|
||||||
|
existingUser.Authentications = append(existingUser.Authentications, domain.UserAuthentication{
|
||||||
|
BaseModel: domain.BaseModel{
|
||||||
|
CreatedBy: ctxUserInfo.UserId(),
|
||||||
|
UpdatedBy: ctxUserInfo.UserId(),
|
||||||
|
CreatedAt: now,
|
||||||
|
UpdatedAt: now,
|
||||||
|
},
|
||||||
|
UserIdentifier: existingUser.Identifier,
|
||||||
|
Source: source,
|
||||||
|
ProviderName: provider,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if existingUser.PersistLocalChanges {
|
||||||
|
if !foundAuthSource {
|
||||||
|
// Even if local changes are persisted, we need to save the new authentication source
|
||||||
|
_, err := a.users.UpdateUserInternal(ctx, existingUser)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
isChanged := false
|
isChanged := false
|
||||||
if existingUser.Email != userInfo.Email {
|
if existingUser.Email != userInfo.Email {
|
||||||
existingUser.Email = userInfo.Email
|
existingUser.Email = userInfo.Email
|
||||||
@@ -664,33 +723,24 @@ func (a *Authenticator) updateExternalUser(
|
|||||||
existingUser.Department = userInfo.Department
|
existingUser.Department = userInfo.Department
|
||||||
isChanged = true
|
isChanged = true
|
||||||
}
|
}
|
||||||
if existingUser.IsAdmin != userInfo.IsAdmin {
|
if userInfo.AdminInfoAvailable && existingUser.IsAdmin != userInfo.IsAdmin {
|
||||||
existingUser.IsAdmin = userInfo.IsAdmin
|
existingUser.IsAdmin = userInfo.IsAdmin
|
||||||
isChanged = true
|
isChanged = true
|
||||||
}
|
}
|
||||||
if existingUser.Source != source {
|
|
||||||
existingUser.Source = source
|
|
||||||
isChanged = true
|
|
||||||
}
|
|
||||||
if existingUser.ProviderName != provider {
|
|
||||||
existingUser.ProviderName = provider
|
|
||||||
isChanged = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if !isChanged {
|
if isChanged || !foundAuthSource {
|
||||||
return nil // nothing to update
|
_, err := a.users.UpdateUserInternal(ctx, existingUser)
|
||||||
}
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to update user: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
_, err := a.users.UpdateUser(ctx, existingUser)
|
slog.Debug("updated user with data from external authentication provider",
|
||||||
if err != nil {
|
"user", existingUser.Identifier,
|
||||||
return fmt.Errorf("failed to update user: %w", err)
|
"adminInfoAvailable", userInfo.AdminInfoAvailable,
|
||||||
|
"isAdmin", existingUser.IsAdmin,
|
||||||
|
"provider", source)
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Debug("updated user with data from external authentication provider",
|
|
||||||
"user", existingUser.Identifier,
|
|
||||||
"isAdmin", existingUser.IsAdmin,
|
|
||||||
"provider", source)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -127,18 +127,26 @@ func (l LdapAuthenticator) GetUserInfo(_ context.Context, userId domain.UserIden
|
|||||||
|
|
||||||
// ParseUserInfo parses the user information from the LDAP server into a domain.AuthenticatorUserInfo struct.
|
// ParseUserInfo parses the user information from the LDAP server into a domain.AuthenticatorUserInfo struct.
|
||||||
func (l LdapAuthenticator) ParseUserInfo(raw map[string]any) (*domain.AuthenticatorUserInfo, error) {
|
func (l LdapAuthenticator) ParseUserInfo(raw map[string]any) (*domain.AuthenticatorUserInfo, error) {
|
||||||
isAdmin, err := internal.LdapIsMemberOf(raw[l.cfg.FieldMap.GroupMembership].([][]byte), l.cfg.ParsedAdminGroupDN)
|
isAdmin := false
|
||||||
if err != nil {
|
adminInfoAvailable := false
|
||||||
return nil, fmt.Errorf("failed to check admin group: %w", err)
|
if l.cfg.FieldMap.GroupMembership != "" {
|
||||||
|
adminInfoAvailable = true
|
||||||
|
var err error
|
||||||
|
isAdmin, err = internal.LdapIsMemberOf(raw[l.cfg.FieldMap.GroupMembership].([][]byte), l.cfg.ParsedAdminGroupDN)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to check admin group: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
userInfo := &domain.AuthenticatorUserInfo{
|
userInfo := &domain.AuthenticatorUserInfo{
|
||||||
Identifier: domain.UserIdentifier(internal.MapDefaultString(raw, l.cfg.FieldMap.UserIdentifier, "")),
|
Identifier: domain.UserIdentifier(internal.MapDefaultString(raw, l.cfg.FieldMap.UserIdentifier, "")),
|
||||||
Email: internal.MapDefaultString(raw, l.cfg.FieldMap.Email, ""),
|
Email: internal.MapDefaultString(raw, l.cfg.FieldMap.Email, ""),
|
||||||
Firstname: internal.MapDefaultString(raw, l.cfg.FieldMap.Firstname, ""),
|
Firstname: internal.MapDefaultString(raw, l.cfg.FieldMap.Firstname, ""),
|
||||||
Lastname: internal.MapDefaultString(raw, l.cfg.FieldMap.Lastname, ""),
|
Lastname: internal.MapDefaultString(raw, l.cfg.FieldMap.Lastname, ""),
|
||||||
Phone: internal.MapDefaultString(raw, l.cfg.FieldMap.Phone, ""),
|
Phone: internal.MapDefaultString(raw, l.cfg.FieldMap.Phone, ""),
|
||||||
Department: internal.MapDefaultString(raw, l.cfg.FieldMap.Department, ""),
|
Department: internal.MapDefaultString(raw, l.cfg.FieldMap.Department, ""),
|
||||||
IsAdmin: isAdmin,
|
IsAdmin: isAdmin,
|
||||||
|
AdminInfoAvailable: adminInfoAvailable,
|
||||||
}
|
}
|
||||||
|
|
||||||
return userInfo, nil
|
return userInfo, nil
|
||||||
|
|||||||
@@ -15,9 +15,11 @@ func parseOauthUserInfo(
|
|||||||
raw map[string]any,
|
raw map[string]any,
|
||||||
) (*domain.AuthenticatorUserInfo, error) {
|
) (*domain.AuthenticatorUserInfo, error) {
|
||||||
var isAdmin bool
|
var isAdmin bool
|
||||||
|
var adminInfoAvailable bool
|
||||||
|
|
||||||
// first try to match the is_admin field against the given regex
|
// first try to match the is_admin field against the given regex
|
||||||
if mapping.IsAdmin != "" {
|
if mapping.IsAdmin != "" {
|
||||||
|
adminInfoAvailable = true
|
||||||
re := adminMapping.GetAdminValueRegex()
|
re := adminMapping.GetAdminValueRegex()
|
||||||
if re.MatchString(strings.TrimSpace(internal.MapDefaultString(raw, mapping.IsAdmin, ""))) {
|
if re.MatchString(strings.TrimSpace(internal.MapDefaultString(raw, mapping.IsAdmin, ""))) {
|
||||||
isAdmin = true
|
isAdmin = true
|
||||||
@@ -26,6 +28,7 @@ func parseOauthUserInfo(
|
|||||||
|
|
||||||
// next try to parse the user's groups
|
// next try to parse the user's groups
|
||||||
if !isAdmin && mapping.UserGroups != "" && adminMapping.AdminGroupRegex != "" {
|
if !isAdmin && mapping.UserGroups != "" && adminMapping.AdminGroupRegex != "" {
|
||||||
|
adminInfoAvailable = true
|
||||||
userGroups := internal.MapDefaultStringSlice(raw, mapping.UserGroups, nil)
|
userGroups := internal.MapDefaultStringSlice(raw, mapping.UserGroups, nil)
|
||||||
re := adminMapping.GetAdminGroupRegex()
|
re := adminMapping.GetAdminGroupRegex()
|
||||||
for _, group := range userGroups {
|
for _, group := range userGroups {
|
||||||
@@ -37,13 +40,14 @@ func parseOauthUserInfo(
|
|||||||
}
|
}
|
||||||
|
|
||||||
userInfo := &domain.AuthenticatorUserInfo{
|
userInfo := &domain.AuthenticatorUserInfo{
|
||||||
Identifier: domain.UserIdentifier(internal.MapDefaultString(raw, mapping.UserIdentifier, "")),
|
Identifier: domain.UserIdentifier(internal.MapDefaultString(raw, mapping.UserIdentifier, "")),
|
||||||
Email: internal.MapDefaultString(raw, mapping.Email, ""),
|
Email: internal.MapDefaultString(raw, mapping.Email, ""),
|
||||||
Firstname: internal.MapDefaultString(raw, mapping.Firstname, ""),
|
Firstname: internal.MapDefaultString(raw, mapping.Firstname, ""),
|
||||||
Lastname: internal.MapDefaultString(raw, mapping.Lastname, ""),
|
Lastname: internal.MapDefaultString(raw, mapping.Lastname, ""),
|
||||||
Phone: internal.MapDefaultString(raw, mapping.Phone, ""),
|
Phone: internal.MapDefaultString(raw, mapping.Phone, ""),
|
||||||
Department: internal.MapDefaultString(raw, mapping.Department, ""),
|
Department: internal.MapDefaultString(raw, mapping.Department, ""),
|
||||||
IsAdmin: isAdmin,
|
IsAdmin: isAdmin,
|
||||||
|
AdminInfoAvailable: adminInfoAvailable,
|
||||||
}
|
}
|
||||||
|
|
||||||
return userInfo, nil
|
return userInfo, nil
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ type WebAuthnUserManager interface {
|
|||||||
GetUser(context.Context, domain.UserIdentifier) (*domain.User, error)
|
GetUser(context.Context, domain.UserIdentifier) (*domain.User, error)
|
||||||
// GetUserByWebAuthnCredential returns a user by its WebAuthn ID.
|
// GetUserByWebAuthnCredential returns a user by its WebAuthn ID.
|
||||||
GetUserByWebAuthnCredential(ctx context.Context, credentialIdBase64 string) (*domain.User, error)
|
GetUserByWebAuthnCredential(ctx context.Context, credentialIdBase64 string) (*domain.User, error)
|
||||||
// UpdateUser updates an existing user in the database.
|
// UpdateUserInternal updates an existing user in the database.
|
||||||
UpdateUser(ctx context.Context, user *domain.User) (*domain.User, error)
|
UpdateUserInternal(ctx context.Context, user *domain.User) (*domain.User, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type WebAuthnAuthenticator struct {
|
type WebAuthnAuthenticator struct {
|
||||||
@@ -89,7 +89,7 @@ func (a *WebAuthnAuthenticator) StartWebAuthnRegistration(ctx context.Context, u
|
|||||||
|
|
||||||
if user.WebAuthnId == "" {
|
if user.WebAuthnId == "" {
|
||||||
user.GenerateWebAuthnId()
|
user.GenerateWebAuthnId()
|
||||||
user, err = a.users.UpdateUser(ctx, user)
|
user, err = a.users.UpdateUserInternal(ctx, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("failed to store webauthn id to user: %w", err)
|
return nil, nil, fmt.Errorf("failed to store webauthn id to user: %w", err)
|
||||||
}
|
}
|
||||||
@@ -150,7 +150,7 @@ func (a *WebAuthnAuthenticator) FinishWebAuthnRegistration(
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err = a.users.UpdateUser(ctx, user)
|
user, err = a.users.UpdateUserInternal(ctx, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -181,7 +181,7 @@ func (a *WebAuthnAuthenticator) RemoveCredential(
|
|||||||
}
|
}
|
||||||
|
|
||||||
user.RemoveCredential(credentialIdBase64)
|
user.RemoveCredential(credentialIdBase64)
|
||||||
user, err = a.users.UpdateUser(ctx, user)
|
user, err = a.users.UpdateUserInternal(ctx, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -205,7 +205,7 @@ func (a *WebAuthnAuthenticator) UpdateCredential(
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err = a.users.UpdateUser(ctx, user)
|
user, err = a.users.UpdateUserInternal(ctx, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ const TopicUserEnabled = "user:enabled"
|
|||||||
const TopicInterfaceCreated = "interface:created"
|
const TopicInterfaceCreated = "interface:created"
|
||||||
const TopicInterfaceUpdated = "interface:updated"
|
const TopicInterfaceUpdated = "interface:updated"
|
||||||
const TopicInterfaceDeleted = "interface:deleted"
|
const TopicInterfaceDeleted = "interface:deleted"
|
||||||
|
const TopicInterfaceStatsUpdated = "interface:stats:updated"
|
||||||
|
|
||||||
// endregion interface-events
|
// endregion interface-events
|
||||||
|
|
||||||
@@ -37,6 +38,7 @@ const TopicPeerUpdated = "peer:updated"
|
|||||||
const TopicPeerInterfaceUpdated = "peer:interface:updated"
|
const TopicPeerInterfaceUpdated = "peer:interface:updated"
|
||||||
const TopicPeerIdentifierUpdated = "peer:identifier:updated"
|
const TopicPeerIdentifierUpdated = "peer:identifier:updated"
|
||||||
const TopicPeerStateChanged = "peer:state:changed"
|
const TopicPeerStateChanged = "peer:state:changed"
|
||||||
|
const TopicPeerStatsUpdated = "peer:stats:updated"
|
||||||
|
|
||||||
// endregion peer-events
|
// endregion peer-events
|
||||||
|
|
||||||
|
|||||||
@@ -144,8 +144,6 @@ func migrateV1Users(oldDb, newDb *gorm.DB) error {
|
|||||||
},
|
},
|
||||||
Identifier: domain.UserIdentifier(oldUser.Email),
|
Identifier: domain.UserIdentifier(oldUser.Email),
|
||||||
Email: oldUser.Email,
|
Email: oldUser.Email,
|
||||||
Source: domain.UserSource(oldUser.Source),
|
|
||||||
ProviderName: "",
|
|
||||||
IsAdmin: oldUser.IsAdmin,
|
IsAdmin: oldUser.IsAdmin,
|
||||||
Firstname: oldUser.Firstname,
|
Firstname: oldUser.Firstname,
|
||||||
Lastname: oldUser.Lastname,
|
Lastname: oldUser.Lastname,
|
||||||
@@ -159,11 +157,25 @@ func migrateV1Users(oldDb, newDb *gorm.DB) error {
|
|||||||
LockedReason: "",
|
LockedReason: "",
|
||||||
LinkedPeerCount: 0,
|
LinkedPeerCount: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := newDb.Create(&newUser).Error; err != nil {
|
if err := newDb.Create(&newUser).Error; err != nil {
|
||||||
return fmt.Errorf("failed to migrate user %s: %w", oldUser.Email, err)
|
return fmt.Errorf("failed to migrate user %s: %w", oldUser.Email, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
authentication := domain.UserAuthentication{
|
||||||
|
BaseModel: domain.BaseModel{
|
||||||
|
CreatedBy: domain.CtxSystemV1Migrator,
|
||||||
|
UpdatedBy: domain.CtxSystemV1Migrator,
|
||||||
|
CreatedAt: oldUser.CreatedAt,
|
||||||
|
UpdatedAt: oldUser.UpdatedAt,
|
||||||
|
},
|
||||||
|
UserIdentifier: domain.UserIdentifier(oldUser.Email),
|
||||||
|
Source: domain.UserSource(oldUser.Source),
|
||||||
|
ProviderName: "", // unknown
|
||||||
|
}
|
||||||
|
if err := newDb.Create(&authentication).Error; err != nil {
|
||||||
|
return fmt.Errorf("failed to migrate user-authentication %s: %w", oldUser.Email, err)
|
||||||
|
}
|
||||||
|
|
||||||
slog.Debug("user migrated successfully", "identifier", newUser.Identifier)
|
slog.Debug("user migrated successfully", "identifier", newUser.Identifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -346,8 +358,6 @@ func migrateV1Peers(oldDb, newDb *gorm.DB) error {
|
|||||||
},
|
},
|
||||||
Identifier: domain.UserIdentifier(oldPeer.Email),
|
Identifier: domain.UserIdentifier(oldPeer.Email),
|
||||||
Email: oldPeer.Email,
|
Email: oldPeer.Email,
|
||||||
Source: domain.UserSourceDatabase,
|
|
||||||
ProviderName: "",
|
|
||||||
IsAdmin: false,
|
IsAdmin: false,
|
||||||
Locked: &now,
|
Locked: &now,
|
||||||
LockedReason: domain.DisabledReasonMigrationDummy,
|
LockedReason: domain.DisabledReasonMigrationDummy,
|
||||||
@@ -358,6 +368,21 @@ func migrateV1Peers(oldDb, newDb *gorm.DB) error {
|
|||||||
return fmt.Errorf("failed to migrate dummy user %s: %w", oldPeer.Email, err)
|
return fmt.Errorf("failed to migrate dummy user %s: %w", oldPeer.Email, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
authentication := domain.UserAuthentication{
|
||||||
|
BaseModel: domain.BaseModel{
|
||||||
|
CreatedBy: domain.CtxSystemV1Migrator,
|
||||||
|
UpdatedBy: domain.CtxSystemV1Migrator,
|
||||||
|
CreatedAt: now,
|
||||||
|
UpdatedAt: now,
|
||||||
|
},
|
||||||
|
UserIdentifier: domain.UserIdentifier(oldPeer.Email),
|
||||||
|
Source: domain.UserSourceDatabase,
|
||||||
|
ProviderName: "", // unknown
|
||||||
|
}
|
||||||
|
if err := newDb.Create(&authentication).Error; err != nil {
|
||||||
|
return fmt.Errorf("failed to migrate dummy user-authentication %s: %w", oldPeer.Email, err)
|
||||||
|
}
|
||||||
|
|
||||||
slog.Debug("dummy user migrated successfully", "identifier", user.Identifier)
|
slog.Debug("dummy user migrated successfully", "identifier", user.Identifier)
|
||||||
}
|
}
|
||||||
newPeer := domain.Peer{
|
newPeer := domain.Peer{
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package users
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -25,6 +26,8 @@ func convertRawLdapUser(
|
|||||||
return nil, fmt.Errorf("failed to check admin group: %w", err)
|
return nil, fmt.Errorf("failed to check admin group: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uid := domain.UserIdentifier(internal.MapDefaultString(rawUser, fields.UserIdentifier, ""))
|
||||||
|
|
||||||
return &domain.User{
|
return &domain.User{
|
||||||
BaseModel: domain.BaseModel{
|
BaseModel: domain.BaseModel{
|
||||||
CreatedBy: domain.CtxSystemLdapSyncer,
|
CreatedBy: domain.CtxSystemLdapSyncer,
|
||||||
@@ -32,18 +35,23 @@ func convertRawLdapUser(
|
|||||||
CreatedAt: now,
|
CreatedAt: now,
|
||||||
UpdatedAt: now,
|
UpdatedAt: now,
|
||||||
},
|
},
|
||||||
Identifier: domain.UserIdentifier(internal.MapDefaultString(rawUser, fields.UserIdentifier, "")),
|
Identifier: uid,
|
||||||
Email: strings.ToLower(internal.MapDefaultString(rawUser, fields.Email, "")),
|
Email: strings.ToLower(internal.MapDefaultString(rawUser, fields.Email, "")),
|
||||||
Source: domain.UserSourceLdap,
|
IsAdmin: isAdmin,
|
||||||
ProviderName: providerName,
|
Authentications: []domain.UserAuthentication{
|
||||||
IsAdmin: isAdmin,
|
{
|
||||||
Firstname: internal.MapDefaultString(rawUser, fields.Firstname, ""),
|
UserIdentifier: uid,
|
||||||
Lastname: internal.MapDefaultString(rawUser, fields.Lastname, ""),
|
Source: domain.UserSourceLdap,
|
||||||
Phone: internal.MapDefaultString(rawUser, fields.Phone, ""),
|
ProviderName: providerName,
|
||||||
Department: internal.MapDefaultString(rawUser, fields.Department, ""),
|
},
|
||||||
Notes: "",
|
},
|
||||||
Password: "",
|
Firstname: internal.MapDefaultString(rawUser, fields.Firstname, ""),
|
||||||
Disabled: nil,
|
Lastname: internal.MapDefaultString(rawUser, fields.Lastname, ""),
|
||||||
|
Phone: internal.MapDefaultString(rawUser, fields.Phone, ""),
|
||||||
|
Department: internal.MapDefaultString(rawUser, fields.Department, ""),
|
||||||
|
Notes: "",
|
||||||
|
Password: "",
|
||||||
|
Disabled: nil,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +80,9 @@ func userChangedInLdap(dbUser, ldapUser *domain.User) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if dbUser.ProviderName != ldapUser.ProviderName {
|
if !slices.ContainsFunc(dbUser.Authentications, func(authentication domain.UserAuthentication) bool {
|
||||||
|
return authentication.Source == ldapUser.Authentications[0].Source
|
||||||
|
}) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
239
internal/app/users/ldap_sync.go
Normal file
239
internal/app/users/ldap_sync.go
Normal file
@@ -0,0 +1,239 @@
|
|||||||
|
package users
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-ldap/ldap/v3"
|
||||||
|
|
||||||
|
"github.com/h44z/wg-portal/internal"
|
||||||
|
"github.com/h44z/wg-portal/internal/app"
|
||||||
|
"github.com/h44z/wg-portal/internal/config"
|
||||||
|
"github.com/h44z/wg-portal/internal/domain"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (m Manager) runLdapSynchronizationService(ctx context.Context) {
|
||||||
|
ctx = domain.SetUserInfo(ctx, domain.LdapSyncContextUserInfo()) // switch to service context for LDAP sync
|
||||||
|
|
||||||
|
for _, ldapCfg := range m.cfg.Auth.Ldap { // LDAP Auth providers
|
||||||
|
go func(cfg config.LdapProvider) {
|
||||||
|
syncInterval := cfg.SyncInterval
|
||||||
|
if syncInterval == 0 {
|
||||||
|
slog.Debug("sync disabled for LDAP server", "provider", cfg.ProviderName)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// perform initial sync
|
||||||
|
err := m.synchronizeLdapUsers(ctx, &cfg)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("failed to synchronize LDAP users", "provider", cfg.ProviderName, "error", err)
|
||||||
|
} else {
|
||||||
|
slog.Debug("initial LDAP user sync completed", "provider", cfg.ProviderName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// start periodic sync
|
||||||
|
running := true
|
||||||
|
for running {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
running = false
|
||||||
|
continue
|
||||||
|
case <-time.After(syncInterval):
|
||||||
|
// select blocks until one of the cases evaluate to true
|
||||||
|
}
|
||||||
|
|
||||||
|
err := m.synchronizeLdapUsers(ctx, &cfg)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("failed to synchronize LDAP users", "provider", cfg.ProviderName, "error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}(ldapCfg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Manager) synchronizeLdapUsers(ctx context.Context, provider *config.LdapProvider) error {
|
||||||
|
slog.Debug("starting to synchronize users", "provider", provider.ProviderName)
|
||||||
|
|
||||||
|
dn, err := ldap.ParseDN(provider.AdminGroupDN)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to parse admin group DN: %w", err)
|
||||||
|
}
|
||||||
|
provider.ParsedAdminGroupDN = dn
|
||||||
|
|
||||||
|
conn, err := internal.LdapConnect(provider)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to setup LDAP connection: %w", err)
|
||||||
|
}
|
||||||
|
defer internal.LdapDisconnect(conn)
|
||||||
|
|
||||||
|
rawUsers, err := internal.LdapFindAllUsers(conn, provider.BaseDN, provider.SyncFilter, &provider.FieldMap)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Debug("fetched raw ldap users", "count", len(rawUsers), "provider", provider.ProviderName)
|
||||||
|
|
||||||
|
// Update existing LDAP users
|
||||||
|
err = m.updateLdapUsers(ctx, provider, rawUsers, &provider.FieldMap, provider.ParsedAdminGroupDN)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disable missing LDAP users
|
||||||
|
if provider.DisableMissing {
|
||||||
|
err = m.disableMissingLdapUsers(ctx, provider.ProviderName, rawUsers, &provider.FieldMap)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Manager) updateLdapUsers(
|
||||||
|
ctx context.Context,
|
||||||
|
provider *config.LdapProvider,
|
||||||
|
rawUsers []internal.RawLdapUser,
|
||||||
|
fields *config.LdapFields,
|
||||||
|
adminGroupDN *ldap.DN,
|
||||||
|
) error {
|
||||||
|
for _, rawUser := range rawUsers {
|
||||||
|
user, err := convertRawLdapUser(provider.ProviderName, rawUser, fields, adminGroupDN)
|
||||||
|
if err != nil && !errors.Is(err, domain.ErrNotFound) {
|
||||||
|
return fmt.Errorf("failed to convert LDAP data for %v: %w", rawUser["dn"], err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if provider.SyncLogUserInfo {
|
||||||
|
slog.Debug("ldap user data",
|
||||||
|
"raw-user", rawUser, "user", user.Identifier,
|
||||||
|
"is-admin", user.IsAdmin, "provider", provider.ProviderName)
|
||||||
|
}
|
||||||
|
|
||||||
|
existingUser, err := m.users.GetUser(ctx, user.Identifier)
|
||||||
|
if err != nil && !errors.Is(err, domain.ErrNotFound) {
|
||||||
|
return fmt.Errorf("find error for user id %s: %w", user.Identifier, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||||
|
tctx = domain.SetUserInfo(tctx, domain.SystemAdminContextUserInfo())
|
||||||
|
|
||||||
|
if existingUser == nil {
|
||||||
|
// create new user
|
||||||
|
slog.Debug("creating new user from provider", "user", user.Identifier, "provider", provider.ProviderName)
|
||||||
|
|
||||||
|
_, err := m.create(tctx, user)
|
||||||
|
if err != nil {
|
||||||
|
cancel()
|
||||||
|
return fmt.Errorf("create error for user id %s: %w", user.Identifier, err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// update existing user
|
||||||
|
if provider.AutoReEnable && existingUser.DisabledReason == domain.DisabledReasonLdapMissing {
|
||||||
|
user.Disabled = nil
|
||||||
|
user.DisabledReason = ""
|
||||||
|
} else {
|
||||||
|
user.Disabled = existingUser.Disabled
|
||||||
|
user.DisabledReason = existingUser.DisabledReason
|
||||||
|
}
|
||||||
|
|
||||||
|
if existingUser.PersistLocalChanges {
|
||||||
|
cancel()
|
||||||
|
continue // skip synchronization for this user
|
||||||
|
}
|
||||||
|
|
||||||
|
if userChangedInLdap(existingUser, user) {
|
||||||
|
syncedUser, err := m.users.GetUser(ctx, user.Identifier)
|
||||||
|
if err != nil && !errors.Is(err, domain.ErrNotFound) {
|
||||||
|
cancel()
|
||||||
|
return fmt.Errorf("find error for user id %s: %w", user.Identifier, err)
|
||||||
|
}
|
||||||
|
syncedUser.UpdatedAt = time.Now()
|
||||||
|
syncedUser.UpdatedBy = domain.CtxSystemLdapSyncer
|
||||||
|
syncedUser.MergeAuthSources(user.Authentications...)
|
||||||
|
syncedUser.Email = user.Email
|
||||||
|
syncedUser.Firstname = user.Firstname
|
||||||
|
syncedUser.Lastname = user.Lastname
|
||||||
|
syncedUser.Phone = user.Phone
|
||||||
|
syncedUser.Department = user.Department
|
||||||
|
syncedUser.IsAdmin = user.IsAdmin
|
||||||
|
syncedUser.Disabled = user.Disabled
|
||||||
|
syncedUser.DisabledReason = user.DisabledReason
|
||||||
|
|
||||||
|
_, err = m.update(tctx, existingUser, syncedUser, false)
|
||||||
|
if err != nil {
|
||||||
|
cancel()
|
||||||
|
return fmt.Errorf("update error for user id %s: %w", user.Identifier, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Manager) disableMissingLdapUsers(
|
||||||
|
ctx context.Context,
|
||||||
|
providerName string,
|
||||||
|
rawUsers []internal.RawLdapUser,
|
||||||
|
fields *config.LdapFields,
|
||||||
|
) error {
|
||||||
|
allUsers, err := m.users.GetAllUsers(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, user := range allUsers {
|
||||||
|
userHasAuthSource := false
|
||||||
|
for _, auth := range user.Authentications {
|
||||||
|
if auth.Source == domain.UserSourceLdap && auth.ProviderName == providerName {
|
||||||
|
userHasAuthSource = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !userHasAuthSource {
|
||||||
|
continue // ignore non ldap users
|
||||||
|
}
|
||||||
|
if user.IsDisabled() {
|
||||||
|
continue // ignore deactivated
|
||||||
|
}
|
||||||
|
if user.PersistLocalChanges {
|
||||||
|
continue // skip sync for this user
|
||||||
|
}
|
||||||
|
|
||||||
|
existsInLDAP := false
|
||||||
|
for _, rawUser := range rawUsers {
|
||||||
|
userId := domain.UserIdentifier(internal.MapDefaultString(rawUser, fields.UserIdentifier, ""))
|
||||||
|
if user.Identifier == userId {
|
||||||
|
existsInLDAP = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if existsInLDAP {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Debug("user is missing in ldap provider, disabling", "user", user.Identifier, "provider", providerName)
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
user.Disabled = &now
|
||||||
|
user.DisabledReason = domain.DisabledReasonLdapMissing
|
||||||
|
|
||||||
|
err := m.users.SaveUser(ctx, user.Identifier, func(u *domain.User) (*domain.User, error) {
|
||||||
|
u.Disabled = user.Disabled
|
||||||
|
u.DisabledReason = user.DisabledReason
|
||||||
|
return u, nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("disable error for user id %s: %w", user.Identifier, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
m.bus.Publish(app.TopicUserDisabled, user)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -4,15 +4,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
|
||||||
"math"
|
"math"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-ldap/ldap/v3"
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
"github.com/h44z/wg-portal/internal"
|
|
||||||
"github.com/h44z/wg-portal/internal/app"
|
"github.com/h44z/wg-portal/internal/app"
|
||||||
"github.com/h44z/wg-portal/internal/config"
|
"github.com/h44z/wg-portal/internal/config"
|
||||||
"github.com/h44z/wg-portal/internal/domain"
|
"github.com/h44z/wg-portal/internal/domain"
|
||||||
@@ -79,7 +76,7 @@ func (m Manager) RegisterUser(ctx context.Context, user *domain.User) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
createdUser, err := m.CreateUser(ctx, user)
|
createdUser, err := m.create(ctx, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -101,20 +98,11 @@ func (m Manager) GetUser(ctx context.Context, id domain.UserIdentifier) (*domain
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := m.users.GetUser(ctx, id)
|
return m.getUser(ctx, id)
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("unable to load user %s: %w", id, err)
|
|
||||||
}
|
|
||||||
peers, _ := m.peers.GetUserPeers(ctx, id) // ignore error, list will be empty in error case
|
|
||||||
|
|
||||||
user.LinkedPeerCount = len(peers)
|
|
||||||
|
|
||||||
return user, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserByEmail returns the user with the given email address.
|
// GetUserByEmail returns the user with the given email address.
|
||||||
func (m Manager) GetUserByEmail(ctx context.Context, email string) (*domain.User, error) {
|
func (m Manager) GetUserByEmail(ctx context.Context, email string) (*domain.User, error) {
|
||||||
|
|
||||||
user, err := m.users.GetUserByEmail(ctx, email)
|
user, err := m.users.GetUserByEmail(ctx, email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to load user for email %s: %w", email, err)
|
return nil, fmt.Errorf("unable to load user for email %s: %w", email, err)
|
||||||
@@ -124,16 +112,11 @@ func (m Manager) GetUserByEmail(ctx context.Context, email string) (*domain.User
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
peers, _ := m.peers.GetUserPeers(ctx, user.Identifier) // ignore error, list will be empty in error case
|
return m.enrichUser(ctx, user), nil
|
||||||
|
|
||||||
user.LinkedPeerCount = len(peers)
|
|
||||||
|
|
||||||
return user, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserByWebAuthnCredential returns the user for the given WebAuthn credential.
|
// GetUserByWebAuthnCredential returns the user for the given WebAuthn credential.
|
||||||
func (m Manager) GetUserByWebAuthnCredential(ctx context.Context, credentialIdBase64 string) (*domain.User, error) {
|
func (m Manager) GetUserByWebAuthnCredential(ctx context.Context, credentialIdBase64 string) (*domain.User, error) {
|
||||||
|
|
||||||
user, err := m.users.GetUserByWebAuthnCredential(ctx, credentialIdBase64)
|
user, err := m.users.GetUserByWebAuthnCredential(ctx, credentialIdBase64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to load user for webauthn credential %s: %w", credentialIdBase64, err)
|
return nil, fmt.Errorf("unable to load user for webauthn credential %s: %w", credentialIdBase64, err)
|
||||||
@@ -143,11 +126,7 @@ func (m Manager) GetUserByWebAuthnCredential(ctx context.Context, credentialIdBa
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
peers, _ := m.peers.GetUserPeers(ctx, user.Identifier) // ignore error, list will be empty in error case
|
return m.enrichUser(ctx, user), nil
|
||||||
|
|
||||||
user.LinkedPeerCount = len(peers)
|
|
||||||
|
|
||||||
return user, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllUsers returns all users.
|
// GetAllUsers returns all users.
|
||||||
@@ -169,8 +148,7 @@ func (m Manager) GetAllUsers(ctx context.Context) ([]domain.User, error) {
|
|||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
for user := range ch {
|
for user := range ch {
|
||||||
peers, _ := m.peers.GetUserPeers(ctx, user.Identifier) // ignore error, list will be empty in error case
|
m.enrichUser(ctx, user)
|
||||||
user.LinkedPeerCount = len(peers)
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
@@ -194,77 +172,29 @@ func (m Manager) UpdateUser(ctx context.Context, user *domain.User) (*domain.Use
|
|||||||
return nil, fmt.Errorf("unable to load existing user %s: %w", user.Identifier, err)
|
return nil, fmt.Errorf("unable to load existing user %s: %w", user.Identifier, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := m.validateModifications(ctx, existingUser, user); err != nil {
|
user.CopyCalculatedAttributes(existingUser, true) // ensure that crucial attributes stay the same
|
||||||
return nil, fmt.Errorf("update not allowed: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
user.CopyCalculatedAttributes(existingUser)
|
return m.update(ctx, existingUser, user, true)
|
||||||
err = user.HashPassword()
|
}
|
||||||
|
|
||||||
|
// UpdateUserInternal updates the user with the given identifier. This function must never be called from external.
|
||||||
|
// This function allows to override authentications and webauthn credentials.
|
||||||
|
func (m Manager) UpdateUserInternal(ctx context.Context, user *domain.User) (*domain.User, error) {
|
||||||
|
existingUser, err := m.users.GetUser(ctx, user.Identifier)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("unable to load existing user %s: %w", user.Identifier, err)
|
||||||
}
|
|
||||||
if user.Password == "" { // keep old password
|
|
||||||
user.Password = existingUser.Password
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = m.users.SaveUser(ctx, existingUser.Identifier, func(u *domain.User) (*domain.User, error) {
|
return m.update(ctx, existingUser, user, false)
|
||||||
user.CopyCalculatedAttributes(u)
|
|
||||||
return user, nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("update failure: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
m.bus.Publish(app.TopicUserUpdated, *user)
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case !existingUser.IsDisabled() && user.IsDisabled():
|
|
||||||
m.bus.Publish(app.TopicUserDisabled, *user)
|
|
||||||
case existingUser.IsDisabled() && !user.IsDisabled():
|
|
||||||
m.bus.Publish(app.TopicUserEnabled, *user)
|
|
||||||
}
|
|
||||||
|
|
||||||
return user, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateUser creates a new user.
|
// CreateUser creates a new user.
|
||||||
func (m Manager) CreateUser(ctx context.Context, user *domain.User) (*domain.User, error) {
|
func (m Manager) CreateUser(ctx context.Context, user *domain.User) (*domain.User, error) {
|
||||||
if user.Identifier == "" {
|
|
||||||
return nil, errors.New("missing user identifier")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := domain.ValidateAdminAccessRights(ctx); err != nil {
|
if err := domain.ValidateAdminAccessRights(ctx); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
existingUser, err := m.users.GetUser(ctx, user.Identifier)
|
return m.create(ctx, user)
|
||||||
if err != nil && !errors.Is(err, domain.ErrNotFound) {
|
|
||||||
return nil, fmt.Errorf("unable to load existing user %s: %w", user.Identifier, err)
|
|
||||||
}
|
|
||||||
if existingUser != nil {
|
|
||||||
return nil, errors.Join(fmt.Errorf("user %s already exists", user.Identifier), domain.ErrDuplicateEntry)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := m.validateCreation(ctx, user); err != nil {
|
|
||||||
return nil, fmt.Errorf("creation not allowed: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = user.HashPassword()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = m.users.SaveUser(ctx, user.Identifier, func(u *domain.User) (*domain.User, error) {
|
|
||||||
user.CopyCalculatedAttributes(u)
|
|
||||||
return user, nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("creation failure: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
m.bus.Publish(app.TopicUserCreated, *user)
|
|
||||||
|
|
||||||
return user, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteUser deletes the user with the given identifier.
|
// DeleteUser deletes the user with the given identifier.
|
||||||
@@ -307,15 +237,10 @@ func (m Manager) ActivateApi(ctx context.Context, id domain.UserIdentifier) (*do
|
|||||||
user.ApiToken = uuid.New().String()
|
user.ApiToken = uuid.New().String()
|
||||||
user.ApiTokenCreated = &now
|
user.ApiTokenCreated = &now
|
||||||
|
|
||||||
err = m.users.SaveUser(ctx, user.Identifier, func(u *domain.User) (*domain.User, error) {
|
user, err = m.update(ctx, user, user, true) // self-update
|
||||||
user.CopyCalculatedAttributes(u)
|
|
||||||
return user, nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("update failure: %w", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
m.bus.Publish(app.TopicUserUpdated, *user)
|
|
||||||
m.bus.Publish(app.TopicUserApiEnabled, *user)
|
m.bus.Publish(app.TopicUserApiEnabled, *user)
|
||||||
|
|
||||||
return user, nil
|
return user, nil
|
||||||
@@ -335,15 +260,10 @@ func (m Manager) DeactivateApi(ctx context.Context, id domain.UserIdentifier) (*
|
|||||||
user.ApiToken = ""
|
user.ApiToken = ""
|
||||||
user.ApiTokenCreated = nil
|
user.ApiTokenCreated = nil
|
||||||
|
|
||||||
err = m.users.SaveUser(ctx, user.Identifier, func(u *domain.User) (*domain.User, error) {
|
user, err = m.update(ctx, user, user, true) // self-update
|
||||||
user.CopyCalculatedAttributes(u)
|
|
||||||
return user, nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("update failure: %w", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
m.bus.Publish(app.TopicUserUpdated, *user)
|
|
||||||
m.bus.Publish(app.TopicUserApiDisabled, *user)
|
m.bus.Publish(app.TopicUserApiDisabled, *user)
|
||||||
|
|
||||||
return user, nil
|
return user, nil
|
||||||
@@ -380,10 +300,6 @@ func (m Manager) validateModifications(ctx context.Context, old, new *domain.Use
|
|||||||
return fmt.Errorf("cannot lock own user: %w", domain.ErrInvalidData)
|
return fmt.Errorf("cannot lock own user: %w", domain.ErrInvalidData)
|
||||||
}
|
}
|
||||||
|
|
||||||
if old.Source != new.Source {
|
|
||||||
return fmt.Errorf("cannot change user source: %w", domain.ErrInvalidData)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -414,14 +330,19 @@ func (m Manager) validateCreation(ctx context.Context, new *domain.User) error {
|
|||||||
return fmt.Errorf("reserved user identifier: %w", domain.ErrInvalidData)
|
return fmt.Errorf("reserved user identifier: %w", domain.ErrInvalidData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(new.Authentications) != 1 {
|
||||||
|
return fmt.Errorf("invalid number of authentications: %d, expected 1: %w",
|
||||||
|
len(new.Authentications), domain.ErrInvalidData)
|
||||||
|
}
|
||||||
|
|
||||||
// Admins are allowed to create users for arbitrary sources.
|
// Admins are allowed to create users for arbitrary sources.
|
||||||
if new.Source != domain.UserSourceDatabase && !currentUser.IsAdmin {
|
if new.Authentications[0].Source != domain.UserSourceDatabase && !currentUser.IsAdmin {
|
||||||
return fmt.Errorf("invalid user source: %s, only %s is allowed: %w",
|
return fmt.Errorf("invalid user source: %s, only %s is allowed: %w",
|
||||||
new.Source, domain.UserSourceDatabase, domain.ErrInvalidData)
|
new.Authentications[0].Source, domain.UserSourceDatabase, domain.ErrInvalidData)
|
||||||
}
|
}
|
||||||
|
|
||||||
// database users must have a password
|
// database users must have a password
|
||||||
if new.Source == domain.UserSourceDatabase && string(new.Password) == "" {
|
if new.Authentications[0].Source == domain.UserSourceDatabase && string(new.Password) == "" {
|
||||||
return fmt.Errorf("missing password: %w", domain.ErrInvalidData)
|
return fmt.Errorf("missing password: %w", domain.ErrInvalidData)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -460,214 +381,112 @@ func (m Manager) validateApiChange(ctx context.Context, user *domain.User) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Manager) runLdapSynchronizationService(ctx context.Context) {
|
// region internal-modifiers
|
||||||
ctx = domain.SetUserInfo(ctx, domain.LdapSyncContextUserInfo()) // switch to service context for LDAP sync
|
|
||||||
|
|
||||||
for _, ldapCfg := range m.cfg.Auth.Ldap { // LDAP Auth providers
|
func (m Manager) enrichUser(ctx context.Context, user *domain.User) *domain.User {
|
||||||
go func(cfg config.LdapProvider) {
|
if user == nil {
|
||||||
syncInterval := cfg.SyncInterval
|
return nil
|
||||||
if syncInterval == 0 {
|
|
||||||
slog.Debug("sync disabled for LDAP server", "provider", cfg.ProviderName)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// perform initial sync
|
|
||||||
err := m.synchronizeLdapUsers(ctx, &cfg)
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("failed to synchronize LDAP users", "provider", cfg.ProviderName, "error", err)
|
|
||||||
} else {
|
|
||||||
slog.Debug("initial LDAP user sync completed", "provider", cfg.ProviderName)
|
|
||||||
}
|
|
||||||
|
|
||||||
// start periodic sync
|
|
||||||
running := true
|
|
||||||
for running {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
running = false
|
|
||||||
continue
|
|
||||||
case <-time.After(syncInterval):
|
|
||||||
// select blocks until one of the cases evaluate to true
|
|
||||||
}
|
|
||||||
|
|
||||||
err := m.synchronizeLdapUsers(ctx, &cfg)
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("failed to synchronize LDAP users", "provider", cfg.ProviderName, "error", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}(ldapCfg)
|
|
||||||
}
|
}
|
||||||
|
peers, _ := m.peers.GetUserPeers(ctx, user.Identifier) // ignore error, list will be empty in error case
|
||||||
|
user.LinkedPeerCount = len(peers)
|
||||||
|
return user
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Manager) synchronizeLdapUsers(ctx context.Context, provider *config.LdapProvider) error {
|
func (m Manager) getUser(ctx context.Context, id domain.UserIdentifier) (*domain.User, error) {
|
||||||
slog.Debug("starting to synchronize users", "provider", provider.ProviderName)
|
user, err := m.users.GetUser(ctx, id)
|
||||||
|
|
||||||
dn, err := ldap.ParseDN(provider.AdminGroupDN)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to parse admin group DN: %w", err)
|
return nil, fmt.Errorf("unable to load user %s: %w", id, err)
|
||||||
}
|
}
|
||||||
provider.ParsedAdminGroupDN = dn
|
return m.enrichUser(ctx, user), nil
|
||||||
|
|
||||||
conn, err := internal.LdapConnect(provider)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to setup LDAP connection: %w", err)
|
|
||||||
}
|
|
||||||
defer internal.LdapDisconnect(conn)
|
|
||||||
|
|
||||||
rawUsers, err := internal.LdapFindAllUsers(conn, provider.BaseDN, provider.SyncFilter, &provider.FieldMap)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
slog.Debug("fetched raw ldap users", "count", len(rawUsers), "provider", provider.ProviderName)
|
|
||||||
|
|
||||||
// Update existing LDAP users
|
|
||||||
err = m.updateLdapUsers(ctx, provider, rawUsers, &provider.FieldMap, provider.ParsedAdminGroupDN)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Disable missing LDAP users
|
|
||||||
if provider.DisableMissing {
|
|
||||||
err = m.disableMissingLdapUsers(ctx, provider.ProviderName, rawUsers, &provider.FieldMap)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Manager) updateLdapUsers(
|
func (m Manager) update(ctx context.Context, existingUser, user *domain.User, keepAuthentications bool) (
|
||||||
ctx context.Context,
|
*domain.User,
|
||||||
provider *config.LdapProvider,
|
error,
|
||||||
rawUsers []internal.RawLdapUser,
|
) {
|
||||||
fields *config.LdapFields,
|
if err := m.validateModifications(ctx, existingUser, user); err != nil {
|
||||||
adminGroupDN *ldap.DN,
|
return nil, fmt.Errorf("update not allowed: %w", err)
|
||||||
) error {
|
|
||||||
for _, rawUser := range rawUsers {
|
|
||||||
user, err := convertRawLdapUser(provider.ProviderName, rawUser, fields, adminGroupDN)
|
|
||||||
if err != nil && !errors.Is(err, domain.ErrNotFound) {
|
|
||||||
return fmt.Errorf("failed to convert LDAP data for %v: %w", rawUser["dn"], err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if provider.SyncLogUserInfo {
|
|
||||||
slog.Debug("ldap user data",
|
|
||||||
"raw-user", rawUser, "user", user.Identifier,
|
|
||||||
"is-admin", user.IsAdmin, "provider", provider.ProviderName)
|
|
||||||
}
|
|
||||||
|
|
||||||
existingUser, err := m.users.GetUser(ctx, user.Identifier)
|
|
||||||
if err != nil && !errors.Is(err, domain.ErrNotFound) {
|
|
||||||
return fmt.Errorf("find error for user id %s: %w", user.Identifier, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
tctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
|
||||||
tctx = domain.SetUserInfo(tctx, domain.SystemAdminContextUserInfo())
|
|
||||||
|
|
||||||
if existingUser == nil {
|
|
||||||
// create new user
|
|
||||||
slog.Debug("creating new user from provider", "user", user.Identifier, "provider", provider.ProviderName)
|
|
||||||
|
|
||||||
_, err := m.CreateUser(tctx, user)
|
|
||||||
if err != nil {
|
|
||||||
cancel()
|
|
||||||
return fmt.Errorf("create error for user id %s: %w", user.Identifier, err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// update existing user
|
|
||||||
if provider.AutoReEnable && existingUser.DisabledReason == domain.DisabledReasonLdapMissing {
|
|
||||||
user.Disabled = nil
|
|
||||||
user.DisabledReason = ""
|
|
||||||
} else {
|
|
||||||
user.Disabled = existingUser.Disabled
|
|
||||||
user.DisabledReason = existingUser.DisabledReason
|
|
||||||
}
|
|
||||||
if existingUser.Source == domain.UserSourceLdap && userChangedInLdap(existingUser, user) {
|
|
||||||
err := m.users.SaveUser(tctx, user.Identifier, func(u *domain.User) (*domain.User, error) {
|
|
||||||
u.UpdatedAt = time.Now()
|
|
||||||
u.UpdatedBy = domain.CtxSystemLdapSyncer
|
|
||||||
u.Source = user.Source
|
|
||||||
u.ProviderName = user.ProviderName
|
|
||||||
u.Email = user.Email
|
|
||||||
u.Firstname = user.Firstname
|
|
||||||
u.Lastname = user.Lastname
|
|
||||||
u.Phone = user.Phone
|
|
||||||
u.Department = user.Department
|
|
||||||
u.IsAdmin = user.IsAdmin
|
|
||||||
u.Disabled = nil
|
|
||||||
u.DisabledReason = ""
|
|
||||||
|
|
||||||
return u, nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
cancel()
|
|
||||||
return fmt.Errorf("update error for user id %s: %w", user.Identifier, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if existingUser.IsDisabled() && !user.IsDisabled() {
|
|
||||||
m.bus.Publish(app.TopicUserEnabled, *user)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cancel()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
err := user.HashPassword()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if user.Password == "" { // keep old password
|
||||||
|
user.Password = existingUser.Password
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.users.SaveUser(ctx, existingUser.Identifier, func(u *domain.User) (*domain.User, error) {
|
||||||
|
user.CopyCalculatedAttributes(u, keepAuthentications)
|
||||||
|
return user, nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("update failure: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
m.bus.Publish(app.TopicUserUpdated, *user)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case !existingUser.IsDisabled() && user.IsDisabled():
|
||||||
|
m.bus.Publish(app.TopicUserDisabled, *user)
|
||||||
|
case existingUser.IsDisabled() && !user.IsDisabled():
|
||||||
|
m.bus.Publish(app.TopicUserEnabled, *user)
|
||||||
|
}
|
||||||
|
|
||||||
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Manager) disableMissingLdapUsers(
|
func (m Manager) create(ctx context.Context, user *domain.User) (*domain.User, error) {
|
||||||
ctx context.Context,
|
if user.Identifier == "" {
|
||||||
providerName string,
|
return nil, errors.New("missing user identifier")
|
||||||
rawUsers []internal.RawLdapUser,
|
|
||||||
fields *config.LdapFields,
|
|
||||||
) error {
|
|
||||||
allUsers, err := m.users.GetAllUsers(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
for _, user := range allUsers {
|
|
||||||
if user.Source != domain.UserSourceLdap {
|
|
||||||
continue // ignore non ldap users
|
|
||||||
}
|
|
||||||
if user.ProviderName != providerName {
|
|
||||||
continue // user was synchronized through different provider
|
|
||||||
}
|
|
||||||
if user.IsDisabled() {
|
|
||||||
continue // ignore deactivated
|
|
||||||
}
|
|
||||||
|
|
||||||
existsInLDAP := false
|
existingUser, err := m.users.GetUser(ctx, user.Identifier)
|
||||||
for _, rawUser := range rawUsers {
|
if err != nil && !errors.Is(err, domain.ErrNotFound) {
|
||||||
userId := domain.UserIdentifier(internal.MapDefaultString(rawUser, fields.UserIdentifier, ""))
|
return nil, fmt.Errorf("unable to load existing user %s: %w", user.Identifier, err)
|
||||||
if user.Identifier == userId {
|
}
|
||||||
existsInLDAP = true
|
if existingUser != nil {
|
||||||
break
|
return nil, errors.Join(fmt.Errorf("user %s already exists", user.Identifier), domain.ErrDuplicateEntry)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if existsInLDAP {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
slog.Debug("user is missing in ldap provider, disabling", "user", user.Identifier, "provider", providerName)
|
|
||||||
|
|
||||||
|
// Add default authentication if missing
|
||||||
|
if len(user.Authentications) == 0 {
|
||||||
|
ctxUserInfo := domain.GetUserInfo(ctx)
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
user.Disabled = &now
|
user.Authentications = []domain.UserAuthentication{
|
||||||
user.DisabledReason = domain.DisabledReasonLdapMissing
|
{
|
||||||
|
BaseModel: domain.BaseModel{
|
||||||
err := m.users.SaveUser(ctx, user.Identifier, func(u *domain.User) (*domain.User, error) {
|
CreatedBy: ctxUserInfo.UserId(),
|
||||||
u.Disabled = user.Disabled
|
UpdatedBy: ctxUserInfo.UserId(),
|
||||||
u.DisabledReason = user.DisabledReason
|
CreatedAt: now,
|
||||||
return u, nil
|
UpdatedAt: now,
|
||||||
})
|
},
|
||||||
if err != nil {
|
UserIdentifier: user.Identifier,
|
||||||
return fmt.Errorf("disable error for user id %s: %w", user.Identifier, err)
|
Source: domain.UserSourceDatabase,
|
||||||
|
ProviderName: "",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
m.bus.Publish(app.TopicUserDisabled, user)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
if err := m.validateCreation(ctx, user); err != nil {
|
||||||
|
return nil, fmt.Errorf("creation not allowed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = user.HashPassword()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.users.SaveUser(ctx, user.Identifier, func(u *domain.User) (*domain.User, error) {
|
||||||
|
return user, nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("creation failure: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
m.bus.Publish(app.TopicUserCreated, *user)
|
||||||
|
|
||||||
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// endregion internal-modifiers
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/h44z/wg-portal/internal"
|
||||||
"github.com/h44z/wg-portal/internal/domain"
|
"github.com/h44z/wg-portal/internal/domain"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,11 +14,10 @@ type User struct {
|
|||||||
CreatedAt time.Time `json:"CreatedAt"`
|
CreatedAt time.Time `json:"CreatedAt"`
|
||||||
UpdatedAt time.Time `json:"UpdatedAt"`
|
UpdatedAt time.Time `json:"UpdatedAt"`
|
||||||
|
|
||||||
Identifier string `json:"Identifier"`
|
Identifier string `json:"Identifier"`
|
||||||
Email string `json:"Email"`
|
Email string `json:"Email"`
|
||||||
Source string `json:"Source"`
|
AuthSources []UserAuthSource `json:"AuthSources"`
|
||||||
ProviderName string `json:"ProviderName"`
|
IsAdmin bool `json:"IsAdmin"`
|
||||||
IsAdmin bool `json:"IsAdmin"`
|
|
||||||
|
|
||||||
Firstname string `json:"Firstname,omitempty"`
|
Firstname string `json:"Firstname,omitempty"`
|
||||||
Lastname string `json:"Lastname,omitempty"`
|
Lastname string `json:"Lastname,omitempty"`
|
||||||
@@ -31,8 +31,22 @@ type User struct {
|
|||||||
LockedReason string `json:"LockedReason,omitempty"`
|
LockedReason string `json:"LockedReason,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UserAuthSource represents a single authentication source for a user.
|
||||||
|
// For details about the fields, see the domain.UserAuthentication struct.
|
||||||
|
type UserAuthSource struct {
|
||||||
|
Source string `json:"Source"`
|
||||||
|
ProviderName string `json:"ProviderName"`
|
||||||
|
}
|
||||||
|
|
||||||
// NewUser creates a new User model from a domain.User
|
// NewUser creates a new User model from a domain.User
|
||||||
func NewUser(src domain.User) User {
|
func NewUser(src domain.User) User {
|
||||||
|
authSources := internal.Map(src.Authentications, func(authentication domain.UserAuthentication) UserAuthSource {
|
||||||
|
return UserAuthSource{
|
||||||
|
Source: string(authentication.Source),
|
||||||
|
ProviderName: authentication.ProviderName,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
return User{
|
return User{
|
||||||
CreatedBy: src.CreatedBy,
|
CreatedBy: src.CreatedBy,
|
||||||
UpdatedBy: src.UpdatedBy,
|
UpdatedBy: src.UpdatedBy,
|
||||||
@@ -40,8 +54,7 @@ func NewUser(src domain.User) User {
|
|||||||
UpdatedAt: src.UpdatedAt,
|
UpdatedAt: src.UpdatedAt,
|
||||||
Identifier: string(src.Identifier),
|
Identifier: string(src.Identifier),
|
||||||
Email: src.Email,
|
Email: src.Email,
|
||||||
Source: string(src.Source),
|
AuthSources: authSources,
|
||||||
ProviderName: src.ProviderName,
|
|
||||||
IsAdmin: src.IsAdmin,
|
IsAdmin: src.IsAdmin,
|
||||||
Firstname: src.Firstname,
|
Firstname: src.Firstname,
|
||||||
Lastname: src.Lastname,
|
Lastname: src.Lastname,
|
||||||
|
|||||||
@@ -121,15 +121,25 @@ func (c *StatisticsCollector) collectInterfaceData(ctx context.Context) {
|
|||||||
"error", err)
|
"error", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
now := time.Now()
|
||||||
err = c.db.UpdateInterfaceStatus(ctx, in.Identifier,
|
err = c.db.UpdateInterfaceStatus(ctx, in.Identifier,
|
||||||
func(i *domain.InterfaceStatus) (*domain.InterfaceStatus, error) {
|
func(i *domain.InterfaceStatus) (*domain.InterfaceStatus, error) {
|
||||||
i.UpdatedAt = time.Now()
|
td := domain.CalculateTrafficDelta(
|
||||||
|
string(in.Identifier),
|
||||||
|
i.UpdatedAt, now,
|
||||||
|
i.BytesTransmitted, physicalInterface.BytesUpload,
|
||||||
|
i.BytesReceived, physicalInterface.BytesDownload,
|
||||||
|
)
|
||||||
|
i.UpdatedAt = now
|
||||||
i.BytesReceived = physicalInterface.BytesDownload
|
i.BytesReceived = physicalInterface.BytesDownload
|
||||||
i.BytesTransmitted = physicalInterface.BytesUpload
|
i.BytesTransmitted = physicalInterface.BytesUpload
|
||||||
|
|
||||||
// Update prometheus metrics
|
// Update prometheus metrics
|
||||||
go c.updateInterfaceMetrics(*i)
|
go c.updateInterfaceMetrics(*i)
|
||||||
|
|
||||||
|
// Publish stats update event
|
||||||
|
c.bus.Publish(app.TopicInterfaceStatsUpdated, td)
|
||||||
|
|
||||||
return i, nil
|
return i, nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -172,6 +182,7 @@ func (c *StatisticsCollector) collectPeerData(ctx context.Context) {
|
|||||||
slog.Warn("failed to fetch peers for data collection", "interface", in.Identifier, "error", err)
|
slog.Warn("failed to fetch peers for data collection", "interface", in.Identifier, "error", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
now := time.Now()
|
||||||
for _, peer := range peers {
|
for _, peer := range peers {
|
||||||
var connectionStateChanged bool
|
var connectionStateChanged bool
|
||||||
var newPeerStatus domain.PeerStatus
|
var newPeerStatus domain.PeerStatus
|
||||||
@@ -184,8 +195,15 @@ func (c *StatisticsCollector) collectPeerData(ctx context.Context) {
|
|||||||
lastHandshake = &peer.LastHandshake
|
lastHandshake = &peer.LastHandshake
|
||||||
}
|
}
|
||||||
|
|
||||||
|
td := domain.CalculateTrafficDelta(
|
||||||
|
string(peer.Identifier),
|
||||||
|
p.UpdatedAt, now,
|
||||||
|
p.BytesTransmitted, peer.BytesDownload,
|
||||||
|
p.BytesReceived, peer.BytesUpload,
|
||||||
|
)
|
||||||
|
|
||||||
// calculate if session was restarted
|
// calculate if session was restarted
|
||||||
p.UpdatedAt = time.Now()
|
p.UpdatedAt = now
|
||||||
p.LastSessionStart = getSessionStartTime(*p, peer.BytesUpload, peer.BytesDownload,
|
p.LastSessionStart = getSessionStartTime(*p, peer.BytesUpload, peer.BytesDownload,
|
||||||
lastHandshake)
|
lastHandshake)
|
||||||
p.BytesReceived = peer.BytesUpload // store bytes that where uploaded from the peer and received by the server
|
p.BytesReceived = peer.BytesUpload // store bytes that where uploaded from the peer and received by the server
|
||||||
@@ -195,7 +213,8 @@ func (c *StatisticsCollector) collectPeerData(ctx context.Context) {
|
|||||||
p.CalcConnected()
|
p.CalcConnected()
|
||||||
|
|
||||||
if wasConnected != p.IsConnected {
|
if wasConnected != p.IsConnected {
|
||||||
slog.Debug("peer connection state changed", "peer", peer.Identifier, "connected", p.IsConnected)
|
slog.Debug("peer connection state changed",
|
||||||
|
"peer", peer.Identifier, "connected", p.IsConnected)
|
||||||
connectionStateChanged = true
|
connectionStateChanged = true
|
||||||
newPeerStatus = *p // store new status for event publishing
|
newPeerStatus = *p // store new status for event publishing
|
||||||
}
|
}
|
||||||
@@ -203,6 +222,9 @@ func (c *StatisticsCollector) collectPeerData(ctx context.Context) {
|
|||||||
// Update prometheus metrics
|
// Update prometheus metrics
|
||||||
go c.updatePeerMetrics(ctx, *p)
|
go c.updatePeerMetrics(ctx, *p)
|
||||||
|
|
||||||
|
// Publish stats update event
|
||||||
|
c.bus.Publish(app.TopicPeerStatsUpdated, td)
|
||||||
|
|
||||||
return p, nil
|
return p, nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -374,6 +374,7 @@ func (m Manager) PrepareInterface(ctx context.Context) (*domain.Interface, error
|
|||||||
SaveConfig: m.cfg.Advanced.ConfigStoragePath != "",
|
SaveConfig: m.cfg.Advanced.ConfigStoragePath != "",
|
||||||
DisplayName: string(id),
|
DisplayName: string(id),
|
||||||
Type: domain.InterfaceTypeServer,
|
Type: domain.InterfaceTypeServer,
|
||||||
|
CreateDefaultPeer: m.cfg.Core.CreateDefaultPeer,
|
||||||
DriverType: "",
|
DriverType: "",
|
||||||
Disabled: nil,
|
Disabled: nil,
|
||||||
DisabledReason: "",
|
DisabledReason: "",
|
||||||
@@ -984,7 +985,26 @@ func (m Manager) importPeer(ctx context.Context, in *domain.Interface, p *domain
|
|||||||
peer.InterfaceIdentifier = in.Identifier
|
peer.InterfaceIdentifier = in.Identifier
|
||||||
peer.EndpointPublicKey = domain.NewConfigOption(in.PublicKey, true)
|
peer.EndpointPublicKey = domain.NewConfigOption(in.PublicKey, true)
|
||||||
peer.AllowedIPsStr = domain.NewConfigOption(in.PeerDefAllowedIPsStr, true)
|
peer.AllowedIPsStr = domain.NewConfigOption(in.PeerDefAllowedIPsStr, true)
|
||||||
peer.Interface.Addresses = p.AllowedIPs // use allowed IP's as the peer IP's TODO: Should this also match server interface address' prefix length?
|
|
||||||
|
// split allowed IP's into interface addresses and extra allowed IP's
|
||||||
|
var interfaceAddresses []domain.Cidr
|
||||||
|
var extraAllowedIPs []domain.Cidr
|
||||||
|
for _, allowedIP := range p.AllowedIPs {
|
||||||
|
isHost := (allowedIP.IsV4() && allowedIP.NetLength == 32) || (!allowedIP.IsV4() && allowedIP.NetLength == 128)
|
||||||
|
isNetworkAddr := allowedIP.Addr == allowedIP.NetworkAddr().Addr
|
||||||
|
|
||||||
|
// Network addresses (e.g. 10.0.0.0/24) will always be extra allowed IP's.
|
||||||
|
// For IP addresses, such as 10.0.0.1/24, it is challenging to tell whether it is an interface address or
|
||||||
|
// an extra allowed IP, therefore we treat such addresses as interface addresses.
|
||||||
|
if !isHost && isNetworkAddr {
|
||||||
|
extraAllowedIPs = append(extraAllowedIPs, allowedIP)
|
||||||
|
} else {
|
||||||
|
interfaceAddresses = append(interfaceAddresses, allowedIP)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
peer.Interface.Addresses = interfaceAddresses
|
||||||
|
peer.ExtraAllowedIPsStr = domain.CidrsToString(extraAllowedIPs)
|
||||||
|
|
||||||
peer.Interface.DnsStr = domain.NewConfigOption(in.PeerDefDnsStr, true)
|
peer.Interface.DnsStr = domain.NewConfigOption(in.PeerDefDnsStr, true)
|
||||||
peer.Interface.DnsSearchStr = domain.NewConfigOption(in.PeerDefDnsSearchStr, true)
|
peer.Interface.DnsSearchStr = domain.NewConfigOption(in.PeerDefDnsSearchStr, true)
|
||||||
peer.Interface.Mtu = domain.NewConfigOption(in.PeerDefMtu, true)
|
peer.Interface.Mtu = domain.NewConfigOption(in.PeerDefMtu, true)
|
||||||
|
|||||||
94
internal/app/wireguard/wireguard_interfaces_test.go
Normal file
94
internal/app/wireguard/wireguard_interfaces_test.go
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
package wireguard
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/h44z/wg-portal/internal/domain"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestImportPeer_AddressMapping(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
allowedIPs []string
|
||||||
|
expectedInterface []string
|
||||||
|
expectedExtraAllowed string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "IPv4 host address",
|
||||||
|
allowedIPs: []string{"10.0.0.1/32"},
|
||||||
|
expectedInterface: []string{"10.0.0.1/32"},
|
||||||
|
expectedExtraAllowed: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "IPv6 host address",
|
||||||
|
allowedIPs: []string{"fd00::1/128"},
|
||||||
|
expectedInterface: []string{"fd00::1/128"},
|
||||||
|
expectedExtraAllowed: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "IPv4 network address",
|
||||||
|
allowedIPs: []string{"10.0.1.0/24"},
|
||||||
|
expectedInterface: []string{},
|
||||||
|
expectedExtraAllowed: "10.0.1.0/24",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "IPv4 normal address with mask",
|
||||||
|
allowedIPs: []string{"10.0.1.5/24"},
|
||||||
|
expectedInterface: []string{"10.0.1.5/24"},
|
||||||
|
expectedExtraAllowed: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Mixed addresses",
|
||||||
|
allowedIPs: []string{
|
||||||
|
"10.0.0.1/32", "192.168.1.0/24", "172.16.0.5/24", "fd00::1/128", "fd00:1::/64",
|
||||||
|
},
|
||||||
|
expectedInterface: []string{"10.0.0.1/32", "172.16.0.5/24", "fd00::1/128"},
|
||||||
|
expectedExtraAllowed: "192.168.1.0/24,fd00:1::/64",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
db := &mockDB{}
|
||||||
|
m := Manager{
|
||||||
|
db: db,
|
||||||
|
}
|
||||||
|
|
||||||
|
iface := &domain.Interface{
|
||||||
|
Identifier: "wg0",
|
||||||
|
Type: domain.InterfaceTypeServer,
|
||||||
|
}
|
||||||
|
|
||||||
|
allowedIPs := make([]domain.Cidr, len(tt.allowedIPs))
|
||||||
|
for i, s := range tt.allowedIPs {
|
||||||
|
cidr, _ := domain.CidrFromString(s)
|
||||||
|
allowedIPs[i] = cidr
|
||||||
|
}
|
||||||
|
|
||||||
|
p := &domain.PhysicalPeer{
|
||||||
|
Identifier: "peer1",
|
||||||
|
KeyPair: domain.KeyPair{PublicKey: "peer1-public-key-is-long-enough"},
|
||||||
|
AllowedIPs: allowedIPs,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := m.importPeer(context.Background(), iface, p)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
savedPeer := db.savedPeers["peer1"]
|
||||||
|
assert.NotNil(t, savedPeer)
|
||||||
|
|
||||||
|
// Check interface addresses
|
||||||
|
actualInterface := make([]string, len(savedPeer.Interface.Addresses))
|
||||||
|
for i, addr := range savedPeer.Interface.Addresses {
|
||||||
|
actualInterface[i] = addr.String()
|
||||||
|
}
|
||||||
|
assert.ElementsMatch(t, tt.expectedInterface, actualInterface)
|
||||||
|
|
||||||
|
// Check extra allowed IPs
|
||||||
|
assert.Equal(t, tt.expectedExtraAllowed, savedPeer.ExtraAllowedIPsStr)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,6 +35,10 @@ func (m Manager) CreateDefaultPeer(ctx context.Context, userId domain.UserIdenti
|
|||||||
continue // only create default peers for server interfaces
|
continue // only create default peers for server interfaces
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !iface.CreateDefaultPeer {
|
||||||
|
continue // only create default peers if the interface flag is set
|
||||||
|
}
|
||||||
|
|
||||||
peerAlreadyCreated := slices.ContainsFunc(userPeers, func(peer domain.Peer) bool {
|
peerAlreadyCreated := slices.ContainsFunc(userPeers, func(peer domain.Peer) bool {
|
||||||
return peer.InterfaceIdentifier == iface.Identifier
|
return peer.InterfaceIdentifier == iface.Identifier
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -78,7 +78,12 @@ func (f *mockDB) GetInterfaceAndPeers(ctx context.Context, id domain.InterfaceId
|
|||||||
func (f *mockDB) GetPeersStats(ctx context.Context, ids ...domain.PeerIdentifier) ([]domain.PeerStatus, error) {
|
func (f *mockDB) GetPeersStats(ctx context.Context, ids ...domain.PeerIdentifier) ([]domain.PeerStatus, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
func (f *mockDB) GetAllInterfaces(ctx context.Context) ([]domain.Interface, error) { return nil, nil }
|
func (f *mockDB) GetAllInterfaces(ctx context.Context) ([]domain.Interface, error) {
|
||||||
|
if f.iface != nil {
|
||||||
|
return []domain.Interface{*f.iface}, nil
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
func (f *mockDB) GetInterfaceIps(ctx context.Context) (map[domain.InterfaceIdentifier][]domain.Cidr, error) {
|
func (f *mockDB) GetInterfaceIps(ctx context.Context) (map[domain.InterfaceIdentifier][]domain.Cidr, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@@ -192,3 +197,58 @@ func TestCreatePeer_SetsIdentifier_FromPublicKey(t *testing.T) {
|
|||||||
t.Fatalf("expected peer with identifier %q to be saved in DB", expectedId)
|
t.Fatalf("expected peer with identifier %q to be saved in DB", expectedId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateDefaultPeer_RespectsInterfaceFlag(t *testing.T) {
|
||||||
|
// Arrange
|
||||||
|
cfg := &config.Config{}
|
||||||
|
cfg.Core.CreateDefaultPeer = true
|
||||||
|
|
||||||
|
bus := &mockBus{}
|
||||||
|
ctrlMgr := &ControllerManager{
|
||||||
|
controllers: map[domain.InterfaceBackend]backendInstance{
|
||||||
|
config.LocalBackendName: {Implementation: &mockController{}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
db := &mockDB{
|
||||||
|
iface: &domain.Interface{
|
||||||
|
Identifier: "wg0",
|
||||||
|
Type: domain.InterfaceTypeServer,
|
||||||
|
CreateDefaultPeer: false, // Flag is disabled!
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
m := Manager{
|
||||||
|
cfg: cfg,
|
||||||
|
bus: bus,
|
||||||
|
db: db,
|
||||||
|
wg: ctrlMgr,
|
||||||
|
}
|
||||||
|
|
||||||
|
userId := domain.UserIdentifier("user@example.com")
|
||||||
|
ctx := domain.SetUserInfo(context.Background(), &domain.ContextUserInfo{Id: userId, IsAdmin: true})
|
||||||
|
|
||||||
|
// Act
|
||||||
|
err := m.CreateDefaultPeer(ctx, userId)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateDefaultPeer returned error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(db.savedPeers) != 0 {
|
||||||
|
t.Fatalf("expected no peers to be created because interface flag is false, but got %d", len(db.savedPeers))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now enable the flag and try again
|
||||||
|
db.iface.CreateDefaultPeer = true
|
||||||
|
err = m.CreateDefaultPeer(ctx, userId)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateDefaultPeer returned error after enabling flag: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(db.savedPeers) != 1 {
|
||||||
|
t.Fatalf("expected 1 peer to be created because interface flag is true, but got %d", len(db.savedPeers))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,11 +10,12 @@ type LoginProviderInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type AuthenticatorUserInfo struct {
|
type AuthenticatorUserInfo struct {
|
||||||
Identifier UserIdentifier
|
Identifier UserIdentifier
|
||||||
Email string
|
Email string
|
||||||
Firstname string
|
Firstname string
|
||||||
Lastname string
|
Lastname string
|
||||||
Phone string
|
Phone string
|
||||||
Department string
|
Department string
|
||||||
IsAdmin bool
|
IsAdmin bool
|
||||||
|
AdminInfoAvailable bool // true if the IsAdmin flag is valid
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ const (
|
|||||||
CtxSystemLdapSyncer = "_WG_SYS_LDAP_SYNCER_"
|
CtxSystemLdapSyncer = "_WG_SYS_LDAP_SYNCER_"
|
||||||
CtxSystemWgImporter = "_WG_SYS_WG_IMPORTER_"
|
CtxSystemWgImporter = "_WG_SYS_WG_IMPORTER_"
|
||||||
CtxSystemV1Migrator = "_WG_SYS_V1_MIGRATOR_"
|
CtxSystemV1Migrator = "_WG_SYS_V1_MIGRATOR_"
|
||||||
|
CtxSystemDBMigrator = "_WG_SYS_DB_MIGRATOR_"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ContextUserInfo struct {
|
type ContextUserInfo struct {
|
||||||
|
|||||||
@@ -53,12 +53,13 @@ type Interface struct {
|
|||||||
SaveConfig bool // automatically persist config changes to the wgX.conf file
|
SaveConfig bool // automatically persist config changes to the wgX.conf file
|
||||||
|
|
||||||
// WG Portal specific
|
// WG Portal specific
|
||||||
DisplayName string // a nice display name/ description for the interface
|
DisplayName string // a nice display name/ description for the interface
|
||||||
Type InterfaceType // the interface type, either InterfaceTypeServer or InterfaceTypeClient
|
Type InterfaceType // the interface type, either InterfaceTypeServer or InterfaceTypeClient
|
||||||
Backend InterfaceBackend // the backend that is used to manage the interface (wgctrl, mikrotik, ...)
|
CreateDefaultPeer bool // if true, default peers will be created for this interface
|
||||||
DriverType string // the interface driver type (linux, software, ...)
|
Backend InterfaceBackend // the backend that is used to manage the interface (wgctrl, mikrotik, ...)
|
||||||
Disabled *time.Time `gorm:"index"` // flag that specifies if the interface is enabled (up) or not (down)
|
DriverType string // the interface driver type (linux, software, ...)
|
||||||
DisabledReason string // the reason why the interface has been disabled
|
Disabled *time.Time `gorm:"index"` // flag that specifies if the interface is enabled (up) or not (down)
|
||||||
|
DisabledReason string // the reason why the interface has been disabled
|
||||||
|
|
||||||
// Default settings for the peer, used for new peers, those settings will be published to ConfigOption options of
|
// Default settings for the peer, used for new peers, those settings will be published to ConfigOption options of
|
||||||
// the peer config
|
// the peer config
|
||||||
|
|||||||
@@ -61,3 +61,25 @@ func (r PingerResult) AverageRtt() time.Duration {
|
|||||||
}
|
}
|
||||||
return total / time.Duration(len(r.Rtts))
|
return total / time.Duration(len(r.Rtts))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TrafficDelta struct {
|
||||||
|
EntityId string `json:"EntityId"` // Either peerId or interfaceId
|
||||||
|
BytesReceivedPerSecond uint64 `json:"BytesReceived"`
|
||||||
|
BytesTransmittedPerSecond uint64 `json:"BytesTransmitted"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func CalculateTrafficDelta(id string, oldTime, newTime time.Time, oldTx, newTx, oldRx, newRx uint64) TrafficDelta {
|
||||||
|
timeDiff := uint64(newTime.Sub(oldTime).Seconds())
|
||||||
|
if timeDiff == 0 {
|
||||||
|
return TrafficDelta{
|
||||||
|
EntityId: id,
|
||||||
|
BytesReceivedPerSecond: 0,
|
||||||
|
BytesTransmittedPerSecond: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return TrafficDelta{
|
||||||
|
EntityId: id,
|
||||||
|
BytesReceivedPerSecond: (newRx - oldRx) / timeDiff,
|
||||||
|
BytesTransmittedPerSecond: (newTx - oldTx) / timeDiff,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -25,6 +25,14 @@ type UserIdentifier string
|
|||||||
|
|
||||||
type UserSource string
|
type UserSource string
|
||||||
|
|
||||||
|
type UserAuthentication struct {
|
||||||
|
BaseModel
|
||||||
|
|
||||||
|
UserIdentifier UserIdentifier `gorm:"primaryKey;column:user_identifier"` // sAMAccountName, sub, etc.
|
||||||
|
Source UserSource `gorm:"primaryKey;column:source"`
|
||||||
|
ProviderName string `gorm:"primaryKey;column:provider_name"`
|
||||||
|
}
|
||||||
|
|
||||||
// User is the user model that gets linked to peer entries, by default an empty user model with only the email address is created
|
// User is the user model that gets linked to peer entries, by default an empty user model with only the email address is created
|
||||||
type User struct {
|
type User struct {
|
||||||
BaseModel
|
BaseModel
|
||||||
@@ -32,10 +40,15 @@ type User struct {
|
|||||||
// required fields
|
// required fields
|
||||||
Identifier UserIdentifier `gorm:"primaryKey;column:identifier"`
|
Identifier UserIdentifier `gorm:"primaryKey;column:identifier"`
|
||||||
Email string `form:"email" binding:"required,email"`
|
Email string `form:"email" binding:"required,email"`
|
||||||
Source UserSource
|
Source UserSource // deprecated: moved to Authentications.Source
|
||||||
ProviderName string
|
ProviderName string // deprecated: moved to Authentications.ProviderName
|
||||||
IsAdmin bool
|
IsAdmin bool
|
||||||
|
|
||||||
|
// authentication sources
|
||||||
|
Authentications []UserAuthentication `gorm:"foreignKey:user_identifier"`
|
||||||
|
// synchronization behavior
|
||||||
|
PersistLocalChanges bool `gorm:"column:persist_local_changes"`
|
||||||
|
|
||||||
// optional fields
|
// optional fields
|
||||||
Firstname string `form:"firstname" binding:"omitempty"`
|
Firstname string `form:"firstname" binding:"omitempty"`
|
||||||
Lastname string `form:"lastname" binding:"omitempty"`
|
Lastname string `form:"lastname" binding:"omitempty"`
|
||||||
@@ -81,15 +94,19 @@ func (u *User) IsApiEnabled() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *User) CanChangePassword() error {
|
func (u *User) CanChangePassword() error {
|
||||||
if u.Source == UserSourceDatabase {
|
if slices.ContainsFunc(u.Authentications, func(e UserAuthentication) bool {
|
||||||
return nil
|
return e.Source == UserSourceDatabase
|
||||||
|
}) {
|
||||||
|
return nil // password can be changed for database users
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors.New("password change only allowed for database source")
|
return errors.New("password change only allowed for database source")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *User) HasWeakPassword(minLength int) error {
|
func (u *User) HasWeakPassword(minLength int) error {
|
||||||
if u.Source != UserSourceDatabase {
|
if !slices.ContainsFunc(u.Authentications, func(e UserAuthentication) bool {
|
||||||
|
return e.Source == UserSourceDatabase
|
||||||
|
}) {
|
||||||
return nil // password is not required for non-database users, so no check needed
|
return nil // password is not required for non-database users, so no check needed
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,13 +122,16 @@ func (u *User) HasWeakPassword(minLength int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *User) EditAllowed(new *User) error {
|
func (u *User) EditAllowed(new *User) error {
|
||||||
if u.Source == UserSourceDatabase {
|
if len(u.Authentications) == 1 && u.Authentications[0].Source == UserSourceDatabase {
|
||||||
return nil
|
return nil // database-only users can be edited always
|
||||||
|
}
|
||||||
|
|
||||||
|
if new.PersistLocalChanges {
|
||||||
|
return nil // if changes will be persisted locally, they can be edited always
|
||||||
}
|
}
|
||||||
|
|
||||||
// for users which are not database users, only the notes field and the disabled flag can be updated
|
// for users which are not database users, only the notes field and the disabled flag can be updated
|
||||||
updateOk := u.Identifier == new.Identifier
|
updateOk := u.Identifier == new.Identifier
|
||||||
updateOk = updateOk && u.Source == new.Source
|
|
||||||
updateOk = updateOk && u.IsAdmin == new.IsAdmin
|
updateOk = updateOk && u.IsAdmin == new.IsAdmin
|
||||||
updateOk = updateOk && u.Email == new.Email
|
updateOk = updateOk && u.Email == new.Email
|
||||||
updateOk = updateOk && u.Firstname == new.Firstname
|
updateOk = updateOk && u.Firstname == new.Firstname
|
||||||
@@ -120,7 +140,7 @@ func (u *User) EditAllowed(new *User) error {
|
|||||||
updateOk = updateOk && u.Department == new.Department
|
updateOk = updateOk && u.Department == new.Department
|
||||||
|
|
||||||
if !updateOk {
|
if !updateOk {
|
||||||
return errors.New("edit only allowed for database source")
|
return errors.New("edit only allowed for reserved fields")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -131,8 +151,10 @@ func (u *User) DeleteAllowed() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *User) CheckPassword(password string) error {
|
func (u *User) CheckPassword(password string) error {
|
||||||
if u.Source != UserSourceDatabase {
|
if !slices.ContainsFunc(u.Authentications, func(e UserAuthentication) bool {
|
||||||
return errors.New("invalid user source")
|
return e.Source == UserSourceDatabase
|
||||||
|
}) {
|
||||||
|
return errors.New("invalid user source") // password can only be checked for database users
|
||||||
}
|
}
|
||||||
|
|
||||||
if u.IsDisabled() {
|
if u.IsDisabled() {
|
||||||
@@ -180,9 +202,24 @@ func (u *User) HashPassword() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *User) CopyCalculatedAttributes(src *User) {
|
func (u *User) CopyCalculatedAttributes(src *User, withAuthentications bool) {
|
||||||
u.BaseModel = src.BaseModel
|
u.BaseModel = src.BaseModel
|
||||||
u.LinkedPeerCount = src.LinkedPeerCount
|
u.LinkedPeerCount = src.LinkedPeerCount
|
||||||
|
if withAuthentications {
|
||||||
|
u.Authentications = src.Authentications
|
||||||
|
u.WebAuthnId = src.WebAuthnId
|
||||||
|
u.WebAuthnCredentialList = src.WebAuthnCredentialList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MergeAuthSources merges the given authentication sources with the existing ones.
|
||||||
|
// Already existing sources are not overwritten, nor will be added any duplicates.
|
||||||
|
func (u *User) MergeAuthSources(extSources ...UserAuthentication) {
|
||||||
|
for _, src := range extSources {
|
||||||
|
if !slices.Contains(u.Authentications, src) {
|
||||||
|
u.Authentications = append(u.Authentications, src)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisplayName returns the display name of the user.
|
// DisplayName returns the display name of the user.
|
||||||
|
|||||||
@@ -35,19 +35,25 @@ func TestUser_IsApiEnabled(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUser_CanChangePassword(t *testing.T) {
|
func TestUser_CanChangePassword(t *testing.T) {
|
||||||
user := &User{Source: UserSourceDatabase}
|
user := &User{Authentications: []UserAuthentication{{Source: UserSourceDatabase}}}
|
||||||
assert.NoError(t, user.CanChangePassword())
|
assert.NoError(t, user.CanChangePassword())
|
||||||
|
|
||||||
user.Source = UserSourceLdap
|
user.Authentications = []UserAuthentication{{Source: UserSourceLdap}}
|
||||||
assert.Error(t, user.CanChangePassword())
|
assert.Error(t, user.CanChangePassword())
|
||||||
|
|
||||||
user.Source = UserSourceOauth
|
user.Authentications = []UserAuthentication{{Source: UserSourceOauth}}
|
||||||
assert.Error(t, user.CanChangePassword())
|
assert.Error(t, user.CanChangePassword())
|
||||||
|
|
||||||
|
user.Authentications = []UserAuthentication{{Source: UserSourceLdap}, {Source: UserSourceDatabase}}
|
||||||
|
assert.NoError(t, user.CanChangePassword())
|
||||||
|
|
||||||
|
user.Authentications = []UserAuthentication{{Source: UserSourceOauth}, {Source: UserSourceDatabase}}
|
||||||
|
assert.NoError(t, user.CanChangePassword())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUser_EditAllowed(t *testing.T) {
|
func TestUser_EditAllowed(t *testing.T) {
|
||||||
user := &User{Source: UserSourceDatabase}
|
user := &User{Authentications: []UserAuthentication{{Source: UserSourceDatabase}}}
|
||||||
newUser := &User{Source: UserSourceDatabase}
|
newUser := &User{Authentications: []UserAuthentication{{Source: UserSourceDatabase}}}
|
||||||
assert.NoError(t, user.EditAllowed(newUser))
|
assert.NoError(t, user.EditAllowed(newUser))
|
||||||
|
|
||||||
newUser.Notes = "notes can be changed"
|
newUser.Notes = "notes can be changed"
|
||||||
@@ -59,8 +65,8 @@ func TestUser_EditAllowed(t *testing.T) {
|
|||||||
newUser.Lastname = "lastname or other fields can be changed"
|
newUser.Lastname = "lastname or other fields can be changed"
|
||||||
assert.NoError(t, user.EditAllowed(newUser))
|
assert.NoError(t, user.EditAllowed(newUser))
|
||||||
|
|
||||||
user.Source = UserSourceLdap
|
user.Authentications = []UserAuthentication{{Source: UserSourceLdap}}
|
||||||
newUser.Source = UserSourceLdap
|
newUser.Authentications = []UserAuthentication{{Source: UserSourceLdap}}
|
||||||
newUser.Disabled = nil
|
newUser.Disabled = nil
|
||||||
newUser.Lastname = ""
|
newUser.Lastname = ""
|
||||||
newUser.Notes = "notes can be changed"
|
newUser.Notes = "notes can be changed"
|
||||||
@@ -72,8 +78,8 @@ func TestUser_EditAllowed(t *testing.T) {
|
|||||||
newUser.Lastname = "lastname or other fields can not be changed"
|
newUser.Lastname = "lastname or other fields can not be changed"
|
||||||
assert.Error(t, user.EditAllowed(newUser))
|
assert.Error(t, user.EditAllowed(newUser))
|
||||||
|
|
||||||
user.Source = UserSourceOauth
|
user.Authentications = []UserAuthentication{{Source: UserSourceOauth}}
|
||||||
newUser.Source = UserSourceOauth
|
newUser.Authentications = []UserAuthentication{{Source: UserSourceOauth}}
|
||||||
newUser.Disabled = nil
|
newUser.Disabled = nil
|
||||||
newUser.Lastname = ""
|
newUser.Lastname = ""
|
||||||
newUser.Notes = "notes can be changed"
|
newUser.Notes = "notes can be changed"
|
||||||
@@ -84,6 +90,20 @@ func TestUser_EditAllowed(t *testing.T) {
|
|||||||
|
|
||||||
newUser.Lastname = "lastname or other fields can not be changed"
|
newUser.Lastname = "lastname or other fields can not be changed"
|
||||||
assert.Error(t, user.EditAllowed(newUser))
|
assert.Error(t, user.EditAllowed(newUser))
|
||||||
|
|
||||||
|
user.Authentications = []UserAuthentication{{Source: UserSourceOauth}, {Source: UserSourceDatabase}}
|
||||||
|
newUser.Authentications = []UserAuthentication{{Source: UserSourceOauth}, {Source: UserSourceDatabase}}
|
||||||
|
newUser.PersistLocalChanges = true
|
||||||
|
newUser.Disabled = nil
|
||||||
|
newUser.Lastname = ""
|
||||||
|
newUser.Notes = "notes can be changed"
|
||||||
|
assert.NoError(t, user.EditAllowed(newUser))
|
||||||
|
|
||||||
|
newUser.Disabled = &time.Time{}
|
||||||
|
assert.NoError(t, user.EditAllowed(newUser))
|
||||||
|
|
||||||
|
newUser.Lastname = "lastname or other fields can be changed"
|
||||||
|
assert.NoError(t, user.EditAllowed(newUser))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUser_DeleteAllowed(t *testing.T) {
|
func TestUser_DeleteAllowed(t *testing.T) {
|
||||||
@@ -95,13 +115,15 @@ func TestUser_CheckPassword(t *testing.T) {
|
|||||||
password := "password"
|
password := "password"
|
||||||
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
|
|
||||||
user := &User{Source: UserSourceDatabase, Password: PrivateString(hashedPassword)}
|
user := &User{
|
||||||
|
Authentications: []UserAuthentication{{Source: UserSourceDatabase}}, Password: PrivateString(hashedPassword),
|
||||||
|
}
|
||||||
assert.NoError(t, user.CheckPassword(password))
|
assert.NoError(t, user.CheckPassword(password))
|
||||||
|
|
||||||
user.Password = ""
|
user.Password = ""
|
||||||
assert.Error(t, user.CheckPassword(password))
|
assert.Error(t, user.CheckPassword(password))
|
||||||
|
|
||||||
user.Source = UserSourceLdap
|
user.Authentications = []UserAuthentication{{Source: UserSourceLdap}}
|
||||||
assert.Error(t, user.CheckPassword(password))
|
assert.Error(t, user.CheckPassword(password))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -168,3 +168,12 @@ func BoolToFloat64(b bool) float64 {
|
|||||||
}
|
}
|
||||||
return 0.0
|
return 0.0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Map applies the given function to each element of the given slice and returns the resulting slice
|
||||||
|
func Map[T, V any](ts []T, fn func(T) V) []V {
|
||||||
|
result := make([]V, len(ts))
|
||||||
|
for i, t := range ts {
|
||||||
|
result[i] = fn(t)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
@@ -85,7 +85,8 @@ nav:
|
|||||||
- Usage:
|
- Usage:
|
||||||
- General: documentation/usage/general.md
|
- General: documentation/usage/general.md
|
||||||
- Backends: documentation/usage/backends.md
|
- Backends: documentation/usage/backends.md
|
||||||
- LDAP: documentation/usage/ldap.md
|
- Authentication: documentation/usage/authentication.md
|
||||||
|
- User Management: documentation/usage/user-sync.md
|
||||||
- Security: documentation/usage/security.md
|
- Security: documentation/usage/security.md
|
||||||
- Webhooks: documentation/usage/webhooks.md
|
- Webhooks: documentation/usage/webhooks.md
|
||||||
- Mail Templates: documentation/usage/mail-templates.md
|
- Mail Templates: documentation/usage/mail-templates.md
|
||||||
|
|||||||
Reference in New Issue
Block a user