mirror of
				https://github.com/dani-garcia/vaultwarden.git
				synced 2025-10-26 07:50:02 +02:00 
			
		
		
		
	Compare commits
	
		
			45 Commits
		
	
	
		
			1.34.1
			...
			8e7eeab293
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | 8e7eeab293 | ||
|  | e35c6f8705 | ||
|  | ae7b725c0f | ||
|  | 2a5489a4b2 | ||
|  | 8fd0ee4211 | ||
|  | 4a5516e150 | ||
|  | 7fc94516ce | ||
|  | 5ea0779d6b | ||
|  | a133d4e90c | ||
|  | 49eff787de | ||
|  | cff6c2b3af | ||
|  | a0c76284fd | ||
|  | 318653b0e5 | ||
|  | 5d84f17600 | ||
|  | 0db4b00007 | ||
|  | a0198d8d7c | ||
|  | dfad931dca | ||
|  | 25865efd79 | ||
|  | bcf627930e | ||
|  | ce70cd2cf4 | ||
|  | 2ac589d4b4 | ||
|  | b2e2aef7de | ||
|  | 0755bb19c0 | ||
|  | fee0c1c711 | ||
|  | f58539f0b4 | ||
|  | e718afb441 | ||
|  | 55945ad793 | ||
|  | 4fd22d8e3b | ||
|  | d6a8fb8e48 | ||
|  | 3b48e6e903 | ||
|  | 6b9333b33e | ||
|  | a545636ee5 | ||
|  | f125d5f1a1 | ||
|  | ad75ce281e | ||
|  | 9059437c35 | ||
|  | c84db0daca | ||
|  | 72adc239f5 | ||
|  | 34ebeeca76 | ||
|  | 0469d9ba4c | ||
|  | eaa6ad06ed | ||
|  | 0d3f283c37 | ||
|  | 51a1d641c5 | ||
|  | 90f7e5ff80 | ||
|  | 200999c94e | ||
|  | d363e647e9 | 
| @@ -15,6 +15,14 @@ | ||||
| #################### | ||||
|  | ||||
| ## Main data folder | ||||
| ## This can be a path to local folder or a path to an external location | ||||
| ## depending on features enabled at build time. Possible external locations: | ||||
| ## | ||||
| ## - AWS S3 Bucket (via `s3` feature): s3://bucket-name/path/to/folder | ||||
| ## | ||||
| ## When using an external location, make sure to set TMP_FOLDER, | ||||
| ## TEMPLATES_FOLDER, and DATABASE_URL to local paths and/or a remote database | ||||
| ## location. | ||||
| # DATA_FOLDER=data | ||||
|  | ||||
| ## Individual folders, these override %DATA_FOLDER% | ||||
| @@ -22,10 +30,13 @@ | ||||
| # ICON_CACHE_FOLDER=data/icon_cache | ||||
| # ATTACHMENTS_FOLDER=data/attachments | ||||
| # SENDS_FOLDER=data/sends | ||||
|  | ||||
| ## Temporary folder used for storing temporary file uploads | ||||
| ## Must be a local path. | ||||
| # TMP_FOLDER=data/tmp | ||||
|  | ||||
| ## Templates data folder, by default uses embedded templates | ||||
| ## Check source code to see the format | ||||
| ## HTML template overrides data folder | ||||
| ## Must be a local path. | ||||
| # TEMPLATES_FOLDER=data/templates | ||||
| ## Automatically reload the templates for every request, slow, use only for development | ||||
| # RELOAD_TEMPLATES=false | ||||
| @@ -39,7 +50,9 @@ | ||||
| ######################### | ||||
|  | ||||
| ## Database URL | ||||
| ## When using SQLite, this is the path to the DB file, default to %DATA_FOLDER%/db.sqlite3 | ||||
| ## When using SQLite, this is the path to the DB file, and it defaults to | ||||
| ## %DATA_FOLDER%/db.sqlite3. If DATA_FOLDER is set to an external location, this | ||||
| ## must be set to a local sqlite3 file path. | ||||
| # DATABASE_URL=data/db.sqlite3 | ||||
| ## When using MySQL, specify an appropriate connection URI. | ||||
| ## Details: https://docs.diesel.rs/2.1.x/diesel/mysql/struct.MysqlConnection.html | ||||
| @@ -117,7 +130,7 @@ | ||||
| ## and are always in terms of UTC time (regardless of your local time zone settings). | ||||
| ## | ||||
| ## The schedule format is a bit different from crontab as crontab does not contains seconds. | ||||
| ## You can test the the format here: https://crontab.guru, but remove the first digit! | ||||
| ## You can test the format here: https://crontab.guru, but remove the first digit! | ||||
| ## SEC  MIN   HOUR   DAY OF MONTH    MONTH   DAY OF WEEK | ||||
| ## "0   30   9,12,15     1,15       May-Aug  Mon,Wed,Fri" | ||||
| ## "0   30     *          *            *          *     " | ||||
| @@ -161,6 +174,10 @@ | ||||
| ## Cron schedule of the job that cleans expired Duo contexts from the database. Does nothing if Duo MFA is disabled or set to use the legacy iframe prompt. | ||||
| ## Defaults to every minute. Set blank to disable this job. | ||||
| # DUO_CONTEXT_PURGE_SCHEDULE="30 * * * * *" | ||||
| # | ||||
| ## Cron schedule of the job that cleans sso nonce from incomplete flow | ||||
| ## Defaults to daily (20 minutes after midnight). Set blank to disable this job. | ||||
| # PURGE_INCOMPLETE_SSO_NONCE="0 20 0 * * *" | ||||
|  | ||||
| ######################## | ||||
| ### General settings ### | ||||
| @@ -260,7 +277,7 @@ | ||||
| ## A comma-separated list means only those users can create orgs: | ||||
| # ORG_CREATION_USERS=admin1@example.com,admin2@example.com | ||||
|  | ||||
| ## Invitations org admins to invite users, even when signups are disabled | ||||
| ## Allows org admins to invite users, even when signups are disabled | ||||
| # INVITATIONS_ALLOWED=true | ||||
| ## Name shown in the invitation emails that don't come from a specific organization | ||||
| # INVITATION_ORG_NAME=Vaultwarden | ||||
| @@ -328,16 +345,16 @@ | ||||
|  | ||||
| ## Icon download timeout | ||||
| ## Configure the timeout value when downloading the favicons. | ||||
| ## The default is 10 seconds, but this could be to low on slower network connections | ||||
| ## The default is 10 seconds, but this could be too low on slower network connections | ||||
| # ICON_DOWNLOAD_TIMEOUT=10 | ||||
|  | ||||
| ## Block HTTP domains/IPs by Regex | ||||
| ## Any domains or IPs that match this regex won't be fetched by the internal HTTP client. | ||||
| ## Useful to hide other servers in the local network. Check the WIKI for more details | ||||
| ## NOTE: Always enclose this regex withing single quotes! | ||||
| ## NOTE: Always enclose this regex within single quotes! | ||||
| # HTTP_REQUEST_BLOCK_REGEX='^(192\.168\.0\.[0-9]+|192\.168\.1\.[0-9]+)$' | ||||
|  | ||||
| ## Enabling this will cause the internal HTTP client to refuse to connect to any non global IP address. | ||||
| ## Enabling this will cause the internal HTTP client to refuse to connect to any non-global IP address. | ||||
| ## Useful to secure your internal environment: See https://en.wikipedia.org/wiki/Reserved_IP_addresses for a list of IPs which it will block | ||||
| # HTTP_REQUEST_BLOCK_NON_GLOBAL_IPS=true | ||||
|  | ||||
| @@ -446,6 +463,55 @@ | ||||
| ## Setting this to true will enforce the Single Org Policy to be enabled before you can enable the Reset Password policy. | ||||
| # ENFORCE_SINGLE_ORG_WITH_RESET_PW_POLICY=false | ||||
|  | ||||
| ##################################### | ||||
| ### SSO settings (OpenID Connect) ### | ||||
| ##################################### | ||||
|  | ||||
| ## Controls whether users can login using an OpenID Connect identity provider | ||||
| # SSO_ENABLED=false | ||||
|  | ||||
| ## Prevent users from logging in directly without going through SSO | ||||
| # SSO_ONLY=false | ||||
|  | ||||
| ## On SSO Signup if a user with a matching email already exists make the association | ||||
| # SSO_SIGNUPS_MATCH_EMAIL=true | ||||
|  | ||||
| ## Allow unknown email verification status. Allowing this with `SSO_SIGNUPS_MATCH_EMAIL=true` open potential account takeover. | ||||
| # SSO_ALLOW_UNKNOWN_EMAIL_VERIFICATION=false | ||||
|  | ||||
| ## Base URL of the OIDC server (auto-discovery is used) | ||||
| ##  - Should not include the `/.well-known/openid-configuration` part and no trailing `/` | ||||
| ##  - ${SSO_AUTHORITY}/.well-known/openid-configuration should return a json document: https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationResponse | ||||
| # SSO_AUTHORITY=https://auth.example.com | ||||
|  | ||||
| ## Authorization request scopes. Optional SSO scopes, override if email and profile are not enough (`openid` is implicit). | ||||
| # SSO_SCOPES="email profile" | ||||
|  | ||||
| ## Additional authorization url parameters (ex: to obtain a `refresh_token` with Google Auth). | ||||
| # SSO_AUTHORIZE_EXTRA_PARAMS="access_type=offline&prompt=consent" | ||||
|  | ||||
| ## Activate PKCE for the Auth Code flow. | ||||
| # SSO_PKCE=true | ||||
|  | ||||
| ## Regex for additional trusted Id token audience (by default only the client_id is trusted). | ||||
| # SSO_AUDIENCE_TRUSTED='^$' | ||||
|  | ||||
| ## Set your Client ID and Client Key | ||||
| # SSO_CLIENT_ID=11111 | ||||
| # SSO_CLIENT_SECRET=AAAAAAAAAAAAAAAAAAAAAAAA | ||||
|  | ||||
| ## Optional Master password policy (minComplexity=[0-4]), `enforceOnLogin` is not supported at the moment. | ||||
| # SSO_MASTER_PASSWORD_POLICY='{"enforceOnLogin":false,"minComplexity":3,"minLength":12,"requireLower":false,"requireNumbers":false,"requireSpecial":false,"requireUpper":false}' | ||||
|  | ||||
| ## Use sso only for authentication not the session lifecycle | ||||
| # SSO_AUTH_ONLY_NOT_SESSION=false | ||||
|  | ||||
| ## Client cache for discovery endpoint. Duration in seconds (0 to disable). | ||||
| # SSO_CLIENT_CACHE_EXPIRATION=0 | ||||
|  | ||||
| ## Log all the tokens, LOG_LEVEL=debug is required | ||||
| # SSO_DEBUG_TOKENS=false | ||||
|  | ||||
| ######################## | ||||
| ### MFA/2FA settings ### | ||||
| ######################## | ||||
| @@ -505,7 +571,7 @@ | ||||
| ## | ||||
| ## According to the RFC6238 (https://tools.ietf.org/html/rfc6238), | ||||
| ## we allow by default the TOTP code which was valid one step back and one in the future. | ||||
| ## This can however allow attackers to be a bit more lucky with there attempts because there are 3 valid codes. | ||||
| ## This can however allow attackers to be a bit more lucky with their attempts because there are 3 valid codes. | ||||
| ## You can disable this, so that only the current TOTP Code is allowed. | ||||
| ## Keep in mind that when a sever drifts out of time, valid codes could be marked as invalid. | ||||
| ## In any case, if a code has been used it can not be used again, also codes which predates it will be invalid. | ||||
|   | ||||
							
								
								
									
										1
									
								
								.github/CODEOWNERS
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.github/CODEOWNERS
									
									
									
									
										vendored
									
									
								
							| @@ -1,5 +1,6 @@ | ||||
| /.github @dani-garcia @BlackDex | ||||
| /.github/** @dani-garcia @BlackDex | ||||
| /.github/CODEOWNERS @dani-garcia @BlackDex | ||||
| /.github/ISSUE_TEMPLATE/** @dani-garcia @BlackDex | ||||
| /.github/workflows/** @dani-garcia @BlackDex | ||||
| /SECURITY.md @dani-garcia @BlackDex | ||||
|   | ||||
							
								
								
									
										25
									
								
								.github/ISSUE_TEMPLATE/bug_report.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										25
									
								
								.github/ISSUE_TEMPLATE/bug_report.yml
									
									
									
									
										vendored
									
									
								
							| @@ -8,15 +8,30 @@ body: | ||||
|       value: | | ||||
|         Thanks for taking the time to fill out this bug report! | ||||
|  | ||||
|         Please *do not* submit feature requests or ask for help on how to configure Vaultwarden here. | ||||
|         Please **do not** submit feature requests or ask for help on how to configure Vaultwarden here! | ||||
|  | ||||
|         The [GitHub Discussions](https://github.com/dani-garcia/vaultwarden/discussions/) has sections for Questions and Ideas. | ||||
|  | ||||
|         Our [Wiki](https://github.com/dani-garcia/vaultwarden/wiki/) has topics on how to configure Vaultwarden. | ||||
|  | ||||
|         Also, make sure you are running [](https://github.com/dani-garcia/vaultwarden/releases/latest) of Vaultwarden! | ||||
|         And search for existing open or closed issues or discussions regarding your topic before posting. | ||||
|  | ||||
|         Be sure to check and validate the Vaultwarden Admin Diagnostics (`/admin/diagnostics`) page for any errors! | ||||
|         See here [how to enable the admin page](https://github.com/dani-garcia/vaultwarden/wiki/Enabling-admin-page). | ||||
|  | ||||
|         > [!IMPORTANT] | ||||
|         > ## :bangbang: Search for existing **Closed _AND_ Open** [Issues](https://github.com/dani-garcia/vaultwarden/issues?q=is%3Aissue%20) **_AND_** [Discussions](https://github.com/dani-garcia/vaultwarden/discussions?discussions_q=) regarding your topic before posting! :bangbang: | ||||
|   # | ||||
|   - type: checkboxes | ||||
|     id: checklist | ||||
|     attributes: | ||||
|       label: Prerequisites | ||||
|       description: Please confirm you have completed the following before submitting an issue! | ||||
|       options: | ||||
|         - label: I have searched the existing **Closed _AND_ Open** [Issues](https://github.com/dani-garcia/vaultwarden/issues?q=is%3Aissue%20) **_AND_** [Discussions](https://github.com/dani-garcia/vaultwarden/discussions?discussions_q=) | ||||
|           required: true | ||||
|         - label: I have searched and read the [documentation](https://github.com/dani-garcia/vaultwarden/wiki/) | ||||
|           required: true | ||||
|   # | ||||
|   - id: support-string | ||||
|     type: textarea | ||||
| @@ -36,7 +51,7 @@ body: | ||||
|     attributes: | ||||
|       label: Vaultwarden Build Version | ||||
|       description: What version of Vaultwarden are you running? | ||||
|       placeholder: ex. v1.31.0 or v1.32.0-3466a804 | ||||
|       placeholder: ex. v1.34.0 or v1.34.1-53f58b14 | ||||
|     validations: | ||||
|       required: true | ||||
|   # | ||||
| @@ -67,7 +82,7 @@ body: | ||||
|     attributes: | ||||
|       label: Reverse Proxy | ||||
|       description: Are you using a reverse proxy, if so which and what version? | ||||
|       placeholder: ex. nginx 1.26.2, caddy 2.8.4, traefik 3.1.2, haproxy 3.0 | ||||
|       placeholder: ex. nginx 1.29.0, caddy 2.10.0, traefik 3.4.4, haproxy 3.2 | ||||
|     validations: | ||||
|       required: true | ||||
|   # | ||||
| @@ -115,7 +130,7 @@ body: | ||||
|     attributes: | ||||
|       label: Client Version | ||||
|       description: What version(s) of the client(s) are you seeing the problem on? | ||||
|       placeholder: ex. CLI v2024.7.2, Firefox 130 - v2024.7.0 | ||||
|       placeholder: ex. CLI v2025.7.0, Firefox 140 - v2025.6.1 | ||||
|   # | ||||
|   - id: reproduce | ||||
|     type: textarea | ||||
|   | ||||
							
								
								
									
										14
									
								
								.github/workflows/build.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										14
									
								
								.github/workflows/build.yml
									
									
									
									
										vendored
									
									
								
							| @@ -66,13 +66,15 @@ jobs: | ||||
|       - name: Init Variables | ||||
|         id: toolchain | ||||
|         shell: bash | ||||
|         env: | ||||
|           CHANNEL: ${{ matrix.channel }} | ||||
|         run: | | ||||
|           if [[ "${{ matrix.channel }}" == 'rust-toolchain' ]]; then | ||||
|           if [[ "${CHANNEL}" == 'rust-toolchain' ]]; then | ||||
|             RUST_TOOLCHAIN="$(grep -oP 'channel.*"(\K.*?)(?=")' rust-toolchain.toml)" | ||||
|           elif [[ "${{ matrix.channel }}" == 'msrv' ]]; then | ||||
|           elif [[ "${CHANNEL}" == 'msrv' ]]; then | ||||
|             RUST_TOOLCHAIN="$(grep -oP 'rust-version.*"(\K.*?)(?=")' Cargo.toml)" | ||||
|           else | ||||
|             RUST_TOOLCHAIN="${{ matrix.channel }}" | ||||
|             RUST_TOOLCHAIN="${CHANNEL}" | ||||
|           fi | ||||
|           echo "RUST_TOOLCHAIN=${RUST_TOOLCHAIN}" | tee -a "${GITHUB_OUTPUT}" | ||||
|       # End Determine rust-toolchain version | ||||
| @@ -80,7 +82,7 @@ jobs: | ||||
|  | ||||
|       # Only install the clippy and rustfmt components on the default rust-toolchain | ||||
|       - name: "Install rust-toolchain version" | ||||
|         uses: dtolnay/rust-toolchain@56f84321dbccf38fb67ce29ab63e4754056677e0 # master @ Mar 18, 2025, 8:14 PM GMT+1 | ||||
|         uses: dtolnay/rust-toolchain@b3b07ba8b418998c39fb20f53e8b695cdcc8de1b # master @ Apr 29, 2025, 9:22 PM GMT+2 | ||||
|         if: ${{ matrix.channel == 'rust-toolchain' }} | ||||
|         with: | ||||
|           toolchain: "${{steps.toolchain.outputs.RUST_TOOLCHAIN}}" | ||||
| @@ -90,7 +92,7 @@ jobs: | ||||
|  | ||||
|       # Install the any other channel to be used for which we do not execute clippy and rustfmt | ||||
|       - name: "Install MSRV version" | ||||
|         uses: dtolnay/rust-toolchain@56f84321dbccf38fb67ce29ab63e4754056677e0 # master @ Mar 18, 2025, 8:14 PM GMT+1 | ||||
|         uses: dtolnay/rust-toolchain@b3b07ba8b418998c39fb20f53e8b695cdcc8de1b # master @ Apr 29, 2025, 9:22 PM GMT+2 | ||||
|         if: ${{ matrix.channel != 'rust-toolchain' }} | ||||
|         with: | ||||
|           toolchain: "${{steps.toolchain.outputs.RUST_TOOLCHAIN}}" | ||||
| @@ -115,7 +117,7 @@ jobs: | ||||
|  | ||||
|       # Enable Rust Caching | ||||
|       - name: Rust Caching | ||||
|         uses: Swatinem/rust-cache@9d47c6ad4b02e050fd481d890b2ea34778fd09d6 # v2.7.8 | ||||
|         uses: Swatinem/rust-cache@98c8021b550208e191a6a3145459bfc9fb29c4c0 # v2.8.0 | ||||
|         with: | ||||
|           # Use a custom prefix-key to force a fresh start. This is sometimes needed with bigger changes. | ||||
|           # Like changing the build host from Ubuntu 20.04 to 22.04 for example. | ||||
|   | ||||
							
								
								
									
										5
									
								
								.github/workflows/check-templates.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								.github/workflows/check-templates.yml
									
									
									
									
										vendored
									
									
								
							| @@ -4,7 +4,8 @@ permissions: {} | ||||
| on: [ push, pull_request ] | ||||
|  | ||||
| jobs: | ||||
|   docker-templates:	 | ||||
|   docker-templates: | ||||
|     name: Validate docker templates | ||||
|     permissions: | ||||
|       contents: read | ||||
|     runs-on: ubuntu-24.04 | ||||
| @@ -20,7 +21,7 @@ jobs: | ||||
|  | ||||
|       - name: Run make to rebuild templates | ||||
|         working-directory: docker | ||||
|         run: make  | ||||
|         run: make | ||||
|  | ||||
|       - name: Check for unstaged changes | ||||
|         working-directory: docker | ||||
|   | ||||
							
								
								
									
										2
									
								
								.github/workflows/hadolint.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/hadolint.yml
									
									
									
									
										vendored
									
									
								
							| @@ -14,7 +14,7 @@ jobs: | ||||
|     steps: | ||||
|       # Start Docker Buildx | ||||
|       - name: Setup Docker Buildx | ||||
|         uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 # v3.10.0 | ||||
|         uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1 | ||||
|         # https://github.com/moby/buildkit/issues/3969 | ||||
|         # Also set max parallelism to 2, the default of 4 breaks GitHub Actions and causes OOMKills | ||||
|         with: | ||||
|   | ||||
							
								
								
									
										28
									
								
								.github/workflows/release.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										28
									
								
								.github/workflows/release.yml
									
									
									
									
										vendored
									
									
								
							| @@ -47,7 +47,7 @@ jobs: | ||||
|     # Start a local docker registry to extract the compiled binaries to upload as artifacts and attest them | ||||
|     services: | ||||
|       registry: | ||||
|         image: registry:2 | ||||
|         image: registry@sha256:1fc7de654f2ac1247f0b67e8a459e273b0993be7d2beda1f3f56fbf1001ed3e7 # v3.0.0 | ||||
|         ports: | ||||
|           - 5000:5000 | ||||
|     env: | ||||
| @@ -76,7 +76,7 @@ jobs: | ||||
|  | ||||
|       # Start Docker Buildx | ||||
|       - name: Setup Docker Buildx | ||||
|         uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 # v3.10.0 | ||||
|         uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1 | ||||
|         # https://github.com/moby/buildkit/issues/3969 | ||||
|         # Also set max parallelism to 2, the default of 4 breaks GitHub Actions and causes OOMKills | ||||
|         with: | ||||
| @@ -192,7 +192,7 @@ jobs: | ||||
|  | ||||
|       - name: Bake ${{ matrix.base_image }} containers | ||||
|         id: bake_vw | ||||
|         uses: docker/bake-action@4ba453fbc2db7735392b93edf935aaf9b1e8f747 # v6.5.0 | ||||
|         uses: docker/bake-action@37816e747588cb137173af99ab33873600c46ea8 # v6.8.0 | ||||
|         env: | ||||
|           BASE_TAGS: "${{ env.BASE_TAGS }}" | ||||
|           SOURCE_COMMIT: "${{ env.SOURCE_COMMIT }}" | ||||
| @@ -213,14 +213,15 @@ jobs: | ||||
|         shell: bash | ||||
|         env: | ||||
|           BAKE_METADATA: ${{ steps.bake_vw.outputs.metadata }} | ||||
|           BASE_IMAGE: ${{ matrix.base_image }} | ||||
|         run: | | ||||
|           GET_DIGEST_SHA="$(jq -r '.["${{ matrix.base_image }}-multi"]."containerimage.digest"' <<< "${BAKE_METADATA}")" | ||||
|           GET_DIGEST_SHA="$(jq -r --arg base "$BASE_IMAGE" '.[$base + "-multi"]."containerimage.digest"' <<< "${BAKE_METADATA}")" | ||||
|           echo "DIGEST_SHA=${GET_DIGEST_SHA}" | tee -a "${GITHUB_ENV}" | ||||
|  | ||||
|       # Attest container images | ||||
|       - name: Attest - docker.io - ${{ matrix.base_image }} | ||||
|         if: ${{ env.HAVE_DOCKERHUB_LOGIN == 'true' && steps.bake_vw.outputs.metadata != ''}} | ||||
|         uses: actions/attest-build-provenance@c074443f1aee8d4aeeae555aebba3282517141b2 # v2.2.3 | ||||
|         uses: actions/attest-build-provenance@e8998f949152b193b063cb0ec769d69d929409be # v2.4.0 | ||||
|         with: | ||||
|           subject-name: ${{ vars.DOCKERHUB_REPO }} | ||||
|           subject-digest: ${{ env.DIGEST_SHA }} | ||||
| @@ -228,7 +229,7 @@ jobs: | ||||
|  | ||||
|       - name: Attest - ghcr.io - ${{ matrix.base_image }} | ||||
|         if: ${{ env.HAVE_GHCR_LOGIN == 'true' && steps.bake_vw.outputs.metadata != ''}} | ||||
|         uses: actions/attest-build-provenance@c074443f1aee8d4aeeae555aebba3282517141b2 # v2.2.3 | ||||
|         uses: actions/attest-build-provenance@e8998f949152b193b063cb0ec769d69d929409be # v2.4.0 | ||||
|         with: | ||||
|           subject-name: ${{ vars.GHCR_REPO }} | ||||
|           subject-digest: ${{ env.DIGEST_SHA }} | ||||
| @@ -236,7 +237,7 @@ jobs: | ||||
|  | ||||
|       - name: Attest - quay.io - ${{ matrix.base_image }} | ||||
|         if: ${{ env.HAVE_QUAY_LOGIN == 'true' && steps.bake_vw.outputs.metadata != ''}} | ||||
|         uses: actions/attest-build-provenance@c074443f1aee8d4aeeae555aebba3282517141b2 # v2.2.3 | ||||
|         uses: actions/attest-build-provenance@e8998f949152b193b063cb0ec769d69d929409be # v2.4.0 | ||||
|         with: | ||||
|           subject-name: ${{ vars.QUAY_REPO }} | ||||
|           subject-digest: ${{ env.DIGEST_SHA }} | ||||
| @@ -248,6 +249,7 @@ jobs: | ||||
|         shell: bash | ||||
|         env: | ||||
|           REF_TYPE: ${{ github.ref_type }} | ||||
|           BASE_IMAGE: ${{ matrix.base_image }} | ||||
|         run: | | ||||
|           # Check which main tag we are going to build determined by ref_type | ||||
|           if [[ "${REF_TYPE}" == "tag" ]]; then | ||||
| @@ -257,7 +259,7 @@ jobs: | ||||
|           fi | ||||
|  | ||||
|           # Check which base_image was used and append -alpine if needed | ||||
|           if [[ "${{ matrix.base_image }}" == "alpine" ]]; then | ||||
|           if [[ "${BASE_IMAGE}" == "alpine" ]]; then | ||||
|             EXTRACT_TAG="${EXTRACT_TAG}-alpine" | ||||
|           fi | ||||
|  | ||||
| @@ -266,25 +268,25 @@ jobs: | ||||
|  | ||||
|           # Extract amd64 binary | ||||
|           docker create --name amd64 --platform=linux/amd64 "localhost:5000/vaultwarden/server:${EXTRACT_TAG}" | ||||
|           docker cp amd64:/vaultwarden vaultwarden-amd64-${{ matrix.base_image }} | ||||
|           docker cp amd64:/vaultwarden vaultwarden-amd64-${BASE_IMAGE} | ||||
|           docker rm --force amd64 | ||||
|           docker rmi --force "localhost:5000/vaultwarden/server:${EXTRACT_TAG}" | ||||
|  | ||||
|           # Extract arm64 binary | ||||
|           docker create --name arm64 --platform=linux/arm64 "localhost:5000/vaultwarden/server:${EXTRACT_TAG}" | ||||
|           docker cp arm64:/vaultwarden vaultwarden-arm64-${{ matrix.base_image }} | ||||
|           docker cp arm64:/vaultwarden vaultwarden-arm64-${BASE_IMAGE} | ||||
|           docker rm --force arm64 | ||||
|           docker rmi --force "localhost:5000/vaultwarden/server:${EXTRACT_TAG}" | ||||
|  | ||||
|           # Extract armv7 binary | ||||
|           docker create --name armv7 --platform=linux/arm/v7 "localhost:5000/vaultwarden/server:${EXTRACT_TAG}" | ||||
|           docker cp armv7:/vaultwarden vaultwarden-armv7-${{ matrix.base_image }} | ||||
|           docker cp armv7:/vaultwarden vaultwarden-armv7-${BASE_IMAGE} | ||||
|           docker rm --force armv7 | ||||
|           docker rmi --force "localhost:5000/vaultwarden/server:${EXTRACT_TAG}" | ||||
|  | ||||
|           # Extract armv6 binary | ||||
|           docker create --name armv6 --platform=linux/arm/v6 "localhost:5000/vaultwarden/server:${EXTRACT_TAG}" | ||||
|           docker cp armv6:/vaultwarden vaultwarden-armv6-${{ matrix.base_image }} | ||||
|           docker cp armv6:/vaultwarden vaultwarden-armv6-${BASE_IMAGE} | ||||
|           docker rm --force armv6 | ||||
|           docker rmi --force "localhost:5000/vaultwarden/server:${EXTRACT_TAG}" | ||||
|  | ||||
| @@ -314,7 +316,7 @@ jobs: | ||||
|           path: vaultwarden-armv6-${{ matrix.base_image }} | ||||
|  | ||||
|       - name: "Attest artifacts ${{ matrix.base_image }}" | ||||
|         uses: actions/attest-build-provenance@c074443f1aee8d4aeeae555aebba3282517141b2 # v2.2.3 | ||||
|         uses: actions/attest-build-provenance@e8998f949152b193b063cb0ec769d69d929409be # v2.4.0 | ||||
|         with: | ||||
|           subject-path: vaultwarden-* | ||||
|       # End Upload artifacts to Github Actions | ||||
|   | ||||
							
								
								
									
										4
									
								
								.github/workflows/trivy.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.github/workflows/trivy.yml
									
									
									
									
										vendored
									
									
								
							| @@ -36,7 +36,7 @@ jobs: | ||||
|           persist-credentials: false | ||||
|  | ||||
|       - name: Run Trivy vulnerability scanner | ||||
|         uses: aquasecurity/trivy-action@6c175e9c4083a92bbca2f9724c8a5e33bc2d97a5 # v0.30.0 | ||||
|         uses: aquasecurity/trivy-action@dc5a429b52fcf669ce959baa2c2dd26090d2a6c4 # v0.32.0 | ||||
|         env: | ||||
|           TRIVY_DB_REPOSITORY: docker.io/aquasec/trivy-db:2,public.ecr.aws/aquasecurity/trivy-db:2,ghcr.io/aquasecurity/trivy-db:2 | ||||
|           TRIVY_JAVA_DB_REPOSITORY: docker.io/aquasec/trivy-java-db:1,public.ecr.aws/aquasecurity/trivy-java-db:1,ghcr.io/aquasecurity/trivy-java-db:1 | ||||
| @@ -48,6 +48,6 @@ jobs: | ||||
|           severity: CRITICAL,HIGH | ||||
|  | ||||
|       - name: Upload Trivy scan results to GitHub Security tab | ||||
|         uses: github/codeql-action/upload-sarif@86b04fb0e47484f7282357688f21d5d0e32175fe # v3.27.5 | ||||
|         uses: github/codeql-action/upload-sarif@4e828ff8d448a8a6e532957b1811f387a63867e8 # v3.29.4 | ||||
|         with: | ||||
|           sarif_file: 'trivy-results.sarif' | ||||
|   | ||||
							
								
								
									
										28
									
								
								.github/workflows/zizmor.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								.github/workflows/zizmor.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | ||||
| name: Security Analysis with zizmor | ||||
|  | ||||
| on: | ||||
|   push: | ||||
|     branches: ["main"] | ||||
|   pull_request: | ||||
|     branches: ["**"] | ||||
|  | ||||
| permissions: {} | ||||
|  | ||||
| jobs: | ||||
|   zizmor: | ||||
|     name: Run zizmor | ||||
|     runs-on: ubuntu-latest | ||||
|     permissions: | ||||
|       security-events: write | ||||
|     steps: | ||||
|       - name: Checkout repository | ||||
|         uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | ||||
|         with: | ||||
|           persist-credentials: false | ||||
|  | ||||
|       - name: Run zizmor | ||||
|         uses: zizmorcore/zizmor-action@f52a838cfabf134edcbaa7c8b3677dde20045018 # v0.1.1 | ||||
|         with: | ||||
|           # intentionally not scanning the entire repository, | ||||
|           # since it contains integration tests. | ||||
|           inputs: ./.github/ | ||||
							
								
								
									
										2366
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2366
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										60
									
								
								Cargo.toml
									
									
									
									
									
								
							
							
						
						
									
										60
									
								
								Cargo.toml
									
									
									
									
									
								
							| @@ -6,7 +6,7 @@ name = "vaultwarden" | ||||
| version = "1.0.0" | ||||
| authors = ["Daniel García <dani-garcia@users.noreply.github.com>"] | ||||
| edition = "2021" | ||||
| rust-version = "1.85.0" | ||||
| rust-version = "1.87.0" | ||||
| resolver = "2" | ||||
|  | ||||
| repository = "https://github.com/dani-garcia/vaultwarden" | ||||
| @@ -32,6 +32,11 @@ enable_mimalloc = ["dep:mimalloc"] | ||||
| # You also need to set an env variable `QUERY_LOGGER=1` to fully activate this so you do not have to re-compile | ||||
| # if you want to turn off the logging for a specific run. | ||||
| query_logger = ["dep:diesel_logger"] | ||||
| s3 = ["opendal/services-s3", "dep:aws-config", "dep:aws-credential-types", "dep:aws-smithy-runtime-api", "dep:anyhow", "dep:http", "dep:reqsign"] | ||||
|  | ||||
| # OIDC specific features | ||||
| oidc-accept-rfc3339-timestamps = ["openidconnect/accept-rfc3339-timestamps"] | ||||
| oidc-accept-string-booleans = ["openidconnect/accept-string-booleans"] | ||||
|  | ||||
| # Enable unstable features, requires nightly | ||||
| # Currently only used to enable rusts official ip support | ||||
| @@ -72,14 +77,15 @@ dashmap = "6.1.0" | ||||
|  | ||||
| # Async futures | ||||
| futures = "0.3.31" | ||||
| tokio = { version = "1.45.1", features = ["rt-multi-thread", "fs", "io-util", "parking_lot", "time", "signal", "net"] } | ||||
| tokio = { version = "1.47.1", features = ["rt-multi-thread", "fs", "io-util", "parking_lot", "time", "signal", "net"] } | ||||
| tokio-util = { version = "0.7.16", features = ["compat"]} | ||||
|  | ||||
| # A generic serialization/deserialization framework | ||||
| serde = { version = "1.0.219", features = ["derive"] } | ||||
| serde_json = "1.0.140" | ||||
| serde_json = "1.0.142" | ||||
|  | ||||
| # A safe, extensible ORM and Query builder | ||||
| diesel = { version = "2.2.10", features = ["chrono", "r2d2", "numeric"] } | ||||
| diesel = { version = "2.2.12", features = ["chrono", "r2d2", "numeric"] } | ||||
| diesel_migrations = "2.2.0" | ||||
| diesel_logger = { version = "0.4.0", optional = true } | ||||
|  | ||||
| @@ -87,10 +93,10 @@ derive_more = { version = "2.0.1", features = ["from", "into", "as_ref", "deref" | ||||
| diesel-derive-newtype = "2.1.2" | ||||
|  | ||||
| # Bundled/Static SQLite | ||||
| libsqlite3-sys = { version = "0.33.0", features = ["bundled"], optional = true } | ||||
| libsqlite3-sys = { version = "0.35.0", features = ["bundled"], optional = true } | ||||
|  | ||||
| # Crypto-related libraries | ||||
| rand = "0.9.1" | ||||
| rand = "0.9.2" | ||||
| ring = "0.17.14" | ||||
| subtle = "2.6.1" | ||||
|  | ||||
| @@ -99,7 +105,7 @@ uuid = { version = "1.17.0", features = ["v4"] } | ||||
|  | ||||
| # Date and time libraries | ||||
| chrono = { version = "0.4.41", features = ["clock", "serde"], default-features = false } | ||||
| chrono-tz = "0.10.3" | ||||
| chrono-tz = "0.10.4" | ||||
| time = "0.3.41" | ||||
|  | ||||
| # Job scheduler | ||||
| @@ -118,13 +124,18 @@ totp-lite = "2.0.1" | ||||
| yubico = { package = "yubico_ng", version = "0.13.0", features = ["online-tokio"], default-features = false } | ||||
|  | ||||
| # WebAuthn libraries | ||||
| webauthn-rs = "0.3.2" | ||||
| # danger-allow-state-serialisation is needed to save the state in the db | ||||
| # danger-credential-internals is needed to support U2F to Webauthn migration | ||||
| # danger-user-presence-only-security-keys is needed to disable UV | ||||
| webauthn-rs = { version = "0.5.2", features = ["danger-allow-state-serialisation", "danger-credential-internals", "danger-user-presence-only-security-keys"] } | ||||
| webauthn-rs-proto = "0.5.2" | ||||
| webauthn-rs-core = "0.5.2" | ||||
|  | ||||
| # Handling of URL's for WebAuthn and favicons | ||||
| url = "2.5.4" | ||||
|  | ||||
| # Email libraries | ||||
| lettre = { version = "0.11.16", features = ["smtp-transport", "sendmail-transport", "builder", "serde", "tokio1-native-tls", "hostname", "tracing", "tokio1"], default-features = false } | ||||
| lettre = { version = "0.11.18", features = ["smtp-transport", "sendmail-transport", "builder", "serde", "hostname", "tracing", "tokio1-rustls", "ring", "rustls-native-certs"], default-features = false } | ||||
| percent-encoding = "2.3.1" # URL encoding library used for URL's in the emails | ||||
| email_address = "0.2.9" | ||||
|  | ||||
| @@ -132,7 +143,7 @@ email_address = "0.2.9" | ||||
| handlebars = { version = "6.3.2", features = ["dir_source"] } | ||||
|  | ||||
| # HTTP client (Used for favicons, version check, DUO and HIBP API) | ||||
| reqwest = { version = "0.12.15", features = ["native-tls-alpn", "stream", "json", "gzip", "brotli", "socks", "cookies"] } | ||||
| reqwest = { version = "0.12.22", features = ["rustls-tls", "rustls-tls-native-roots", "stream", "json", "deflate", "gzip", "brotli", "zstd", "socks", "cookies", "charset", "http2", "system-proxy"], default-features = false} | ||||
| hickory-resolver = "0.25.2" | ||||
|  | ||||
| # Favicon extraction libraries | ||||
| @@ -140,32 +151,37 @@ html5gum = "0.7.0" | ||||
| regex = { version = "1.11.1", features = ["std", "perf", "unicode-perl"], default-features = false } | ||||
| data-url = "0.3.1" | ||||
| bytes = "1.10.1" | ||||
| svg-hush = "0.9.5" | ||||
|  | ||||
| # Cache function results (Used for version check and favicon fetching) | ||||
| cached = { version = "0.55.1", features = ["async"] } | ||||
| cached = { version = "0.56.0", features = ["async"] } | ||||
|  | ||||
| # Used for custom short lived cookie jar during favicon extraction | ||||
| cookie = "0.18.1" | ||||
| cookie_store = "0.21.1" | ||||
|  | ||||
| # Used by U2F, JWT and PostgreSQL | ||||
| openssl = "0.10.72" | ||||
| openssl = "0.10.73" | ||||
|  | ||||
| # CLI argument parsing | ||||
| pico-args = "0.5.0" | ||||
|  | ||||
| # Macro ident concatenation | ||||
| pastey = "0.1.0" | ||||
| governor = "0.10.0" | ||||
| governor = "0.10.1" | ||||
|  | ||||
| # OIDC for SSO | ||||
| openidconnect = { version = "4.0.1", features = ["reqwest", "native-tls"] } | ||||
| mini-moka = "0.10.3" | ||||
|  | ||||
| # Check client versions for specific features. | ||||
| semver = "1.0.26" | ||||
|  | ||||
| # Allow overriding the default memory allocator | ||||
| # Mainly used for the musl builds, since the default musl malloc is very slow | ||||
| mimalloc = { version = "0.1.46", features = ["secure"], default-features = false, optional = true } | ||||
| mimalloc = { version = "0.1.47", features = ["secure"], default-features = false, optional = true } | ||||
|  | ||||
| which = "7.0.3" | ||||
| which = "8.0.0" | ||||
|  | ||||
| # Argon2 library with support for the PHC format | ||||
| argon2 = "0.5.3" | ||||
| @@ -176,6 +192,17 @@ rpassword = "7.4.0" | ||||
| # Loading a dynamic CSS Stylesheet | ||||
| grass_compiler = { version = "0.13.4", default-features = false } | ||||
|  | ||||
| # File are accessed through Apache OpenDAL | ||||
| opendal = { version = "0.54.0", features = ["services-fs"], default-features = false } | ||||
|  | ||||
| # For retrieving AWS credentials, including temporary SSO credentials | ||||
| anyhow = { version = "1.0.98", optional = true } | ||||
| aws-config = { version = "1.8.4", features = ["behavior-version-latest", "rt-tokio", "credentials-process", "sso"], default-features = false, optional = true } | ||||
| aws-credential-types = { version = "1.2.5", optional = true } | ||||
| aws-smithy-runtime-api = { version = "1.8.7", optional = true } | ||||
| http = { version = "1.3.1", optional = true } | ||||
| reqsign = { version = "0.16.5", optional = true } | ||||
|  | ||||
| # Strip debuginfo from the release builds | ||||
| # The debug symbols are to provide better panic traces | ||||
| # Also enable fat LTO and use 1 codegen unit for optimizations | ||||
| @@ -257,6 +284,7 @@ clone_on_ref_ptr = "deny" | ||||
| equatable_if_let = "deny" | ||||
| filter_map_next = "deny" | ||||
| float_cmp_const = "deny" | ||||
| implicit_clone = "deny" | ||||
| inefficient_to_string = "deny" | ||||
| iter_on_empty_collections = "deny" | ||||
| iter_on_single_items = "deny" | ||||
| @@ -265,14 +293,12 @@ macro_use_imports = "deny" | ||||
| manual_assert = "deny" | ||||
| manual_instant_elapsed = "deny" | ||||
| manual_string_new = "deny" | ||||
| match_on_vec_items = "deny" | ||||
| match_wildcard_for_single_variants = "deny" | ||||
| mem_forget = "deny" | ||||
| needless_continue = "deny" | ||||
| needless_lifetimes = "deny" | ||||
| option_option = "deny" | ||||
| string_add_assign = "deny" | ||||
| string_to_string = "deny" | ||||
| unnecessary_join = "deny" | ||||
| unnecessary_self_imports = "deny" | ||||
| unnested_or_patterns = "deny" | ||||
|   | ||||
							
								
								
									
										24
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								README.md
									
									
									
									
									
								
							| @@ -59,19 +59,21 @@ A nearly complete implementation of the Bitwarden Client API is provided, includ | ||||
| ## Usage | ||||
|  | ||||
| > [!IMPORTANT] | ||||
| > Most modern web browsers disallow the use of Web Crypto APIs in insecure contexts. In this case, you might get an error like `Cannot read property 'importKey'`. To solve this problem, you need to access the web vault via HTTPS or localhost. | ||||
| > | ||||
| >This can be configured in [Vaultwarden directly](https://github.com/dani-garcia/vaultwarden/wiki/Enabling-HTTPS) or using a third-party reverse proxy ([some examples](https://github.com/dani-garcia/vaultwarden/wiki/Proxy-examples)). | ||||
| > | ||||
| >If you have an available domain name, you can get HTTPS certificates with [Let's Encrypt](https://letsencrypt.org/), or you can generate self-signed certificates with utilities like [mkcert](https://github.com/FiloSottile/mkcert). Some proxies automatically do this step, like Caddy or Traefik (see examples linked above). | ||||
| > The web-vault requires the use a secure context for the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API). | ||||
| > That means it will only work via `http://localhost:8000` (using the port from the example below) or if you [enable HTTPS](https://github.com/dani-garcia/vaultwarden/wiki/Enabling-HTTPS). | ||||
|  | ||||
| The recommended way to install and use Vaultwarden is via our container images which are published to [ghcr.io](https://github.com/dani-garcia/vaultwarden/pkgs/container/vaultwarden), [docker.io](https://hub.docker.com/r/vaultwarden/server) and [quay.io](https://quay.io/repository/vaultwarden/server). | ||||
| See [which container image to use](https://github.com/dani-garcia/vaultwarden/wiki/Which-container-image-to-use) for an explanation of the provided tags. | ||||
|  | ||||
| There are also [community driven packages](https://github.com/dani-garcia/vaultwarden/wiki/Third-party-packages) which can be used, but those might be lagging behind the latest version or might deviate in the way Vaultwarden is configured, as described in our [Wiki](https://github.com/dani-garcia/vaultwarden/wiki). | ||||
|  | ||||
| Alternatively, you can also [build Vaultwarden](https://github.com/dani-garcia/vaultwarden/wiki/Building-binary) yourself. | ||||
|  | ||||
| While Vaultwarden is based upon the [Rocket web framework](https://rocket.rs) which has built-in support for TLS our recommendation would be that you setup a reverse proxy (see [proxy examples](https://github.com/dani-garcia/vaultwarden/wiki/Proxy-examples)). | ||||
|  | ||||
| > [!TIP] | ||||
| >**For more detailed examples on how to install, use and configure Vaultwarden you can check our [Wiki](https://github.com/dani-garcia/vaultwarden/wiki).** | ||||
|  | ||||
| The main way to use Vaultwarden is via our container images which are published to [ghcr.io](https://github.com/dani-garcia/vaultwarden/pkgs/container/vaultwarden), [docker.io](https://hub.docker.com/r/vaultwarden/server) and [quay.io](https://quay.io/repository/vaultwarden/server). | ||||
|  | ||||
| There are also [community driven packages](https://github.com/dani-garcia/vaultwarden/wiki/Third-party-packages) which can be used, but those might be lagging behind the latest version or might deviate in the way Vaultwarden is configured, as described in our [Wiki](https://github.com/dani-garcia/vaultwarden/wiki). | ||||
|  | ||||
| ### Docker/Podman CLI | ||||
|  | ||||
| Pull the container image and mount a volume from the host for persistent storage.<br> | ||||
| @@ -83,7 +85,7 @@ docker run --detach --name vaultwarden \ | ||||
|   --env DOMAIN="https://vw.domain.tld" \ | ||||
|   --volume /vw-data/:/data/ \ | ||||
|   --restart unless-stopped \ | ||||
|   --publish 80:80 \ | ||||
|   --publish 127.0.0.1:8000:80 \ | ||||
|   vaultwarden/server:latest | ||||
| ``` | ||||
|  | ||||
| @@ -104,7 +106,7 @@ services: | ||||
|     volumes: | ||||
|       - ./vw-data/:/data/ | ||||
|     ports: | ||||
|       - 80:80 | ||||
|       - 127.0.0.1:8000:80 | ||||
| ``` | ||||
|  | ||||
| <br> | ||||
|   | ||||
							
								
								
									
										3
									
								
								build.rs
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								build.rs
									
									
									
									
									
								
							| @@ -11,6 +11,8 @@ fn main() { | ||||
|     println!("cargo:rustc-cfg=postgresql"); | ||||
|     #[cfg(feature = "query_logger")] | ||||
|     println!("cargo:rustc-cfg=query_logger"); | ||||
|     #[cfg(feature = "s3")] | ||||
|     println!("cargo:rustc-cfg=s3"); | ||||
|  | ||||
|     #[cfg(not(any(feature = "sqlite", feature = "mysql", feature = "postgresql")))] | ||||
|     compile_error!( | ||||
| @@ -23,6 +25,7 @@ fn main() { | ||||
|     println!("cargo::rustc-check-cfg=cfg(mysql)"); | ||||
|     println!("cargo::rustc-check-cfg=cfg(postgresql)"); | ||||
|     println!("cargo::rustc-check-cfg=cfg(query_logger)"); | ||||
|     println!("cargo::rustc-check-cfg=cfg(s3)"); | ||||
|  | ||||
|     // Rerun when these paths are changed. | ||||
|     // Someone could have checked-out a tag or specific commit, but no other files changed. | ||||
|   | ||||
| @@ -1,13 +1,13 @@ | ||||
| --- | ||||
| vault_version: "v2025.5.0" | ||||
| vault_image_digest: "sha256:a0a377b810e66a4ebf1416f732d2be06f3262bf5a5238695af88d3ec6871cc0e" | ||||
| vault_version: "v2025.7.0" | ||||
| vault_image_digest: "sha256:f6ac819a2cd9e226f2cd2ec26196ede94a41e672e9672a11b5f307a19278b15e" | ||||
| # Cross Compile Docker Helper Scripts v1.6.1 | ||||
| # We use the linux/amd64 platform shell scripts since there is no difference between the different platform scripts | ||||
| # https://github.com/tonistiigi/xx | https://hub.docker.com/r/tonistiigi/xx/tags | ||||
| xx_image_digest: "sha256:9c207bead753dda9430bdd15425c6518fc7a03d866103c516a2c6889188f5894" | ||||
| rust_version: 1.87.0 # Rust version to be used | ||||
| rust_version: 1.89.0 # Rust version to be used | ||||
| debian_version: bookworm # Debian release name to be used | ||||
| alpine_version: "3.21" # Alpine version to be used | ||||
| alpine_version: "3.22" # Alpine version to be used | ||||
| # For which platforms/architectures will we try to build images | ||||
| platforms: ["linux/amd64", "linux/arm64", "linux/arm/v7", "linux/arm/v6"] | ||||
| # Determine the build images per OS/Arch | ||||
|   | ||||
| @@ -19,23 +19,23 @@ | ||||
| # - From https://hub.docker.com/r/vaultwarden/web-vault/tags, | ||||
| #   click the tag name to view the digest of the image it currently points to. | ||||
| # - From the command line: | ||||
| #     $ docker pull docker.io/vaultwarden/web-vault:v2025.5.0 | ||||
| #     $ docker image inspect --format "{{.RepoDigests}}" docker.io/vaultwarden/web-vault:v2025.5.0 | ||||
| #     [docker.io/vaultwarden/web-vault@sha256:a0a377b810e66a4ebf1416f732d2be06f3262bf5a5238695af88d3ec6871cc0e] | ||||
| #     $ docker pull docker.io/vaultwarden/web-vault:v2025.7.0 | ||||
| #     $ docker image inspect --format "{{.RepoDigests}}" docker.io/vaultwarden/web-vault:v2025.7.0 | ||||
| #     [docker.io/vaultwarden/web-vault@sha256:f6ac819a2cd9e226f2cd2ec26196ede94a41e672e9672a11b5f307a19278b15e] | ||||
| # | ||||
| # - Conversely, to get the tag name from the digest: | ||||
| #     $ docker image inspect --format "{{.RepoTags}}" docker.io/vaultwarden/web-vault@sha256:a0a377b810e66a4ebf1416f732d2be06f3262bf5a5238695af88d3ec6871cc0e | ||||
| #     [docker.io/vaultwarden/web-vault:v2025.5.0] | ||||
| #     $ docker image inspect --format "{{.RepoTags}}" docker.io/vaultwarden/web-vault@sha256:f6ac819a2cd9e226f2cd2ec26196ede94a41e672e9672a11b5f307a19278b15e | ||||
| #     [docker.io/vaultwarden/web-vault:v2025.7.0] | ||||
| # | ||||
| FROM --platform=linux/amd64 docker.io/vaultwarden/web-vault@sha256:a0a377b810e66a4ebf1416f732d2be06f3262bf5a5238695af88d3ec6871cc0e AS vault | ||||
| FROM --platform=linux/amd64 docker.io/vaultwarden/web-vault@sha256:f6ac819a2cd9e226f2cd2ec26196ede94a41e672e9672a11b5f307a19278b15e AS vault | ||||
|  | ||||
| ########################## ALPINE BUILD IMAGES ########################## | ||||
| ## NOTE: The Alpine Base Images do not support other platforms then linux/amd64 | ||||
| ## And for Alpine we define all build images here, they will only be loaded when actually used | ||||
| FROM --platform=linux/amd64 ghcr.io/blackdex/rust-musl:x86_64-musl-stable-1.87.0 AS build_amd64 | ||||
| FROM --platform=linux/amd64 ghcr.io/blackdex/rust-musl:aarch64-musl-stable-1.87.0 AS build_arm64 | ||||
| FROM --platform=linux/amd64 ghcr.io/blackdex/rust-musl:armv7-musleabihf-stable-1.87.0 AS build_armv7 | ||||
| FROM --platform=linux/amd64 ghcr.io/blackdex/rust-musl:arm-musleabi-stable-1.87.0 AS build_armv6 | ||||
| FROM --platform=linux/amd64 ghcr.io/blackdex/rust-musl:x86_64-musl-stable-1.89.0 AS build_amd64 | ||||
| FROM --platform=linux/amd64 ghcr.io/blackdex/rust-musl:aarch64-musl-stable-1.89.0 AS build_arm64 | ||||
| FROM --platform=linux/amd64 ghcr.io/blackdex/rust-musl:armv7-musleabihf-stable-1.89.0 AS build_armv7 | ||||
| FROM --platform=linux/amd64 ghcr.io/blackdex/rust-musl:arm-musleabi-stable-1.89.0 AS build_armv6 | ||||
|  | ||||
| ########################## BUILD IMAGE ########################## | ||||
| # hadolint ignore=DL3006 | ||||
| @@ -127,7 +127,7 @@ RUN source /env-cargo && \ | ||||
| # To uninstall: docker run --privileged --rm tonistiigi/binfmt --uninstall 'qemu-*' | ||||
| # | ||||
| # We need to add `--platform` here, because of a podman bug: https://github.com/containers/buildah/issues/4742 | ||||
| FROM --platform=$TARGETPLATFORM docker.io/library/alpine:3.21 | ||||
| FROM --platform=$TARGETPLATFORM docker.io/library/alpine:3.22 | ||||
|  | ||||
| ENV ROCKET_PROFILE="release" \ | ||||
|     ROCKET_ADDRESS=0.0.0.0 \ | ||||
|   | ||||
| @@ -19,15 +19,15 @@ | ||||
| # - From https://hub.docker.com/r/vaultwarden/web-vault/tags, | ||||
| #   click the tag name to view the digest of the image it currently points to. | ||||
| # - From the command line: | ||||
| #     $ docker pull docker.io/vaultwarden/web-vault:v2025.5.0 | ||||
| #     $ docker image inspect --format "{{.RepoDigests}}" docker.io/vaultwarden/web-vault:v2025.5.0 | ||||
| #     [docker.io/vaultwarden/web-vault@sha256:a0a377b810e66a4ebf1416f732d2be06f3262bf5a5238695af88d3ec6871cc0e] | ||||
| #     $ docker pull docker.io/vaultwarden/web-vault:v2025.7.0 | ||||
| #     $ docker image inspect --format "{{.RepoDigests}}" docker.io/vaultwarden/web-vault:v2025.7.0 | ||||
| #     [docker.io/vaultwarden/web-vault@sha256:f6ac819a2cd9e226f2cd2ec26196ede94a41e672e9672a11b5f307a19278b15e] | ||||
| # | ||||
| # - Conversely, to get the tag name from the digest: | ||||
| #     $ docker image inspect --format "{{.RepoTags}}" docker.io/vaultwarden/web-vault@sha256:a0a377b810e66a4ebf1416f732d2be06f3262bf5a5238695af88d3ec6871cc0e | ||||
| #     [docker.io/vaultwarden/web-vault:v2025.5.0] | ||||
| #     $ docker image inspect --format "{{.RepoTags}}" docker.io/vaultwarden/web-vault@sha256:f6ac819a2cd9e226f2cd2ec26196ede94a41e672e9672a11b5f307a19278b15e | ||||
| #     [docker.io/vaultwarden/web-vault:v2025.7.0] | ||||
| # | ||||
| FROM --platform=linux/amd64 docker.io/vaultwarden/web-vault@sha256:a0a377b810e66a4ebf1416f732d2be06f3262bf5a5238695af88d3ec6871cc0e AS vault | ||||
| FROM --platform=linux/amd64 docker.io/vaultwarden/web-vault@sha256:f6ac819a2cd9e226f2cd2ec26196ede94a41e672e9672a11b5f307a19278b15e AS vault | ||||
|  | ||||
| ########################## Cross Compile Docker Helper Scripts ########################## | ||||
| ## We use the linux/amd64 no matter which Build Platform, since these are all bash scripts | ||||
| @@ -36,7 +36,7 @@ FROM --platform=linux/amd64 docker.io/tonistiigi/xx@sha256:9c207bead753dda9430bd | ||||
|  | ||||
| ########################## BUILD IMAGE ########################## | ||||
| # hadolint ignore=DL3006 | ||||
| FROM --platform=$BUILDPLATFORM docker.io/library/rust:1.87.0-slim-bookworm AS build | ||||
| FROM --platform=$BUILDPLATFORM docker.io/library/rust:1.89.0-slim-bookworm AS build | ||||
| COPY --from=xx / / | ||||
| ARG TARGETARCH | ||||
| ARG TARGETVARIANT | ||||
|   | ||||
| @@ -10,7 +10,7 @@ proc-macro = true | ||||
|  | ||||
| [dependencies] | ||||
| quote = "1.0.40" | ||||
| syn = "2.0.101" | ||||
| syn = "2.0.104" | ||||
|  | ||||
| [lints] | ||||
| workspace = true | ||||
|   | ||||
							
								
								
									
										1
									
								
								migrations/mysql/2023-09-10-133000_add_sso/down.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								migrations/mysql/2023-09-10-133000_add_sso/down.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| DROP TABLE sso_nonce; | ||||
							
								
								
									
										4
									
								
								migrations/mysql/2023-09-10-133000_add_sso/up.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								migrations/mysql/2023-09-10-133000_add_sso/up.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,4 @@ | ||||
| CREATE TABLE sso_nonce ( | ||||
|   nonce               CHAR(36) NOT NULL PRIMARY KEY, | ||||
|   created_at          DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP | ||||
| ); | ||||
| @@ -0,0 +1 @@ | ||||
| ALTER TABLE users_organizations DROP COLUMN invited_by_email; | ||||
| @@ -0,0 +1 @@ | ||||
| ALTER TABLE users_organizations ADD COLUMN invited_by_email TEXT DEFAULT NULL; | ||||
| @@ -0,0 +1,6 @@ | ||||
| DROP TABLE IF EXISTS sso_nonce; | ||||
|  | ||||
| CREATE TABLE sso_nonce ( | ||||
|   nonce               CHAR(36) NOT NULL PRIMARY KEY, | ||||
|   created_at          DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP | ||||
| ); | ||||
| @@ -0,0 +1,8 @@ | ||||
| DROP TABLE IF EXISTS sso_nonce; | ||||
|  | ||||
| CREATE TABLE sso_nonce ( | ||||
| 	state               VARCHAR(512) NOT NULL PRIMARY KEY, | ||||
|   	nonce               TEXT NOT NULL, | ||||
|   	redirect_uri 		TEXT NOT NULL, | ||||
|   	created_at          TIMESTAMP NOT NULL DEFAULT now() | ||||
| ); | ||||
| @@ -0,0 +1,8 @@ | ||||
| DROP TABLE IF EXISTS sso_nonce; | ||||
|  | ||||
| CREATE TABLE sso_nonce ( | ||||
|     state               VARCHAR(512) NOT NULL PRIMARY KEY, | ||||
|     nonce               TEXT NOT NULL, | ||||
|     redirect_uri        TEXT NOT NULL, | ||||
|     created_at          TIMESTAMP NOT NULL DEFAULT now() | ||||
| ); | ||||
| @@ -0,0 +1,9 @@ | ||||
| DROP TABLE IF EXISTS sso_nonce; | ||||
|  | ||||
| CREATE TABLE sso_nonce ( | ||||
|     state               VARCHAR(512) NOT NULL PRIMARY KEY, | ||||
|   	nonce               TEXT NOT NULL, | ||||
|     verifier            TEXT, | ||||
|   	redirect_uri 		TEXT NOT NULL, | ||||
|   	created_at          TIMESTAMP NOT NULL DEFAULT now() | ||||
| ); | ||||
| @@ -0,0 +1 @@ | ||||
| DROP TABLE IF EXISTS sso_users; | ||||
							
								
								
									
										7
									
								
								migrations/mysql/2024-03-06-170000_add_sso_users/up.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								migrations/mysql/2024-03-06-170000_add_sso_users/up.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| CREATE TABLE sso_users ( | ||||
|   user_uuid           CHAR(36) NOT NULL PRIMARY KEY, | ||||
|   identifier          VARCHAR(768) NOT NULL UNIQUE, | ||||
|   created_at          TIMESTAMP NOT NULL DEFAULT now(), | ||||
|  | ||||
|   FOREIGN KEY(user_uuid) REFERENCES users(uuid) | ||||
| ); | ||||
| @@ -0,0 +1,2 @@ | ||||
| ALTER TABLE sso_users DROP FOREIGN KEY `sso_users_ibfk_1`; | ||||
| ALTER TABLE sso_users ADD FOREIGN KEY(user_uuid) REFERENCES users(uuid) ON UPDATE CASCADE ON DELETE CASCADE; | ||||
							
								
								
									
										1
									
								
								migrations/postgresql/2023-09-10-133000_add_sso/down.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								migrations/postgresql/2023-09-10-133000_add_sso/down.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| DROP TABLE sso_nonce; | ||||
							
								
								
									
										4
									
								
								migrations/postgresql/2023-09-10-133000_add_sso/up.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								migrations/postgresql/2023-09-10-133000_add_sso/up.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,4 @@ | ||||
| CREATE TABLE sso_nonce ( | ||||
|   nonce               CHAR(36) NOT NULL PRIMARY KEY, | ||||
|   created_at          TIMESTAMP NOT NULL DEFAULT now() | ||||
| ); | ||||
| @@ -0,0 +1 @@ | ||||
| ALTER TABLE users_organizations DROP COLUMN invited_by_email; | ||||
| @@ -0,0 +1 @@ | ||||
| ALTER TABLE users_organizations ADD COLUMN invited_by_email TEXT DEFAULT NULL; | ||||
| @@ -0,0 +1,6 @@ | ||||
| DROP TABLE sso_nonce; | ||||
|  | ||||
| CREATE TABLE sso_nonce ( | ||||
|   nonce               CHAR(36) NOT NULL PRIMARY KEY, | ||||
|   created_at          TIMESTAMP NOT NULL DEFAULT now() | ||||
| ); | ||||
| @@ -0,0 +1,8 @@ | ||||
| DROP TABLE sso_nonce; | ||||
|  | ||||
| CREATE TABLE sso_nonce ( | ||||
| 	state               TEXT NOT NULL PRIMARY KEY, | ||||
|   	nonce               TEXT NOT NULL, | ||||
|   	redirect_uri 		TEXT NOT NULL, | ||||
|   	created_at          TIMESTAMP NOT NULL DEFAULT now() | ||||
| ); | ||||
| @@ -0,0 +1,8 @@ | ||||
| DROP TABLE IF EXISTS sso_nonce; | ||||
|  | ||||
| CREATE TABLE sso_nonce ( | ||||
|     state               TEXT NOT NULL PRIMARY KEY, | ||||
|     nonce               TEXT NOT NULL, | ||||
|     redirect_uri        TEXT NOT NULL, | ||||
|     created_at          TIMESTAMP NOT NULL DEFAULT now() | ||||
| ); | ||||
| @@ -0,0 +1,9 @@ | ||||
| DROP TABLE IF EXISTS sso_nonce; | ||||
|  | ||||
| CREATE TABLE sso_nonce ( | ||||
|     state               TEXT NOT NULL PRIMARY KEY, | ||||
|     nonce               TEXT NOT NULL, | ||||
|     verifier            TEXT, | ||||
|     redirect_uri        TEXT NOT NULL, | ||||
|     created_at          TIMESTAMP NOT NULL DEFAULT now() | ||||
| ); | ||||
| @@ -0,0 +1 @@ | ||||
| DROP TABLE IF EXISTS sso_users; | ||||
| @@ -0,0 +1,7 @@ | ||||
| CREATE TABLE sso_users ( | ||||
|   user_uuid           CHAR(36) NOT NULL PRIMARY KEY, | ||||
|   identifier          TEXT NOT NULL UNIQUE, | ||||
|   created_at          TIMESTAMP NOT NULL DEFAULT now(), | ||||
|  | ||||
|   FOREIGN KEY(user_uuid) REFERENCES users(uuid) | ||||
| ); | ||||
| @@ -0,0 +1,3 @@ | ||||
| ALTER TABLE sso_users | ||||
|   DROP CONSTRAINT "sso_users_user_uuid_fkey", | ||||
|   ADD CONSTRAINT "sso_users_user_uuid_fkey" FOREIGN KEY(user_uuid) REFERENCES users(uuid) ON UPDATE CASCADE ON DELETE CASCADE; | ||||
							
								
								
									
										1
									
								
								migrations/sqlite/2023-09-10-133000_add_sso/down.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								migrations/sqlite/2023-09-10-133000_add_sso/down.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| DROP TABLE sso_nonce; | ||||
							
								
								
									
										4
									
								
								migrations/sqlite/2023-09-10-133000_add_sso/up.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								migrations/sqlite/2023-09-10-133000_add_sso/up.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,4 @@ | ||||
| CREATE TABLE sso_nonce ( | ||||
|   nonce               CHAR(36) NOT NULL PRIMARY KEY, | ||||
|   created_at          DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP | ||||
| ); | ||||
| @@ -0,0 +1 @@ | ||||
| ALTER TABLE users_organizations DROP COLUMN invited_by_email; | ||||
| @@ -0,0 +1 @@ | ||||
| ALTER TABLE users_organizations ADD COLUMN invited_by_email TEXT DEFAULT NULL; | ||||
| @@ -0,0 +1,6 @@ | ||||
| DROP TABLE sso_nonce; | ||||
|  | ||||
| CREATE TABLE sso_nonce ( | ||||
|   nonce               CHAR(36) NOT NULL PRIMARY KEY, | ||||
|   created_at          DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP | ||||
| ); | ||||
| @@ -0,0 +1,8 @@ | ||||
| DROP TABLE sso_nonce; | ||||
|  | ||||
| CREATE TABLE sso_nonce ( | ||||
|   state               TEXT NOT NULL PRIMARY KEY, | ||||
|   nonce               TEXT NOT NULL, | ||||
|   redirect_uri        TEXT NOT NULL, | ||||
|   created_at          DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP | ||||
| ); | ||||
| @@ -0,0 +1,8 @@ | ||||
| DROP TABLE IF EXISTS sso_nonce; | ||||
|  | ||||
| CREATE TABLE sso_nonce ( | ||||
|   state               TEXT NOT NULL PRIMARY KEY, | ||||
|   nonce               TEXT NOT NULL, | ||||
|   redirect_uri        TEXT NOT NULL, | ||||
|   created_at          DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP | ||||
| ); | ||||
| @@ -0,0 +1,9 @@ | ||||
| DROP TABLE IF EXISTS sso_nonce; | ||||
|  | ||||
| CREATE TABLE sso_nonce ( | ||||
|   state               TEXT NOT NULL PRIMARY KEY, | ||||
|   nonce               TEXT NOT NULL, | ||||
|   verifier            TEXT, | ||||
|   redirect_uri        TEXT NOT NULL, | ||||
|   created_at          DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP | ||||
| ); | ||||
| @@ -0,0 +1 @@ | ||||
| DROP TABLE IF EXISTS sso_users; | ||||
							
								
								
									
										7
									
								
								migrations/sqlite/2024-03-06-170000_add_sso_users/up.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								migrations/sqlite/2024-03-06-170000_add_sso_users/up.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| CREATE TABLE sso_users ( | ||||
|   user_uuid           CHAR(36) NOT NULL PRIMARY KEY, | ||||
|   identifier          TEXT NOT NULL UNIQUE, | ||||
|   created_at          TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||||
|  | ||||
|   FOREIGN KEY(user_uuid) REFERENCES users(uuid) | ||||
| ); | ||||
| @@ -0,0 +1,9 @@ | ||||
| DROP TABLE IF EXISTS sso_users; | ||||
|  | ||||
| CREATE TABLE sso_users ( | ||||
|   user_uuid           CHAR(36) NOT NULL PRIMARY KEY, | ||||
|   identifier          TEXT NOT NULL UNIQUE, | ||||
|   created_at          TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||||
|  | ||||
|   FOREIGN KEY(user_uuid) REFERENCES users(uuid) ON UPDATE CASCADE ON DELETE CASCADE | ||||
| ); | ||||
							
								
								
									
										64
									
								
								playwright/.env.template
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								playwright/.env.template
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,64 @@ | ||||
| ################################# | ||||
| ### Conf to run dev instances ### | ||||
| ################################# | ||||
| ENV=dev | ||||
| DC_ENV_FILE=.env | ||||
| COMPOSE_IGNORE_ORPHANS=True | ||||
| DOCKER_BUILDKIT=1 | ||||
|  | ||||
| ################ | ||||
| # Users Config # | ||||
| ################ | ||||
| TEST_USER=test | ||||
| TEST_USER_PASSWORD=${TEST_USER} | ||||
| TEST_USER_MAIL=${TEST_USER}@yopmail.com | ||||
|  | ||||
| TEST_USER2=test2 | ||||
| TEST_USER2_PASSWORD=${TEST_USER2} | ||||
| TEST_USER2_MAIL=${TEST_USER2}@yopmail.com | ||||
|  | ||||
| TEST_USER3=test3 | ||||
| TEST_USER3_PASSWORD=${TEST_USER3} | ||||
| TEST_USER3_MAIL=${TEST_USER3}@yopmail.com | ||||
|  | ||||
| ################### | ||||
| # Keycloak Config # | ||||
| ################### | ||||
| KEYCLOAK_ADMIN=admin | ||||
| KEYCLOAK_ADMIN_PASSWORD=${KEYCLOAK_ADMIN} | ||||
| KC_HTTP_HOST=127.0.0.1 | ||||
| KC_HTTP_PORT=8080 | ||||
|  | ||||
| # Script parameters (use Keycloak and Vaultwarden config too) | ||||
| TEST_REALM=test | ||||
| DUMMY_REALM=dummy | ||||
| DUMMY_AUTHORITY=http://${KC_HTTP_HOST}:${KC_HTTP_PORT}/realms/${DUMMY_REALM} | ||||
|  | ||||
| ###################### | ||||
| # Vaultwarden Config # | ||||
| ###################### | ||||
| ROCKET_ADDRESS=0.0.0.0 | ||||
| ROCKET_PORT=8000 | ||||
| DOMAIN=http://127.0.0.1:${ROCKET_PORT} | ||||
| LOG_LEVEL=info,oidcwarden::sso=debug | ||||
| I_REALLY_WANT_VOLATILE_STORAGE=true | ||||
|  | ||||
| SSO_ENABLED=true | ||||
| SSO_ONLY=false | ||||
| SSO_CLIENT_ID=warden | ||||
| SSO_CLIENT_SECRET=warden | ||||
| SSO_AUTHORITY=http://${KC_HTTP_HOST}:${KC_HTTP_PORT}/realms/${TEST_REALM} | ||||
|  | ||||
| SMTP_HOST=127.0.0.1 | ||||
| SMTP_PORT=1025 | ||||
| SMTP_SECURITY=off | ||||
| SMTP_TIMEOUT=5 | ||||
| SMTP_FROM=vaultwarden@test | ||||
| SMTP_FROM_NAME=Vaultwarden | ||||
|  | ||||
| ######################################################## | ||||
| # DUMMY values for docker-compose to stop bothering us # | ||||
| ######################################################## | ||||
| MARIADB_PORT=3305 | ||||
| MYSQL_PORT=3307 | ||||
| POSTGRES_PORT=5432 | ||||
							
								
								
									
										6
									
								
								playwright/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								playwright/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | ||||
| logs | ||||
| node_modules/ | ||||
| /test-results/ | ||||
| /playwright-report/ | ||||
| /playwright/.cache/ | ||||
| temp | ||||
							
								
								
									
										169
									
								
								playwright/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										169
									
								
								playwright/README.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,169 @@ | ||||
| # Integration tests | ||||
|  | ||||
| This allows running integration tests using [Playwright](https://playwright.dev/). | ||||
|  | ||||
| It uses its own `test.env` with different ports to not collide with a running dev instance. | ||||
|  | ||||
| ## Install | ||||
|  | ||||
| This relies on `docker` and the `compose` [plugin](https://docs.docker.com/compose/install/). | ||||
| Databases (`Mariadb`, `Mysql` and `Postgres`) and `Playwright` will run in containers. | ||||
|  | ||||
| ### Running Playwright outside docker | ||||
|  | ||||
| It is possible to run `Playwright` outside of the container, this removes the need to rebuild the image for each change. | ||||
| You will additionally need `nodejs` then run: | ||||
|  | ||||
| ```bash | ||||
| npm install | ||||
| npx playwright install-deps | ||||
| npx playwright install firefox | ||||
| ``` | ||||
|  | ||||
| ## Usage | ||||
|  | ||||
| To run all the tests: | ||||
|  | ||||
| ```bash | ||||
| DOCKER_BUILDKIT=1 docker compose --profile playwright --env-file test.env run Playwright | ||||
| ``` | ||||
|  | ||||
| To force a rebuild of the Playwright image: | ||||
| ```bash | ||||
| DOCKER_BUILDKIT=1 docker compose --env-file test.env build Playwright | ||||
| ``` | ||||
|  | ||||
| To access the UI to easily run test individually and debug if needed (this will not work in docker): | ||||
|  | ||||
| ```bash | ||||
| npx playwright test --ui | ||||
| ``` | ||||
|  | ||||
| ### DB | ||||
|  | ||||
| Projects are configured to allow to run tests only on specific database. | ||||
|  | ||||
| You can use: | ||||
|  | ||||
| ```bash | ||||
| DOCKER_BUILDKIT=1 docker compose --profile playwright --env-file test.env run Playwright test --project=mariadb | ||||
| DOCKER_BUILDKIT=1 docker compose --profile playwright --env-file test.env run Playwright test --project=mysql | ||||
| DOCKER_BUILDKIT=1 docker compose --profile playwright --env-file test.env run Playwright test --project=postgres | ||||
| DOCKER_BUILDKIT=1 docker compose --profile playwright --env-file test.env run Playwright test --project=sqlite | ||||
| ``` | ||||
|  | ||||
| ### SSO | ||||
|  | ||||
| To run the SSO tests: | ||||
|  | ||||
| ```bash | ||||
| DOCKER_BUILDKIT=1 docker compose --profile playwright --env-file test.env run Playwright test --project sso-sqlite | ||||
| ``` | ||||
|  | ||||
| ### Keep services running | ||||
|  | ||||
| If you want you can keep the DB and Keycloak runnning (states are not impacted by the tests): | ||||
|  | ||||
| ```bash | ||||
| PW_KEEP_SERVICE_RUNNNING=true npx playwright test | ||||
| ``` | ||||
|  | ||||
| ### Running specific tests | ||||
|  | ||||
| To run a whole file you can : | ||||
|  | ||||
| ```bash | ||||
| DOCKER_BUILDKIT=1 docker compose --profile playwright --env-file test.env run Playwright test --project=sqlite tests/login.spec.ts | ||||
| DOCKER_BUILDKIT=1 docker compose --profile playwright --env-file test.env run Playwright test --project=sqlite login | ||||
| ``` | ||||
|  | ||||
| To run only a specifc test (It might fail if it has dependency): | ||||
|  | ||||
| ```bash | ||||
| DOCKER_BUILDKIT=1 docker compose --profile playwright --env-file test.env run Playwright test --project=sqlite -g "Account creation" | ||||
| DOCKER_BUILDKIT=1 docker compose --profile playwright --env-file test.env run Playwright test --project=sqlite tests/login.spec.ts:16 | ||||
| ``` | ||||
|  | ||||
| ## Writing scenario | ||||
|  | ||||
| When creating new scenario use the recorder to more easily identify elements | ||||
| (in general try to rely on visible hint to identify elements and not hidden IDs). | ||||
| This does not start the server, you will need to start it manually. | ||||
|  | ||||
| ```bash | ||||
| npx playwright codegen "http://127.0.0.1:8000" | ||||
| ``` | ||||
|  | ||||
| ## Override web-vault | ||||
|  | ||||
| It is possible to change the `web-vault` used by referencing a different `bw_web_builds` commit. | ||||
|  | ||||
| ```bash | ||||
| export PW_WV_REPO_URL=https://github.com/Timshel/oidc_web_builds.git | ||||
| export PW_WV_COMMIT_HASH=8707dc76df3f0cceef2be5bfae37bb29bd17fae6 | ||||
| DOCKER_BUILDKIT=1 docker compose --profile playwright --env-file test.env build Playwright | ||||
| ``` | ||||
|  | ||||
| # OpenID Connect test setup | ||||
|  | ||||
| Additionally this `docker-compose` template allows to run locally Vaultwarden, | ||||
| [Keycloak](https://www.keycloak.org/) and [Maildev](https://github.com/timshel/maildev) to test OIDC. | ||||
|  | ||||
| ## Setup | ||||
|  | ||||
| This rely on `docker` and the `compose` [plugin](https://docs.docker.com/compose/install/). | ||||
| First create a copy of `.env.template` as `.env` (This is done to prevent committing your custom settings, Ex `SMTP_`). | ||||
|  | ||||
| ## Usage | ||||
|  | ||||
| Then start the stack (the `profile` is required to run `Vaultwarden`) : | ||||
|  | ||||
| ```bash | ||||
| > docker compose --profile vaultwarden --env-file .env up | ||||
| .... | ||||
| keycloakSetup_1  | Logging into http://127.0.0.1:8080 as user admin of realm master | ||||
| keycloakSetup_1  | Created new realm with id 'test' | ||||
| keycloakSetup_1  | 74af4933-e386-4e64-ba15-a7b61212c45e | ||||
| oidc_keycloakSetup_1 exited with code 0 | ||||
| ``` | ||||
|  | ||||
| Wait until `oidc_keycloakSetup_1 exited with code 0` which indicates the correct setup of the Keycloak realm, client and user | ||||
| (It is normal for this container to stop once the configuration is done). | ||||
|  | ||||
| Then you can access : | ||||
|  | ||||
| - `Vaultwarden` on http://0.0.0.0:8000 with the default user `test@yopmail.com/test`. | ||||
| - `Keycloak` on http://0.0.0.0:8080/admin/master/console/ with the default user `admin/admin` | ||||
| - `Maildev` on http://0.0.0.0:1080 | ||||
|  | ||||
| To proceed with an SSO login after you enter the email, on the screen prompting for `Master Password` the SSO button should be visible. | ||||
| To use your computer external ip (for example when testing with a phone) you will have to configure `KC_HTTP_HOST` and `DOMAIN`. | ||||
|  | ||||
| ## Running only Keycloak | ||||
|  | ||||
| You can run just `Keycloak` with `--profile keycloak`: | ||||
|  | ||||
| ```bash | ||||
| > docker compose --profile keycloak --env-file .env up | ||||
| ``` | ||||
| When running with a local Vaultwarden, you can use a front-end build from [dani-garcia/bw_web_builds](https://github.com/dani-garcia/bw_web_builds/releases). | ||||
|  | ||||
| ## Rebuilding the Vaultwarden | ||||
|  | ||||
| To force rebuilding the Vaultwarden image you can run | ||||
|  | ||||
| ```bash | ||||
| docker compose --profile vaultwarden --env-file .env build VaultwardenPrebuild Vaultwarden | ||||
| ``` | ||||
|  | ||||
| ## Configuration | ||||
|  | ||||
| All configuration for `keycloak` / `Vaultwarden` / `keycloak_setup.sh` can be found in [.env](.env.template). | ||||
| The content of the file will be loaded as environment variables in all containers. | ||||
|  | ||||
| - `keycloak` [configuration](https://www.keycloak.org/server/all-config) includes `KEYCLOAK_ADMIN` / `KEYCLOAK_ADMIN_PASSWORD` and any variable prefixed `KC_` ([more information](https://www.keycloak.org/server/configuration#_example_configuring_the_db_url_host_parameter)). | ||||
| - All `Vaultwarden` configuration can be set (EX: `SMTP_*`) | ||||
|  | ||||
| ## Cleanup | ||||
|  | ||||
| Use `docker compose --profile vaultwarden down`. | ||||
							
								
								
									
										40
									
								
								playwright/compose/keycloak/Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								playwright/compose/keycloak/Dockerfile
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | ||||
| FROM docker.io/library/debian:bookworm-slim as build | ||||
|  | ||||
| ENV DEBIAN_FRONTEND=noninteractive | ||||
| ARG KEYCLOAK_VERSION | ||||
|  | ||||
| SHELL ["/bin/bash", "-o", "pipefail", "-c"] | ||||
|  | ||||
| RUN apt-get update \ | ||||
|     && apt-get install -y ca-certificates curl wget \ | ||||
|     && rm -rf /var/lib/apt/lists/* | ||||
|  | ||||
| WORKDIR / | ||||
|  | ||||
| RUN wget -c https://github.com/keycloak/keycloak/releases/download/${KEYCLOAK_VERSION}/keycloak-${KEYCLOAK_VERSION}.tar.gz -O - | tar -xz | ||||
|  | ||||
| FROM docker.io/library/debian:bookworm-slim | ||||
|  | ||||
| ENV DEBIAN_FRONTEND=noninteractive | ||||
| ARG KEYCLOAK_VERSION | ||||
|  | ||||
| SHELL ["/bin/bash", "-o", "pipefail", "-c"] | ||||
|  | ||||
| RUN apt-get update \ | ||||
|     && apt-get install -y ca-certificates curl wget \ | ||||
|     && rm -rf /var/lib/apt/lists/* | ||||
|  | ||||
| ARG JAVA_URL | ||||
| ARG JAVA_VERSION | ||||
|  | ||||
| ENV JAVA_VERSION=${JAVA_VERSION} | ||||
|  | ||||
| RUN mkdir -p /opt/openjdk && cd /opt/openjdk \ | ||||
|     && wget -c "${JAVA_URL}"  -O - | tar -xz | ||||
|  | ||||
| WORKDIR / | ||||
|  | ||||
| COPY setup.sh /setup.sh | ||||
| COPY --from=build /keycloak-${KEYCLOAK_VERSION}/bin /opt/keycloak/bin | ||||
|  | ||||
| CMD "/setup.sh" | ||||
							
								
								
									
										36
									
								
								playwright/compose/keycloak/setup.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										36
									
								
								playwright/compose/keycloak/setup.sh
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,36 @@ | ||||
| #!/bin/bash | ||||
|  | ||||
| export PATH=/opt/keycloak/bin:/opt/openjdk/jdk-${JAVA_VERSION}/bin:$PATH | ||||
| export JAVA_HOME=/opt/openjdk/jdk-${JAVA_VERSION} | ||||
|  | ||||
| STATUS_CODE=0 | ||||
| while [[ "$STATUS_CODE" != "404" ]] ; do | ||||
|     echo "Will retry in 2 seconds" | ||||
|     sleep 2 | ||||
|  | ||||
|     STATUS_CODE=$(curl -s -o /dev/null -w "%{http_code}"  "$DUMMY_AUTHORITY") | ||||
|  | ||||
|     if [[ "$STATUS_CODE" = "200" ]]; then | ||||
|         echo "Setup should already be done. Will not run." | ||||
|         exit 0 | ||||
|     fi | ||||
| done | ||||
|  | ||||
| set -e | ||||
|  | ||||
| kcadm.sh config credentials --server "http://${KC_HTTP_HOST}:${KC_HTTP_PORT}" --realm master --user "$KEYCLOAK_ADMIN" --password "$KEYCLOAK_ADMIN_PASSWORD" --client admin-cli | ||||
|  | ||||
| kcadm.sh create realms -s realm="$TEST_REALM" -s enabled=true -s "accessTokenLifespan=600" | ||||
| kcadm.sh create clients -r test -s "clientId=$SSO_CLIENT_ID" -s "secret=$SSO_CLIENT_SECRET" -s "redirectUris=[\"$DOMAIN/*\"]" -i | ||||
|  | ||||
| TEST_USER_ID=$(kcadm.sh create users -r "$TEST_REALM" -s "username=$TEST_USER" -s "firstName=$TEST_USER" -s "lastName=$TEST_USER" -s "email=$TEST_USER_MAIL"  -s emailVerified=true -s enabled=true -i) | ||||
| kcadm.sh update users/$TEST_USER_ID/reset-password -r "$TEST_REALM" -s type=password -s "value=$TEST_USER_PASSWORD" -n | ||||
|  | ||||
| TEST_USER2_ID=$(kcadm.sh create users -r "$TEST_REALM" -s "username=$TEST_USER2" -s "firstName=$TEST_USER2" -s "lastName=$TEST_USER2" -s "email=$TEST_USER2_MAIL"  -s emailVerified=true -s enabled=true -i) | ||||
| kcadm.sh update users/$TEST_USER2_ID/reset-password -r "$TEST_REALM" -s type=password -s "value=$TEST_USER2_PASSWORD" -n | ||||
|  | ||||
| TEST_USER3_ID=$(kcadm.sh create users -r "$TEST_REALM" -s "username=$TEST_USER3" -s "firstName=$TEST_USER3" -s "lastName=$TEST_USER3" -s "email=$TEST_USER3_MAIL"  -s emailVerified=true -s enabled=true -i) | ||||
| kcadm.sh update users/$TEST_USER3_ID/reset-password -r "$TEST_REALM" -s type=password -s "value=$TEST_USER3_PASSWORD" -n | ||||
|  | ||||
| # Dummy realm to mark end of setup | ||||
| kcadm.sh create realms -s realm="$DUMMY_REALM" -s enabled=true -s "accessTokenLifespan=600" | ||||
							
								
								
									
										40
									
								
								playwright/compose/playwright/Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								playwright/compose/playwright/Dockerfile
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | ||||
| FROM docker.io/library/debian:bookworm-slim | ||||
|  | ||||
| SHELL ["/bin/bash", "-o", "pipefail", "-c"] | ||||
|  | ||||
| ENV DEBIAN_FRONTEND=noninteractive | ||||
|  | ||||
| RUN apt-get update \ | ||||
|     && apt-get install -y ca-certificates curl \ | ||||
|     && curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc \ | ||||
|     && chmod a+r /etc/apt/keyrings/docker.asc \ | ||||
|     && echo "deb [signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian bookworm stable" | tee /etc/apt/sources.list.d/docker.list \ | ||||
|     && apt-get update \ | ||||
|     && apt-get install -y --no-install-recommends \ | ||||
|         containerd.io \ | ||||
|         docker-buildx-plugin \ | ||||
|         docker-ce \ | ||||
|         docker-ce-cli \ | ||||
|         docker-compose-plugin \ | ||||
|         git \ | ||||
|         libmariadb-dev-compat \ | ||||
|         libpq5 \ | ||||
|         nodejs \ | ||||
|         npm \ | ||||
|         openssl \ | ||||
|     && rm -rf /var/lib/apt/lists/* | ||||
|  | ||||
| RUN mkdir /playwright | ||||
| WORKDIR /playwright | ||||
|  | ||||
| COPY package.json . | ||||
| RUN npm install && npx playwright install-deps && npx playwright install firefox | ||||
|  | ||||
| COPY docker-compose.yml test.env ./ | ||||
| COPY compose ./compose | ||||
|  | ||||
| COPY *.ts test.env ./ | ||||
| COPY tests ./tests | ||||
|  | ||||
| ENTRYPOINT ["/usr/bin/npx", "playwright"] | ||||
| CMD ["test"] | ||||
							
								
								
									
										40
									
								
								playwright/compose/warden/Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								playwright/compose/warden/Dockerfile
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | ||||
| FROM playwright_oidc_vaultwarden_prebuilt AS prebuilt | ||||
|  | ||||
| FROM node:22-bookworm AS build | ||||
|  | ||||
| ARG REPO_URL | ||||
| ARG COMMIT_HASH | ||||
|  | ||||
| ENV REPO_URL=$REPO_URL | ||||
| ENV COMMIT_HASH=$COMMIT_HASH | ||||
|  | ||||
| COPY --from=prebuilt /web-vault /web-vault | ||||
|  | ||||
| COPY build.sh /build.sh | ||||
| RUN /build.sh | ||||
|  | ||||
| ######################## RUNTIME IMAGE  ######################## | ||||
| FROM docker.io/library/debian:bookworm-slim | ||||
|  | ||||
| ENV DEBIAN_FRONTEND=noninteractive | ||||
|  | ||||
| # Create data folder and Install needed libraries | ||||
| RUN mkdir /data && \ | ||||
|     apt-get update && apt-get install -y \ | ||||
|         --no-install-recommends \ | ||||
|         ca-certificates \ | ||||
|         curl \ | ||||
|         libmariadb-dev-compat \ | ||||
|         libpq5 \ | ||||
|         openssl && \ | ||||
|     rm -rf /var/lib/apt/lists/* | ||||
|  | ||||
| # Copies the files from the context (Rocket.toml file and web-vault) | ||||
| # and the binary from the "build" stage to the current stage | ||||
| WORKDIR / | ||||
|  | ||||
| COPY --from=prebuilt /start.sh . | ||||
| COPY --from=prebuilt /vaultwarden . | ||||
| COPY --from=build /web-vault ./web-vault | ||||
|  | ||||
| ENTRYPOINT ["/start.sh"] | ||||
							
								
								
									
										24
									
								
								playwright/compose/warden/build.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										24
									
								
								playwright/compose/warden/build.sh
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,24 @@ | ||||
| #!/bin/bash | ||||
|  | ||||
| echo $REPO_URL | ||||
| echo $COMMIT_HASH | ||||
|  | ||||
| if [[ ! -z "$REPO_URL" ]] && [[ ! -z "$COMMIT_HASH" ]] ; then | ||||
|     rm -rf /web-vault | ||||
|  | ||||
|     mkdir bw_web_builds; | ||||
|     cd bw_web_builds; | ||||
|  | ||||
|     git -c init.defaultBranch=main init | ||||
|     git remote add origin "$REPO_URL" | ||||
|     git fetch --depth 1 origin "$COMMIT_HASH" | ||||
|     git -c advice.detachedHead=false checkout FETCH_HEAD | ||||
|  | ||||
|     export VAULT_VERSION=$(cat Dockerfile | grep "ARG VAULT_VERSION" | cut -d "=" -f2) | ||||
|     ./scripts/checkout_web_vault.sh | ||||
|     ./scripts/patch_web_vault.sh | ||||
|     ./scripts/build_web_vault.sh | ||||
|     printf '{"version":"%s"}' "$COMMIT_HASH" > ./web-vault/apps/web/build/vw-version.json | ||||
|  | ||||
|     mv ./web-vault/apps/web/build /web-vault | ||||
| fi | ||||
							
								
								
									
										124
									
								
								playwright/docker-compose.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										124
									
								
								playwright/docker-compose.yml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,124 @@ | ||||
| services: | ||||
|   VaultwardenPrebuild: | ||||
|     profiles: ["playwright", "vaultwarden"] | ||||
|     container_name: playwright_oidc_vaultwarden_prebuilt | ||||
|     image: playwright_oidc_vaultwarden_prebuilt | ||||
|     build: | ||||
|       context: .. | ||||
|       dockerfile: Dockerfile | ||||
|     entrypoint: /bin/bash | ||||
|     restart: "no" | ||||
|  | ||||
|   Vaultwarden: | ||||
|     profiles: ["playwright", "vaultwarden"] | ||||
|     container_name: playwright_oidc_vaultwarden-${ENV:-dev} | ||||
|     image: playwright_oidc_vaultwarden-${ENV:-dev} | ||||
|     network_mode: "host" | ||||
|     build: | ||||
|       context: compose/warden | ||||
|       dockerfile: Dockerfile | ||||
|       args: | ||||
|         REPO_URL: ${PW_WV_REPO_URL:-} | ||||
|         COMMIT_HASH: ${PW_WV_COMMIT_HASH:-} | ||||
|     env_file: ${DC_ENV_FILE:-.env} | ||||
|     environment: | ||||
|       - DATABASE_URL | ||||
|       - I_REALLY_WANT_VOLATILE_STORAGE | ||||
|       - LOG_LEVEL | ||||
|       - LOGIN_RATELIMIT_MAX_BURST | ||||
|       - SMTP_HOST | ||||
|       - SMTP_FROM | ||||
|       - SMTP_DEBUG | ||||
|       - SSO_DEBUG_TOKENS | ||||
|       - SSO_FRONTEND | ||||
|       - SSO_ENABLED | ||||
|       - SSO_ONLY | ||||
|     restart: "no" | ||||
|     depends_on: | ||||
|       - VaultwardenPrebuild | ||||
|  | ||||
|   Playwright: | ||||
|     profiles: ["playwright"] | ||||
|     container_name: playwright_oidc_playwright | ||||
|     image: playwright_oidc_playwright | ||||
|     network_mode: "host" | ||||
|     build: | ||||
|       context: . | ||||
|       dockerfile: compose/playwright/Dockerfile | ||||
|     environment: | ||||
|       - PW_WV_REPO_URL | ||||
|       - PW_WV_COMMIT_HASH | ||||
|     restart: "no" | ||||
|     volumes: | ||||
|       - /var/run/docker.sock:/var/run/docker.sock | ||||
|       - ..:/project | ||||
|  | ||||
|   Mariadb: | ||||
|     profiles: ["playwright"] | ||||
|     container_name: playwright_mariadb | ||||
|     image: mariadb:11.2.4 | ||||
|     env_file: test.env | ||||
|     healthcheck: | ||||
|       test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"] | ||||
|       start_period: 10s | ||||
|       interval: 10s | ||||
|     ports: | ||||
|       - ${MARIADB_PORT}:3306 | ||||
|  | ||||
|   Mysql: | ||||
|     profiles: ["playwright"] | ||||
|     container_name: playwright_mysql | ||||
|     image: mysql:8.4.1 | ||||
|     env_file: test.env | ||||
|     healthcheck: | ||||
|       test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"] | ||||
|       start_period: 10s | ||||
|       interval: 10s | ||||
|     ports: | ||||
|       - ${MYSQL_PORT}:3306 | ||||
|  | ||||
|   Postgres: | ||||
|     profiles: ["playwright"] | ||||
|     container_name: playwright_postgres | ||||
|     image: postgres:16.3 | ||||
|     env_file: test.env | ||||
|     healthcheck: | ||||
|       test: ["CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}"] | ||||
|       start_period: 20s | ||||
|       interval: 30s | ||||
|     ports: | ||||
|       - ${POSTGRES_PORT}:5432 | ||||
|  | ||||
|   Maildev: | ||||
|     profiles: ["vaultwarden", "maildev"] | ||||
|     container_name: maildev | ||||
|     image: timshel/maildev:3.0.4 | ||||
|     ports: | ||||
|       - ${SMTP_PORT}:1025 | ||||
|       - 1080:1080 | ||||
|  | ||||
|   Keycloak: | ||||
|     profiles: ["keycloak", "vaultwarden"] | ||||
|     container_name: keycloak-${ENV:-dev} | ||||
|     image: quay.io/keycloak/keycloak:25.0.4 | ||||
|     network_mode: "host" | ||||
|     command: | ||||
|       - start-dev | ||||
|     env_file: ${DC_ENV_FILE:-.env} | ||||
|  | ||||
|   KeycloakSetup: | ||||
|     profiles: ["keycloak", "vaultwarden"] | ||||
|     container_name: keycloakSetup-${ENV:-dev} | ||||
|     image: keycloak_setup-${ENV:-dev} | ||||
|     build: | ||||
|       context: compose/keycloak | ||||
|       dockerfile: Dockerfile | ||||
|       args: | ||||
|         KEYCLOAK_VERSION: 25.0.4 | ||||
|         JAVA_URL: https://download.java.net/java/GA/jdk21.0.2/f2283984656d49d69e91c558476027ac/13/GPL/openjdk-21.0.2_linux-x64_bin.tar.gz | ||||
|         JAVA_VERSION: 21.0.2 | ||||
|     network_mode: "host" | ||||
|     depends_on: | ||||
|       - Keycloak | ||||
|     restart: "no" | ||||
|     env_file: ${DC_ENV_FILE:-.env} | ||||
							
								
								
									
										22
									
								
								playwright/global-setup.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								playwright/global-setup.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | ||||
| import { firefox, type FullConfig } from '@playwright/test'; | ||||
| import { execSync } from 'node:child_process'; | ||||
| import fs from 'fs'; | ||||
|  | ||||
| const utils = require('./global-utils'); | ||||
|  | ||||
| utils.loadEnv(); | ||||
|  | ||||
| async function globalSetup(config: FullConfig) { | ||||
|     // Are we running in docker and the project is mounted ? | ||||
|     const path = (fs.existsSync("/project/playwright/playwright.config.ts") ? "/project/playwright" : "."); | ||||
|     execSync(`docker compose --project-directory ${path} --profile playwright --env-file test.env build VaultwardenPrebuild`, { | ||||
|         env: { ...process.env }, | ||||
|         stdio: "inherit" | ||||
|     }); | ||||
|     execSync(`docker compose --project-directory ${path} --profile playwright --env-file test.env build Vaultwarden`, { | ||||
|         env: { ...process.env }, | ||||
|         stdio: "inherit" | ||||
|     }); | ||||
| } | ||||
|  | ||||
| export default globalSetup; | ||||
							
								
								
									
										246
									
								
								playwright/global-utils.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										246
									
								
								playwright/global-utils.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,246 @@ | ||||
| import { expect, type Browser, type TestInfo } from '@playwright/test'; | ||||
| import { EventEmitter } from "events"; | ||||
| import { type Mail, MailServer } from 'maildev'; | ||||
| import { execSync } from 'node:child_process'; | ||||
|  | ||||
| import dotenv from 'dotenv'; | ||||
| import dotenvExpand from 'dotenv-expand'; | ||||
|  | ||||
| const fs = require("fs"); | ||||
| const { spawn } = require('node:child_process'); | ||||
|  | ||||
| export function loadEnv(){ | ||||
|     var myEnv = dotenv.config({ path: 'test.env' }); | ||||
|     dotenvExpand.expand(myEnv); | ||||
|  | ||||
|     return { | ||||
|         user1: { | ||||
|             email: process.env.TEST_USER_MAIL, | ||||
|             name: process.env.TEST_USER, | ||||
|             password: process.env.TEST_USER_PASSWORD, | ||||
|         }, | ||||
|         user2: { | ||||
|             email: process.env.TEST_USER2_MAIL, | ||||
|             name: process.env.TEST_USER2, | ||||
|             password: process.env.TEST_USER2_PASSWORD, | ||||
|         }, | ||||
|         user3: { | ||||
|             email: process.env.TEST_USER3_MAIL, | ||||
|             name: process.env.TEST_USER3, | ||||
|             password: process.env.TEST_USER3_PASSWORD, | ||||
|         }, | ||||
|     } | ||||
| } | ||||
|  | ||||
| export async function waitFor(url: String, browser: Browser) { | ||||
|     var ready = false; | ||||
|     var context; | ||||
|  | ||||
|     do { | ||||
|         try { | ||||
|             context = await browser.newContext(); | ||||
|             const page = await context.newPage(); | ||||
|             await page.waitForTimeout(500); | ||||
|             const result = await page.goto(url); | ||||
|             ready = result.status() === 200; | ||||
|         } catch(e) { | ||||
|             if( !e.message.includes("CONNECTION_REFUSED") ){ | ||||
|                 throw e; | ||||
|             } | ||||
|         } finally { | ||||
|             await context.close(); | ||||
|         } | ||||
|     } while(!ready); | ||||
| } | ||||
|  | ||||
| export function startComposeService(serviceName: String){ | ||||
|     console.log(`Starting ${serviceName}`); | ||||
|     execSync(`docker compose --profile playwright --env-file test.env  up -d ${serviceName}`); | ||||
| } | ||||
|  | ||||
| export function stopComposeService(serviceName: String){ | ||||
|     console.log(`Stopping ${serviceName}`); | ||||
|     execSync(`docker compose --profile playwright --env-file test.env  stop ${serviceName}`); | ||||
| } | ||||
|  | ||||
| function wipeSqlite(){ | ||||
|     console.log(`Delete Vaultwarden container to wipe sqlite`); | ||||
|     execSync(`docker compose --env-file test.env stop Vaultwarden`); | ||||
|     execSync(`docker compose --env-file test.env rm -f Vaultwarden`); | ||||
| } | ||||
|  | ||||
| async function wipeMariaDB(){ | ||||
|     var mysql = require('mysql2/promise'); | ||||
|     var ready = false; | ||||
|     var connection; | ||||
|  | ||||
|     do { | ||||
|         try { | ||||
|             connection = await mysql.createConnection({ | ||||
|                 user: process.env.MARIADB_USER, | ||||
|                 host: "127.0.0.1", | ||||
|                 database: process.env.MARIADB_DATABASE, | ||||
|                 password: process.env.MARIADB_PASSWORD, | ||||
|                 port: process.env.MARIADB_PORT, | ||||
|             }); | ||||
|  | ||||
|             await connection.execute(`DROP DATABASE ${process.env.MARIADB_DATABASE}`); | ||||
|             await connection.execute(`CREATE DATABASE ${process.env.MARIADB_DATABASE}`); | ||||
|             console.log('Successfully wiped mariadb'); | ||||
|             ready = true; | ||||
|         } catch (err) { | ||||
|             console.log(`Error when wiping mariadb: ${err}`); | ||||
|         } finally { | ||||
|             if( connection ){ | ||||
|                 connection.end(); | ||||
|             } | ||||
|         } | ||||
|         await new Promise(r => setTimeout(r, 1000)); | ||||
|     } while(!ready); | ||||
| } | ||||
|  | ||||
| async function wipeMysqlDB(){ | ||||
|     var mysql = require('mysql2/promise'); | ||||
|     var ready = false; | ||||
|     var connection; | ||||
|  | ||||
|     do{ | ||||
|         try { | ||||
|             connection = await mysql.createConnection({ | ||||
|                 user: process.env.MYSQL_USER, | ||||
|                 host: "127.0.0.1", | ||||
|                 database: process.env.MYSQL_DATABASE, | ||||
|                 password: process.env.MYSQL_PASSWORD, | ||||
|                 port: process.env.MYSQL_PORT, | ||||
|             }); | ||||
|  | ||||
|             await connection.execute(`DROP DATABASE ${process.env.MYSQL_DATABASE}`); | ||||
|             await connection.execute(`CREATE DATABASE ${process.env.MYSQL_DATABASE}`); | ||||
|             console.log('Successfully wiped mysql'); | ||||
|             ready = true; | ||||
|         } catch (err) { | ||||
|             console.log(`Error when wiping mysql: ${err}`); | ||||
|         } finally { | ||||
|             if( connection ){ | ||||
|                 connection.end(); | ||||
|             } | ||||
|         } | ||||
|         await new Promise(r => setTimeout(r, 1000)); | ||||
|     } while(!ready); | ||||
| } | ||||
|  | ||||
| async function wipePostgres(){ | ||||
|     const { Client } = require('pg'); | ||||
|  | ||||
|     const client = new Client({ | ||||
|         user: process.env.POSTGRES_USER, | ||||
|         host: "127.0.0.1", | ||||
|         database: "postgres", | ||||
|         password: process.env.POSTGRES_PASSWORD, | ||||
|         port: process.env.POSTGRES_PORT, | ||||
|     }); | ||||
|  | ||||
|     try { | ||||
|         await client.connect(); | ||||
|         await client.query(`DROP DATABASE ${process.env.POSTGRES_DB}`); | ||||
|         await client.query(`CREATE DATABASE ${process.env.POSTGRES_DB}`); | ||||
|         console.log('Successfully wiped postgres'); | ||||
|     } catch (err) { | ||||
|         console.log(`Error when wiping postgres: ${err}`); | ||||
|     } finally { | ||||
|         client.end(); | ||||
|     } | ||||
| } | ||||
|  | ||||
| function dbConfig(testInfo: TestInfo){ | ||||
|     switch(testInfo.project.name) { | ||||
|         case "postgres": | ||||
|         case "sso-postgres": | ||||
|             return { DATABASE_URL: `postgresql://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@127.0.0.1:${process.env.POSTGRES_PORT}/${process.env.POSTGRES_DB}` }; | ||||
|         case "mariadb": | ||||
|         case "sso-mariadb": | ||||
|             return { DATABASE_URL: `mysql://${process.env.MARIADB_USER}:${process.env.MARIADB_PASSWORD}@127.0.0.1:${process.env.MARIADB_PORT}/${process.env.MARIADB_DATABASE}` }; | ||||
|         case "mysql": | ||||
|         case "sso-mysql": | ||||
|             return { DATABASE_URL: `mysql://${process.env.MYSQL_USER}:${process.env.MYSQL_PASSWORD}@127.0.0.1:${process.env.MYSQL_PORT}/${process.env.MYSQL_DATABASE}`}; | ||||
|         case "sqlite": | ||||
|         case "sso-sqlite": | ||||
|             return { I_REALLY_WANT_VOLATILE_STORAGE: true }; | ||||
|         default: | ||||
|             throw new Error(`Unknow database name: ${testInfo.project.name}`); | ||||
|     } | ||||
| } | ||||
|  | ||||
| /** | ||||
|  *  All parameters passed in `env` need to be added to the docker-compose.yml | ||||
|  **/ | ||||
| export async function startVault(browser: Browser, testInfo: TestInfo, env = {}, resetDB: Boolean = true) { | ||||
|     if( resetDB ){ | ||||
|         switch(testInfo.project.name) { | ||||
|             case "postgres": | ||||
|             case "sso-postgres": | ||||
|                 await wipePostgres(); | ||||
|                 break; | ||||
|             case "mariadb": | ||||
|             case "sso-mariadb": | ||||
|                 await wipeMariaDB(); | ||||
|                 break; | ||||
|             case "mysql": | ||||
|             case "sso-mysql": | ||||
|                 await wipeMysqlDB(); | ||||
|                 break; | ||||
|             case "sqlite": | ||||
|             case "sso-sqlite": | ||||
|                 wipeSqlite(); | ||||
|                 break; | ||||
|             default: | ||||
|                 throw new Error(`Unknow database name: ${testInfo.project.name}`); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     console.log(`Starting Vaultwarden`); | ||||
|     execSync(`docker compose --profile playwright --env-file test.env up -d Vaultwarden`, { | ||||
|         env: { ...env, ...dbConfig(testInfo) }, | ||||
|     }); | ||||
|     await waitFor("/", browser); | ||||
|     console.log(`Vaultwarden running on: ${process.env.DOMAIN}`); | ||||
| } | ||||
|  | ||||
| export async function stopVault(force: boolean = false) { | ||||
|     if( force === false && process.env.PW_KEEP_SERVICE_RUNNNING === "true" ) { | ||||
|         console.log(`Keep vaultwarden running on: ${process.env.DOMAIN}`); | ||||
|     } else { | ||||
|         console.log(`Vaultwarden stopping`); | ||||
|         execSync(`docker compose --profile playwright --env-file test.env stop Vaultwarden`); | ||||
|     } | ||||
| } | ||||
|  | ||||
| export async function restartVault(page: Page, testInfo: TestInfo, env, resetDB: Boolean = true) { | ||||
|     stopVault(true); | ||||
|     return startVault(page.context().browser(), testInfo, env, resetDB); | ||||
| } | ||||
|  | ||||
| export async function checkNotification(page: Page, hasText: string) { | ||||
|     await expect(page.locator('bit-toast').filter({ hasText })).toBeVisible(); | ||||
|     await page.locator('bit-toast').filter({ hasText }).getByRole('button').click(); | ||||
|     await expect(page.locator('bit-toast').filter({ hasText })).toHaveCount(0); | ||||
| } | ||||
|  | ||||
| export async function cleanLanding(page: Page) { | ||||
|     await page.goto('/', { waitUntil: 'domcontentloaded' }); | ||||
|     await expect(page.getByRole('button').nth(0)).toBeVisible(); | ||||
|  | ||||
|     const logged = await page.getByRole('button', { name: 'Log out' }).count(); | ||||
|     if( logged > 0 ){ | ||||
|         await page.getByRole('button', { name: 'Log out' }).click(); | ||||
|         await page.getByRole('button', { name: 'Log out' }).click(); | ||||
|     } | ||||
| } | ||||
|  | ||||
| export async function logout(test: Test, page: Page, user: { name: string }) { | ||||
|     await test.step('logout', async () => { | ||||
|         await page.getByRole('button', { name: user.name, exact: true }).click(); | ||||
|         await page.getByRole('menuitem', { name: 'Log out' }).click(); | ||||
|         await expect(page.getByRole('heading', { name: 'Log in' })).toBeVisible(); | ||||
|     }); | ||||
| } | ||||
							
								
								
									
										2547
									
								
								playwright/package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										2547
									
								
								playwright/package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										21
									
								
								playwright/package.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								playwright/package.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,21 @@ | ||||
| { | ||||
|     "name": "scenarios", | ||||
|     "version": "1.0.0", | ||||
|     "description": "", | ||||
|     "main": "index.js", | ||||
|     "scripts": {}, | ||||
|     "keywords": [], | ||||
|     "author": "", | ||||
|     "license": "ISC", | ||||
|     "devDependencies": { | ||||
|         "@playwright/test": "^1.53.0", | ||||
|         "dotenv": "^16.5.0", | ||||
|         "dotenv-expand": "^12.0.2", | ||||
|         "maildev": "npm:@timshel_npm/maildev@^3.1.2" | ||||
|     }, | ||||
|     "dependencies": { | ||||
|         "mysql2": "^3.14.1", | ||||
|         "otpauth": "^9.4.0", | ||||
|         "pg": "^8.16.0" | ||||
|     } | ||||
| } | ||||
							
								
								
									
										143
									
								
								playwright/playwright.config.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										143
									
								
								playwright/playwright.config.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,143 @@ | ||||
| import { defineConfig, devices } from '@playwright/test'; | ||||
| import { exec } from 'node:child_process'; | ||||
|  | ||||
| const utils = require('./global-utils'); | ||||
|  | ||||
| utils.loadEnv(); | ||||
|  | ||||
| /** | ||||
|  * See https://playwright.dev/docs/test-configuration. | ||||
|  */ | ||||
| export default defineConfig({ | ||||
|     testDir: './.', | ||||
|     /* Run tests in files in parallel */ | ||||
|     fullyParallel: false, | ||||
|  | ||||
|     /* Fail the build on CI if you accidentally left test.only in the source code. */ | ||||
|     forbidOnly: !!process.env.CI, | ||||
|  | ||||
|     retries: 0, | ||||
|     workers: 1, | ||||
|  | ||||
|     /* Reporter to use. See https://playwright.dev/docs/test-reporters */ | ||||
|     reporter: 'html', | ||||
|  | ||||
|     /* Long global timeout for complex tests | ||||
|      * But short action/nav/expect timeouts to fail on specific step (raise locally if not enough). | ||||
|      */ | ||||
|     timeout: 120 * 1000, | ||||
|     actionTimeout: 10 * 1000, | ||||
|     navigationTimeout: 10 * 1000, | ||||
|     expect: { timeout: 10 * 1000 }, | ||||
|  | ||||
|     /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ | ||||
|     use: { | ||||
|         /* Base URL to use in actions like `await page.goto('/')`. */ | ||||
|         baseURL: process.env.DOMAIN, | ||||
|         browserName: 'firefox', | ||||
|         locale: 'en-GB', | ||||
|         timezoneId: 'Europe/London', | ||||
|  | ||||
|         /* Always collect trace (other values add random test failures) See https://playwright.dev/docs/trace-viewer */ | ||||
|         trace: 'on', | ||||
|         viewport: { | ||||
|             width: 1080, | ||||
|             height: 720, | ||||
|         }, | ||||
|         video: "on", | ||||
|     }, | ||||
|  | ||||
|     /* Configure projects for major browsers */ | ||||
|     projects: [ | ||||
|         { | ||||
|             name: 'mariadb-setup', | ||||
|             testMatch: 'tests/setups/db-setup.ts', | ||||
|             use: { serviceName: "Mariadb" }, | ||||
|             teardown: 'mariadb-teardown', | ||||
|         }, | ||||
|         { | ||||
|             name: 'mysql-setup', | ||||
|             testMatch: 'tests/setups/db-setup.ts', | ||||
|             use: { serviceName: "Mysql" }, | ||||
|             teardown: 'mysql-teardown', | ||||
|         }, | ||||
|         { | ||||
|             name: 'postgres-setup', | ||||
|             testMatch: 'tests/setups/db-setup.ts', | ||||
|             use: { serviceName: "Postgres" }, | ||||
|             teardown: 'postgres-teardown', | ||||
|         }, | ||||
|         { | ||||
|             name: 'sso-setup', | ||||
|             testMatch: 'tests/setups/sso-setup.ts', | ||||
|             teardown: 'sso-teardown', | ||||
|         }, | ||||
|  | ||||
|         { | ||||
|             name: 'mariadb', | ||||
|             testMatch: 'tests/*.spec.ts', | ||||
|             testIgnore: 'tests/sso_*.spec.ts', | ||||
|             dependencies: ['mariadb-setup'], | ||||
|         }, | ||||
|         { | ||||
|             name: 'mysql', | ||||
|             testMatch: 'tests/*.spec.ts', | ||||
|             testIgnore: 'tests/sso_*.spec.ts', | ||||
|             dependencies: ['mysql-setup'], | ||||
|         }, | ||||
|         { | ||||
|             name: 'postgres', | ||||
|             testMatch: 'tests/*.spec.ts', | ||||
|             testIgnore: 'tests/sso_*.spec.ts', | ||||
|             dependencies: ['postgres-setup'], | ||||
|         }, | ||||
|         { | ||||
|             name: 'sqlite', | ||||
|             testMatch: 'tests/*.spec.ts', | ||||
|             testIgnore: 'tests/sso_*.spec.ts', | ||||
|         }, | ||||
|  | ||||
|         { | ||||
|             name: 'sso-mariadb', | ||||
|             testMatch: 'tests/sso_*.spec.ts', | ||||
|             dependencies: ['sso-setup', 'mariadb-setup'], | ||||
|         }, | ||||
|         { | ||||
|             name: 'sso-mysql', | ||||
|             testMatch: 'tests/sso_*.spec.ts', | ||||
|             dependencies: ['sso-setup', 'mysql-setup'], | ||||
|         }, | ||||
|         { | ||||
|             name: 'sso-postgres', | ||||
|             testMatch: 'tests/sso_*.spec.ts', | ||||
|             dependencies: ['sso-setup', 'postgres-setup'], | ||||
|         }, | ||||
|         { | ||||
|             name: 'sso-sqlite', | ||||
|             testMatch: 'tests/sso_*.spec.ts', | ||||
|             dependencies: ['sso-setup'], | ||||
|         }, | ||||
|  | ||||
|         { | ||||
|             name: 'mariadb-teardown', | ||||
|             testMatch: 'tests/setups/db-teardown.ts', | ||||
|             use: { serviceName: "Mariadb" }, | ||||
|         }, | ||||
|         { | ||||
|             name: 'mysql-teardown', | ||||
|             testMatch: 'tests/setups/db-teardown.ts', | ||||
|             use: { serviceName: "Mysql" }, | ||||
|         }, | ||||
|         { | ||||
|             name: 'postgres-teardown', | ||||
|             testMatch: 'tests/setups/db-teardown.ts', | ||||
|             use: { serviceName: "Postgres" }, | ||||
|         }, | ||||
|         { | ||||
|             name: 'sso-teardown', | ||||
|             testMatch: 'tests/setups/sso-teardown.ts', | ||||
|         }, | ||||
|     ], | ||||
|  | ||||
|     globalSetup: require.resolve('./global-setup'), | ||||
| }); | ||||
							
								
								
									
										93
									
								
								playwright/test.env
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								playwright/test.env
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,93 @@ | ||||
| ################################################################## | ||||
| ### Shared Playwright conf test file Vaultwarden and Databases ### | ||||
| ################################################################## | ||||
|  | ||||
| ENV=test | ||||
| DC_ENV_FILE=test.env | ||||
| COMPOSE_IGNORE_ORPHANS=True | ||||
| DOCKER_BUILDKIT=1 | ||||
|  | ||||
| ##################### | ||||
| # Playwright Config # | ||||
| ##################### | ||||
| PW_KEEP_SERVICE_RUNNNING=${PW_KEEP_SERVICE_RUNNNING:-false} | ||||
| PW_SMTP_FROM=vaultwarden@playwright.test | ||||
|  | ||||
| ##################### | ||||
| # Maildev Config 	# | ||||
| ##################### | ||||
| MAILDEV_HTTP_PORT=1081 | ||||
| MAILDEV_SMTP_PORT=1026 | ||||
| MAILDEV_HOST=127.0.0.1 | ||||
|  | ||||
| ################ | ||||
| # Users Config # | ||||
| ################ | ||||
| TEST_USER=test | ||||
| TEST_USER_PASSWORD=Master Password | ||||
| TEST_USER_MAIL=${TEST_USER}@example.com | ||||
|  | ||||
| TEST_USER2=test2 | ||||
| TEST_USER2_PASSWORD=Master Password | ||||
| TEST_USER2_MAIL=${TEST_USER2}@example.com | ||||
|  | ||||
| TEST_USER3=test3 | ||||
| TEST_USER3_PASSWORD=Master Password | ||||
| TEST_USER3_MAIL=${TEST_USER3}@example.com | ||||
|  | ||||
| ################### | ||||
| # Keycloak Config # | ||||
| ################### | ||||
| KEYCLOAK_ADMIN=admin | ||||
| KEYCLOAK_ADMIN_PASSWORD=${KEYCLOAK_ADMIN} | ||||
| KC_HTTP_HOST=127.0.0.1 | ||||
| KC_HTTP_PORT=8081 | ||||
|  | ||||
| # Script parameters (use Keycloak and Vaultwarden config too) | ||||
| TEST_REALM=test | ||||
| DUMMY_REALM=dummy | ||||
| DUMMY_AUTHORITY=http://${KC_HTTP_HOST}:${KC_HTTP_PORT}/realms/${DUMMY_REALM} | ||||
|  | ||||
| ###################### | ||||
| # Vaultwarden Config # | ||||
| ###################### | ||||
| ROCKET_PORT=8003 | ||||
| DOMAIN=http://127.0.0.1:${ROCKET_PORT} | ||||
| LOG_LEVEL=info,oidcwarden::sso=debug | ||||
| LOGIN_RATELIMIT_MAX_BURST=100 | ||||
|  | ||||
| SMTP_SECURITY=off | ||||
| SMTP_PORT=${MAILDEV_SMTP_PORT} | ||||
| SMTP_FROM_NAME=Vaultwarden | ||||
| SMTP_TIMEOUT=5 | ||||
|  | ||||
| SSO_CLIENT_ID=warden | ||||
| SSO_CLIENT_SECRET=warden | ||||
| SSO_AUTHORITY=http://${KC_HTTP_HOST}:${KC_HTTP_PORT}/realms/${TEST_REALM} | ||||
| SSO_DEBUG_TOKENS=true | ||||
|  | ||||
| ########################### | ||||
| # Docker MariaDb container# | ||||
| ########################### | ||||
| MARIADB_PORT=3307 | ||||
| MARIADB_ROOT_PASSWORD=warden | ||||
| MARIADB_USER=warden | ||||
| MARIADB_PASSWORD=warden | ||||
| MARIADB_DATABASE=warden | ||||
|  | ||||
| ########################### | ||||
| # Docker Mysql container# | ||||
| ########################### | ||||
| MYSQL_PORT=3309 | ||||
| MYSQL_ROOT_PASSWORD=warden | ||||
| MYSQL_USER=warden | ||||
| MYSQL_PASSWORD=warden | ||||
| MYSQL_DATABASE=warden | ||||
|  | ||||
| ############################ | ||||
| # Docker Postgres container# | ||||
| ############################ | ||||
| POSTGRES_PORT=5433 | ||||
| POSTGRES_USER=warden | ||||
| POSTGRES_PASSWORD=warden | ||||
| POSTGRES_DB=warden | ||||
							
								
								
									
										37
									
								
								playwright/tests/collection.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								playwright/tests/collection.spec.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| import { test, expect, type TestInfo } from '@playwright/test'; | ||||
|  | ||||
| import * as utils from "../global-utils"; | ||||
| import { createAccount } from './setups/user'; | ||||
|  | ||||
| let users = utils.loadEnv(); | ||||
|  | ||||
| test.beforeAll('Setup', async ({ browser }, testInfo: TestInfo) => { | ||||
|     await utils.startVault(browser, testInfo); | ||||
| }); | ||||
|  | ||||
| test.afterAll('Teardown', async ({}) => { | ||||
|     utils.stopVault(); | ||||
| }); | ||||
|  | ||||
| test('Create', async ({ page }) => { | ||||
|     await createAccount(test, page, users.user1); | ||||
|  | ||||
|     await test.step('Create Org', async () => { | ||||
|         await page.getByRole('link', { name: 'New organisation' }).click(); | ||||
|         await page.getByLabel('Organisation name (required)').fill('Test'); | ||||
|         await page.getByRole('button', { name: 'Submit' }).click(); | ||||
|         await page.locator('div').filter({ hasText: 'Members' }).nth(2).click(); | ||||
|  | ||||
|         await utils.checkNotification(page, 'Organisation created'); | ||||
|     }); | ||||
|  | ||||
|     await test.step('Create Collection', async () => { | ||||
|         await page.getByRole('link', { name: 'Collections' }).click(); | ||||
|         await page.getByRole('button', { name: 'New' }).click(); | ||||
|         await page.getByRole('menuitem', { name: 'Collection' }).click(); | ||||
|         await page.getByLabel('Name (required)').fill('RandomCollec'); | ||||
|         await page.getByRole('button', { name: 'Save' }).click(); | ||||
|         await utils.checkNotification(page, 'Created collection RandomCollec'); | ||||
|         await expect(page.getByRole('button', { name: 'RandomCollec' })).toBeVisible(); | ||||
|     }); | ||||
| }); | ||||
							
								
								
									
										100
									
								
								playwright/tests/login.smtp.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								playwright/tests/login.smtp.spec.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,100 @@ | ||||
| import { test, expect, type TestInfo } from '@playwright/test'; | ||||
| import { MailDev } from 'maildev'; | ||||
|  | ||||
| const utils = require('../global-utils'); | ||||
| import { createAccount, logUser } from './setups/user'; | ||||
| import { activateEmail, retrieveEmailCode, disableEmail } from './setups/2fa'; | ||||
|  | ||||
| let users = utils.loadEnv(); | ||||
|  | ||||
| let mailserver; | ||||
|  | ||||
| test.beforeAll('Setup', async ({ browser }, testInfo: TestInfo) => { | ||||
|     mailserver = new MailDev({ | ||||
|         port: process.env.MAILDEV_SMTP_PORT, | ||||
|         web: { port: process.env.MAILDEV_HTTP_PORT }, | ||||
|     }) | ||||
|  | ||||
|     await mailserver.listen(); | ||||
|  | ||||
|     await utils.startVault(browser, testInfo, { | ||||
|         SMTP_HOST: process.env.MAILDEV_HOST, | ||||
|         SMTP_FROM: process.env.PW_SMTP_FROM, | ||||
|     }); | ||||
| }); | ||||
|  | ||||
| test.afterAll('Teardown', async ({}) => { | ||||
|     utils.stopVault(); | ||||
|     if( mailserver ){ | ||||
|         await mailserver.close(); | ||||
|     } | ||||
| }); | ||||
|  | ||||
| test('Account creation', async ({ page }) => { | ||||
|     const mailBuffer = mailserver.buffer(users.user1.email); | ||||
|  | ||||
|     await createAccount(test, page, users.user1, mailBuffer); | ||||
|  | ||||
|     mailBuffer.close(); | ||||
| }); | ||||
|  | ||||
| test('Login', async ({ context, page }) => { | ||||
|     const mailBuffer = mailserver.buffer(users.user1.email); | ||||
|  | ||||
|     await logUser(test, page, users.user1, mailBuffer); | ||||
|  | ||||
|     await test.step('verify email', async () => { | ||||
|         await page.getByText('Verify your account\'s email').click(); | ||||
|         await expect(page.getByText('Verify your account\'s email')).toBeVisible(); | ||||
|         await page.getByRole('button', { name: 'Send email' }).click(); | ||||
|  | ||||
|         await utils.checkNotification(page, 'Check your email inbox for a verification link'); | ||||
|  | ||||
|         const verify = await mailBuffer.expect((m) => m.subject === "Verify Your Email"); | ||||
|         expect(verify.from[0]?.address).toBe(process.env.PW_SMTP_FROM); | ||||
|  | ||||
|         const page2 = await context.newPage(); | ||||
|         await page2.setContent(verify.html); | ||||
|         const link = await page2.getByTestId("verify").getAttribute("href"); | ||||
|         await page2.close(); | ||||
|  | ||||
|         await page.goto(link); | ||||
|         await utils.checkNotification(page, 'Account email verified'); | ||||
|     }); | ||||
|  | ||||
|     mailBuffer.close(); | ||||
| }); | ||||
|  | ||||
| test('Activate 2fa', async ({ page }) => { | ||||
|     const emails = mailserver.buffer(users.user1.email); | ||||
|  | ||||
|     await logUser(test, page, users.user1); | ||||
|  | ||||
|     await activateEmail(test, page, users.user1, emails); | ||||
|  | ||||
|     emails.close(); | ||||
| }); | ||||
|  | ||||
| test('2fa', async ({ page }) => { | ||||
|     const emails = mailserver.buffer(users.user1.email); | ||||
|  | ||||
|     await test.step('login', async () => { | ||||
|         await page.goto('/'); | ||||
|  | ||||
|         await page.getByLabel(/Email address/).fill(users.user1.email); | ||||
|         await page.getByRole('button', { name: 'Continue' }).click(); | ||||
|         await page.getByLabel('Master password').fill(users.user1.password); | ||||
|         await page.getByRole('button', { name: 'Log in with master password' }).click(); | ||||
|  | ||||
|         await expect(page.getByRole('heading', { name: 'Verify your Identity' })).toBeVisible(); | ||||
|         const code = await retrieveEmailCode(test, page, emails); | ||||
|         await page.getByLabel(/Verification code/).fill(code); | ||||
|         await page.getByRole('button', { name: 'Continue' }).click(); | ||||
|  | ||||
|         await expect(page).toHaveTitle(/Vaults/); | ||||
|     }) | ||||
|  | ||||
|     await disableEmail(test, page, users.user1); | ||||
|  | ||||
|     emails.close(); | ||||
| }); | ||||
							
								
								
									
										51
									
								
								playwright/tests/login.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								playwright/tests/login.spec.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,51 @@ | ||||
| import { test, expect, type Page, type TestInfo } from '@playwright/test'; | ||||
| import * as OTPAuth from "otpauth"; | ||||
|  | ||||
| import * as utils from "../global-utils"; | ||||
| import { createAccount, logUser } from './setups/user'; | ||||
| import { activateTOTP, disableTOTP } from './setups/2fa'; | ||||
|  | ||||
| let users = utils.loadEnv(); | ||||
| let totp; | ||||
|  | ||||
| test.beforeAll('Setup', async ({ browser }, testInfo: TestInfo) => { | ||||
|     await utils.startVault(browser, testInfo, {}); | ||||
| }); | ||||
|  | ||||
| test.afterAll('Teardown', async ({}) => { | ||||
|     utils.stopVault(); | ||||
| }); | ||||
|  | ||||
| test('Account creation', async ({ page }) => { | ||||
|     await createAccount(test, page, users.user1); | ||||
| }); | ||||
|  | ||||
| test('Master password login', async ({ page }) => { | ||||
|     await logUser(test, page, users.user1); | ||||
| }); | ||||
|  | ||||
| test('Authenticator 2fa', async ({ page }) => { | ||||
|     await logUser(test, page, users.user1); | ||||
|  | ||||
|     let totp = await activateTOTP(test, page, users.user1); | ||||
|  | ||||
|     await utils.logout(test, page, users.user1); | ||||
|  | ||||
|     await test.step('login', async () => { | ||||
|         let timestamp = Date.now(); // Needed to use the next token | ||||
|         timestamp = timestamp + (totp.period - (Math.floor(timestamp / 1000) % totp.period) + 1) * 1000; | ||||
|  | ||||
|         await page.getByLabel(/Email address/).fill(users.user1.email); | ||||
|         await page.getByRole('button', { name: 'Continue' }).click(); | ||||
|         await page.getByLabel('Master password').fill(users.user1.password); | ||||
|         await page.getByRole('button', { name: 'Log in with master password' }).click(); | ||||
|  | ||||
|         await expect(page.getByRole('heading', { name: 'Verify your Identity' })).toBeVisible(); | ||||
|         await page.getByLabel(/Verification code/).fill(totp.generate({timestamp})); | ||||
|         await page.getByRole('button', { name: 'Continue' }).click(); | ||||
|  | ||||
|         await expect(page).toHaveTitle(/Vaultwarden Web/); | ||||
|     }); | ||||
|  | ||||
|     await disableTOTP(test, page, users.user1); | ||||
| }); | ||||
							
								
								
									
										115
									
								
								playwright/tests/organization.smtp.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										115
									
								
								playwright/tests/organization.smtp.spec.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,115 @@ | ||||
| import { test, expect, type TestInfo } from '@playwright/test'; | ||||
| import { MailDev } from 'maildev'; | ||||
|  | ||||
| import * as utils from '../global-utils'; | ||||
| import * as orgs from './setups/orgs'; | ||||
| import { createAccount, logUser } from './setups/user'; | ||||
|  | ||||
| let users = utils.loadEnv(); | ||||
|  | ||||
| let mailServer, mail1Buffer, mail2Buffer, mail3Buffer; | ||||
|  | ||||
| test.beforeAll('Setup', async ({ browser }, testInfo: TestInfo) => { | ||||
|     mailServer = new MailDev({ | ||||
|         port: process.env.MAILDEV_SMTP_PORT, | ||||
|         web: { port: process.env.MAILDEV_HTTP_PORT }, | ||||
|     }) | ||||
|  | ||||
|     await mailServer.listen(); | ||||
|  | ||||
|     await utils.startVault(browser, testInfo, { | ||||
|         SMTP_HOST: process.env.MAILDEV_HOST, | ||||
|         SMTP_FROM: process.env.PW_SMTP_FROM, | ||||
|     }); | ||||
|  | ||||
|     mail1Buffer = mailServer.buffer(users.user1.email); | ||||
|     mail2Buffer = mailServer.buffer(users.user2.email); | ||||
|     mail3Buffer = mailServer.buffer(users.user3.email); | ||||
| }); | ||||
|  | ||||
| test.afterAll('Teardown', async ({}, testInfo: TestInfo) => { | ||||
|     utils.stopVault(testInfo); | ||||
|     [mail1Buffer, mail2Buffer, mail3Buffer, mailServer].map((m) => m?.close()); | ||||
| }); | ||||
|  | ||||
| test('Create user3', async ({ page }) => { | ||||
|     await createAccount(test, page, users.user3, mail3Buffer); | ||||
| }); | ||||
|  | ||||
| test('Invite users', async ({ page }) => { | ||||
|     await createAccount(test, page, users.user1, mail1Buffer); | ||||
|  | ||||
|     await orgs.create(test, page, 'Test'); | ||||
|     await orgs.members(test, page, 'Test'); | ||||
|     await orgs.invite(test, page, 'Test', users.user2.email); | ||||
|     await orgs.invite(test, page, 'Test', users.user3.email, { | ||||
|         navigate: false, | ||||
|     }); | ||||
| }); | ||||
|  | ||||
| test('invited with new account', async ({ page }) => { | ||||
|     const invited = await mail2Buffer.expect((mail) => mail.subject === 'Join Test'); | ||||
|  | ||||
|     await test.step('Create account', async () => { | ||||
|         await page.setContent(invited.html); | ||||
|         const link = await page.getByTestId('invite').getAttribute('href'); | ||||
|         await page.goto(link); | ||||
|         await expect(page).toHaveTitle(/Create account | Vaultwarden Web/); | ||||
|  | ||||
|         //await page.getByLabel('Name').fill(users.user2.name); | ||||
|         await page.getByLabel('New master password (required)', { exact: true }).fill(users.user2.password); | ||||
|         await page.getByLabel('Confirm new master password (').fill(users.user2.password); | ||||
|         await page.getByRole('button', { name: 'Create account' }).click(); | ||||
|         await utils.checkNotification(page, 'Your new account has been created'); | ||||
|  | ||||
|         // Redirected to the vault | ||||
|         await expect(page).toHaveTitle('Vaults | Vaultwarden Web'); | ||||
|         await utils.checkNotification(page, 'You have been logged in!'); | ||||
|         await utils.checkNotification(page, 'Invitation accepted'); | ||||
|     }); | ||||
|  | ||||
|     await test.step('Check mails', async () => { | ||||
|         await mail2Buffer.expect((m) => m.subject === 'Welcome'); | ||||
|         await mail2Buffer.expect((m) => m.subject === 'New Device Logged In From Firefox'); | ||||
|         await mail1Buffer.expect((m) => m.subject.includes('Invitation to Test accepted')); | ||||
|     }); | ||||
| }); | ||||
|  | ||||
| test('invited with existing account', async ({ page }) => { | ||||
|     const invited = await mail3Buffer.expect((mail) => mail.subject === 'Join Test'); | ||||
|  | ||||
|     await page.setContent(invited.html); | ||||
|     const link = await page.getByTestId('invite').getAttribute('href'); | ||||
|  | ||||
|     await page.goto(link); | ||||
|  | ||||
|     // We should be on login page with email prefilled | ||||
|     await expect(page).toHaveTitle(/Vaultwarden Web/); | ||||
|     await page.getByRole('button', { name: 'Continue' }).click(); | ||||
|  | ||||
|     // Unlock page | ||||
|     await page.getByLabel('Master password').fill(users.user3.password); | ||||
|     await page.getByRole('button', { name: 'Log in with master password' }).click(); | ||||
|  | ||||
|     // We are now in the default vault page | ||||
|     await expect(page).toHaveTitle(/Vaultwarden Web/); | ||||
|     await utils.checkNotification(page, 'Invitation accepted'); | ||||
|  | ||||
|     await mail3Buffer.expect((m) => m.subject === 'New Device Logged In From Firefox'); | ||||
|     await mail1Buffer.expect((m) => m.subject.includes('Invitation to Test accepted')); | ||||
| }); | ||||
|  | ||||
| test('Confirm invited user', async ({ page }) => { | ||||
|     await logUser(test, page, users.user1, mail1Buffer); | ||||
|  | ||||
|     await orgs.members(test, page, 'Test'); | ||||
|     await orgs.confirm(test, page, 'Test', users.user2.email); | ||||
|  | ||||
|     await mail2Buffer.expect((m) => m.subject.includes('Invitation to Test confirmed')); | ||||
| }); | ||||
|  | ||||
| test('Organization is visible', async ({ page }) => { | ||||
|     await logUser(test, page, users.user2, mail2Buffer); | ||||
|     await page.getByRole('button', { name: 'vault: Test', exact: true }).click(); | ||||
|     await expect(page.getByLabel('Filter: Default collection')).toBeVisible(); | ||||
| }); | ||||
							
								
								
									
										54
									
								
								playwright/tests/organization.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								playwright/tests/organization.spec.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,54 @@ | ||||
| import { test, expect, type TestInfo } from '@playwright/test'; | ||||
| import { MailDev } from 'maildev'; | ||||
|  | ||||
| import * as utils from "../global-utils"; | ||||
| import * as orgs from './setups/orgs'; | ||||
| import { createAccount, logUser } from './setups/user'; | ||||
|  | ||||
| let users = utils.loadEnv(); | ||||
|  | ||||
| test.beforeAll('Setup', async ({ browser }, testInfo: TestInfo) => { | ||||
|     await utils.startVault(browser, testInfo); | ||||
| }); | ||||
|  | ||||
| test.afterAll('Teardown', async ({}) => { | ||||
|     utils.stopVault(); | ||||
| }); | ||||
|  | ||||
| test('Invite', async ({ page }) => { | ||||
|     await createAccount(test, page, users.user3); | ||||
|     await createAccount(test, page, users.user1); | ||||
|  | ||||
|     await orgs.create(test, page, 'New organisation'); | ||||
|     await orgs.members(test, page, 'New organisation'); | ||||
|  | ||||
|     await test.step('missing user2', async () => { | ||||
|         await orgs.invite(test, page, 'New organisation', users.user2.email); | ||||
|         await expect(page.getByRole('row', { name: users.user2.email })).toHaveText(/Invited/); | ||||
|     }); | ||||
|  | ||||
|     await test.step('existing user3', async () => { | ||||
|         await orgs.invite(test, page, 'New organisation', users.user3.email); | ||||
|         await expect(page.getByRole('row', { name: users.user3.email })).toHaveText(/Needs confirmation/); | ||||
|         await orgs.confirm(test, page, 'New organisation', users.user3.email); | ||||
|     }); | ||||
|  | ||||
|     await test.step('confirm user2', async () => { | ||||
|         await createAccount(test, page, users.user2); | ||||
|         await logUser(test, page, users.user1); | ||||
|         await orgs.members(test, page, 'New organisation'); | ||||
|         await orgs.confirm(test, page, 'New organisation', users.user2.email); | ||||
|     }); | ||||
|  | ||||
|     await test.step('Org visible user2  ', async () => { | ||||
|         await logUser(test, page, users.user2); | ||||
|         await page.getByRole('button', { name: 'vault: New organisation', exact: true }).click(); | ||||
|         await expect(page.getByLabel('Filter: Default collection')).toBeVisible(); | ||||
|     }); | ||||
|  | ||||
|     await test.step('Org visible user3  ', async () => { | ||||
|         await logUser(test, page, users.user3); | ||||
|         await page.getByRole('button', { name: 'vault: New organisation', exact: true }).click(); | ||||
|         await expect(page.getByLabel('Filter: Default collection')).toBeVisible(); | ||||
|     }); | ||||
| }); | ||||
							
								
								
									
										92
									
								
								playwright/tests/setups/2fa.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										92
									
								
								playwright/tests/setups/2fa.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,92 @@ | ||||
| import { expect, type Page, Test } from '@playwright/test'; | ||||
| import { type MailBuffer } from 'maildev'; | ||||
| import * as OTPAuth from "otpauth"; | ||||
|  | ||||
| import * as utils from '../../global-utils'; | ||||
|  | ||||
| export async function activateTOTP(test: Test, page: Page, user: { name: string, password: string }): OTPAuth.TOTP { | ||||
|     return await test.step('Activate TOTP 2FA', async () => { | ||||
|         await page.getByRole('button', { name: user.name }).click(); | ||||
|         await page.getByRole('menuitem', { name: 'Account settings' }).click(); | ||||
|         await page.getByRole('link', { name: 'Security' }).click(); | ||||
|         await page.getByRole('link', { name: 'Two-step login' }).click(); | ||||
|         await page.locator('bit-item').filter({ hasText: /Authenticator app/ }).getByRole('button').click(); | ||||
|         await page.getByLabel('Master password (required)').fill(user.password); | ||||
|         await page.getByRole('button', { name: 'Continue' }).click(); | ||||
|  | ||||
|         const secret = await page.getByLabel('Key').innerText(); | ||||
|         let totp = new OTPAuth.TOTP({ secret, period: 30 }); | ||||
|  | ||||
|         await page.getByLabel(/Verification code/).fill(totp.generate()); | ||||
|         await page.getByRole('button', { name: 'Turn on' }).click(); | ||||
|         await page.getByRole('heading', { name: 'Turned on', exact: true }); | ||||
|         await page.getByLabel('Close').click(); | ||||
|  | ||||
|         return totp; | ||||
|     }) | ||||
| } | ||||
|  | ||||
| export async function disableTOTP(test: Test, page: Page, user: { password: string }) { | ||||
|     await test.step('Disable TOTP 2FA', async () => { | ||||
|         await page.getByRole('button', { name: 'Test' }).click(); | ||||
|         await page.getByRole('menuitem', { name: 'Account settings' }).click(); | ||||
|         await page.getByRole('link', { name: 'Security' }).click(); | ||||
|         await page.getByRole('link', { name: 'Two-step login' }).click(); | ||||
|         await page.locator('bit-item').filter({ hasText: /Authenticator app/ }).getByRole('button').click(); | ||||
|         await page.getByLabel('Master password (required)').click(); | ||||
|         await page.getByLabel('Master password (required)').fill(user.password); | ||||
|         await page.getByRole('button', { name: 'Continue' }).click(); | ||||
|         await page.getByRole('button', { name: 'Turn off' }).click(); | ||||
|         await page.getByRole('button', { name: 'Yes' }).click(); | ||||
|         await utils.checkNotification(page, 'Two-step login provider turned off'); | ||||
|     }); | ||||
| } | ||||
|  | ||||
| export async function activateEmail(test: Test, page: Page, user: { name: string, password: string }, mailBuffer: MailBuffer) { | ||||
|     await test.step('Activate Email 2FA', async () => { | ||||
|         await page.getByRole('button', { name: user.name }).click(); | ||||
|         await page.getByRole('menuitem', { name: 'Account settings' }).click(); | ||||
|         await page.getByRole('link', { name: 'Security' }).click(); | ||||
|         await page.getByRole('link', { name: 'Two-step login' }).click(); | ||||
|         await page.locator('bit-item').filter({ hasText: 'Email Email Enter a code sent' }).getByRole('button').click(); | ||||
|         await page.getByLabel('Master password (required)').fill(user.password); | ||||
|         await page.getByRole('button', { name: 'Continue' }).click(); | ||||
|         await page.getByRole('button', { name: 'Send email' }).click(); | ||||
|     }); | ||||
|  | ||||
|     let code = await retrieveEmailCode(test, page, mailBuffer); | ||||
|  | ||||
|     await test.step('input code', async () => { | ||||
|         await page.getByLabel('2. Enter the resulting 6').fill(code); | ||||
|         await page.getByRole('button', { name: 'Turn on' }).click(); | ||||
|         await page.getByRole('heading', { name: 'Turned on', exact: true }); | ||||
|     }); | ||||
| } | ||||
|  | ||||
| export async function retrieveEmailCode(test: Test, page: Page, mailBuffer: MailBuffer): string { | ||||
|     return await test.step('retrieve code', async () => { | ||||
|         const codeMail = await mailBuffer.expect((mail) => mail.subject.includes("Login Verification Code")); | ||||
|         const page2 = await page.context().newPage(); | ||||
|         await page2.setContent(codeMail.html); | ||||
|         const code = await page2.getByTestId("2fa").innerText(); | ||||
|         await page2.close(); | ||||
|         return code; | ||||
|     }); | ||||
| } | ||||
|  | ||||
| export async function disableEmail(test: Test, page: Page, user: { password: string }) { | ||||
|     await test.step('Disable Email 2FA', async () => { | ||||
|         await page.getByRole('button', { name: 'Test' }).click(); | ||||
|         await page.getByRole('menuitem', { name: 'Account settings' }).click(); | ||||
|         await page.getByRole('link', { name: 'Security' }).click(); | ||||
|         await page.getByRole('link', { name: 'Two-step login' }).click(); | ||||
|         await page.locator('bit-item').filter({ hasText: 'Email' }).getByRole('button').click(); | ||||
|         await page.getByLabel('Master password (required)').click(); | ||||
|         await page.getByLabel('Master password (required)').fill(user.password); | ||||
|         await page.getByRole('button', { name: 'Continue' }).click(); | ||||
|         await page.getByRole('button', { name: 'Turn off' }).click(); | ||||
|         await page.getByRole('button', { name: 'Yes' }).click(); | ||||
|  | ||||
|         await utils.checkNotification(page, 'Two-step login provider turned off'); | ||||
|     }); | ||||
| } | ||||
							
								
								
									
										7
									
								
								playwright/tests/setups/db-setup.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								playwright/tests/setups/db-setup.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| import { test } from './db-test'; | ||||
|  | ||||
| const utils = require('../../global-utils'); | ||||
|  | ||||
| test('DB start', async ({ serviceName }) => { | ||||
| 	utils.startComposeService(serviceName); | ||||
| }); | ||||
							
								
								
									
										11
									
								
								playwright/tests/setups/db-teardown.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								playwright/tests/setups/db-teardown.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| import { test } from './db-test'; | ||||
|  | ||||
| const utils = require('../../global-utils'); | ||||
|  | ||||
| utils.loadEnv(); | ||||
|  | ||||
| test('DB teardown ?', async ({ serviceName }) => { | ||||
|     if( process.env.PW_KEEP_SERVICE_RUNNNING !== "true" ) { | ||||
|         utils.stopComposeService(serviceName); | ||||
|     } | ||||
| }); | ||||
							
								
								
									
										9
									
								
								playwright/tests/setups/db-test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								playwright/tests/setups/db-test.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| import { test as base } from '@playwright/test'; | ||||
|  | ||||
| export type TestOptions = { | ||||
|   serviceName: string; | ||||
| }; | ||||
|  | ||||
| export const test = base.extend<TestOptions>({ | ||||
|   serviceName: ['', { option: true }], | ||||
| }); | ||||
							
								
								
									
										77
									
								
								playwright/tests/setups/orgs.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								playwright/tests/setups/orgs.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,77 @@ | ||||
| import { expect, type Browser,Page } from '@playwright/test'; | ||||
|  | ||||
| import * as utils from '../../global-utils'; | ||||
|  | ||||
| export async function create(test, page: Page, name: string) { | ||||
|     await test.step('Create Org', async () => { | ||||
|         await page.locator('a').filter({ hasText: 'Password Manager' }).first().click(); | ||||
|         await expect(page.getByTitle('All vaults', { exact: true })).toBeVisible(); | ||||
|         await page.getByRole('link', { name: 'New organisation' }).click(); | ||||
|         await page.getByLabel('Organisation name (required)').fill(name); | ||||
|         await page.getByRole('button', { name: 'Submit' }).click(); | ||||
|  | ||||
|         await utils.checkNotification(page, 'Organisation created'); | ||||
|     }); | ||||
| } | ||||
|  | ||||
| export async function policies(test, page: Page, name: string) { | ||||
|     await test.step(`Navigate to ${name} policies`, async () => { | ||||
|         await page.locator('a').filter({ hasText: 'Admin Console' }).first().click(); | ||||
|         await page.locator('org-switcher').getByLabel(/Toggle collapse/).click(); | ||||
|         await page.locator('org-switcher').getByRole('link', { name: `${name}` }).first().click(); | ||||
|         await expect(page.getByRole('heading', { name: `${name} collections` })).toBeVisible(); | ||||
|         await page.getByRole('button', { name: 'Toggle collapse Settings' }).click(); | ||||
|         await page.getByRole('link', { name: 'Policies' }).click(); | ||||
|         await expect(page.getByRole('heading', { name: 'Policies' })).toBeVisible(); | ||||
|     }); | ||||
| } | ||||
|  | ||||
| export async function members(test, page: Page, name: string) { | ||||
|     await test.step(`Navigate to ${name} members`, async () => { | ||||
|         await page.locator('a').filter({ hasText: 'Admin Console' }).first().click(); | ||||
|         await page.locator('org-switcher').getByLabel(/Toggle collapse/).click(); | ||||
|         await page.locator('org-switcher').getByRole('link', { name: `${name}` }).first().click(); | ||||
|         await expect(page.getByRole('heading', { name: `${name} collections` })).toBeVisible(); | ||||
|         await page.locator('div').filter({ hasText: 'Members' }).nth(2).click(); | ||||
|         await expect(page.getByRole('heading', { name: 'Members' })).toBeVisible(); | ||||
|         await expect(page.getByRole('cell', { name: 'All' })).toBeVisible(); | ||||
|     }); | ||||
| } | ||||
|  | ||||
| export async function invite(test, page: Page, name: string, email: string) { | ||||
|     await test.step(`Invite ${email}`, async () => { | ||||
|         await expect(page.getByRole('heading', { name: 'Members' })).toBeVisible(); | ||||
|         await page.getByRole('button', { name: 'Invite member' }).click(); | ||||
|         await page.getByLabel('Email (required)').fill(email); | ||||
|         await page.getByRole('tab', { name: 'Collections' }).click(); | ||||
|         await page.getByRole('combobox', { name: 'Permission' }).click(); | ||||
|         await page.getByText('Edit items', { exact: true }).click(); | ||||
|         await page.getByLabel('Select collections').click(); | ||||
|         await page.getByText('Default collection').click(); | ||||
|         await page.getByRole('cell', { name: 'Collection', exact: true }).click(); | ||||
|         await page.getByRole('button', { name: 'Save' }).click(); | ||||
|         await utils.checkNotification(page, 'User(s) invited'); | ||||
|     }); | ||||
| } | ||||
|  | ||||
| export async function confirm(test, page: Page, name: string, user_email: string) { | ||||
|     await test.step(`Confirm ${user_email}`, async () => { | ||||
|         await expect(page.getByRole('heading', { name: 'Members' })).toBeVisible(); | ||||
|         await page.getByRole('row').filter({hasText: user_email}).getByLabel('Options').click(); | ||||
|         await page.getByRole('menuitem', { name: 'Confirm' }).click(); | ||||
|         await expect(page.getByRole('heading', { name: 'Confirm user' })).toBeVisible(); | ||||
|         await page.getByRole('button', { name: 'Confirm' }).click(); | ||||
|         await utils.checkNotification(page, 'confirmed'); | ||||
|     }); | ||||
| } | ||||
|  | ||||
| export async function revoke(test, page: Page, name: string, user_email: string) { | ||||
|     await test.step(`Revoke ${user_email}`, async () => { | ||||
|         await expect(page.getByRole('heading', { name: 'Members' })).toBeVisible(); | ||||
|         await page.getByRole('row').filter({hasText: user_email}).getByLabel('Options').click(); | ||||
|         await page.getByRole('menuitem', { name: 'Revoke access' }).click(); | ||||
|         await expect(page.getByRole('heading', { name: 'Revoke access' })).toBeVisible(); | ||||
|         await page.getByRole('button', { name: 'Revoke access' }).click(); | ||||
|         await utils.checkNotification(page, 'Revoked organisation access'); | ||||
|     }); | ||||
| } | ||||
							
								
								
									
										18
									
								
								playwright/tests/setups/sso-setup.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								playwright/tests/setups/sso-setup.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| import { test, expect, type TestInfo } from '@playwright/test'; | ||||
|  | ||||
| const { exec } = require('node:child_process'); | ||||
| const utils = require('../../global-utils'); | ||||
|  | ||||
| utils.loadEnv(); | ||||
|  | ||||
| test.beforeAll('Setup', async () => { | ||||
|     console.log("Starting Keycloak"); | ||||
|     exec(`docker compose --profile keycloak --env-file test.env up`); | ||||
| }); | ||||
|  | ||||
| test('Keycloak is up', async ({ page }) => { | ||||
|     await utils.waitFor(process.env.SSO_AUTHORITY, page.context().browser()); | ||||
|     // Dummy authority is created at the end of the setup | ||||
|     await utils.waitFor(process.env.DUMMY_AUTHORITY, page.context().browser()); | ||||
|     console.log(`Keycloak running on: ${process.env.SSO_AUTHORITY}`); | ||||
| }); | ||||
							
								
								
									
										15
									
								
								playwright/tests/setups/sso-teardown.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								playwright/tests/setups/sso-teardown.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| import { test, type FullConfig } from '@playwright/test'; | ||||
|  | ||||
| const { execSync } = require('node:child_process'); | ||||
| const utils = require('../../global-utils'); | ||||
|  | ||||
| utils.loadEnv(); | ||||
|  | ||||
| test('Keycloak teardown', async () => { | ||||
|     if( process.env.PW_KEEP_SERVICE_RUNNNING === "true" ) { | ||||
|         console.log("Keep Keycloak running"); | ||||
|     } else { | ||||
|         console.log("Keycloak stopping"); | ||||
|         execSync(`docker compose --profile keycloak --env-file test.env stop Keycloak`); | ||||
|     } | ||||
| }); | ||||
							
								
								
									
										138
									
								
								playwright/tests/setups/sso.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										138
									
								
								playwright/tests/setups/sso.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,138 @@ | ||||
| import { expect, type Page, Test } from '@playwright/test'; | ||||
| import { type MailBuffer, MailServer } from 'maildev'; | ||||
| import * as OTPAuth from "otpauth"; | ||||
|  | ||||
| import * as utils from '../../global-utils'; | ||||
| import { retrieveEmailCode } from './2fa'; | ||||
|  | ||||
| /** | ||||
|  * If a MailBuffer is passed it will be used and consume the expected emails | ||||
|  */ | ||||
| export async function logNewUser( | ||||
|     test: Test, | ||||
|     page: Page, | ||||
|     user: { email: string, name: string, password: string }, | ||||
|     options: { mailBuffer?: MailBuffer, override?: boolean } = {} | ||||
| ) { | ||||
|     await test.step(`Create user ${user.name}`, async () => { | ||||
|         await page.context().clearCookies(); | ||||
|  | ||||
|         await test.step('Landing page', async () => { | ||||
|             await utils.cleanLanding(page); | ||||
|  | ||||
|             if( options.override ) { | ||||
|                 await page.getByRole('button', { name: 'Continue' }).click(); | ||||
|             } else { | ||||
|                 await page.getByLabel(/Email address/).fill(user.email); | ||||
|                 await page.getByRole('button', { name: /Use single sign-on/ }).click(); | ||||
|             } | ||||
|         }); | ||||
|  | ||||
|         await test.step('Keycloak login', async () => { | ||||
|             await expect(page.getByRole('heading', { name: 'Sign in to your account' })).toBeVisible(); | ||||
|             await page.getByLabel(/Username/).fill(user.name); | ||||
|             await page.getByLabel('Password', { exact: true }).fill(user.password); | ||||
|             await page.getByRole('button', { name: 'Sign In' }).click(); | ||||
|         }); | ||||
|  | ||||
|         await test.step('Create Vault account', async () => { | ||||
|             await expect(page.getByRole('heading', { name: 'Join organisation' })).toBeVisible(); | ||||
|             await page.getByLabel('New master password (required)', { exact: true }).fill(user.password); | ||||
|             await page.getByLabel('Confirm new master password (').fill(user.password); | ||||
|             await page.getByRole('button', { name: 'Create account' }).click(); | ||||
|         }); | ||||
|  | ||||
|         await test.step('Default vault page', async () => { | ||||
|             await expect(page).toHaveTitle(/Vaultwarden Web/); | ||||
|             await expect(page.getByTitle('All vaults', { exact: true })).toBeVisible(); | ||||
|         }); | ||||
|  | ||||
|         await utils.checkNotification(page, 'Account successfully created!'); | ||||
|         await utils.checkNotification(page, 'Invitation accepted'); | ||||
|  | ||||
|         if( options.mailBuffer ){ | ||||
|             let mailBuffer = options.mailBuffer; | ||||
|             await test.step('Check emails', async () => { | ||||
|                 await mailBuffer.expect((m) => m.subject === "Welcome"); | ||||
|                 await mailBuffer.expect((m) => m.subject.includes("New Device Logged")); | ||||
|             }); | ||||
|         } | ||||
|     }); | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * If a MailBuffer is passed it will be used and consume the expected emails | ||||
|  */ | ||||
| export async function logUser( | ||||
|     test: Test, | ||||
|     page: Page, | ||||
|     user: { email: string, password: string }, | ||||
|     options: { | ||||
|         mailBuffer ?: MailBuffer, | ||||
|         override?: boolean, | ||||
|         totp?: OTPAuth.TOTP, | ||||
|         mail2fa?: boolean, | ||||
|     } = {} | ||||
| ) { | ||||
|     let mailBuffer = options.mailBuffer; | ||||
|  | ||||
|     await test.step(`Log user ${user.email}`, async () => { | ||||
|         await page.context().clearCookies(); | ||||
|  | ||||
|         await test.step('Landing page', async () => { | ||||
|             await utils.cleanLanding(page); | ||||
|  | ||||
|             if( options.override ) { | ||||
|                 await page.getByRole('button', { name: 'Continue' }).click(); | ||||
|             } else { | ||||
|                 await page.getByLabel(/Email address/).fill(user.email); | ||||
|                 await page.getByRole('button', { name: /Use single sign-on/ }).click(); | ||||
|             } | ||||
|         }); | ||||
|  | ||||
|         await test.step('Keycloak login', async () => { | ||||
|             await expect(page.getByRole('heading', { name: 'Sign in to your account' })).toBeVisible(); | ||||
|             await page.getByLabel(/Username/).fill(user.name); | ||||
|             await page.getByLabel('Password', { exact: true }).fill(user.password); | ||||
|             await page.getByRole('button', { name: 'Sign In' }).click(); | ||||
|         }); | ||||
|  | ||||
|         if( options.totp || options.mail2fa ){ | ||||
|             let code; | ||||
|  | ||||
|             await test.step('2FA check', async () => { | ||||
|                 await expect(page.getByRole('heading', { name: 'Verify your Identity' })).toBeVisible(); | ||||
|  | ||||
|                 if( options.totp ) { | ||||
|                     const totp = options.totp; | ||||
|                     let timestamp = Date.now(); // Needed to use the next token | ||||
|                     timestamp = timestamp + (totp.period - (Math.floor(timestamp / 1000) % totp.period) + 1) * 1000; | ||||
|                     code = totp.generate({timestamp}); | ||||
|                 } else if( options.mail2fa ){ | ||||
|                     code = await retrieveEmailCode(test, page, mailBuffer); | ||||
|                 } | ||||
|  | ||||
|                 await page.getByLabel(/Verification code/).fill(code); | ||||
|                 await page.getByRole('button', { name: 'Continue' }).click(); | ||||
|             }); | ||||
|         } | ||||
|  | ||||
|         await test.step('Unlock vault', async () => { | ||||
|             await expect(page).toHaveTitle('Vaultwarden Web'); | ||||
|             await expect(page.getByRole('heading', { name: 'Your vault is locked' })).toBeVisible(); | ||||
|             await page.getByLabel('Master password').fill(user.password); | ||||
|             await page.getByRole('button', { name: 'Unlock' }).click(); | ||||
|         }); | ||||
|  | ||||
|         await test.step('Default vault page', async () => { | ||||
|             await expect(page).toHaveTitle(/Vaultwarden Web/); | ||||
|             await expect(page.getByTitle('All vaults', { exact: true })).toBeVisible(); | ||||
|         }); | ||||
|  | ||||
|         if( mailBuffer ){ | ||||
|             await test.step('Check email', async () => { | ||||
|                 await mailBuffer.expect((m) => m.subject.includes("New Device Logged")); | ||||
|             }); | ||||
|         } | ||||
|     }); | ||||
| } | ||||
							
								
								
									
										55
									
								
								playwright/tests/setups/user.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								playwright/tests/setups/user.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,55 @@ | ||||
| import { expect, type Browser, Page } from '@playwright/test'; | ||||
|  | ||||
| import { type MailBuffer } from 'maildev'; | ||||
|  | ||||
| import * as utils from '../../global-utils'; | ||||
|  | ||||
| export async function createAccount(test, page: Page, user: { email: string, name: string, password: string }, mailBuffer?: MailBuffer) { | ||||
|     await test.step(`Create user ${user.name}`, async () => { | ||||
|         await utils.cleanLanding(page); | ||||
|  | ||||
|         await page.getByRole('link', { name: 'Create account' }).click(); | ||||
|  | ||||
|         // Back to Vault create account | ||||
|         await expect(page).toHaveTitle(/Create account | Vaultwarden Web/); | ||||
|         await page.getByLabel(/Email address/).fill(user.email); | ||||
|         await page.getByLabel('Name').fill(user.name); | ||||
|         await page.getByRole('button', { name: 'Continue' }).click(); | ||||
|  | ||||
|         // Vault finish Creation | ||||
|         await page.getByLabel('New master password (required)', { exact: true }).fill(user.password); | ||||
|         await page.getByLabel('Confirm new master password (').fill(user.password); | ||||
|         await page.getByRole('button', { name: 'Create account' }).click(); | ||||
|  | ||||
|         await utils.checkNotification(page, 'Your new account has been created') | ||||
|  | ||||
|         // We are now in the default vault page | ||||
|         await expect(page).toHaveTitle('Vaults | Vaultwarden Web'); | ||||
|         await utils.checkNotification(page, 'You have been logged in!'); | ||||
|  | ||||
|         if( mailBuffer ){ | ||||
|             await mailBuffer.expect((m) => m.subject === "Welcome"); | ||||
|             await mailBuffer.expect((m) => m.subject === "New Device Logged In From Firefox"); | ||||
|         } | ||||
|     }); | ||||
| } | ||||
|  | ||||
| export async function logUser(test, page: Page, user: { email: string, password: string }, mailBuffer?: MailBuffer) { | ||||
|     await test.step(`Log user ${user.email}`, async () => { | ||||
|         await utils.cleanLanding(page); | ||||
|  | ||||
|         await page.getByLabel(/Email address/).fill(user.email); | ||||
|         await page.getByRole('button', { name: 'Continue' }).click(); | ||||
|  | ||||
|         // Unlock page | ||||
|         await page.getByLabel('Master password').fill(user.password); | ||||
|         await page.getByRole('button', { name: 'Log in with master password' }).click(); | ||||
|  | ||||
|         // We are now in the default vault page | ||||
|         await expect(page).toHaveTitle(/Vaultwarden Web/); | ||||
|  | ||||
|         if( mailBuffer ){ | ||||
|             await mailBuffer.expect((m) => m.subject === "New Device Logged In From Firefox"); | ||||
|         } | ||||
|     }); | ||||
| } | ||||
							
								
								
									
										53
									
								
								playwright/tests/sso_login.smtp.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								playwright/tests/sso_login.smtp.spec.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,53 @@ | ||||
| import { test, expect, type TestInfo } from '@playwright/test'; | ||||
| import { MailDev } from 'maildev'; | ||||
|  | ||||
| import { logNewUser, logUser } from './setups/sso'; | ||||
| import { activateEmail, disableEmail } from './setups/2fa'; | ||||
| import * as utils from "../global-utils"; | ||||
|  | ||||
| let users = utils.loadEnv(); | ||||
|  | ||||
| let mailserver; | ||||
|  | ||||
| test.beforeAll('Setup', async ({ browser }, testInfo: TestInfo) => { | ||||
|     mailserver = new MailDev({ | ||||
|         port: process.env.MAILDEV_SMTP_PORT, | ||||
|         web: { port: process.env.MAILDEV_HTTP_PORT }, | ||||
|     }) | ||||
|  | ||||
|     await mailserver.listen(); | ||||
|  | ||||
|     await utils.startVault(browser, testInfo, { | ||||
|         SSO_ENABLED: true, | ||||
|         SSO_ONLY: false, | ||||
|         SMTP_HOST: process.env.MAILDEV_HOST, | ||||
|         SMTP_FROM: process.env.PW_SMTP_FROM, | ||||
|     }); | ||||
| }); | ||||
|  | ||||
| test.afterAll('Teardown', async ({}) => { | ||||
|     utils.stopVault(); | ||||
|     if( mailserver ){ | ||||
|         await mailserver.close(); | ||||
|     } | ||||
| }); | ||||
|  | ||||
| test('Create and activate 2FA', async ({ page }) => { | ||||
|     const mailBuffer = mailserver.buffer(users.user1.email); | ||||
|  | ||||
|     await logNewUser(test, page, users.user1, {mailBuffer: mailBuffer}); | ||||
|  | ||||
|     await activateEmail(test, page, users.user1, mailBuffer); | ||||
|  | ||||
|     mailBuffer.close(); | ||||
| }); | ||||
|  | ||||
| test('Log and disable', async ({ page }) => { | ||||
|     const mailBuffer = mailserver.buffer(users.user1.email); | ||||
|  | ||||
|     await logUser(test, page, users.user1, {mailBuffer: mailBuffer, mail2fa: true}); | ||||
|  | ||||
|     await disableEmail(test, page, users.user1); | ||||
|  | ||||
|     mailBuffer.close(); | ||||
| }); | ||||
							
								
								
									
										94
									
								
								playwright/tests/sso_login.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								playwright/tests/sso_login.spec.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,94 @@ | ||||
| import { test, expect, type TestInfo } from '@playwright/test'; | ||||
|  | ||||
| import { logNewUser, logUser } from './setups/sso'; | ||||
| import { activateTOTP, disableTOTP } from './setups/2fa'; | ||||
| import * as utils from "../global-utils"; | ||||
|  | ||||
| let users = utils.loadEnv(); | ||||
|  | ||||
| test.beforeAll('Setup', async ({ browser }, testInfo: TestInfo) => { | ||||
|     await utils.startVault(browser, testInfo, { | ||||
|         SSO_ENABLED: true, | ||||
|         SSO_ONLY: false | ||||
|     }); | ||||
| }); | ||||
|  | ||||
| test.afterAll('Teardown', async ({}) => { | ||||
|     utils.stopVault(); | ||||
| }); | ||||
|  | ||||
| test('Account creation using SSO', async ({ page }) => { | ||||
|     // Landing page | ||||
|     await logNewUser(test, page, users.user1); | ||||
| }); | ||||
|  | ||||
| test('SSO login', async ({ page }) => { | ||||
|     await logUser(test, page, users.user1); | ||||
| }); | ||||
|  | ||||
| test('Non SSO login', async ({ page }) => { | ||||
|     // Landing page | ||||
|     await page.goto('/'); | ||||
|     await page.getByLabel(/Email address/).fill(users.user1.email); | ||||
|     await page.getByRole('button', { name: 'Continue' }).click(); | ||||
|  | ||||
|     // Unlock page | ||||
|     await page.getByLabel('Master password').fill(users.user1.password); | ||||
|     await page.getByRole('button', { name: 'Log in with master password' }).click(); | ||||
|  | ||||
|     // We are now in the default vault page | ||||
|     await expect(page).toHaveTitle(/Vaultwarden Web/); | ||||
| }); | ||||
|  | ||||
| test('SSO login with TOTP 2fa', async ({ page }) => { | ||||
|     await logUser(test, page, users.user1); | ||||
|  | ||||
|     let totp = await activateTOTP(test, page, users.user1); | ||||
|  | ||||
|     await logUser(test, page, users.user1, { totp }); | ||||
|  | ||||
|     await disableTOTP(test, page, users.user1); | ||||
| }); | ||||
|  | ||||
| test('Non SSO login impossible', async ({ page, browser }, testInfo: TestInfo) => { | ||||
|     await utils.restartVault(page, testInfo, { | ||||
|         SSO_ENABLED: true, | ||||
|         SSO_ONLY: true | ||||
|     }, false); | ||||
|  | ||||
|     // Landing page | ||||
|     await page.goto('/'); | ||||
|     await page.getByLabel(/Email address/).fill(users.user1.email); | ||||
|  | ||||
|     // Check that SSO login is available | ||||
|     await expect(page.getByRole('button', { name: /Use single sign-on/ })).toHaveCount(1); | ||||
|  | ||||
|     await page.getByLabel(/Email address/).fill(users.user1.email); | ||||
|     await page.getByRole('button', { name: 'Continue' }).click(); | ||||
|  | ||||
|     // Unlock page | ||||
|     await page.getByLabel('Master password').fill(users.user1.password); | ||||
|     await page.getByRole('button', { name: 'Log in with master password' }).click(); | ||||
|  | ||||
|     // An error should appear | ||||
|     await page.getByLabel('SSO sign-in is required') | ||||
| }); | ||||
|  | ||||
|  | ||||
| test('No SSO login', async ({ page }, testInfo: TestInfo) => { | ||||
|     await utils.restartVault(page, testInfo, { | ||||
|         SSO_ENABLED: false | ||||
|     }, false); | ||||
|  | ||||
|     // Landing page | ||||
|     await page.goto('/'); | ||||
|     await page.getByLabel(/Email address/).fill(users.user1.email); | ||||
|  | ||||
|     // No SSO button (rely on a correct selector checked in previous test) | ||||
|     await page.getByLabel('Master password'); | ||||
|     await expect(page.getByRole('button', { name: /Use single sign-on/ })).toHaveCount(0); | ||||
|  | ||||
|     // Can continue to Master password | ||||
|     await page.getByRole('button', { name: 'Continue' }).click(); | ||||
|     await expect(page.getByRole('button', { name: /Log in with master password/ })).toHaveCount(1); | ||||
| }); | ||||
							
								
								
									
										121
									
								
								playwright/tests/sso_organization.smtp.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										121
									
								
								playwright/tests/sso_organization.smtp.spec.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,121 @@ | ||||
| import { test, expect, type TestInfo } from '@playwright/test'; | ||||
| import { MailDev } from 'maildev'; | ||||
|  | ||||
| import * as utils from "../global-utils"; | ||||
| import * as orgs from './setups/orgs'; | ||||
| import { logNewUser, logUser } from './setups/sso'; | ||||
|  | ||||
| let users = utils.loadEnv(); | ||||
|  | ||||
| let mailServer, mail1Buffer, mail2Buffer, mail3Buffer; | ||||
|  | ||||
| test.beforeAll('Setup', async ({ browser }, testInfo: TestInfo) => { | ||||
|     mailServer = new MailDev({ | ||||
|         port: process.env.MAILDEV_SMTP_PORT, | ||||
|         web: { port: process.env.MAILDEV_HTTP_PORT }, | ||||
|     }) | ||||
|  | ||||
|     await mailServer.listen(); | ||||
|  | ||||
|     await utils.startVault(browser, testInfo, { | ||||
|         SMTP_HOST: process.env.MAILDEV_HOST, | ||||
|         SMTP_FROM: process.env.PW_SMTP_FROM, | ||||
|         SSO_ENABLED: true, | ||||
|         SSO_ONLY: true, | ||||
|     }); | ||||
|  | ||||
|     mail1Buffer = mailServer.buffer(users.user1.email); | ||||
|     mail2Buffer = mailServer.buffer(users.user2.email); | ||||
|     mail3Buffer = mailServer.buffer(users.user3.email); | ||||
| }); | ||||
|  | ||||
| test.afterAll('Teardown', async ({}) => { | ||||
|     utils.stopVault(); | ||||
|     [mail1Buffer, mail2Buffer, mail3Buffer, mailServer].map((m) => m?.close()); | ||||
| }); | ||||
|  | ||||
| test('Create user3', async ({ page }) => { | ||||
|     await logNewUser(test, page, users.user3, { mailBuffer: mail3Buffer }); | ||||
| }); | ||||
|  | ||||
| test('Invite users', async ({ page }) => { | ||||
|     await logNewUser(test, page, users.user1, { mailBuffer: mail1Buffer }); | ||||
|  | ||||
|     await orgs.create(test, page, '/Test'); | ||||
|     await orgs.members(test, page, '/Test'); | ||||
|     await orgs.invite(test, page, '/Test', users.user2.email); | ||||
|     await orgs.invite(test, page, '/Test', users.user3.email); | ||||
| }); | ||||
|  | ||||
| test('invited with new account', async ({ page }) => { | ||||
|     const link = await test.step('Extract email link', async () => { | ||||
|         const invited = await mail2Buffer.expect((m) => m.subject === "Join /Test"); | ||||
|         await page.setContent(invited.html); | ||||
|         return await page.getByTestId("invite").getAttribute("href"); | ||||
|     }); | ||||
|  | ||||
|     await test.step('Redirect to Keycloak', async () => { | ||||
|         await page.goto(link); | ||||
|     }); | ||||
|  | ||||
|     await test.step('Keycloak login', async () => { | ||||
|         await expect(page.getByRole('heading', { name: 'Sign in to your account' })).toBeVisible(); | ||||
|         await page.getByLabel(/Username/).fill(users.user2.name); | ||||
|         await page.getByLabel('Password', { exact: true }).fill(users.user2.password); | ||||
|         await page.getByRole('button', { name: 'Sign In' }).click(); | ||||
|     }); | ||||
|  | ||||
|     await test.step('Create Vault account', async () => { | ||||
|         await expect(page.getByRole('heading', { name: 'Join organisation' })).toBeVisible(); | ||||
|         await page.getByLabel('New master password (required)', { exact: true }).fill(users.user2.password); | ||||
|         await page.getByLabel('Confirm new master password (').fill(users.user2.password); | ||||
|         await page.getByRole('button', { name: 'Create account' }).click(); | ||||
|     }); | ||||
|  | ||||
|     await test.step('Default vault page', async () => { | ||||
|         await expect(page).toHaveTitle(/Vaultwarden Web/); | ||||
|  | ||||
|         await utils.checkNotification(page, 'Account successfully created!'); | ||||
|         await utils.checkNotification(page, 'Invitation accepted'); | ||||
|     }); | ||||
|  | ||||
|     await test.step('Check mails', async () => { | ||||
|         await mail2Buffer.expect((m) => m.subject.includes("New Device Logged")); | ||||
|         await mail1Buffer.expect((m) => m.subject === "Invitation to /Test accepted"); | ||||
|     }); | ||||
| }); | ||||
|  | ||||
| test('invited with existing account', async ({ page }) => { | ||||
|     const link = await test.step('Extract email link', async () => { | ||||
|         const invited = await mail3Buffer.expect((m) => m.subject === "Join /Test"); | ||||
|         await page.setContent(invited.html); | ||||
|         return await page.getByTestId("invite").getAttribute("href"); | ||||
|     }); | ||||
|  | ||||
|     await test.step('Redirect to Keycloak', async () => { | ||||
|         await page.goto(link); | ||||
|     }); | ||||
|  | ||||
|     await test.step('Keycloak login', async () => { | ||||
|         await expect(page.getByRole('heading', { name: 'Sign in to your account' })).toBeVisible(); | ||||
|         await page.getByLabel(/Username/).fill(users.user3.name); | ||||
|         await page.getByLabel('Password', { exact: true }).fill(users.user3.password); | ||||
|         await page.getByRole('button', { name: 'Sign In' }).click(); | ||||
|     }); | ||||
|  | ||||
|     await test.step('Unlock vault', async () => { | ||||
|         await expect(page).toHaveTitle('Vaultwarden Web'); | ||||
|         await page.getByLabel('Master password').fill(users.user3.password); | ||||
|         await page.getByRole('button', { name: 'Unlock' }).click(); | ||||
|     }); | ||||
|  | ||||
|     await test.step('Default vault page', async () => { | ||||
|         await expect(page).toHaveTitle(/Vaultwarden Web/); | ||||
|         await utils.checkNotification(page, 'Invitation accepted'); | ||||
|     }); | ||||
|  | ||||
|     await test.step('Check mails', async () => { | ||||
|         await mail3Buffer.expect((m) => m.subject.includes("New Device Logged")); | ||||
|         await mail1Buffer.expect((m) => m.subject === "Invitation to /Test accepted"); | ||||
|     }); | ||||
| }); | ||||
							
								
								
									
										76
									
								
								playwright/tests/sso_organization.spec.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								playwright/tests/sso_organization.spec.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,76 @@ | ||||
| import { test, expect, type TestInfo } from '@playwright/test'; | ||||
| import { MailDev } from 'maildev'; | ||||
|  | ||||
| import * as utils from "../global-utils"; | ||||
| import * as orgs from './setups/orgs'; | ||||
| import { logNewUser, logUser } from './setups/sso'; | ||||
|  | ||||
| let users = utils.loadEnv(); | ||||
|  | ||||
| test.beforeAll('Setup', async ({ browser }, testInfo: TestInfo) => { | ||||
|     await utils.startVault(browser, testInfo, { | ||||
|         SSO_ENABLED: true, | ||||
|         SSO_ONLY: true, | ||||
|     }); | ||||
| }); | ||||
|  | ||||
| test.afterAll('Teardown', async ({}) => { | ||||
|     utils.stopVault(); | ||||
| }); | ||||
|  | ||||
| test('Create user3', async ({ page }) => { | ||||
|     await logNewUser(test, page, users.user3); | ||||
| }); | ||||
|  | ||||
| test('Invite users', async ({ page }) => { | ||||
|     await logNewUser(test, page, users.user1); | ||||
|  | ||||
|     await orgs.create(test, page, '/Test'); | ||||
|     await orgs.members(test, page, '/Test'); | ||||
|     await orgs.invite(test, page, '/Test', users.user2.email); | ||||
|     await orgs.invite(test, page, '/Test', users.user3.email); | ||||
|     await orgs.confirm(test, page, '/Test', users.user3.email); | ||||
| }); | ||||
|  | ||||
| test('Create invited account', async ({ page }) => { | ||||
|     await logNewUser(test, page, users.user2); | ||||
| }); | ||||
|  | ||||
| test('Confirm invited user', async ({ page }) => { | ||||
|     await logUser(test, page, users.user1); | ||||
|     await orgs.members(test, page, '/Test'); | ||||
|     await expect(page.getByRole('row', { name: users.user2.name })).toHaveText(/Needs confirmation/); | ||||
|     await orgs.confirm(test, page, '/Test', users.user2.email); | ||||
| }); | ||||
|  | ||||
| test('Organization is visible', async ({ page }) => { | ||||
|     await logUser(test, page, users.user2); | ||||
|     await page.getByLabel('vault: /Test').click(); | ||||
|     await expect(page.getByLabel('Filter: Default collection')).toBeVisible(); | ||||
| }); | ||||
|  | ||||
| test('Enforce password policy', async ({ page }) => { | ||||
|     await logUser(test, page, users.user1); | ||||
|     await orgs.policies(test, page, '/Test'); | ||||
|  | ||||
|     await test.step(`Set master password policy`, async () => { | ||||
|         await page.getByRole('button', { name: 'Master password requirements' }).click(); | ||||
|         await page.getByRole('checkbox', { name: 'Turn on' }).check(); | ||||
|         await page.getByRole('checkbox', { name: 'Require existing members to' }).check(); | ||||
|         await page.getByRole('spinbutton', { name: 'Minimum length' }).fill('42'); | ||||
|         await page.getByRole('button', { name: 'Save' }).click(); | ||||
|         await utils.checkNotification(page, 'Edited policy Master password requirements.'); | ||||
|     }); | ||||
|  | ||||
|     await utils.logout(test, page, users.user1); | ||||
|  | ||||
|     await test.step(`Unlock trigger policy`, async () => { | ||||
|         await page.getByRole('textbox', { name: 'Email address (required)' }).fill(users.user1.email); | ||||
|         await page.getByRole('button', { name: 'Use single sign-on' }).click(); | ||||
|  | ||||
|         await page.getByRole('textbox', { name: 'Master password (required)' }).fill(users.user1.password); | ||||
|         await page.getByRole('button', { name: 'Unlock' }).click(); | ||||
|  | ||||
|         await expect(page.getByRole('heading', { name: 'Update master password' })).toBeVisible(); | ||||
|     }); | ||||
| }); | ||||
| @@ -1,4 +1,4 @@ | ||||
| [toolchain] | ||||
| channel = "1.87.0" | ||||
| channel = "1.89.0" | ||||
| components = [ "rustfmt", "clippy" ] | ||||
| profile = "minimal" | ||||
|   | ||||
| @@ -46,6 +46,7 @@ pub fn routes() -> Vec<Route> { | ||||
|         invite_user, | ||||
|         logout, | ||||
|         delete_user, | ||||
|         delete_sso_user, | ||||
|         deauth_user, | ||||
|         disable_user, | ||||
|         enable_user, | ||||
| @@ -239,6 +240,7 @@ struct AdminTemplateData { | ||||
|     page_data: Option<Value>, | ||||
|     logged_in: bool, | ||||
|     urlpath: String, | ||||
|     sso_enabled: bool, | ||||
| } | ||||
|  | ||||
| impl AdminTemplateData { | ||||
| @@ -248,6 +250,7 @@ impl AdminTemplateData { | ||||
|             page_data: Some(page_data), | ||||
|             logged_in: true, | ||||
|             urlpath: CONFIG.domain_path(), | ||||
|             sso_enabled: CONFIG.sso_enabled(), | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @@ -296,7 +299,7 @@ async fn invite_user(data: Json<InviteData>, _token: AdminToken, mut conn: DbCon | ||||
|         err_code!("User already exists", Status::Conflict.code) | ||||
|     } | ||||
|  | ||||
|     let mut user = User::new(data.email); | ||||
|     let mut user = User::new(data.email, None); | ||||
|  | ||||
|     async fn _generate_invite(user: &User, conn: &mut DbConn) -> EmptyResult { | ||||
|         if CONFIG.mail_enabled() { | ||||
| @@ -336,7 +339,7 @@ fn logout(cookies: &CookieJar<'_>) -> Redirect { | ||||
| async fn get_users_json(_token: AdminToken, mut conn: DbConn) -> Json<Value> { | ||||
|     let users = User::get_all(&mut conn).await; | ||||
|     let mut users_json = Vec::with_capacity(users.len()); | ||||
|     for u in users { | ||||
|     for (u, _) in users { | ||||
|         let mut usr = u.to_json(&mut conn).await; | ||||
|         usr["userEnabled"] = json!(u.enabled); | ||||
|         usr["createdAt"] = json!(format_naive_datetime_local(&u.created_at, DT_FMT)); | ||||
| @@ -354,7 +357,7 @@ async fn get_users_json(_token: AdminToken, mut conn: DbConn) -> Json<Value> { | ||||
| async fn users_overview(_token: AdminToken, mut conn: DbConn) -> ApiResult<Html<String>> { | ||||
|     let users = User::get_all(&mut conn).await; | ||||
|     let mut users_json = Vec::with_capacity(users.len()); | ||||
|     for u in users { | ||||
|     for (u, sso_u) in users { | ||||
|         let mut usr = u.to_json(&mut conn).await; | ||||
|         usr["cipher_count"] = json!(Cipher::count_owned_by_user(&u.uuid, &mut conn).await); | ||||
|         usr["attachment_count"] = json!(Attachment::count_by_user(&u.uuid, &mut conn).await); | ||||
| @@ -365,6 +368,9 @@ async fn users_overview(_token: AdminToken, mut conn: DbConn) -> ApiResult<Html< | ||||
|             Some(dt) => json!(format_naive_datetime_local(&dt, DT_FMT)), | ||||
|             None => json!("Never"), | ||||
|         }; | ||||
|  | ||||
|         usr["sso_identifier"] = json!(sso_u.map(|u| u.identifier.to_string()).unwrap_or(String::new())); | ||||
|  | ||||
|         users_json.push(usr); | ||||
|     } | ||||
|  | ||||
| @@ -417,6 +423,27 @@ async fn delete_user(user_id: UserId, token: AdminToken, mut conn: DbConn) -> Em | ||||
|     res | ||||
| } | ||||
|  | ||||
| #[delete("/users/<user_id>/sso", format = "application/json")] | ||||
| async fn delete_sso_user(user_id: UserId, token: AdminToken, mut conn: DbConn) -> EmptyResult { | ||||
|     let memberships = Membership::find_any_state_by_user(&user_id, &mut conn).await; | ||||
|     let res = SsoUser::delete(&user_id, &mut conn).await; | ||||
|  | ||||
|     for membership in memberships { | ||||
|         log_event( | ||||
|             EventType::OrganizationUserUnlinkedSso as i32, | ||||
|             &membership.uuid, | ||||
|             &membership.org_uuid, | ||||
|             &ACTING_ADMIN_USER.into(), | ||||
|             14, // Use UnknownBrowser type | ||||
|             &token.ip.ip, | ||||
|             &mut conn, | ||||
|         ) | ||||
|         .await; | ||||
|     } | ||||
|  | ||||
|     res | ||||
| } | ||||
|  | ||||
| #[post("/users/<user_id>/deauth", format = "application/json")] | ||||
| async fn deauth_user(user_id: UserId, _token: AdminToken, mut conn: DbConn, nt: Notify<'_>) -> EmptyResult { | ||||
|     let mut user = get_user_or_404(&user_id, &mut conn).await?; | ||||
| @@ -613,8 +640,9 @@ use cached::proc_macro::cached; | ||||
| /// Cache this function to prevent API call rate limit. Github only allows 60 requests per hour, and we use 3 here already | ||||
| /// It will cache this function for 600 seconds (10 minutes) which should prevent the exhaustion of the rate limit | ||||
| /// Any cache will be lost if Vaultwarden is restarted | ||||
| use std::time::Duration; // Needed for cached | ||||
| #[cached(time = 600, sync_writes = "default")] | ||||
| async fn get_release_info(has_http_access: bool, running_within_container: bool) -> (String, String, String) { | ||||
| async fn get_release_info(has_http_access: bool) -> (String, String, String) { | ||||
|     // If the HTTP Check failed, do not even attempt to check for new versions since we were not able to connect with github.com anyway. | ||||
|     if has_http_access { | ||||
|         ( | ||||
| @@ -633,17 +661,11 @@ async fn get_release_info(has_http_access: bool, running_within_container: bool) | ||||
|             }, | ||||
|             // Do not fetch the web-vault version when running within a container | ||||
|             // The web-vault version is embedded within the container it self, and should not be updated manually | ||||
|             if running_within_container { | ||||
|                 "-".to_string() | ||||
|             } else { | ||||
|                 match get_json_api::<GitRelease>( | ||||
|                     "https://api.github.com/repos/dani-garcia/bw_web_builds/releases/latest", | ||||
|                 ) | ||||
|             match get_json_api::<GitRelease>("https://api.github.com/repos/dani-garcia/bw_web_builds/releases/latest") | ||||
|                 .await | ||||
|                 { | ||||
|                     Ok(r) => r.tag_name.trim_start_matches('v').to_string(), | ||||
|                     _ => "-".to_string(), | ||||
|                 } | ||||
|             { | ||||
|                 Ok(r) => r.tag_name.trim_start_matches('v').to_string(), | ||||
|                 _ => "-".to_string(), | ||||
|             }, | ||||
|         ) | ||||
|     } else { | ||||
| @@ -689,8 +711,7 @@ async fn diagnostics(_token: AdminToken, ip_header: IpHeader, mut conn: DbConn) | ||||
|         _ => "Unable to resolve domain name.".to_string(), | ||||
|     }; | ||||
|  | ||||
|     let (latest_release, latest_commit, latest_web_build) = | ||||
|         get_release_info(has_http_access, running_within_container).await; | ||||
|     let (latest_release, latest_commit, latest_web_build) = get_release_info(has_http_access).await; | ||||
|  | ||||
|     let ip_header_name = &ip_header.0.unwrap_or_default(); | ||||
|  | ||||
| @@ -753,17 +774,17 @@ fn get_diagnostics_http(code: u16, _token: AdminToken) -> EmptyResult { | ||||
| } | ||||
|  | ||||
| #[post("/config", format = "application/json", data = "<data>")] | ||||
| fn post_config(data: Json<ConfigBuilder>, _token: AdminToken) -> EmptyResult { | ||||
| async fn post_config(data: Json<ConfigBuilder>, _token: AdminToken) -> EmptyResult { | ||||
|     let data: ConfigBuilder = data.into_inner(); | ||||
|     if let Err(e) = CONFIG.update_config(data, true) { | ||||
|     if let Err(e) = CONFIG.update_config(data, true).await { | ||||
|         err!(format!("Unable to save config: {e:?}")) | ||||
|     } | ||||
|     Ok(()) | ||||
| } | ||||
|  | ||||
| #[post("/config/delete", format = "application/json")] | ||||
| fn delete_config(_token: AdminToken) -> EmptyResult { | ||||
|     if let Err(e) = CONFIG.delete_user_config() { | ||||
| async fn delete_config(_token: AdminToken) -> EmptyResult { | ||||
|     if let Err(e) = CONFIG.delete_user_config().await { | ||||
|         err!(format!("Unable to delete config: {e:?}")) | ||||
|     } | ||||
|     Ok(()) | ||||
|   | ||||
| @@ -7,9 +7,9 @@ use serde_json::Value; | ||||
|  | ||||
| use crate::{ | ||||
|     api::{ | ||||
|         core::{log_user_event, two_factor::email}, | ||||
|         register_push_device, unregister_push_device, AnonymousNotify, EmptyResult, JsonResult, Notify, | ||||
|         PasswordOrOtpData, UpdateType, | ||||
|         core::{accept_org_invite, log_user_event, two_factor::email}, | ||||
|         master_password_policy, register_push_device, unregister_push_device, AnonymousNotify, ApiResult, EmptyResult, | ||||
|         JsonResult, Notify, PasswordOrOtpData, UpdateType, | ||||
|     }, | ||||
|     auth::{decode_delete, decode_invite, decode_verify_email, ClientHeaders, Headers}, | ||||
|     crypto, | ||||
| @@ -34,6 +34,7 @@ pub fn routes() -> Vec<rocket::Route> { | ||||
|         get_public_keys, | ||||
|         post_keys, | ||||
|         post_password, | ||||
|         post_set_password, | ||||
|         post_kdf, | ||||
|         post_rotatekey, | ||||
|         post_sstamp, | ||||
| @@ -66,15 +67,22 @@ pub fn routes() -> Vec<rocket::Route> { | ||||
|     ] | ||||
| } | ||||
|  | ||||
| #[derive(Debug, Deserialize)] | ||||
| #[serde(rename_all = "camelCase")] | ||||
| pub struct KDFData { | ||||
|     kdf: i32, | ||||
|     kdf_iterations: i32, | ||||
|     kdf_memory: Option<i32>, | ||||
|     kdf_parallelism: Option<i32>, | ||||
| } | ||||
|  | ||||
| #[derive(Debug, Deserialize)] | ||||
| #[serde(rename_all = "camelCase")] | ||||
| pub struct RegisterData { | ||||
|     email: String, | ||||
|  | ||||
|     kdf: Option<i32>, | ||||
|     kdf_iterations: Option<i32>, | ||||
|     kdf_memory: Option<i32>, | ||||
|     kdf_parallelism: Option<i32>, | ||||
|     #[serde(flatten)] | ||||
|     kdf: KDFData, | ||||
|  | ||||
|     #[serde(alias = "userSymmetricKey")] | ||||
|     key: String, | ||||
| @@ -97,6 +105,19 @@ pub struct RegisterData { | ||||
|     org_invite_token: Option<String>, | ||||
| } | ||||
|  | ||||
| #[derive(Debug, Deserialize)] | ||||
| #[serde(rename_all = "camelCase")] | ||||
| pub struct SetPasswordData { | ||||
|     #[serde(flatten)] | ||||
|     kdf: KDFData, | ||||
|  | ||||
|     key: String, | ||||
|     keys: Option<KeysData>, | ||||
|     master_password_hash: String, | ||||
|     master_password_hint: Option<String>, | ||||
|     org_identifier: Option<String>, | ||||
| } | ||||
|  | ||||
| #[derive(Debug, Deserialize)] | ||||
| #[serde(rename_all = "camelCase")] | ||||
| struct KeysData { | ||||
| @@ -237,10 +258,7 @@ pub async fn _register(data: Json<RegisterData>, email_verification: bool, mut c | ||||
|                     err!("Registration email does not match invite email") | ||||
|                 } | ||||
|             } else if Invitation::take(&email, &mut conn).await { | ||||
|                 for membership in Membership::find_invited_by_user(&user.uuid, &mut conn).await.iter_mut() { | ||||
|                     membership.status = MembershipStatus::Accepted as i32; | ||||
|                     membership.save(&mut conn).await?; | ||||
|                 } | ||||
|                 Membership::accept_user_invitations(&user.uuid, &mut conn).await?; | ||||
|                 user | ||||
|             } else if CONFIG.is_signup_allowed(&email) | ||||
|                 || (CONFIG.emergency_access_allowed() | ||||
| @@ -259,7 +277,7 @@ pub async fn _register(data: Json<RegisterData>, email_verification: bool, mut c | ||||
|                 || CONFIG.is_signup_allowed(&email) | ||||
|                 || pending_emergency_access.is_some() | ||||
|             { | ||||
|                 User::new(email.clone()) | ||||
|                 User::new(email.clone(), None) | ||||
|             } else { | ||||
|                 err!("Registration not allowed or user already exists") | ||||
|             } | ||||
| @@ -269,16 +287,7 @@ pub async fn _register(data: Json<RegisterData>, email_verification: bool, mut c | ||||
|     // Make sure we don't leave a lingering invitation. | ||||
|     Invitation::take(&email, &mut conn).await; | ||||
|  | ||||
|     if let Some(client_kdf_type) = data.kdf { | ||||
|         user.client_kdf_type = client_kdf_type; | ||||
|     } | ||||
|  | ||||
|     if let Some(client_kdf_iter) = data.kdf_iterations { | ||||
|         user.client_kdf_iter = client_kdf_iter; | ||||
|     } | ||||
|  | ||||
|     user.client_kdf_memory = data.kdf_memory; | ||||
|     user.client_kdf_parallelism = data.kdf_parallelism; | ||||
|     set_kdf_data(&mut user, data.kdf)?; | ||||
|  | ||||
|     user.set_password(&data.master_password_hash, Some(data.key), true, None); | ||||
|     user.password_hint = password_hint; | ||||
| @@ -327,6 +336,68 @@ pub async fn _register(data: Json<RegisterData>, email_verification: bool, mut c | ||||
|     }))) | ||||
| } | ||||
|  | ||||
| #[post("/accounts/set-password", data = "<data>")] | ||||
| async fn post_set_password(data: Json<SetPasswordData>, headers: Headers, mut conn: DbConn) -> JsonResult { | ||||
|     let data: SetPasswordData = data.into_inner(); | ||||
|     let mut user = headers.user; | ||||
|  | ||||
|     if user.private_key.is_some() { | ||||
|         err!("Account already initialized, cannot set password") | ||||
|     } | ||||
|  | ||||
|     // Check against the password hint setting here so if it fails, | ||||
|     // the user can retry without losing their invitation below. | ||||
|     let password_hint = clean_password_hint(&data.master_password_hint); | ||||
|     enforce_password_hint_setting(&password_hint)?; | ||||
|  | ||||
|     set_kdf_data(&mut user, data.kdf)?; | ||||
|  | ||||
|     user.set_password( | ||||
|         &data.master_password_hash, | ||||
|         Some(data.key), | ||||
|         false, | ||||
|         Some(vec![String::from("revision_date")]), // We need to allow revision-date to use the old security_timestamp | ||||
|     ); | ||||
|     user.password_hint = password_hint; | ||||
|  | ||||
|     if let Some(keys) = data.keys { | ||||
|         user.private_key = Some(keys.encrypted_private_key); | ||||
|         user.public_key = Some(keys.public_key); | ||||
|     } | ||||
|  | ||||
|     if let Some(identifier) = data.org_identifier { | ||||
|         if identifier != crate::sso::FAKE_IDENTIFIER { | ||||
|             let org = match Organization::find_by_name(&identifier, &mut conn).await { | ||||
|                 None => err!("Failed to retrieve the associated organization"), | ||||
|                 Some(org) => org, | ||||
|             }; | ||||
|  | ||||
|             let membership = match Membership::find_by_user_and_org(&user.uuid, &org.uuid, &mut conn).await { | ||||
|                 None => err!("Failed to retrieve the invitation"), | ||||
|                 Some(org) => org, | ||||
|             }; | ||||
|  | ||||
|             accept_org_invite(&user, membership, None, &mut conn).await?; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     if CONFIG.mail_enabled() { | ||||
|         mail::send_welcome(&user.email.to_lowercase()).await?; | ||||
|     } else { | ||||
|         Membership::accept_user_invitations(&user.uuid, &mut conn).await?; | ||||
|     } | ||||
|  | ||||
|     log_user_event(EventType::UserChangedPassword as i32, &user.uuid, headers.device.atype, &headers.ip.ip, &mut conn) | ||||
|         .await; | ||||
|  | ||||
|     user.save(&mut conn).await?; | ||||
|  | ||||
|     Ok(Json(json!({ | ||||
|       "Object": "set-password", | ||||
|       "CaptchaBypassToken": "", | ||||
|     }))) | ||||
| } | ||||
|  | ||||
| #[get("/accounts/profile")] | ||||
| async fn profile(headers: Headers, mut conn: DbConn) -> Json<Value> { | ||||
|     Json(headers.user.to_json(&mut conn).await) | ||||
| @@ -469,25 +540,15 @@ async fn post_password(data: Json<ChangePassData>, headers: Headers, mut conn: D | ||||
| #[derive(Deserialize)] | ||||
| #[serde(rename_all = "camelCase")] | ||||
| struct ChangeKdfData { | ||||
|     kdf: i32, | ||||
|     kdf_iterations: i32, | ||||
|     kdf_memory: Option<i32>, | ||||
|     kdf_parallelism: Option<i32>, | ||||
|     #[serde(flatten)] | ||||
|     kdf: KDFData, | ||||
|  | ||||
|     master_password_hash: String, | ||||
|     new_master_password_hash: String, | ||||
|     key: String, | ||||
| } | ||||
|  | ||||
| #[post("/accounts/kdf", data = "<data>")] | ||||
| async fn post_kdf(data: Json<ChangeKdfData>, headers: Headers, mut conn: DbConn, nt: Notify<'_>) -> EmptyResult { | ||||
|     let data: ChangeKdfData = data.into_inner(); | ||||
|     let mut user = headers.user; | ||||
|  | ||||
|     if !user.check_valid_password(&data.master_password_hash) { | ||||
|         err!("Invalid password") | ||||
|     } | ||||
|  | ||||
| fn set_kdf_data(user: &mut User, data: KDFData) -> EmptyResult { | ||||
|     if data.kdf == UserKdfType::Pbkdf2 as i32 && data.kdf_iterations < 100_000 { | ||||
|         err!("PBKDF2 KDF iterations must be at least 100000.") | ||||
|     } | ||||
| @@ -518,6 +579,21 @@ async fn post_kdf(data: Json<ChangeKdfData>, headers: Headers, mut conn: DbConn, | ||||
|     } | ||||
|     user.client_kdf_iter = data.kdf_iterations; | ||||
|     user.client_kdf_type = data.kdf; | ||||
|  | ||||
|     Ok(()) | ||||
| } | ||||
|  | ||||
| #[post("/accounts/kdf", data = "<data>")] | ||||
| async fn post_kdf(data: Json<ChangeKdfData>, headers: Headers, mut conn: DbConn, nt: Notify<'_>) -> EmptyResult { | ||||
|     let data: ChangeKdfData = data.into_inner(); | ||||
|     let mut user = headers.user; | ||||
|  | ||||
|     if !user.check_valid_password(&data.master_password_hash) { | ||||
|         err!("Invalid password") | ||||
|     } | ||||
|  | ||||
|     set_kdf_data(&mut user, data.kdf)?; | ||||
|  | ||||
|     user.set_password(&data.new_master_password_hash, Some(data.key), true, None); | ||||
|     let save_result = user.save(&mut conn).await; | ||||
|  | ||||
| @@ -556,14 +632,45 @@ use super::sends::{update_send_from_data, SendData}; | ||||
| #[derive(Deserialize)] | ||||
| #[serde(rename_all = "camelCase")] | ||||
| struct KeyData { | ||||
|     account_unlock_data: RotateAccountUnlockData, | ||||
|     account_keys: RotateAccountKeys, | ||||
|     account_data: RotateAccountData, | ||||
|     old_master_key_authentication_hash: String, | ||||
| } | ||||
|  | ||||
| #[derive(Deserialize)] | ||||
| #[serde(rename_all = "camelCase")] | ||||
| struct RotateAccountUnlockData { | ||||
|     emergency_access_unlock_data: Vec<UpdateEmergencyAccessData>, | ||||
|     master_password_unlock_data: MasterPasswordUnlockData, | ||||
|     organization_account_recovery_unlock_data: Vec<UpdateResetPasswordData>, | ||||
| } | ||||
|  | ||||
| #[derive(Deserialize)] | ||||
| #[serde(rename_all = "camelCase")] | ||||
| struct MasterPasswordUnlockData { | ||||
|     kdf_type: i32, | ||||
|     kdf_iterations: i32, | ||||
|     kdf_parallelism: Option<i32>, | ||||
|     kdf_memory: Option<i32>, | ||||
|     email: String, | ||||
|     master_key_authentication_hash: String, | ||||
|     master_key_encrypted_user_key: String, | ||||
| } | ||||
|  | ||||
| #[derive(Deserialize)] | ||||
| #[serde(rename_all = "camelCase")] | ||||
| struct RotateAccountKeys { | ||||
|     user_key_encrypted_account_private_key: String, | ||||
|     account_public_key: String, | ||||
| } | ||||
|  | ||||
| #[derive(Deserialize)] | ||||
| #[serde(rename_all = "camelCase")] | ||||
| struct RotateAccountData { | ||||
|     ciphers: Vec<CipherData>, | ||||
|     folders: Vec<UpdateFolderData>, | ||||
|     sends: Vec<SendData>, | ||||
|     emergency_access_keys: Vec<UpdateEmergencyAccessData>, | ||||
|     reset_password_keys: Vec<UpdateResetPasswordData>, | ||||
|     key: String, | ||||
|     master_password_hash: String, | ||||
|     private_key: String, | ||||
| } | ||||
|  | ||||
| fn validate_keydata( | ||||
| @@ -573,10 +680,24 @@ fn validate_keydata( | ||||
|     existing_emergency_access: &[EmergencyAccess], | ||||
|     existing_memberships: &[Membership], | ||||
|     existing_sends: &[Send], | ||||
|     user: &User, | ||||
| ) -> EmptyResult { | ||||
|     if user.client_kdf_type != data.account_unlock_data.master_password_unlock_data.kdf_type | ||||
|         || user.client_kdf_iter != data.account_unlock_data.master_password_unlock_data.kdf_iterations | ||||
|         || user.client_kdf_memory != data.account_unlock_data.master_password_unlock_data.kdf_memory | ||||
|         || user.client_kdf_parallelism != data.account_unlock_data.master_password_unlock_data.kdf_parallelism | ||||
|         || user.email != data.account_unlock_data.master_password_unlock_data.email | ||||
|     { | ||||
|         err!("Changing the kdf variant or email is not supported during key rotation"); | ||||
|     } | ||||
|     if user.public_key.as_ref() != Some(&data.account_keys.account_public_key) { | ||||
|         err!("Changing the asymmetric keypair is not possible during key rotation") | ||||
|     } | ||||
|  | ||||
|     // Check that we're correctly rotating all the user's ciphers | ||||
|     let existing_cipher_ids = existing_ciphers.iter().map(|c| &c.uuid).collect::<HashSet<&CipherId>>(); | ||||
|     let provided_cipher_ids = data | ||||
|         .account_data | ||||
|         .ciphers | ||||
|         .iter() | ||||
|         .filter(|c| c.organization_id.is_none()) | ||||
| @@ -588,7 +709,8 @@ fn validate_keydata( | ||||
|  | ||||
|     // Check that we're correctly rotating all the user's folders | ||||
|     let existing_folder_ids = existing_folders.iter().map(|f| &f.uuid).collect::<HashSet<&FolderId>>(); | ||||
|     let provided_folder_ids = data.folders.iter().filter_map(|f| f.id.as_ref()).collect::<HashSet<&FolderId>>(); | ||||
|     let provided_folder_ids = | ||||
|         data.account_data.folders.iter().filter_map(|f| f.id.as_ref()).collect::<HashSet<&FolderId>>(); | ||||
|     if !provided_folder_ids.is_superset(&existing_folder_ids) { | ||||
|         err!("All existing folders must be included in the rotation") | ||||
|     } | ||||
| @@ -596,8 +718,12 @@ fn validate_keydata( | ||||
|     // Check that we're correctly rotating all the user's emergency access keys | ||||
|     let existing_emergency_access_ids = | ||||
|         existing_emergency_access.iter().map(|ea| &ea.uuid).collect::<HashSet<&EmergencyAccessId>>(); | ||||
|     let provided_emergency_access_ids = | ||||
|         data.emergency_access_keys.iter().map(|ea| &ea.id).collect::<HashSet<&EmergencyAccessId>>(); | ||||
|     let provided_emergency_access_ids = data | ||||
|         .account_unlock_data | ||||
|         .emergency_access_unlock_data | ||||
|         .iter() | ||||
|         .map(|ea| &ea.id) | ||||
|         .collect::<HashSet<&EmergencyAccessId>>(); | ||||
|     if !provided_emergency_access_ids.is_superset(&existing_emergency_access_ids) { | ||||
|         err!("All existing emergency access keys must be included in the rotation") | ||||
|     } | ||||
| @@ -605,15 +731,19 @@ fn validate_keydata( | ||||
|     // Check that we're correctly rotating all the user's reset password keys | ||||
|     let existing_reset_password_ids = | ||||
|         existing_memberships.iter().map(|m| &m.org_uuid).collect::<HashSet<&OrganizationId>>(); | ||||
|     let provided_reset_password_ids = | ||||
|         data.reset_password_keys.iter().map(|rp| &rp.organization_id).collect::<HashSet<&OrganizationId>>(); | ||||
|     let provided_reset_password_ids = data | ||||
|         .account_unlock_data | ||||
|         .organization_account_recovery_unlock_data | ||||
|         .iter() | ||||
|         .map(|rp| &rp.organization_id) | ||||
|         .collect::<HashSet<&OrganizationId>>(); | ||||
|     if !provided_reset_password_ids.is_superset(&existing_reset_password_ids) { | ||||
|         err!("All existing reset password keys must be included in the rotation") | ||||
|     } | ||||
|  | ||||
|     // Check that we're correctly rotating all the user's sends | ||||
|     let existing_send_ids = existing_sends.iter().map(|s| &s.uuid).collect::<HashSet<&SendId>>(); | ||||
|     let provided_send_ids = data.sends.iter().filter_map(|s| s.id.as_ref()).collect::<HashSet<&SendId>>(); | ||||
|     let provided_send_ids = data.account_data.sends.iter().filter_map(|s| s.id.as_ref()).collect::<HashSet<&SendId>>(); | ||||
|     if !provided_send_ids.is_superset(&existing_send_ids) { | ||||
|         err!("All existing sends must be included in the rotation") | ||||
|     } | ||||
| @@ -621,12 +751,12 @@ fn validate_keydata( | ||||
|     Ok(()) | ||||
| } | ||||
|  | ||||
| #[post("/accounts/key", data = "<data>")] | ||||
| #[post("/accounts/key-management/rotate-user-account-keys", data = "<data>")] | ||||
| async fn post_rotatekey(data: Json<KeyData>, headers: Headers, mut conn: DbConn, nt: Notify<'_>) -> EmptyResult { | ||||
|     // TODO: See if we can wrap everything within a SQL Transaction. If something fails it should revert everything. | ||||
|     let data: KeyData = data.into_inner(); | ||||
|  | ||||
|     if !headers.user.check_valid_password(&data.master_password_hash) { | ||||
|     if !headers.user.check_valid_password(&data.old_master_key_authentication_hash) { | ||||
|         err!("Invalid password") | ||||
|     } | ||||
|  | ||||
| @@ -634,7 +764,7 @@ async fn post_rotatekey(data: Json<KeyData>, headers: Headers, mut conn: DbConn, | ||||
|     // Bitwarden does not process the import if there is one item invalid. | ||||
|     // Since we check for the size of the encrypted note length, we need to do that here to pre-validate it. | ||||
|     // TODO: See if we can optimize the whole cipher adding/importing and prevent duplicate code and checks. | ||||
|     Cipher::validate_cipher_data(&data.ciphers)?; | ||||
|     Cipher::validate_cipher_data(&data.account_data.ciphers)?; | ||||
|  | ||||
|     let user_id = &headers.user.uuid; | ||||
|  | ||||
| @@ -655,10 +785,11 @@ async fn post_rotatekey(data: Json<KeyData>, headers: Headers, mut conn: DbConn, | ||||
|         &existing_emergency_access, | ||||
|         &existing_memberships, | ||||
|         &existing_sends, | ||||
|         &headers.user, | ||||
|     )?; | ||||
|  | ||||
|     // Update folder data | ||||
|     for folder_data in data.folders { | ||||
|     for folder_data in data.account_data.folders { | ||||
|         // Skip `null` folder id entries. | ||||
|         // See: https://github.com/bitwarden/clients/issues/8453 | ||||
|         if let Some(folder_id) = folder_data.id { | ||||
| @@ -672,7 +803,7 @@ async fn post_rotatekey(data: Json<KeyData>, headers: Headers, mut conn: DbConn, | ||||
|     } | ||||
|  | ||||
|     // Update emergency access data | ||||
|     for emergency_access_data in data.emergency_access_keys { | ||||
|     for emergency_access_data in data.account_unlock_data.emergency_access_unlock_data { | ||||
|         let Some(saved_emergency_access) = | ||||
|             existing_emergency_access.iter_mut().find(|ea| ea.uuid == emergency_access_data.id) | ||||
|         else { | ||||
| @@ -684,7 +815,7 @@ async fn post_rotatekey(data: Json<KeyData>, headers: Headers, mut conn: DbConn, | ||||
|     } | ||||
|  | ||||
|     // Update reset password data | ||||
|     for reset_password_data in data.reset_password_keys { | ||||
|     for reset_password_data in data.account_unlock_data.organization_account_recovery_unlock_data { | ||||
|         let Some(membership) = | ||||
|             existing_memberships.iter_mut().find(|m| m.org_uuid == reset_password_data.organization_id) | ||||
|         else { | ||||
| @@ -696,7 +827,7 @@ async fn post_rotatekey(data: Json<KeyData>, headers: Headers, mut conn: DbConn, | ||||
|     } | ||||
|  | ||||
|     // Update send data | ||||
|     for send_data in data.sends { | ||||
|     for send_data in data.account_data.sends { | ||||
|         let Some(send) = existing_sends.iter_mut().find(|s| &s.uuid == send_data.id.as_ref().unwrap()) else { | ||||
|             err!("Send doesn't exist") | ||||
|         }; | ||||
| @@ -707,7 +838,7 @@ async fn post_rotatekey(data: Json<KeyData>, headers: Headers, mut conn: DbConn, | ||||
|     // Update cipher data | ||||
|     use super::ciphers::update_cipher_from_data; | ||||
|  | ||||
|     for cipher_data in data.ciphers { | ||||
|     for cipher_data in data.account_data.ciphers { | ||||
|         if cipher_data.organization_id.is_none() { | ||||
|             let Some(saved_cipher) = existing_ciphers.iter_mut().find(|c| &c.uuid == cipher_data.id.as_ref().unwrap()) | ||||
|             else { | ||||
| @@ -724,9 +855,13 @@ async fn post_rotatekey(data: Json<KeyData>, headers: Headers, mut conn: DbConn, | ||||
|     // Update user data | ||||
|     let mut user = headers.user; | ||||
|  | ||||
|     user.akey = data.key; | ||||
|     user.private_key = Some(data.private_key); | ||||
|     user.reset_security_stamp(); | ||||
|     user.private_key = Some(data.account_keys.user_key_encrypted_account_private_key); | ||||
|     user.set_password( | ||||
|         &data.account_unlock_data.master_password_unlock_data.master_key_authentication_hash, | ||||
|         Some(data.account_unlock_data.master_password_unlock_data.master_key_encrypted_user_key), | ||||
|         true, | ||||
|         None, | ||||
|     ); | ||||
|  | ||||
|     let save_result = user.save(&mut conn).await; | ||||
|  | ||||
| @@ -1067,16 +1202,31 @@ struct SecretVerificationRequest { | ||||
|     master_password_hash: String, | ||||
| } | ||||
|  | ||||
| // Change the KDF Iterations if necessary | ||||
| pub async fn kdf_upgrade(user: &mut User, pwd_hash: &str, conn: &mut DbConn) -> ApiResult<()> { | ||||
|     if user.password_iterations < CONFIG.password_iterations() { | ||||
|         user.password_iterations = CONFIG.password_iterations(); | ||||
|         user.set_password(pwd_hash, None, false, None); | ||||
|  | ||||
|         if let Err(e) = user.save(conn).await { | ||||
|             error!("Error updating user: {e:#?}"); | ||||
|         } | ||||
|     } | ||||
|     Ok(()) | ||||
| } | ||||
|  | ||||
| #[post("/accounts/verify-password", data = "<data>")] | ||||
| fn verify_password(data: Json<SecretVerificationRequest>, headers: Headers) -> EmptyResult { | ||||
| async fn verify_password(data: Json<SecretVerificationRequest>, headers: Headers, mut conn: DbConn) -> JsonResult { | ||||
|     let data: SecretVerificationRequest = data.into_inner(); | ||||
|     let user = headers.user; | ||||
|     let mut user = headers.user; | ||||
|  | ||||
|     if !user.check_valid_password(&data.master_password_hash) { | ||||
|         err!("Invalid password") | ||||
|     } | ||||
|  | ||||
|     Ok(()) | ||||
|     kdf_upgrade(&mut user, &data.master_password_hash, &mut conn).await?; | ||||
|  | ||||
|     Ok(Json(master_password_policy(&user, &conn).await)) | ||||
| } | ||||
|  | ||||
| async fn _api_key(data: Json<PasswordOrOtpData>, rotate: bool, headers: Headers, mut conn: DbConn) -> JsonResult { | ||||
|   | ||||
| @@ -11,10 +11,11 @@ use rocket::{ | ||||
| use serde_json::Value; | ||||
|  | ||||
| use crate::auth::ClientVersion; | ||||
| use crate::util::NumberOrString; | ||||
| use crate::util::{save_temp_file, NumberOrString}; | ||||
| use crate::{ | ||||
|     api::{self, core::log_event, EmptyResult, JsonResult, Notify, PasswordOrOtpData, UpdateType}, | ||||
|     auth::Headers, | ||||
|     config::PathType, | ||||
|     crypto, | ||||
|     db::{models::*, DbConn, DbPool}, | ||||
|     CONFIG, | ||||
| @@ -77,6 +78,7 @@ pub fn routes() -> Vec<Route> { | ||||
|         restore_cipher_put, | ||||
|         restore_cipher_put_admin, | ||||
|         restore_cipher_selected, | ||||
|         restore_cipher_selected_admin, | ||||
|         delete_all, | ||||
|         move_cipher_selected, | ||||
|         move_cipher_selected_put, | ||||
| @@ -105,12 +107,7 @@ struct SyncData { | ||||
| } | ||||
|  | ||||
| #[get("/sync?<data..>")] | ||||
| async fn sync( | ||||
|     data: SyncData, | ||||
|     headers: Headers, | ||||
|     client_version: Option<ClientVersion>, | ||||
|     mut conn: DbConn, | ||||
| ) -> Json<Value> { | ||||
| async fn sync(data: SyncData, headers: Headers, client_version: Option<ClientVersion>, mut conn: DbConn) -> JsonResult { | ||||
|     let user_json = headers.user.to_json(&mut conn).await; | ||||
|  | ||||
|     // Get all ciphers which are visible by the user | ||||
| @@ -134,7 +131,7 @@ async fn sync( | ||||
|     for c in ciphers { | ||||
|         ciphers_json.push( | ||||
|             c.to_json(&headers.host, &headers.user.uuid, Some(&cipher_sync_data), CipherSyncType::User, &mut conn) | ||||
|                 .await, | ||||
|                 .await?, | ||||
|         ); | ||||
|     } | ||||
|  | ||||
| @@ -159,7 +156,7 @@ async fn sync( | ||||
|         api::core::_get_eq_domains(headers, true).into_inner() | ||||
|     }; | ||||
|  | ||||
|     Json(json!({ | ||||
|     Ok(Json(json!({ | ||||
|         "profile": user_json, | ||||
|         "folders": folders_json, | ||||
|         "collections": collections_json, | ||||
| @@ -168,11 +165,11 @@ async fn sync( | ||||
|         "domains": domains_json, | ||||
|         "sends": sends_json, | ||||
|         "object": "sync" | ||||
|     })) | ||||
|     }))) | ||||
| } | ||||
|  | ||||
| #[get("/ciphers")] | ||||
| async fn get_ciphers(headers: Headers, mut conn: DbConn) -> Json<Value> { | ||||
| async fn get_ciphers(headers: Headers, mut conn: DbConn) -> JsonResult { | ||||
|     let ciphers = Cipher::find_by_user_visible(&headers.user.uuid, &mut conn).await; | ||||
|     let cipher_sync_data = CipherSyncData::new(&headers.user.uuid, CipherSyncType::User, &mut conn).await; | ||||
|  | ||||
| @@ -180,15 +177,15 @@ async fn get_ciphers(headers: Headers, mut conn: DbConn) -> Json<Value> { | ||||
|     for c in ciphers { | ||||
|         ciphers_json.push( | ||||
|             c.to_json(&headers.host, &headers.user.uuid, Some(&cipher_sync_data), CipherSyncType::User, &mut conn) | ||||
|                 .await, | ||||
|                 .await?, | ||||
|         ); | ||||
|     } | ||||
|  | ||||
|     Json(json!({ | ||||
|     Ok(Json(json!({ | ||||
|       "data": ciphers_json, | ||||
|       "object": "list", | ||||
|       "continuationToken": null | ||||
|     })) | ||||
|     }))) | ||||
| } | ||||
|  | ||||
| #[get("/ciphers/<cipher_id>")] | ||||
| @@ -201,7 +198,7 @@ async fn get_cipher(cipher_id: CipherId, headers: Headers, mut conn: DbConn) -> | ||||
|         err!("Cipher is not owned by user") | ||||
|     } | ||||
|  | ||||
|     Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, &mut conn).await)) | ||||
|     Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, &mut conn).await?)) | ||||
| } | ||||
|  | ||||
| #[get("/ciphers/<cipher_id>/admin")] | ||||
| @@ -322,7 +319,7 @@ async fn post_ciphers_create( | ||||
|     // or otherwise), we can just ignore this field entirely. | ||||
|     data.cipher.last_known_revision_date = None; | ||||
|  | ||||
|     share_cipher_by_uuid(&cipher.uuid, data, &headers, &mut conn, &nt).await | ||||
|     share_cipher_by_uuid(&cipher.uuid, data, &headers, &mut conn, &nt, None).await | ||||
| } | ||||
|  | ||||
| /// Called when creating a new user-owned cipher. | ||||
| @@ -339,7 +336,7 @@ async fn post_ciphers(data: Json<CipherData>, headers: Headers, mut conn: DbConn | ||||
|     let mut cipher = Cipher::new(data.r#type, data.name.clone()); | ||||
|     update_cipher_from_data(&mut cipher, data, &headers, None, &mut conn, &nt, UpdateType::SyncCipherCreate).await?; | ||||
|  | ||||
|     Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, &mut conn).await)) | ||||
|     Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, &mut conn).await?)) | ||||
| } | ||||
|  | ||||
| /// Enforces the personal ownership policy on user-owned ciphers, if applicable. | ||||
| @@ -676,7 +673,7 @@ async fn put_cipher( | ||||
|  | ||||
|     update_cipher_from_data(&mut cipher, data, &headers, None, &mut conn, &nt, UpdateType::SyncCipherUpdate).await?; | ||||
|  | ||||
|     Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, &mut conn).await)) | ||||
|     Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, &mut conn).await?)) | ||||
| } | ||||
|  | ||||
| #[post("/ciphers/<cipher_id>/partial", data = "<data>")] | ||||
| @@ -714,7 +711,7 @@ async fn put_cipher_partial( | ||||
|     // Update favorite | ||||
|     cipher.set_favorite(Some(data.favorite), &headers.user.uuid, &mut conn).await?; | ||||
|  | ||||
|     Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, &mut conn).await)) | ||||
|     Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, &mut conn).await?)) | ||||
| } | ||||
|  | ||||
| #[derive(Deserialize)] | ||||
| @@ -825,7 +822,7 @@ async fn post_collections_update( | ||||
|     ) | ||||
|     .await; | ||||
|  | ||||
|     Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, &mut conn).await)) | ||||
|     Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, &mut conn).await?)) | ||||
| } | ||||
|  | ||||
| #[put("/ciphers/<cipher_id>/collections-admin", data = "<data>")] | ||||
| @@ -924,7 +921,7 @@ async fn post_cipher_share( | ||||
| ) -> JsonResult { | ||||
|     let data: ShareCipherData = data.into_inner(); | ||||
|  | ||||
|     share_cipher_by_uuid(&cipher_id, data, &headers, &mut conn, &nt).await | ||||
|     share_cipher_by_uuid(&cipher_id, data, &headers, &mut conn, &nt, None).await | ||||
| } | ||||
|  | ||||
| #[put("/ciphers/<cipher_id>/share", data = "<data>")] | ||||
| @@ -937,7 +934,7 @@ async fn put_cipher_share( | ||||
| ) -> JsonResult { | ||||
|     let data: ShareCipherData = data.into_inner(); | ||||
|  | ||||
|     share_cipher_by_uuid(&cipher_id, data, &headers, &mut conn, &nt).await | ||||
|     share_cipher_by_uuid(&cipher_id, data, &headers, &mut conn, &nt, None).await | ||||
| } | ||||
|  | ||||
| #[derive(Deserialize)] | ||||
| @@ -977,11 +974,16 @@ async fn put_cipher_share_selected( | ||||
|         }; | ||||
|  | ||||
|         match shared_cipher_data.cipher.id.take() { | ||||
|             Some(id) => share_cipher_by_uuid(&id, shared_cipher_data, &headers, &mut conn, &nt).await?, | ||||
|             Some(id) => { | ||||
|                 share_cipher_by_uuid(&id, shared_cipher_data, &headers, &mut conn, &nt, Some(UpdateType::None)).await? | ||||
|             } | ||||
|             None => err!("Request missing ids field"), | ||||
|         }; | ||||
|     } | ||||
|  | ||||
|     // Multi share actions do not send out a push for each cipher, we need to send a general sync here | ||||
|     nt.send_user_update(UpdateType::SyncCiphers, &headers.user, &headers.device.push_uuid, &mut conn).await; | ||||
|  | ||||
|     Ok(()) | ||||
| } | ||||
|  | ||||
| @@ -991,6 +993,7 @@ async fn share_cipher_by_uuid( | ||||
|     headers: &Headers, | ||||
|     conn: &mut DbConn, | ||||
|     nt: &Notify<'_>, | ||||
|     override_ut: Option<UpdateType>, | ||||
| ) -> JsonResult { | ||||
|     let mut cipher = match Cipher::find_by_uuid(cipher_id, conn).await { | ||||
|         Some(cipher) => { | ||||
| @@ -1022,7 +1025,10 @@ async fn share_cipher_by_uuid( | ||||
|     }; | ||||
|  | ||||
|     // When LastKnownRevisionDate is None, it is a new cipher, so send CipherCreate. | ||||
|     let ut = if data.cipher.last_known_revision_date.is_some() { | ||||
|     // If there is an override, like when handling multiple items, we want to prevent a push notification for every single item | ||||
|     let ut = if let Some(ut) = override_ut { | ||||
|         ut | ||||
|     } else if data.cipher.last_known_revision_date.is_some() { | ||||
|         UpdateType::SyncCipherUpdate | ||||
|     } else { | ||||
|         UpdateType::SyncCipherCreate | ||||
| @@ -1030,7 +1036,7 @@ async fn share_cipher_by_uuid( | ||||
|  | ||||
|     update_cipher_from_data(&mut cipher, data.cipher, headers, Some(shared_to_collections), conn, nt, ut).await?; | ||||
|  | ||||
|     Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, conn).await)) | ||||
|     Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, conn).await?)) | ||||
| } | ||||
|  | ||||
| /// v2 API for downloading an attachment. This just redirects the client to | ||||
| @@ -1055,7 +1061,7 @@ async fn get_attachment( | ||||
|     } | ||||
|  | ||||
|     match Attachment::find_by_id(&attachment_id, &mut conn).await { | ||||
|         Some(attachment) if cipher_id == attachment.cipher_uuid => Ok(Json(attachment.to_json(&headers.host))), | ||||
|         Some(attachment) if cipher_id == attachment.cipher_uuid => Ok(Json(attachment.to_json(&headers.host).await?)), | ||||
|         Some(_) => err!("Attachment doesn't belong to cipher"), | ||||
|         None => err!("Attachment doesn't exist"), | ||||
|     } | ||||
| @@ -1116,7 +1122,7 @@ async fn post_attachment_v2( | ||||
|         "attachmentId": attachment_id, | ||||
|         "url": url, | ||||
|         "fileUploadType": FileUploadType::Direct as i32, | ||||
|         response_key: cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, &mut conn).await, | ||||
|         response_key: cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, &mut conn).await?, | ||||
|     }))) | ||||
| } | ||||
|  | ||||
| @@ -1142,7 +1148,7 @@ async fn save_attachment( | ||||
|     mut conn: DbConn, | ||||
|     nt: Notify<'_>, | ||||
| ) -> Result<(Cipher, DbConn), crate::error::Error> { | ||||
|     let mut data = data.into_inner(); | ||||
|     let data = data.into_inner(); | ||||
|  | ||||
|     let Some(size) = data.data.len().to_i64() else { | ||||
|         err!("Attachment data size overflow"); | ||||
| @@ -1269,13 +1275,7 @@ async fn save_attachment( | ||||
|         attachment.save(&mut conn).await.expect("Error saving attachment"); | ||||
|     } | ||||
|  | ||||
|     let folder_path = tokio::fs::canonicalize(&CONFIG.attachments_folder()).await?.join(cipher_id.as_ref()); | ||||
|     let file_path = folder_path.join(file_id.as_ref()); | ||||
|     tokio::fs::create_dir_all(&folder_path).await?; | ||||
|  | ||||
|     if let Err(_err) = data.data.persist_to(&file_path).await { | ||||
|         data.data.move_copy_to(file_path).await? | ||||
|     } | ||||
|     save_temp_file(PathType::Attachments, &format!("{cipher_id}/{file_id}"), data.data, true).await?; | ||||
|  | ||||
|     nt.send_cipher_update( | ||||
|         UpdateType::SyncCipherUpdate, | ||||
| @@ -1342,7 +1342,7 @@ async fn post_attachment( | ||||
|  | ||||
|     let (cipher, mut conn) = save_attachment(attachment, cipher_id, data, &headers, conn, nt).await?; | ||||
|  | ||||
|     Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, &mut conn).await)) | ||||
|     Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, &mut conn).await?)) | ||||
| } | ||||
|  | ||||
| #[post("/ciphers/<cipher_id>/attachment-admin", format = "multipart/form-data", data = "<data>")] | ||||
| @@ -1415,7 +1415,7 @@ async fn delete_attachment_admin( | ||||
|  | ||||
| #[post("/ciphers/<cipher_id>/delete")] | ||||
| async fn delete_cipher_post(cipher_id: CipherId, headers: Headers, mut conn: DbConn, nt: Notify<'_>) -> EmptyResult { | ||||
|     _delete_cipher_by_uuid(&cipher_id, &headers, &mut conn, false, &nt).await | ||||
|     _delete_cipher_by_uuid(&cipher_id, &headers, &mut conn, &CipherDeleteOptions::HardSingle, &nt).await | ||||
|     // permanent delete | ||||
| } | ||||
|  | ||||
| @@ -1426,13 +1426,13 @@ async fn delete_cipher_post_admin( | ||||
|     mut conn: DbConn, | ||||
|     nt: Notify<'_>, | ||||
| ) -> EmptyResult { | ||||
|     _delete_cipher_by_uuid(&cipher_id, &headers, &mut conn, false, &nt).await | ||||
|     _delete_cipher_by_uuid(&cipher_id, &headers, &mut conn, &CipherDeleteOptions::HardSingle, &nt).await | ||||
|     // permanent delete | ||||
| } | ||||
|  | ||||
| #[put("/ciphers/<cipher_id>/delete")] | ||||
| async fn delete_cipher_put(cipher_id: CipherId, headers: Headers, mut conn: DbConn, nt: Notify<'_>) -> EmptyResult { | ||||
|     _delete_cipher_by_uuid(&cipher_id, &headers, &mut conn, true, &nt).await | ||||
|     _delete_cipher_by_uuid(&cipher_id, &headers, &mut conn, &CipherDeleteOptions::SoftSingle, &nt).await | ||||
|     // soft delete | ||||
| } | ||||
|  | ||||
| @@ -1443,18 +1443,19 @@ async fn delete_cipher_put_admin( | ||||
|     mut conn: DbConn, | ||||
|     nt: Notify<'_>, | ||||
| ) -> EmptyResult { | ||||
|     _delete_cipher_by_uuid(&cipher_id, &headers, &mut conn, true, &nt).await | ||||
|     _delete_cipher_by_uuid(&cipher_id, &headers, &mut conn, &CipherDeleteOptions::SoftSingle, &nt).await | ||||
|     // soft delete | ||||
| } | ||||
|  | ||||
| #[delete("/ciphers/<cipher_id>")] | ||||
| async fn delete_cipher(cipher_id: CipherId, headers: Headers, mut conn: DbConn, nt: Notify<'_>) -> EmptyResult { | ||||
|     _delete_cipher_by_uuid(&cipher_id, &headers, &mut conn, false, &nt).await | ||||
|     _delete_cipher_by_uuid(&cipher_id, &headers, &mut conn, &CipherDeleteOptions::HardSingle, &nt).await | ||||
|     // permanent delete | ||||
| } | ||||
|  | ||||
| #[delete("/ciphers/<cipher_id>/admin")] | ||||
| async fn delete_cipher_admin(cipher_id: CipherId, headers: Headers, mut conn: DbConn, nt: Notify<'_>) -> EmptyResult { | ||||
|     _delete_cipher_by_uuid(&cipher_id, &headers, &mut conn, false, &nt).await | ||||
|     _delete_cipher_by_uuid(&cipher_id, &headers, &mut conn, &CipherDeleteOptions::HardSingle, &nt).await | ||||
|     // permanent delete | ||||
| } | ||||
|  | ||||
| @@ -1465,7 +1466,8 @@ async fn delete_cipher_selected( | ||||
|     conn: DbConn, | ||||
|     nt: Notify<'_>, | ||||
| ) -> EmptyResult { | ||||
|     _delete_multiple_ciphers(data, headers, conn, false, nt).await // permanent delete | ||||
|     _delete_multiple_ciphers(data, headers, conn, CipherDeleteOptions::HardMulti, nt).await | ||||
|     // permanent delete | ||||
| } | ||||
|  | ||||
| #[post("/ciphers/delete", data = "<data>")] | ||||
| @@ -1475,7 +1477,8 @@ async fn delete_cipher_selected_post( | ||||
|     conn: DbConn, | ||||
|     nt: Notify<'_>, | ||||
| ) -> EmptyResult { | ||||
|     _delete_multiple_ciphers(data, headers, conn, false, nt).await // permanent delete | ||||
|     _delete_multiple_ciphers(data, headers, conn, CipherDeleteOptions::HardMulti, nt).await | ||||
|     // permanent delete | ||||
| } | ||||
|  | ||||
| #[put("/ciphers/delete", data = "<data>")] | ||||
| @@ -1485,7 +1488,8 @@ async fn delete_cipher_selected_put( | ||||
|     conn: DbConn, | ||||
|     nt: Notify<'_>, | ||||
| ) -> EmptyResult { | ||||
|     _delete_multiple_ciphers(data, headers, conn, true, nt).await // soft delete | ||||
|     _delete_multiple_ciphers(data, headers, conn, CipherDeleteOptions::SoftMulti, nt).await | ||||
|     // soft delete | ||||
| } | ||||
|  | ||||
| #[delete("/ciphers/admin", data = "<data>")] | ||||
| @@ -1495,7 +1499,8 @@ async fn delete_cipher_selected_admin( | ||||
|     conn: DbConn, | ||||
|     nt: Notify<'_>, | ||||
| ) -> EmptyResult { | ||||
|     _delete_multiple_ciphers(data, headers, conn, false, nt).await // permanent delete | ||||
|     _delete_multiple_ciphers(data, headers, conn, CipherDeleteOptions::HardMulti, nt).await | ||||
|     // permanent delete | ||||
| } | ||||
|  | ||||
| #[post("/ciphers/delete-admin", data = "<data>")] | ||||
| @@ -1505,7 +1510,8 @@ async fn delete_cipher_selected_post_admin( | ||||
|     conn: DbConn, | ||||
|     nt: Notify<'_>, | ||||
| ) -> EmptyResult { | ||||
|     _delete_multiple_ciphers(data, headers, conn, false, nt).await // permanent delete | ||||
|     _delete_multiple_ciphers(data, headers, conn, CipherDeleteOptions::HardMulti, nt).await | ||||
|     // permanent delete | ||||
| } | ||||
|  | ||||
| #[put("/ciphers/delete-admin", data = "<data>")] | ||||
| @@ -1515,12 +1521,13 @@ async fn delete_cipher_selected_put_admin( | ||||
|     conn: DbConn, | ||||
|     nt: Notify<'_>, | ||||
| ) -> EmptyResult { | ||||
|     _delete_multiple_ciphers(data, headers, conn, true, nt).await // soft delete | ||||
|     _delete_multiple_ciphers(data, headers, conn, CipherDeleteOptions::SoftMulti, nt).await | ||||
|     // soft delete | ||||
| } | ||||
|  | ||||
| #[put("/ciphers/<cipher_id>/restore")] | ||||
| async fn restore_cipher_put(cipher_id: CipherId, headers: Headers, mut conn: DbConn, nt: Notify<'_>) -> JsonResult { | ||||
|     _restore_cipher_by_uuid(&cipher_id, &headers, &mut conn, &nt).await | ||||
|     _restore_cipher_by_uuid(&cipher_id, &headers, false, &mut conn, &nt).await | ||||
| } | ||||
|  | ||||
| #[put("/ciphers/<cipher_id>/restore-admin")] | ||||
| @@ -1530,7 +1537,17 @@ async fn restore_cipher_put_admin( | ||||
|     mut conn: DbConn, | ||||
|     nt: Notify<'_>, | ||||
| ) -> JsonResult { | ||||
|     _restore_cipher_by_uuid(&cipher_id, &headers, &mut conn, &nt).await | ||||
|     _restore_cipher_by_uuid(&cipher_id, &headers, false, &mut conn, &nt).await | ||||
| } | ||||
|  | ||||
| #[put("/ciphers/restore-admin", data = "<data>")] | ||||
| async fn restore_cipher_selected_admin( | ||||
|     data: Json<CipherIdsData>, | ||||
|     headers: Headers, | ||||
|     mut conn: DbConn, | ||||
|     nt: Notify<'_>, | ||||
| ) -> JsonResult { | ||||
|     _restore_multiple_ciphers(data, &headers, &mut conn, &nt).await | ||||
| } | ||||
|  | ||||
| #[put("/ciphers/restore", data = "<data>")] | ||||
| @@ -1558,35 +1575,47 @@ async fn move_cipher_selected( | ||||
|     nt: Notify<'_>, | ||||
| ) -> EmptyResult { | ||||
|     let data = data.into_inner(); | ||||
|     let user_id = headers.user.uuid; | ||||
|     let user_id = &headers.user.uuid; | ||||
|  | ||||
|     if let Some(ref folder_id) = data.folder_id { | ||||
|         if Folder::find_by_uuid_and_user(folder_id, &user_id, &mut conn).await.is_none() { | ||||
|         if Folder::find_by_uuid_and_user(folder_id, user_id, &mut conn).await.is_none() { | ||||
|             err!("Invalid folder", "Folder does not exist or belongs to another user"); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     for cipher_id in data.ids { | ||||
|         let Some(cipher) = Cipher::find_by_uuid(&cipher_id, &mut conn).await else { | ||||
|             err!("Cipher doesn't exist") | ||||
|         }; | ||||
|     let cipher_count = data.ids.len(); | ||||
|     let mut single_cipher: Option<Cipher> = None; | ||||
|  | ||||
|         if !cipher.is_accessible_to_user(&user_id, &mut conn).await { | ||||
|             err!("Cipher is not accessible by user") | ||||
|     // TODO: Convert this to use a single query (or at least less) to update all items | ||||
|     // Find all ciphers a user has access to, all others will be ignored | ||||
|     let accessible_ciphers = Cipher::find_by_user_and_ciphers(user_id, &data.ids, &mut conn).await; | ||||
|     let accessible_ciphers_count = accessible_ciphers.len(); | ||||
|     for cipher in accessible_ciphers { | ||||
|         cipher.move_to_folder(data.folder_id.clone(), user_id, &mut conn).await?; | ||||
|         if cipher_count == 1 { | ||||
|             single_cipher = Some(cipher); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|         // Move cipher | ||||
|         cipher.move_to_folder(data.folder_id.clone(), &user_id, &mut conn).await?; | ||||
|  | ||||
|     if let Some(cipher) = single_cipher { | ||||
|         nt.send_cipher_update( | ||||
|             UpdateType::SyncCipherUpdate, | ||||
|             &cipher, | ||||
|             std::slice::from_ref(&user_id), | ||||
|             std::slice::from_ref(user_id), | ||||
|             &headers.device, | ||||
|             None, | ||||
|             &mut conn, | ||||
|         ) | ||||
|         .await; | ||||
|     } else { | ||||
|         // Multi move actions do not send out a push for each cipher, we need to send a general sync here | ||||
|         nt.send_user_update(UpdateType::SyncCiphers, &headers.user, &headers.device.push_uuid, &mut conn).await; | ||||
|     } | ||||
|  | ||||
|     if cipher_count != accessible_ciphers_count { | ||||
|         err!(format!( | ||||
|             "Not all ciphers are moved! {accessible_ciphers_count} of the selected {cipher_count} were moved." | ||||
|         )) | ||||
|     } | ||||
|  | ||||
|     Ok(()) | ||||
| @@ -1669,11 +1698,19 @@ async fn delete_all( | ||||
|     } | ||||
| } | ||||
|  | ||||
| #[derive(PartialEq)] | ||||
| pub enum CipherDeleteOptions { | ||||
|     SoftSingle, | ||||
|     SoftMulti, | ||||
|     HardSingle, | ||||
|     HardMulti, | ||||
| } | ||||
|  | ||||
| async fn _delete_cipher_by_uuid( | ||||
|     cipher_id: &CipherId, | ||||
|     headers: &Headers, | ||||
|     conn: &mut DbConn, | ||||
|     soft_delete: bool, | ||||
|     delete_options: &CipherDeleteOptions, | ||||
|     nt: &Notify<'_>, | ||||
| ) -> EmptyResult { | ||||
|     let Some(mut cipher) = Cipher::find_by_uuid(cipher_id, conn).await else { | ||||
| @@ -1684,35 +1721,42 @@ async fn _delete_cipher_by_uuid( | ||||
|         err!("Cipher can't be deleted by user") | ||||
|     } | ||||
|  | ||||
|     if soft_delete { | ||||
|     if *delete_options == CipherDeleteOptions::SoftSingle || *delete_options == CipherDeleteOptions::SoftMulti { | ||||
|         cipher.deleted_at = Some(Utc::now().naive_utc()); | ||||
|         cipher.save(conn).await?; | ||||
|         nt.send_cipher_update( | ||||
|             UpdateType::SyncCipherUpdate, | ||||
|             &cipher, | ||||
|             &cipher.update_users_revision(conn).await, | ||||
|             &headers.device, | ||||
|             None, | ||||
|             conn, | ||||
|         ) | ||||
|         .await; | ||||
|         if *delete_options == CipherDeleteOptions::SoftSingle { | ||||
|             nt.send_cipher_update( | ||||
|                 UpdateType::SyncCipherUpdate, | ||||
|                 &cipher, | ||||
|                 &cipher.update_users_revision(conn).await, | ||||
|                 &headers.device, | ||||
|                 None, | ||||
|                 conn, | ||||
|             ) | ||||
|             .await; | ||||
|         } | ||||
|     } else { | ||||
|         cipher.delete(conn).await?; | ||||
|         nt.send_cipher_update( | ||||
|             UpdateType::SyncCipherDelete, | ||||
|             &cipher, | ||||
|             &cipher.update_users_revision(conn).await, | ||||
|             &headers.device, | ||||
|             None, | ||||
|             conn, | ||||
|         ) | ||||
|         .await; | ||||
|         if *delete_options == CipherDeleteOptions::HardSingle { | ||||
|             nt.send_cipher_update( | ||||
|                 UpdateType::SyncLoginDelete, | ||||
|                 &cipher, | ||||
|                 &cipher.update_users_revision(conn).await, | ||||
|                 &headers.device, | ||||
|                 None, | ||||
|                 conn, | ||||
|             ) | ||||
|             .await; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     if let Some(org_id) = cipher.organization_uuid { | ||||
|         let event_type = match soft_delete { | ||||
|             true => EventType::CipherSoftDeleted as i32, | ||||
|             false => EventType::CipherDeleted as i32, | ||||
|         let event_type = if *delete_options == CipherDeleteOptions::SoftSingle | ||||
|             || *delete_options == CipherDeleteOptions::SoftMulti | ||||
|         { | ||||
|             EventType::CipherSoftDeleted as i32 | ||||
|         } else { | ||||
|             EventType::CipherDeleted as i32 | ||||
|         }; | ||||
|  | ||||
|         log_event(event_type, &cipher.uuid, &org_id, &headers.user.uuid, headers.device.atype, &headers.ip.ip, conn) | ||||
| @@ -1732,23 +1776,27 @@ async fn _delete_multiple_ciphers( | ||||
|     data: Json<CipherIdsData>, | ||||
|     headers: Headers, | ||||
|     mut conn: DbConn, | ||||
|     soft_delete: bool, | ||||
|     delete_options: CipherDeleteOptions, | ||||
|     nt: Notify<'_>, | ||||
| ) -> EmptyResult { | ||||
|     let data = data.into_inner(); | ||||
|  | ||||
|     for cipher_id in data.ids { | ||||
|         if let error @ Err(_) = _delete_cipher_by_uuid(&cipher_id, &headers, &mut conn, soft_delete, &nt).await { | ||||
|         if let error @ Err(_) = _delete_cipher_by_uuid(&cipher_id, &headers, &mut conn, &delete_options, &nt).await { | ||||
|             return error; | ||||
|         }; | ||||
|     } | ||||
|  | ||||
|     // Multi delete actions do not send out a push for each cipher, we need to send a general sync here | ||||
|     nt.send_user_update(UpdateType::SyncCiphers, &headers.user, &headers.device.push_uuid, &mut conn).await; | ||||
|  | ||||
|     Ok(()) | ||||
| } | ||||
|  | ||||
| async fn _restore_cipher_by_uuid( | ||||
|     cipher_id: &CipherId, | ||||
|     headers: &Headers, | ||||
|     multi_restore: bool, | ||||
|     conn: &mut DbConn, | ||||
|     nt: &Notify<'_>, | ||||
| ) -> JsonResult { | ||||
| @@ -1763,15 +1811,17 @@ async fn _restore_cipher_by_uuid( | ||||
|     cipher.deleted_at = None; | ||||
|     cipher.save(conn).await?; | ||||
|  | ||||
|     nt.send_cipher_update( | ||||
|         UpdateType::SyncCipherUpdate, | ||||
|         &cipher, | ||||
|         &cipher.update_users_revision(conn).await, | ||||
|         &headers.device, | ||||
|         None, | ||||
|         conn, | ||||
|     ) | ||||
|     .await; | ||||
|     if !multi_restore { | ||||
|         nt.send_cipher_update( | ||||
|             UpdateType::SyncCipherUpdate, | ||||
|             &cipher, | ||||
|             &cipher.update_users_revision(conn).await, | ||||
|             &headers.device, | ||||
|             None, | ||||
|             conn, | ||||
|         ) | ||||
|         .await; | ||||
|     } | ||||
|  | ||||
|     if let Some(org_id) = &cipher.organization_uuid { | ||||
|         log_event( | ||||
| @@ -1786,7 +1836,7 @@ async fn _restore_cipher_by_uuid( | ||||
|         .await; | ||||
|     } | ||||
|  | ||||
|     Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, conn).await)) | ||||
|     Ok(Json(cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, conn).await?)) | ||||
| } | ||||
|  | ||||
| async fn _restore_multiple_ciphers( | ||||
| @@ -1799,12 +1849,15 @@ async fn _restore_multiple_ciphers( | ||||
|  | ||||
|     let mut ciphers: Vec<Value> = Vec::new(); | ||||
|     for cipher_id in data.ids { | ||||
|         match _restore_cipher_by_uuid(&cipher_id, headers, conn, nt).await { | ||||
|         match _restore_cipher_by_uuid(&cipher_id, headers, true, conn, nt).await { | ||||
|             Ok(json) => ciphers.push(json.into_inner()), | ||||
|             err => return err, | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     // Multi move actions do not send out a push for each cipher, we need to send a general sync here | ||||
|     nt.send_user_update(UpdateType::SyncCiphers, &headers.user, &headers.device.push_uuid, conn).await; | ||||
|  | ||||
|     Ok(Json(json!({ | ||||
|       "data": ciphers, | ||||
|       "object": "list", | ||||
| @@ -1859,7 +1912,7 @@ async fn _delete_cipher_attachment_by_id( | ||||
|         ) | ||||
|         .await; | ||||
|     } | ||||
|     let cipher_json = cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, conn).await; | ||||
|     let cipher_json = cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, conn).await?; | ||||
|     Ok(Json(json!({"cipher":cipher_json}))) | ||||
| } | ||||
|  | ||||
| @@ -1934,11 +1987,21 @@ impl CipherSyncData { | ||||
|  | ||||
|         // Generate a HashMap with the collections_uuid as key and the CollectionGroup record | ||||
|         let user_collections_groups: HashMap<CollectionId, CollectionGroup> = if CONFIG.org_groups_enabled() { | ||||
|             CollectionGroup::find_by_user(user_id, conn) | ||||
|                 .await | ||||
|                 .into_iter() | ||||
|                 .map(|collection_group| (collection_group.collections_uuid.clone(), collection_group)) | ||||
|                 .collect() | ||||
|             CollectionGroup::find_by_user(user_id, conn).await.into_iter().fold( | ||||
|                 HashMap::new(), | ||||
|                 |mut combined_permissions, cg| { | ||||
|                     combined_permissions | ||||
|                         .entry(cg.collections_uuid.clone()) | ||||
|                         .and_modify(|existing| { | ||||
|                             // Combine permissions: take the most permissive settings. | ||||
|                             existing.read_only &= cg.read_only; // false if ANY group allows write | ||||
|                             existing.hide_passwords &= cg.hide_passwords; // false if ANY group allows password view | ||||
|                             existing.manage |= cg.manage; // true if ANY group allows manage | ||||
|                         }) | ||||
|                         .or_insert(cg); | ||||
|                     combined_permissions | ||||
|                 }, | ||||
|             ) | ||||
|         } else { | ||||
|             HashMap::new() | ||||
|         }; | ||||
|   | ||||
| @@ -239,7 +239,7 @@ async fn send_invite(data: Json<EmergencyAccessInviteData>, headers: Headers, mu | ||||
|                 invitation.save(&mut conn).await?; | ||||
|             } | ||||
|  | ||||
|             let mut user = User::new(email.clone()); | ||||
|             let mut user = User::new(email.clone(), None); | ||||
|             user.save(&mut conn).await?; | ||||
|             (user, true) | ||||
|         } | ||||
| @@ -582,7 +582,7 @@ async fn view_emergency_access(emer_id: EmergencyAccessId, headers: Headers, mut | ||||
|                 CipherSyncType::User, | ||||
|                 &mut conn, | ||||
|             ) | ||||
|             .await, | ||||
|             .await?, | ||||
|         ); | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -50,11 +50,12 @@ pub fn events_routes() -> Vec<Route> { | ||||
| use rocket::{serde::json::Json, serde::json::Value, Catcher, Route}; | ||||
|  | ||||
| use crate::{ | ||||
|     api::{JsonResult, Notify, UpdateType}, | ||||
|     api::{EmptyResult, JsonResult, Notify, UpdateType}, | ||||
|     auth::Headers, | ||||
|     db::DbConn, | ||||
|     db::{models::*, DbConn}, | ||||
|     error::Error, | ||||
|     http_client::make_http_request, | ||||
|     mail, | ||||
|     util::parse_experimental_client_feature_flags, | ||||
| }; | ||||
|  | ||||
| @@ -200,15 +201,17 @@ fn get_api_webauthn(_headers: Headers) -> Json<Value> { | ||||
| fn config() -> Json<Value> { | ||||
|     let domain = crate::CONFIG.domain(); | ||||
|     // Official available feature flags can be found here: | ||||
|     // Server (v2025.5.0): https://github.com/bitwarden/server/blob/4a7db112a0952c6df8bacf36c317e9c4e58c3651/src/Core/Constants.cs#L102 | ||||
|     // Client (v2025.5.0): https://github.com/bitwarden/clients/blob/9df8a3cc50ed45f52513e62c23fcc8a4b745f078/libs/common/src/enums/feature-flag.enum.ts#L10 | ||||
|     // Android (v2025.4.0): https://github.com/bitwarden/android/blob/bee09de972c3870de0d54a0067996be473ec55c7/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/model/FlagKey.kt#L27 | ||||
|     // iOS (v2025.4.0): https://github.com/bitwarden/ios/blob/956e05db67344c912e3a1b8cb2609165d67da1c9/BitwardenShared/Core/Platform/Models/Enum/FeatureFlag.swift#L7 | ||||
|     // Server (v2025.6.2): https://github.com/bitwarden/server/blob/d094be3267f2030bd0dc62106bc6871cf82682f5/src/Core/Constants.cs#L103 | ||||
|     // Client (web-v2025.6.1): https://github.com/bitwarden/clients/blob/747c2fd6a1c348a57a76e4a7de8128466ffd3c01/libs/common/src/enums/feature-flag.enum.ts#L12 | ||||
|     // Android (v2025.6.0): https://github.com/bitwarden/android/blob/b5b022caaad33390c31b3021b2c1205925b0e1a2/app/src/main/kotlin/com/x8bit/bitwarden/data/platform/manager/model/FlagKey.kt#L22 | ||||
|     // iOS (v2025.6.0): https://github.com/bitwarden/ios/blob/ff06d9c6cc8da89f78f37f376495800201d7261a/BitwardenShared/Core/Platform/Models/Enum/FeatureFlag.swift#L7 | ||||
|     let mut feature_states = | ||||
|         parse_experimental_client_feature_flags(&crate::CONFIG.experimental_client_feature_flags()); | ||||
|     feature_states.insert("duo-redirect".to_string(), true); | ||||
|     feature_states.insert("email-verification".to_string(), true); | ||||
|     feature_states.insert("unauth-ui-refresh".to_string(), true); | ||||
|     feature_states.insert("enable-pm-flight-recorder".to_string(), true); | ||||
|     feature_states.insert("mobile-error-reporting".to_string(), true); | ||||
|  | ||||
|     Json(json!({ | ||||
|         // Note: The clients use this version to handle backwards compatibility concerns | ||||
| @@ -216,14 +219,14 @@ fn config() -> Json<Value> { | ||||
|         // We should make sure that we keep this updated when we support the new server features | ||||
|         // Version history: | ||||
|         // - Individual cipher key encryption: 2024.2.0 | ||||
|         "version": "2025.4.0", | ||||
|         "version": "2025.6.0", | ||||
|         "gitHash": option_env!("GIT_REV"), | ||||
|         "server": { | ||||
|           "name": "Vaultwarden", | ||||
|           "url": "https://github.com/dani-garcia/vaultwarden" | ||||
|         }, | ||||
|         "settings": { | ||||
|             "disableUserRegistration": !crate::CONFIG.signups_allowed() && crate::CONFIG.signups_domains_whitelist().is_empty(), | ||||
|             "disableUserRegistration": crate::CONFIG.is_signup_disabled() | ||||
|         }, | ||||
|         "environment": { | ||||
|           "vault": domain, | ||||
| @@ -257,3 +260,49 @@ fn api_not_found() -> Json<Value> { | ||||
|         } | ||||
|     })) | ||||
| } | ||||
|  | ||||
| async fn accept_org_invite( | ||||
|     user: &User, | ||||
|     mut member: Membership, | ||||
|     reset_password_key: Option<String>, | ||||
|     conn: &mut DbConn, | ||||
| ) -> EmptyResult { | ||||
|     if member.status != MembershipStatus::Invited as i32 { | ||||
|         err!("User already accepted the invitation"); | ||||
|     } | ||||
|  | ||||
|     // This check is also done at accept_invite, _confirm_invite, _activate_member, edit_member, admin::update_membership_type | ||||
|     // It returns different error messages per function. | ||||
|     if member.atype < MembershipType::Admin { | ||||
|         match OrgPolicy::is_user_allowed(&member.user_uuid, &member.org_uuid, false, conn).await { | ||||
|             Ok(_) => {} | ||||
|             Err(OrgPolicyErr::TwoFactorMissing) => { | ||||
|                 if crate::CONFIG.email_2fa_auto_fallback() { | ||||
|                     two_factor::email::activate_email_2fa(user, conn).await?; | ||||
|                 } else { | ||||
|                     err!("You cannot join this organization until you enable two-step login on your user account"); | ||||
|                 } | ||||
|             } | ||||
|             Err(OrgPolicyErr::SingleOrgEnforced) => { | ||||
|                 err!("You cannot join this organization because you are a member of an organization which forbids it"); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     member.status = MembershipStatus::Accepted as i32; | ||||
|     member.reset_password_key = reset_password_key; | ||||
|  | ||||
|     member.save(conn).await?; | ||||
|  | ||||
|     if crate::CONFIG.mail_enabled() { | ||||
|         let org = match Organization::find_by_uuid(&member.org_uuid, conn).await { | ||||
|             Some(org) => org, | ||||
|             None => err!("Organization not found."), | ||||
|         }; | ||||
|         // User was invited to an organization, so they must be confirmed manually after acceptance | ||||
|         mail::send_invite_accepted(&user.email, &member.invited_by_email.unwrap_or(org.billing_email), &org.name) | ||||
|             .await?; | ||||
|     } | ||||
|  | ||||
|     Ok(()) | ||||
| } | ||||
|   | ||||
| @@ -7,13 +7,13 @@ use std::collections::{HashMap, HashSet}; | ||||
| use crate::api::admin::FAKE_ADMIN_UUID; | ||||
| use crate::{ | ||||
|     api::{ | ||||
|         core::{log_event, two_factor, CipherSyncData, CipherSyncType}, | ||||
|         core::{accept_org_invite, log_event, two_factor, CipherSyncData, CipherSyncType}, | ||||
|         EmptyResult, JsonResult, Notify, PasswordOrOtpData, UpdateType, | ||||
|     }, | ||||
|     auth::{decode_invite, AdminHeaders, Headers, ManagerHeaders, ManagerHeadersLoose, OrgMemberHeaders, OwnerHeaders}, | ||||
|     db::{models::*, DbConn}, | ||||
|     mail, | ||||
|     util::{convert_json_key_lcase_first, NumberOrString}, | ||||
|     util::{convert_json_key_lcase_first, get_uuid, NumberOrString}, | ||||
|     CONFIG, | ||||
| }; | ||||
|  | ||||
| @@ -43,6 +43,7 @@ pub fn routes() -> Vec<Route> { | ||||
|         bulk_delete_organization_collections, | ||||
|         post_bulk_collections, | ||||
|         get_org_details, | ||||
|         get_org_domain_sso_verified, | ||||
|         get_members, | ||||
|         send_invite, | ||||
|         reinvite_member, | ||||
| @@ -60,6 +61,7 @@ pub fn routes() -> Vec<Route> { | ||||
|         post_org_import, | ||||
|         list_policies, | ||||
|         list_policies_token, | ||||
|         get_master_password_policy, | ||||
|         get_policy, | ||||
|         put_policy, | ||||
|         get_organization_tax, | ||||
| @@ -103,6 +105,7 @@ pub fn routes() -> Vec<Route> { | ||||
|         api_key, | ||||
|         rotate_api_key, | ||||
|         get_billing_metadata, | ||||
|         get_auto_enroll_status, | ||||
|     ] | ||||
| } | ||||
|  | ||||
| @@ -192,7 +195,7 @@ async fn create_organization(headers: Headers, data: Json<OrgData>, mut conn: Db | ||||
|     }; | ||||
|  | ||||
|     let org = Organization::new(data.name, data.billing_email, private_key, public_key); | ||||
|     let mut member = Membership::new(headers.user.uuid, org.uuid.clone()); | ||||
|     let mut member = Membership::new(headers.user.uuid, org.uuid.clone(), None); | ||||
|     let collection = Collection::new(org.uuid.clone(), data.collection_name, None); | ||||
|  | ||||
|     member.akey = data.key; | ||||
| @@ -335,6 +338,34 @@ async fn get_user_collections(headers: Headers, mut conn: DbConn) -> Json<Value> | ||||
|     })) | ||||
| } | ||||
|  | ||||
| // Called during the SSO enrollment | ||||
| // The `identifier` should be the value returned by `get_org_domain_sso_details` | ||||
| // The returned `Id` will then be passed to `get_master_password_policy` which will mainly ignore it | ||||
| #[get("/organizations/<identifier>/auto-enroll-status")] | ||||
| async fn get_auto_enroll_status(identifier: &str, headers: Headers, mut conn: DbConn) -> JsonResult { | ||||
|     let org = if identifier == crate::sso::FAKE_IDENTIFIER { | ||||
|         match Membership::find_main_user_org(&headers.user.uuid, &mut conn).await { | ||||
|             Some(member) => Organization::find_by_uuid(&member.org_uuid, &mut conn).await, | ||||
|             None => None, | ||||
|         } | ||||
|     } else { | ||||
|         Organization::find_by_name(identifier, &mut conn).await | ||||
|     }; | ||||
|  | ||||
|     let (id, identifier, rp_auto_enroll) = match org { | ||||
|         None => (get_uuid(), identifier.to_string(), false), | ||||
|         Some(org) => { | ||||
|             (org.uuid.to_string(), org.name, OrgPolicy::org_is_reset_password_auto_enroll(&org.uuid, &mut conn).await) | ||||
|         } | ||||
|     }; | ||||
|  | ||||
|     Ok(Json(json!({ | ||||
|         "Id": id, | ||||
|         "Identifier": identifier, | ||||
|         "ResetPasswordEnabled": rp_auto_enroll, | ||||
|     }))) | ||||
| } | ||||
|  | ||||
| #[get("/organizations/<org_id>/collections")] | ||||
| async fn get_org_collections(org_id: OrganizationId, headers: ManagerHeadersLoose, mut conn: DbConn) -> JsonResult { | ||||
|     if org_id != headers.membership.org_uuid { | ||||
| @@ -726,15 +757,6 @@ async fn delete_organization_collection( | ||||
|     _delete_organization_collection(&org_id, &col_id, &headers, &mut conn).await | ||||
| } | ||||
|  | ||||
| #[derive(Deserialize, Debug)] | ||||
| #[serde(rename_all = "camelCase")] | ||||
| struct DeleteCollectionData { | ||||
|     #[allow(dead_code)] | ||||
|     id: String, | ||||
|     #[allow(dead_code)] | ||||
|     org_id: OrganizationId, | ||||
| } | ||||
|  | ||||
| #[post("/organizations/<org_id>/collections/<col_id>/delete")] | ||||
| async fn post_organization_collection_delete( | ||||
|     org_id: OrganizationId, | ||||
| @@ -917,21 +939,59 @@ async fn get_org_details(data: OrgIdData, headers: OrgMemberHeaders, mut conn: D | ||||
|     } | ||||
|  | ||||
|     Ok(Json(json!({ | ||||
|         "data": _get_org_details(&data.organization_id, &headers.host, &headers.user.uuid, &mut conn).await, | ||||
|         "data": _get_org_details(&data.organization_id, &headers.host, &headers.user.uuid, &mut conn).await?, | ||||
|         "object": "list", | ||||
|         "continuationToken": null, | ||||
|     }))) | ||||
| } | ||||
|  | ||||
| async fn _get_org_details(org_id: &OrganizationId, host: &str, user_id: &UserId, conn: &mut DbConn) -> Value { | ||||
| async fn _get_org_details( | ||||
|     org_id: &OrganizationId, | ||||
|     host: &str, | ||||
|     user_id: &UserId, | ||||
|     conn: &mut DbConn, | ||||
| ) -> Result<Value, crate::Error> { | ||||
|     let ciphers = Cipher::find_by_org(org_id, conn).await; | ||||
|     let cipher_sync_data = CipherSyncData::new(user_id, CipherSyncType::Organization, conn).await; | ||||
|  | ||||
|     let mut ciphers_json = Vec::with_capacity(ciphers.len()); | ||||
|     for c in ciphers { | ||||
|         ciphers_json.push(c.to_json(host, user_id, Some(&cipher_sync_data), CipherSyncType::Organization, conn).await); | ||||
|         ciphers_json.push(c.to_json(host, user_id, Some(&cipher_sync_data), CipherSyncType::Organization, conn).await?); | ||||
|     } | ||||
|     json!(ciphers_json) | ||||
|     Ok(json!(ciphers_json)) | ||||
| } | ||||
|  | ||||
| #[derive(Deserialize)] | ||||
| #[serde(rename_all = "camelCase")] | ||||
| struct OrgDomainDetails { | ||||
|     email: String, | ||||
| } | ||||
|  | ||||
| // Returning a Domain/Organization here allow to prefill it and prevent prompting the user | ||||
| // So we either return an Org name associated to the user or a dummy value. | ||||
| // In use since `v2025.6.0`, appears to use only the first `organizationIdentifier` | ||||
| #[post("/organizations/domain/sso/verified", data = "<data>")] | ||||
| async fn get_org_domain_sso_verified(data: Json<OrgDomainDetails>, mut conn: DbConn) -> JsonResult { | ||||
|     let data: OrgDomainDetails = data.into_inner(); | ||||
|  | ||||
|     let identifiers = match Organization::find_org_user_email(&data.email, &mut conn) | ||||
|         .await | ||||
|         .into_iter() | ||||
|         .map(|o| o.name) | ||||
|         .collect::<Vec<String>>() | ||||
|     { | ||||
|         v if !v.is_empty() => v, | ||||
|         _ => vec![crate::sso::FAKE_IDENTIFIER.to_string()], | ||||
|     }; | ||||
|  | ||||
|     Ok(Json(json!({ | ||||
|         "object": "list", | ||||
|         "data": identifiers.into_iter().map(|identifier| json!({ | ||||
|             "organizationName": identifier,     // appear unused | ||||
|             "organizationIdentifier": identifier, | ||||
|             "domainName": CONFIG.domain(),      // appear unused | ||||
|         })).collect::<Vec<Value>>() | ||||
|     }))) | ||||
| } | ||||
|  | ||||
| #[derive(FromForm)] | ||||
| @@ -1067,7 +1127,7 @@ async fn send_invite( | ||||
|                     Invitation::new(email).save(&mut conn).await?; | ||||
|                 } | ||||
|  | ||||
|                 let mut new_user = User::new(email.clone()); | ||||
|                 let mut new_user = User::new(email.clone(), None); | ||||
|                 new_user.save(&mut conn).await?; | ||||
|                 user_created = true; | ||||
|                 new_user | ||||
| @@ -1085,7 +1145,7 @@ async fn send_invite( | ||||
|             } | ||||
|         }; | ||||
|  | ||||
|         let mut new_member = Membership::new(user.uuid.clone(), org_id.clone()); | ||||
|         let mut new_member = Membership::new(user.uuid.clone(), org_id.clone(), Some(headers.user.email.clone())); | ||||
|         new_member.access_all = access_all; | ||||
|         new_member.atype = new_type; | ||||
|         new_member.status = member_status; | ||||
| @@ -1271,71 +1331,39 @@ async fn accept_invite( | ||||
|         err!("Invitation was issued to a different account", "Claim does not match user_id") | ||||
|     } | ||||
|  | ||||
|     // If a claim org_id does not match the one in from the URI, something is wrong. | ||||
|     if !claims.org_id.eq(&org_id) { | ||||
|         err!("Error accepting the invitation", "Claim does not match the org_id") | ||||
|     } | ||||
|  | ||||
|     // If a claim does not have a member_id or it does not match the one in from the URI, something is wrong. | ||||
|     if !claims.member_id.eq(&member_id) { | ||||
|         err!("Error accepting the invitation", "Claim does not match the member_id") | ||||
|     } | ||||
|  | ||||
|     let member = &claims.member_id; | ||||
|     let org = &claims.org_id; | ||||
|  | ||||
|     let member_id = &claims.member_id; | ||||
|     Invitation::take(&claims.email, &mut conn).await; | ||||
|  | ||||
|     // skip invitation logic when we were invited via the /admin panel | ||||
|     if **member != FAKE_ADMIN_UUID { | ||||
|         let Some(mut member) = Membership::find_by_uuid_and_org(member, org, &mut conn).await else { | ||||
|     if **member_id != FAKE_ADMIN_UUID { | ||||
|         let Some(mut member) = Membership::find_by_uuid_and_org(member_id, &claims.org_id, &mut conn).await else { | ||||
|             err!("Error accepting the invitation") | ||||
|         }; | ||||
|  | ||||
|         if member.status != MembershipStatus::Invited as i32 { | ||||
|             err!("User already accepted the invitation") | ||||
|         } | ||||
|         let reset_password_key = match OrgPolicy::org_is_reset_password_auto_enroll(&member.org_uuid, &mut conn).await { | ||||
|             true if data.reset_password_key.is_none() => err!("Reset password key is required, but not provided."), | ||||
|             true => data.reset_password_key, | ||||
|             false => None, | ||||
|         }; | ||||
|  | ||||
|         let master_password_required = OrgPolicy::org_is_reset_password_auto_enroll(org, &mut conn).await; | ||||
|         if data.reset_password_key.is_none() && master_password_required { | ||||
|             err!("Reset password key is required, but not provided."); | ||||
|         } | ||||
|         // In case the user was invited before the mail was saved in db. | ||||
|         member.invited_by_email = member.invited_by_email.or(claims.invited_by_email); | ||||
|  | ||||
|         // This check is also done at accept_invite, _confirm_invite, _activate_member, edit_member, admin::update_membership_type | ||||
|         // It returns different error messages per function. | ||||
|         if member.atype < MembershipType::Admin { | ||||
|             match OrgPolicy::is_user_allowed(&member.user_uuid, &org_id, false, &mut conn).await { | ||||
|                 Ok(_) => {} | ||||
|                 Err(OrgPolicyErr::TwoFactorMissing) => { | ||||
|                     if CONFIG.email_2fa_auto_fallback() { | ||||
|                         two_factor::email::activate_email_2fa(&headers.user, &mut conn).await?; | ||||
|                     } else { | ||||
|                         err!("You cannot join this organization until you enable two-step login on your user account"); | ||||
|                     } | ||||
|                 } | ||||
|                 Err(OrgPolicyErr::SingleOrgEnforced) => { | ||||
|                     err!("You cannot join this organization because you are a member of an organization which forbids it"); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         member.status = MembershipStatus::Accepted as i32; | ||||
|  | ||||
|         if master_password_required { | ||||
|             member.reset_password_key = data.reset_password_key; | ||||
|         } | ||||
|  | ||||
|         member.save(&mut conn).await?; | ||||
|     } | ||||
|  | ||||
|     if CONFIG.mail_enabled() { | ||||
|         if let Some(invited_by_email) = &claims.invited_by_email { | ||||
|             let org_name = match Organization::find_by_uuid(&claims.org_id, &mut conn).await { | ||||
|                 Some(org) => org.name, | ||||
|                 None => err!("Organization not found."), | ||||
|             }; | ||||
|             // User was invited to an organization, so they must be confirmed manually after acceptance | ||||
|             mail::send_invite_accepted(&claims.email, invited_by_email, &org_name).await?; | ||||
|         } else { | ||||
|             // User was invited from /admin, so they are automatically confirmed | ||||
|             let org_name = CONFIG.invitation_org_name(); | ||||
|             mail::send_invite_confirmed(&claims.email, &org_name).await?; | ||||
|         } | ||||
|         accept_org_invite(&headers.user, member, reset_password_key, &mut conn).await?; | ||||
|     } else if CONFIG.mail_enabled() { | ||||
|         // User was invited from /admin, so they are automatically confirmed | ||||
|         let org_name = CONFIG.invitation_org_name(); | ||||
|         mail::send_invite_confirmed(&claims.email, &org_name).await?; | ||||
|     } | ||||
|  | ||||
|     Ok(()) | ||||
| @@ -2029,18 +2057,36 @@ async fn list_policies_token(org_id: OrganizationId, token: &str, mut conn: DbCo | ||||
|     }))) | ||||
| } | ||||
|  | ||||
| #[get("/organizations/<org_id>/policies/<pol_type>")] | ||||
| // Called during the SSO enrollment. | ||||
| // Return the org policy if it exists, otherwise use the default one. | ||||
| #[get("/organizations/<org_id>/policies/master-password", rank = 1)] | ||||
| async fn get_master_password_policy(org_id: OrganizationId, _headers: Headers, mut conn: DbConn) -> JsonResult { | ||||
|     let policy = | ||||
|         OrgPolicy::find_by_org_and_type(&org_id, OrgPolicyType::MasterPassword, &mut conn).await.unwrap_or_else(|| { | ||||
|             let data = match CONFIG.sso_master_password_policy() { | ||||
|                 Some(policy) => policy, | ||||
|                 None => "null".to_string(), | ||||
|             }; | ||||
|  | ||||
|             OrgPolicy::new(org_id, OrgPolicyType::MasterPassword, CONFIG.sso_master_password_policy().is_some(), data) | ||||
|         }); | ||||
|  | ||||
|     Ok(Json(policy.to_json())) | ||||
| } | ||||
|  | ||||
| #[get("/organizations/<org_id>/policies/<pol_type>", rank = 2)] | ||||
| async fn get_policy(org_id: OrganizationId, pol_type: i32, headers: AdminHeaders, mut conn: DbConn) -> JsonResult { | ||||
|     if org_id != headers.org_id { | ||||
|         err!("Organization not found", "Organization id's do not match"); | ||||
|     } | ||||
|  | ||||
|     let Some(pol_type_enum) = OrgPolicyType::from_i32(pol_type) else { | ||||
|         err!("Invalid or unsupported policy type") | ||||
|     }; | ||||
|  | ||||
|     let policy = match OrgPolicy::find_by_org_and_type(&org_id, pol_type_enum, &mut conn).await { | ||||
|         Some(p) => p, | ||||
|         None => OrgPolicy::new(org_id.clone(), pol_type_enum, "null".to_string()), | ||||
|         None => OrgPolicy::new(org_id.clone(), pol_type_enum, false, "null".to_string()), | ||||
|     }; | ||||
|  | ||||
|     Ok(Json(policy.to_json())) | ||||
| @@ -2151,7 +2197,7 @@ async fn put_policy( | ||||
|  | ||||
|     let mut policy = match OrgPolicy::find_by_org_and_type(&org_id, pol_type_enum, &mut conn).await { | ||||
|         Some(p) => p, | ||||
|         None => OrgPolicy::new(org_id.clone(), pol_type_enum, "{}".to_string()), | ||||
|         None => OrgPolicy::new(org_id.clone(), pol_type_enum, false, "{}".to_string()), | ||||
|     }; | ||||
|  | ||||
|     policy.enabled = data.enabled; | ||||
| @@ -2264,7 +2310,7 @@ struct OrgImportData { | ||||
|     users: Vec<OrgImportUserData>, | ||||
| } | ||||
|  | ||||
| /// This function seems to be deprected | ||||
| /// This function seems to be deprecated | ||||
| /// It is only used with older directory connectors | ||||
| /// TODO: Cleanup Tech debt | ||||
| #[post("/organizations/<org_id>/import", data = "<data>")] | ||||
| @@ -2310,7 +2356,8 @@ async fn import(org_id: OrganizationId, data: Json<OrgImportData>, headers: Head | ||||
|                     MembershipStatus::Accepted as i32 // Automatically mark user as accepted if no email invites | ||||
|                 }; | ||||
|  | ||||
|                 let mut new_member = Membership::new(user.uuid.clone(), org_id.clone()); | ||||
|                 let mut new_member = | ||||
|                     Membership::new(user.uuid.clone(), org_id.clone(), Some(headers.user.email.clone())); | ||||
|                 new_member.access_all = false; | ||||
|                 new_member.atype = MembershipType::User as i32; | ||||
|                 new_member.status = member_status; | ||||
| @@ -3329,13 +3376,17 @@ async fn put_reset_password_enrollment( | ||||
|  | ||||
|     let reset_request = data.into_inner(); | ||||
|  | ||||
|     if reset_request.reset_password_key.is_none() | ||||
|         && OrgPolicy::org_is_reset_password_auto_enroll(&org_id, &mut conn).await | ||||
|     { | ||||
|     let reset_password_key = match reset_request.reset_password_key { | ||||
|         None => None, | ||||
|         Some(ref key) if key.is_empty() => None, | ||||
|         Some(key) => Some(key), | ||||
|     }; | ||||
|  | ||||
|     if reset_password_key.is_none() && OrgPolicy::org_is_reset_password_auto_enroll(&org_id, &mut conn).await { | ||||
|         err!("Reset password can't be withdrawn due to an enterprise policy"); | ||||
|     } | ||||
|  | ||||
|     if reset_request.reset_password_key.is_some() { | ||||
|     if reset_password_key.is_some() { | ||||
|         PasswordOrOtpData { | ||||
|             master_password_hash: reset_request.master_password_hash, | ||||
|             otp: reset_request.otp, | ||||
| @@ -3344,7 +3395,7 @@ async fn put_reset_password_enrollment( | ||||
|         .await?; | ||||
|     } | ||||
|  | ||||
|     member.reset_password_key = reset_request.reset_password_key; | ||||
|     member.reset_password_key = reset_password_key; | ||||
|     member.save(&mut conn).await?; | ||||
|  | ||||
|     let log_id = if member.reset_password_key.is_some() { | ||||
| @@ -3372,7 +3423,7 @@ async fn get_org_export(org_id: OrganizationId, headers: AdminHeaders, mut conn: | ||||
|  | ||||
|     Ok(Json(json!({ | ||||
|         "collections": convert_json_key_lcase_first(_get_org_collections(&org_id, &mut conn).await), | ||||
|         "ciphers": convert_json_key_lcase_first(_get_org_details(&org_id, &headers.host, &headers.user.uuid, &mut conn).await), | ||||
|         "ciphers": convert_json_key_lcase_first(_get_org_details(&org_id, &headers.host, &headers.user.uuid, &mut conn).await?), | ||||
|     }))) | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -89,7 +89,7 @@ async fn ldap_import(data: Json<OrgImportData>, token: PublicToken, mut conn: Db | ||||
|                 Some(user) => user, // exists in vaultwarden | ||||
|                 None => { | ||||
|                     // User does not exist yet | ||||
|                     let mut new_user = User::new(user_data.email.clone()); | ||||
|                     let mut new_user = User::new(user_data.email.clone(), None); | ||||
|                     new_user.save(&mut conn).await?; | ||||
|  | ||||
|                     if !CONFIG.mail_enabled() { | ||||
| @@ -105,7 +105,12 @@ async fn ldap_import(data: Json<OrgImportData>, token: PublicToken, mut conn: Db | ||||
|                 MembershipStatus::Accepted as i32 // Automatically mark user as accepted if no email invites | ||||
|             }; | ||||
|  | ||||
|             let mut new_member = Membership::new(user.uuid.clone(), org_id.clone()); | ||||
|             let (org_name, org_email) = match Organization::find_by_uuid(&org_id, &mut conn).await { | ||||
|                 Some(org) => (org.name, org.billing_email), | ||||
|                 None => err!("Error looking up organization"), | ||||
|             }; | ||||
|  | ||||
|             let mut new_member = Membership::new(user.uuid.clone(), org_id.clone(), Some(org_email.clone())); | ||||
|             new_member.set_external_id(Some(user_data.external_id.clone())); | ||||
|             new_member.access_all = false; | ||||
|             new_member.atype = MembershipType::User as i32; | ||||
| @@ -114,11 +119,6 @@ async fn ldap_import(data: Json<OrgImportData>, token: PublicToken, mut conn: Db | ||||
|             new_member.save(&mut conn).await?; | ||||
|  | ||||
|             if CONFIG.mail_enabled() { | ||||
|                 let (org_name, org_email) = match Organization::find_by_uuid(&org_id, &mut conn).await { | ||||
|                     Some(org) => (org.name, org.billing_email), | ||||
|                     None => err!("Error looking up organization"), | ||||
|                 }; | ||||
|  | ||||
|                 if let Err(e) = | ||||
|                     mail::send_invite(&user, org_id.clone(), new_member.uuid.clone(), &org_name, Some(org_email)).await | ||||
|                 { | ||||
|   | ||||
| @@ -1,4 +1,5 @@ | ||||
| use std::path::Path; | ||||
| use std::time::Duration; | ||||
|  | ||||
| use chrono::{DateTime, TimeDelta, Utc}; | ||||
| use num_traits::ToPrimitive; | ||||
| @@ -12,8 +13,9 @@ use serde_json::Value; | ||||
| use crate::{ | ||||
|     api::{ApiResult, EmptyResult, JsonResult, Notify, UpdateType}, | ||||
|     auth::{ClientIp, Headers, Host}, | ||||
|     config::PathType, | ||||
|     db::{models::*, DbConn, DbPool}, | ||||
|     util::NumberOrString, | ||||
|     util::{save_temp_file, NumberOrString}, | ||||
|     CONFIG, | ||||
| }; | ||||
|  | ||||
| @@ -228,7 +230,7 @@ async fn post_send_file(data: Form<UploadData<'_>>, headers: Headers, mut conn: | ||||
|  | ||||
|     let UploadData { | ||||
|         model, | ||||
|         mut data, | ||||
|         data, | ||||
|     } = data.into_inner(); | ||||
|     let model = model.into_inner(); | ||||
|  | ||||
| @@ -268,13 +270,8 @@ async fn post_send_file(data: Form<UploadData<'_>>, headers: Headers, mut conn: | ||||
|     } | ||||
|  | ||||
|     let file_id = crate::crypto::generate_send_file_id(); | ||||
|     let folder_path = tokio::fs::canonicalize(&CONFIG.sends_folder()).await?.join(&send.uuid); | ||||
|     let file_path = folder_path.join(&file_id); | ||||
|     tokio::fs::create_dir_all(&folder_path).await?; | ||||
|  | ||||
|     if let Err(_err) = data.persist_to(&file_path).await { | ||||
|         data.move_copy_to(file_path).await? | ||||
|     } | ||||
|     save_temp_file(PathType::Sends, &format!("{}/{file_id}", send.uuid), data, true).await?; | ||||
|  | ||||
|     let mut data_value: Value = serde_json::from_str(&send.data)?; | ||||
|     if let Some(o) = data_value.as_object_mut() { | ||||
| @@ -381,7 +378,7 @@ async fn post_send_file_v2_data( | ||||
| ) -> EmptyResult { | ||||
|     enforce_disable_send_policy(&headers, &mut conn).await?; | ||||
|  | ||||
|     let mut data = data.into_inner(); | ||||
|     let data = data.into_inner(); | ||||
|  | ||||
|     let Some(send) = Send::find_by_uuid_and_user(&send_id, &headers.user.uuid, &mut conn).await else { | ||||
|         err!("Send not found. Unable to save the file.", "Invalid send uuid or does not belong to user.") | ||||
| @@ -424,19 +421,9 @@ async fn post_send_file_v2_data( | ||||
|         err!("Send file size does not match.", format!("Expected a file size of {} got {size}", send_data.size)); | ||||
|     } | ||||
|  | ||||
|     let folder_path = tokio::fs::canonicalize(&CONFIG.sends_folder()).await?.join(send_id); | ||||
|     let file_path = folder_path.join(file_id); | ||||
|     let file_path = format!("{send_id}/{file_id}"); | ||||
|  | ||||
|     // Check if the file already exists, if that is the case do not overwrite it | ||||
|     if tokio::fs::metadata(&file_path).await.is_ok() { | ||||
|         err!("Send file has already been uploaded.", format!("File {file_path:?} already exists")) | ||||
|     } | ||||
|  | ||||
|     tokio::fs::create_dir_all(&folder_path).await?; | ||||
|  | ||||
|     if let Err(_err) = data.data.persist_to(&file_path).await { | ||||
|         data.data.move_copy_to(file_path).await? | ||||
|     } | ||||
|     save_temp_file(PathType::Sends, &file_path, data.data, false).await?; | ||||
|  | ||||
|     nt.send_send_update( | ||||
|         UpdateType::SyncSendCreate, | ||||
| @@ -569,15 +556,26 @@ async fn post_access_file( | ||||
|     ) | ||||
|     .await; | ||||
|  | ||||
|     let token_claims = crate::auth::generate_send_claims(&send_id, &file_id); | ||||
|     let token = crate::auth::encode_jwt(&token_claims); | ||||
|     Ok(Json(json!({ | ||||
|         "object": "send-fileDownload", | ||||
|         "id": file_id, | ||||
|         "url": format!("{}/api/sends/{send_id}/{file_id}?t={token}", &host.host) | ||||
|         "url": download_url(&host, &send_id, &file_id).await?, | ||||
|     }))) | ||||
| } | ||||
|  | ||||
| async fn download_url(host: &Host, send_id: &SendId, file_id: &SendFileId) -> Result<String, crate::Error> { | ||||
|     let operator = CONFIG.opendal_operator_for_path_type(PathType::Sends)?; | ||||
|  | ||||
|     if operator.info().scheme() == opendal::Scheme::Fs { | ||||
|         let token_claims = crate::auth::generate_send_claims(send_id, file_id); | ||||
|         let token = crate::auth::encode_jwt(&token_claims); | ||||
|  | ||||
|         Ok(format!("{}/api/sends/{send_id}/{file_id}?t={token}", &host.host)) | ||||
|     } else { | ||||
|         Ok(operator.presign_read(&format!("{send_id}/{file_id}"), Duration::from_secs(5 * 60)).await?.uri().to_string()) | ||||
|     } | ||||
| } | ||||
|  | ||||
| #[get("/sends/<send_id>/<file_id>?<t>")] | ||||
| async fn download_send(send_id: SendId, file_id: SendFileId, t: &str) -> Option<NamedFile> { | ||||
|     if let Ok(claims) = crate::auth::decode_send(t) { | ||||
|   | ||||
| @@ -261,7 +261,7 @@ pub(crate) async fn get_duo_keys_email(email: &str, conn: &mut DbConn) -> ApiRes | ||||
|     } | ||||
|     .map_res("Can't fetch Duo Keys")?; | ||||
|  | ||||
|     Ok((data.ik, data.sk, CONFIG.get_duo_akey(), data.host)) | ||||
|     Ok((data.ik, data.sk, CONFIG.get_duo_akey().await, data.host)) | ||||
| } | ||||
|  | ||||
| pub async fn generate_duo_signature(email: &str, conn: &mut DbConn) -> ApiResult<(String, String)> { | ||||
|   | ||||
| @@ -10,7 +10,7 @@ use crate::{ | ||||
|     auth::Headers, | ||||
|     crypto, | ||||
|     db::{ | ||||
|         models::{EventType, TwoFactor, TwoFactorType, User, UserId}, | ||||
|         models::{DeviceId, EventType, TwoFactor, TwoFactorType, User, UserId}, | ||||
|         DbConn, | ||||
|     }, | ||||
|     error::{Error, MapResult}, | ||||
| @@ -24,11 +24,16 @@ pub fn routes() -> Vec<Route> { | ||||
| #[derive(Deserialize)] | ||||
| #[serde(rename_all = "camelCase")] | ||||
| struct SendEmailLoginData { | ||||
|     // DeviceIdentifier: String, // Currently not used | ||||
|     #[serde(alias = "DeviceIdentifier")] | ||||
|     device_identifier: DeviceId, | ||||
|  | ||||
|     #[allow(unused)] | ||||
|     #[serde(alias = "Email")] | ||||
|     email: String, | ||||
|     email: Option<String>, | ||||
|  | ||||
|     #[allow(unused)] | ||||
|     #[serde(alias = "MasterPasswordHash")] | ||||
|     master_password_hash: String, | ||||
|     master_password_hash: Option<String>, | ||||
| } | ||||
|  | ||||
| /// User is trying to login and wants to use email 2FA. | ||||
| @@ -40,15 +45,10 @@ async fn send_email_login(data: Json<SendEmailLoginData>, mut conn: DbConn) -> E | ||||
|     use crate::db::models::User; | ||||
|  | ||||
|     // Get the user | ||||
|     let Some(user) = User::find_by_mail(&data.email, &mut conn).await else { | ||||
|         err!("Username or password is incorrect. Try again.") | ||||
|     let Some(user) = User::find_by_device_id(&data.device_identifier, &mut conn).await else { | ||||
|         err!("Cannot find user. Try again.") | ||||
|     }; | ||||
|  | ||||
|     // Check password | ||||
|     if !user.check_valid_password(&data.master_password_hash) { | ||||
|         err!("Username or password is incorrect. Try again.") | ||||
|     } | ||||
|  | ||||
|     if !CONFIG._enable_email_2fa() { | ||||
|         err!("Email 2FA is disabled") | ||||
|     } | ||||
|   | ||||
| @@ -1,15 +1,10 @@ | ||||
| use rocket::serde::json::Json; | ||||
| use rocket::Route; | ||||
| use serde_json::Value; | ||||
| use url::Url; | ||||
| use webauthn_rs::{base64_data::Base64UrlSafeData, proto::*, AuthenticationState, RegistrationState, Webauthn}; | ||||
|  | ||||
| use crate::{ | ||||
|     api::{ | ||||
|         core::{log_user_event, two_factor::_generate_recover_code}, | ||||
|         EmptyResult, JsonResult, PasswordOrOtpData, | ||||
|     }, | ||||
|     auth::Headers, | ||||
|     crypto::ct_eq, | ||||
|     db::{ | ||||
|         models::{EventType, TwoFactor, TwoFactorType, UserId}, | ||||
|         DbConn, | ||||
| @@ -18,6 +13,38 @@ use crate::{ | ||||
|     util::NumberOrString, | ||||
|     CONFIG, | ||||
| }; | ||||
| use rocket::serde::json::Json; | ||||
| use rocket::Route; | ||||
| use serde_json::Value; | ||||
| use std::str::FromStr; | ||||
| use std::sync::{Arc, LazyLock}; | ||||
| use std::time::Duration; | ||||
| use url::Url; | ||||
| use uuid::Uuid; | ||||
| use webauthn_rs::prelude::{Base64UrlSafeData, SecurityKey, SecurityKeyAuthentication, SecurityKeyRegistration}; | ||||
| use webauthn_rs::{Webauthn, WebauthnBuilder}; | ||||
| use webauthn_rs_proto::{ | ||||
|     AuthenticationExtensionsClientOutputs, AuthenticatorAssertionResponseRaw, AuthenticatorAttestationResponseRaw, | ||||
|     PublicKeyCredential, RegisterPublicKeyCredential, RegistrationExtensionsClientOutputs, | ||||
|     RequestAuthenticationExtensions, | ||||
| }; | ||||
|  | ||||
| pub static WEBAUTHN_2FA_CONFIG: LazyLock<Arc<Webauthn>> = LazyLock::new(|| { | ||||
|     let domain = CONFIG.domain(); | ||||
|     let domain_origin = CONFIG.domain_origin(); | ||||
|     let rp_id = Url::parse(&domain).map(|u| u.domain().map(str::to_owned)).ok().flatten().unwrap_or_default(); | ||||
|     let rp_origin = Url::parse(&domain_origin).unwrap(); | ||||
|  | ||||
|     let webauthn = WebauthnBuilder::new(&rp_id, &rp_origin) | ||||
|         .expect("Creating WebauthnBuilder failed") | ||||
|         .rp_name(&domain) | ||||
|         .timeout(Duration::from_millis(60000)) | ||||
|         .danger_set_user_presence_only_security_keys(true); | ||||
|  | ||||
|     Arc::new(webauthn.build().expect("Building Webauthn failed")) | ||||
| }); | ||||
|  | ||||
| pub type Webauthn2FaConfig<'a> = &'a rocket::State<Arc<Webauthn>>; | ||||
|  | ||||
| pub fn routes() -> Vec<Route> { | ||||
|     routes![get_webauthn, generate_webauthn_challenge, activate_webauthn, activate_webauthn_put, delete_webauthn,] | ||||
| @@ -45,52 +72,13 @@ pub struct U2FRegistration { | ||||
|     pub migrated: Option<bool>, | ||||
| } | ||||
|  | ||||
| struct WebauthnConfig { | ||||
|     url: String, | ||||
|     origin: Url, | ||||
|     rpid: String, | ||||
| } | ||||
|  | ||||
| impl WebauthnConfig { | ||||
|     fn load() -> Webauthn<Self> { | ||||
|         let domain = CONFIG.domain(); | ||||
|         let domain_origin = CONFIG.domain_origin(); | ||||
|         Webauthn::new(Self { | ||||
|             rpid: Url::parse(&domain).map(|u| u.domain().map(str::to_owned)).ok().flatten().unwrap_or_default(), | ||||
|             url: domain, | ||||
|             origin: Url::parse(&domain_origin).unwrap(), | ||||
|         }) | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl webauthn_rs::WebauthnConfig for WebauthnConfig { | ||||
|     fn get_relying_party_name(&self) -> &str { | ||||
|         &self.url | ||||
|     } | ||||
|  | ||||
|     fn get_origin(&self) -> &Url { | ||||
|         &self.origin | ||||
|     } | ||||
|  | ||||
|     fn get_relying_party_id(&self) -> &str { | ||||
|         &self.rpid | ||||
|     } | ||||
|  | ||||
|     /// We have WebAuthn configured to discourage user verification | ||||
|     /// if we leave this enabled, it will cause verification issues when a keys send UV=1. | ||||
|     /// Upstream (the library they use) ignores this when set to discouraged, so we should too. | ||||
|     fn get_require_uv_consistency(&self) -> bool { | ||||
|         false | ||||
|     } | ||||
| } | ||||
|  | ||||
| #[derive(Debug, Serialize, Deserialize)] | ||||
| pub struct WebauthnRegistration { | ||||
|     pub id: i32, | ||||
|     pub name: String, | ||||
|     pub migrated: bool, | ||||
|  | ||||
|     pub credential: Credential, | ||||
|     pub credential: SecurityKey, | ||||
| } | ||||
|  | ||||
| impl WebauthnRegistration { | ||||
| @@ -125,7 +113,12 @@ async fn get_webauthn(data: Json<PasswordOrOtpData>, headers: Headers, mut conn: | ||||
| } | ||||
|  | ||||
| #[post("/two-factor/get-webauthn-challenge", data = "<data>")] | ||||
| async fn generate_webauthn_challenge(data: Json<PasswordOrOtpData>, headers: Headers, mut conn: DbConn) -> JsonResult { | ||||
| async fn generate_webauthn_challenge( | ||||
|     data: Json<PasswordOrOtpData>, | ||||
|     headers: Headers, | ||||
|     webauthn: Webauthn2FaConfig<'_>, | ||||
|     mut conn: DbConn, | ||||
| ) -> JsonResult { | ||||
|     let data: PasswordOrOtpData = data.into_inner(); | ||||
|     let user = headers.user; | ||||
|  | ||||
| @@ -135,13 +128,13 @@ async fn generate_webauthn_challenge(data: Json<PasswordOrOtpData>, headers: Hea | ||||
|         .await? | ||||
|         .1 | ||||
|         .into_iter() | ||||
|         .map(|r| r.credential.cred_id) // We return the credentialIds to the clients to avoid double registering | ||||
|         .map(|r| r.credential.cred_id().to_owned()) // We return the credentialIds to the clients to avoid double registering | ||||
|         .collect(); | ||||
|  | ||||
|     let (challenge, state) = WebauthnConfig::load().generate_challenge_register_options( | ||||
|         user.uuid.as_bytes().to_vec(), | ||||
|         user.email, | ||||
|         user.name, | ||||
|     let (challenge, state) = webauthn.start_securitykey_registration( | ||||
|         Uuid::from_str(&user.uuid).expect("Failed to parse UUID"), // Should never fail | ||||
|         &user.email, | ||||
|         &user.name, | ||||
|         Some(registrations), | ||||
|         None, | ||||
|         None, | ||||
| @@ -193,8 +186,10 @@ impl From<RegisterPublicKeyCredentialCopy> for RegisterPublicKeyCredential { | ||||
|             response: AuthenticatorAttestationResponseRaw { | ||||
|                 attestation_object: r.response.attestation_object, | ||||
|                 client_data_json: r.response.client_data_json, | ||||
|                 transports: None, | ||||
|             }, | ||||
|             type_: r.r#type, | ||||
|             extensions: RegistrationExtensionsClientOutputs::default(), | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -205,7 +200,7 @@ pub struct PublicKeyCredentialCopy { | ||||
|     pub id: String, | ||||
|     pub raw_id: Base64UrlSafeData, | ||||
|     pub response: AuthenticatorAssertionResponseRawCopy, | ||||
|     pub extensions: Option<AuthenticationExtensionsClientOutputs>, | ||||
|     pub extensions: AuthenticationExtensionsClientOutputs, | ||||
|     pub r#type: String, | ||||
| } | ||||
|  | ||||
| @@ -238,7 +233,12 @@ impl From<PublicKeyCredentialCopy> for PublicKeyCredential { | ||||
| } | ||||
|  | ||||
| #[post("/two-factor/webauthn", data = "<data>")] | ||||
| async fn activate_webauthn(data: Json<EnableWebauthnData>, headers: Headers, mut conn: DbConn) -> JsonResult { | ||||
| async fn activate_webauthn( | ||||
|     data: Json<EnableWebauthnData>, | ||||
|     headers: Headers, | ||||
|     webauthn: Webauthn2FaConfig<'_>, | ||||
|     mut conn: DbConn, | ||||
| ) -> JsonResult { | ||||
|     let data: EnableWebauthnData = data.into_inner(); | ||||
|     let mut user = headers.user; | ||||
|  | ||||
| @@ -253,7 +253,7 @@ async fn activate_webauthn(data: Json<EnableWebauthnData>, headers: Headers, mut | ||||
|     let type_ = TwoFactorType::WebauthnRegisterChallenge as i32; | ||||
|     let state = match TwoFactor::find_by_user_and_type(&user.uuid, type_, &mut conn).await { | ||||
|         Some(tf) => { | ||||
|             let state: RegistrationState = serde_json::from_str(&tf.data)?; | ||||
|             let state: SecurityKeyRegistration = serde_json::from_str(&tf.data)?; | ||||
|             tf.delete(&mut conn).await?; | ||||
|             state | ||||
|         } | ||||
| @@ -261,8 +261,7 @@ async fn activate_webauthn(data: Json<EnableWebauthnData>, headers: Headers, mut | ||||
|     }; | ||||
|  | ||||
|     // Verify the credentials with the saved state | ||||
|     let (credential, _data) = | ||||
|         WebauthnConfig::load().register_credential(&data.device_response.into(), &state, |_| Ok(false))?; | ||||
|     let credential = webauthn.finish_securitykey_registration(&data.device_response.into(), &state)?; | ||||
|  | ||||
|     let mut registrations: Vec<_> = get_webauthn_registrations(&user.uuid, &mut conn).await?.1; | ||||
|     // TODO: Check for repeated ID's | ||||
| @@ -291,8 +290,13 @@ async fn activate_webauthn(data: Json<EnableWebauthnData>, headers: Headers, mut | ||||
| } | ||||
|  | ||||
| #[put("/two-factor/webauthn", data = "<data>")] | ||||
| async fn activate_webauthn_put(data: Json<EnableWebauthnData>, headers: Headers, conn: DbConn) -> JsonResult { | ||||
|     activate_webauthn(data, headers, conn).await | ||||
| async fn activate_webauthn_put( | ||||
|     data: Json<EnableWebauthnData>, | ||||
|     headers: Headers, | ||||
|     webauthn: Webauthn2FaConfig<'_>, | ||||
|     conn: DbConn, | ||||
| ) -> JsonResult { | ||||
|     activate_webauthn(data, headers, webauthn, conn).await | ||||
| } | ||||
|  | ||||
| #[derive(Debug, Deserialize)] | ||||
| @@ -335,7 +339,7 @@ async fn delete_webauthn(data: Json<DeleteU2FData>, headers: Headers, mut conn: | ||||
|             Err(_) => err!("Error parsing U2F data"), | ||||
|         }; | ||||
|  | ||||
|         data.retain(|r| r.reg.key_handle != removed_item.credential.cred_id); | ||||
|         data.retain(|r| r.reg.key_handle != removed_item.credential.cred_id().as_slice()); | ||||
|         let new_data_str = serde_json::to_string(&data)?; | ||||
|  | ||||
|         u2f.data = new_data_str; | ||||
| @@ -362,18 +366,36 @@ pub async fn get_webauthn_registrations( | ||||
|     } | ||||
| } | ||||
|  | ||||
| pub async fn generate_webauthn_login(user_id: &UserId, conn: &mut DbConn) -> JsonResult { | ||||
| pub async fn generate_webauthn_login( | ||||
|     user_id: &UserId, | ||||
|     webauthn: Webauthn2FaConfig<'_>, | ||||
|     conn: &mut DbConn, | ||||
| ) -> JsonResult { | ||||
|     // Load saved credentials | ||||
|     let creds: Vec<Credential> = | ||||
|         get_webauthn_registrations(user_id, conn).await?.1.into_iter().map(|r| r.credential).collect(); | ||||
|     let creds: Vec<_> = get_webauthn_registrations(user_id, conn).await?.1.into_iter().map(|r| r.credential).collect(); | ||||
|  | ||||
|     if creds.is_empty() { | ||||
|         err!("No Webauthn devices registered") | ||||
|     } | ||||
|  | ||||
|     // Generate a challenge based on the credentials | ||||
|     let ext = RequestAuthenticationExtensions::builder().appid(format!("{}/app-id.json", &CONFIG.domain())).build(); | ||||
|     let (response, state) = WebauthnConfig::load().generate_challenge_authenticate_options(creds, Some(ext))?; | ||||
|     let (mut response, state) = webauthn.start_securitykey_authentication(&creds)?; | ||||
|  | ||||
|     // Modify to discourage user verification | ||||
|     let mut state = serde_json::to_value(&state)?; | ||||
|  | ||||
|     // Add appid, this is only needed for U2F compatibility, so maybe it can be removed as well | ||||
|     let app_id = format!("{}/app-id.json", &CONFIG.domain()); | ||||
|     state["ast"]["appid"] = Value::String(app_id.clone()); | ||||
|     response | ||||
|         .public_key | ||||
|         .extensions | ||||
|         .get_or_insert(RequestAuthenticationExtensions { | ||||
|             appid: None, | ||||
|             uvm: None, | ||||
|             hmac_get_secret: None, | ||||
|         }) | ||||
|         .appid = Some(app_id); | ||||
|  | ||||
|     // Save the challenge state for later validation | ||||
|     TwoFactor::new(user_id.clone(), TwoFactorType::WebauthnLoginChallenge, serde_json::to_string(&state)?) | ||||
| @@ -384,11 +406,16 @@ pub async fn generate_webauthn_login(user_id: &UserId, conn: &mut DbConn) -> Jso | ||||
|     Ok(Json(serde_json::to_value(response.public_key)?)) | ||||
| } | ||||
|  | ||||
| pub async fn validate_webauthn_login(user_id: &UserId, response: &str, conn: &mut DbConn) -> EmptyResult { | ||||
| pub async fn validate_webauthn_login( | ||||
|     user_id: &UserId, | ||||
|     response: &str, | ||||
|     webauthn: Webauthn2FaConfig<'_>, | ||||
|     conn: &mut DbConn, | ||||
| ) -> EmptyResult { | ||||
|     let type_ = TwoFactorType::WebauthnLoginChallenge as i32; | ||||
|     let state = match TwoFactor::find_by_user_and_type(user_id, type_, conn).await { | ||||
|         Some(tf) => { | ||||
|             let state: AuthenticationState = serde_json::from_str(&tf.data)?; | ||||
|             let state: SecurityKeyAuthentication = serde_json::from_str(&tf.data)?; | ||||
|             tf.delete(conn).await?; | ||||
|             state | ||||
|         } | ||||
| @@ -405,17 +432,17 @@ pub async fn validate_webauthn_login(user_id: &UserId, response: &str, conn: &mu | ||||
|  | ||||
|     let mut registrations = get_webauthn_registrations(user_id, conn).await?.1; | ||||
|  | ||||
|     // If the credential we received is migrated from U2F, enable the U2F compatibility | ||||
|     //let use_u2f = registrations.iter().any(|r| r.migrated && r.credential.cred_id == rsp.raw_id.0); | ||||
|     let (cred_id, auth_data) = WebauthnConfig::load().authenticate_credential(&rsp, &state)?; | ||||
|     let authentication_result = webauthn.finish_securitykey_authentication(&rsp, &state)?; | ||||
|  | ||||
|     for reg in &mut registrations { | ||||
|         if ®.credential.cred_id == cred_id { | ||||
|             reg.credential.counter = auth_data.counter; | ||||
|  | ||||
|             TwoFactor::new(user_id.clone(), TwoFactorType::Webauthn, serde_json::to_string(®istrations)?) | ||||
|                 .save(conn) | ||||
|                 .await?; | ||||
|         if ct_eq(reg.credential.cred_id(), authentication_result.cred_id()) { | ||||
|             // If the cred id matches and the credential is updated, Some(true) is returned | ||||
|             // In those cases, update the record, else leave it alone | ||||
|             if reg.credential.update_credential(&authentication_result) == Some(true) { | ||||
|                 TwoFactor::new(user_id.clone(), TwoFactorType::Webauthn, serde_json::to_string(®istrations)?) | ||||
|                     .save(conn) | ||||
|                     .await?; | ||||
|             } | ||||
|             return Ok(()); | ||||
|         } | ||||
|     } | ||||
|   | ||||
| @@ -145,15 +145,14 @@ async fn activate_yubikey(data: Json<EnableYubikeyData>, headers: Headers, mut c | ||||
|  | ||||
|     // Ensure they are valid OTPs | ||||
|     for yubikey in &yubikeys { | ||||
|         if yubikey.len() == 12 { | ||||
|             // YubiKey ID | ||||
|         if yubikey.is_empty() || yubikey.len() == 12 { | ||||
|             continue; | ||||
|         } | ||||
|  | ||||
|         verify_yubikey_otp(yubikey.to_owned()).await.map_res("Invalid Yubikey OTP provided")?; | ||||
|     } | ||||
|  | ||||
|     let yubikey_ids: Vec<String> = yubikeys.into_iter().map(|x| (x[..12]).to_owned()).collect(); | ||||
|     let yubikey_ids: Vec<String> = yubikeys.into_iter().filter_map(|x| x.get(..12).map(str::to_owned)).collect(); | ||||
|  | ||||
|     let yubikey_metadata = YubikeyMetadata { | ||||
|         keys: yubikey_ids, | ||||
|   | ||||
							
								
								
									
										110
									
								
								src/api/icons.rs
									
									
									
									
									
								
							
							
						
						
									
										110
									
								
								src/api/icons.rs
									
									
									
									
									
								
							| @@ -14,14 +14,12 @@ use reqwest::{ | ||||
|     Client, Response, | ||||
| }; | ||||
| use rocket::{http::ContentType, response::Redirect, Route}; | ||||
| use tokio::{ | ||||
|     fs::{create_dir_all, remove_file, symlink_metadata, File}, | ||||
|     io::{AsyncReadExt, AsyncWriteExt}, | ||||
| }; | ||||
| use svg_hush::{data_url_filter, Filter}; | ||||
|  | ||||
| use html5gum::{Emitter, HtmlString, Readable, StringReader, Tokenizer}; | ||||
|  | ||||
| use crate::{ | ||||
|     config::PathType, | ||||
|     error::Error, | ||||
|     http_client::{get_reqwest_client_builder, should_block_address, CustomHttpClientError}, | ||||
|     util::Cached, | ||||
| @@ -38,11 +36,29 @@ pub fn routes() -> Vec<Route> { | ||||
| static CLIENT: Lazy<Client> = Lazy::new(|| { | ||||
|     // Generate the default headers | ||||
|     let mut default_headers = HeaderMap::new(); | ||||
|     default_headers.insert(header::USER_AGENT, HeaderValue::from_static("Links (2.22; Linux X86_64; GNU C; text)")); | ||||
|     default_headers.insert(header::ACCEPT, HeaderValue::from_static("text/html, text/*;q=0.5, image/*, */*;q=0.1")); | ||||
|     default_headers.insert(header::ACCEPT_LANGUAGE, HeaderValue::from_static("en,*;q=0.1")); | ||||
|     default_headers.insert( | ||||
|         header::USER_AGENT, | ||||
|         HeaderValue::from_static( | ||||
|             "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36", | ||||
|         ), | ||||
|     ); | ||||
|     default_headers.insert(header::ACCEPT, HeaderValue::from_static("text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")); | ||||
|     default_headers.insert(header::ACCEPT_LANGUAGE, HeaderValue::from_static("en-US,en;q=0.9")); | ||||
|     default_headers.insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache")); | ||||
|     default_headers.insert(header::PRAGMA, HeaderValue::from_static("no-cache")); | ||||
|     default_headers.insert(header::UPGRADE_INSECURE_REQUESTS, HeaderValue::from_static("1")); | ||||
|  | ||||
|     default_headers.insert("Sec-Ch-Ua-Mobile", HeaderValue::from_static("?0")); | ||||
|     default_headers.insert("Sec-Ch-Ua-Platform", HeaderValue::from_static("Linux")); | ||||
|     default_headers.insert( | ||||
|         "Sec-Ch-Ua", | ||||
|         HeaderValue::from_static("\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Google Chrome\";v=\"138\""), | ||||
|     ); | ||||
|  | ||||
|     default_headers.insert("Sec-Fetch-Site", HeaderValue::from_static("none")); | ||||
|     default_headers.insert("Sec-Fetch-Mode", HeaderValue::from_static("navigate")); | ||||
|     default_headers.insert("Sec-Fetch-User", HeaderValue::from_static("?1")); | ||||
|     default_headers.insert("Sec-Fetch-Dest", HeaderValue::from_static("document")); | ||||
|  | ||||
|     // Generate the cookie store | ||||
|     let cookie_store = Arc::new(Jar::default()); | ||||
| @@ -56,6 +72,7 @@ static CLIENT: Lazy<Client> = Lazy::new(|| { | ||||
|         .pool_max_idle_per_host(5) // Configure the Hyper Pool to only have max 5 idle connections | ||||
|         .pool_idle_timeout(pool_idle_timeout) // Configure the Hyper Pool to timeout after 10 seconds | ||||
|         .default_headers(default_headers.clone()) | ||||
|         .http1_title_case_headers() | ||||
|         .build() | ||||
|         .expect("Failed to build client") | ||||
| }); | ||||
| @@ -158,7 +175,7 @@ fn is_valid_domain(domain: &str) -> bool { | ||||
| } | ||||
|  | ||||
| async fn get_icon(domain: &str) -> Option<(Vec<u8>, String)> { | ||||
|     let path = format!("{}/{domain}.png", CONFIG.icon_cache_folder()); | ||||
|     let path = format!("{domain}.png"); | ||||
|  | ||||
|     // Check for expiration of negatively cached copy | ||||
|     if icon_is_negcached(&path).await { | ||||
| @@ -177,7 +194,7 @@ async fn get_icon(domain: &str) -> Option<(Vec<u8>, String)> { | ||||
|     // Get the icon, or None in case of error | ||||
|     match download_icon(domain).await { | ||||
|         Ok((icon, icon_type)) => { | ||||
|             save_icon(&path, &icon).await; | ||||
|             save_icon(&path, icon.to_vec()).await; | ||||
|             Some((icon.to_vec(), icon_type.unwrap_or("x-icon").to_string())) | ||||
|         } | ||||
|         Err(e) => { | ||||
| @@ -190,7 +207,7 @@ async fn get_icon(domain: &str) -> Option<(Vec<u8>, String)> { | ||||
|  | ||||
|             warn!("Unable to download icon: {e:?}"); | ||||
|             let miss_indicator = path + ".miss"; | ||||
|             save_icon(&miss_indicator, &[]).await; | ||||
|             save_icon(&miss_indicator, vec![]).await; | ||||
|             None | ||||
|         } | ||||
|     } | ||||
| @@ -203,11 +220,9 @@ async fn get_cached_icon(path: &str) -> Option<Vec<u8>> { | ||||
|     } | ||||
|  | ||||
|     // Try to read the cached icon, and return it if it exists | ||||
|     if let Ok(mut f) = File::open(path).await { | ||||
|         let mut buffer = Vec::new(); | ||||
|  | ||||
|         if f.read_to_end(&mut buffer).await.is_ok() { | ||||
|             return Some(buffer); | ||||
|     if let Ok(operator) = CONFIG.opendal_operator_for_path_type(PathType::IconCache) { | ||||
|         if let Ok(buf) = operator.read(path).await { | ||||
|             return Some(buf.to_vec()); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @@ -215,9 +230,11 @@ async fn get_cached_icon(path: &str) -> Option<Vec<u8>> { | ||||
| } | ||||
|  | ||||
| async fn file_is_expired(path: &str, ttl: u64) -> Result<bool, Error> { | ||||
|     let meta = symlink_metadata(path).await?; | ||||
|     let modified = meta.modified()?; | ||||
|     let age = SystemTime::now().duration_since(modified)?; | ||||
|     let operator = CONFIG.opendal_operator_for_path_type(PathType::IconCache)?; | ||||
|     let meta = operator.stat(path).await?; | ||||
|     let modified = | ||||
|         meta.last_modified().ok_or_else(|| std::io::Error::other(format!("No last modified time for `{path}`")))?; | ||||
|     let age = SystemTime::now().duration_since(modified.into())?; | ||||
|  | ||||
|     Ok(ttl > 0 && ttl <= age.as_secs()) | ||||
| } | ||||
| @@ -229,8 +246,13 @@ async fn icon_is_negcached(path: &str) -> bool { | ||||
|     match expired { | ||||
|         // No longer negatively cached, drop the marker | ||||
|         Ok(true) => { | ||||
|             if let Err(e) = remove_file(&miss_indicator).await { | ||||
|                 error!("Could not remove negative cache indicator for icon {path:?}: {e:?}"); | ||||
|             match CONFIG.opendal_operator_for_path_type(PathType::IconCache) { | ||||
|                 Ok(operator) => { | ||||
|                     if let Err(e) = operator.delete(&miss_indicator).await { | ||||
|                         error!("Could not remove negative cache indicator for icon {path:?}: {e:?}"); | ||||
|                     } | ||||
|                 } | ||||
|                 Err(e) => error!("Could not remove negative cache indicator for icon {path:?}: {e:?}"), | ||||
|             } | ||||
|             false | ||||
|         } | ||||
| @@ -316,7 +338,7 @@ struct IconUrlResult { | ||||
|  | ||||
| /// Returns a IconUrlResult which holds a Vector IconList and a string which holds the referer. | ||||
| /// There will always two items within the iconlist which holds http(s)://domain.tld/favicon.ico. | ||||
| /// This does not mean that that location does exists, but it is the default location browser use. | ||||
| /// This does not mean that location exists, but (it) is the default location the browser uses. | ||||
| /// | ||||
| /// # Argument | ||||
| /// * `domain` - A string which holds the domain with extension. | ||||
| @@ -559,26 +581,46 @@ async fn download_icon(domain: &str) -> Result<(Bytes, Option<&str>), Error> { | ||||
|  | ||||
|     if buffer.is_empty() { | ||||
|         err_silent!("Empty response or unable find a valid icon", domain); | ||||
|     } else if icon_type == Some("svg+xml") { | ||||
|         let mut svg_filter = Filter::new(); | ||||
|         svg_filter.set_data_url_filter(data_url_filter::allow_standard_images); | ||||
|         let mut sanitized_svg = Vec::new(); | ||||
|         if svg_filter.filter(&*buffer, &mut sanitized_svg).is_err() { | ||||
|             icon_type = None; | ||||
|             buffer.clear(); | ||||
|         } else { | ||||
|             buffer = sanitized_svg.into(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     Ok((buffer, icon_type)) | ||||
| } | ||||
|  | ||||
| async fn save_icon(path: &str, icon: &[u8]) { | ||||
|     match File::create(path).await { | ||||
|         Ok(mut f) => { | ||||
|             f.write_all(icon).await.expect("Error writing icon file"); | ||||
|         } | ||||
|         Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => { | ||||
|             create_dir_all(&CONFIG.icon_cache_folder()).await.expect("Error creating icon cache folder"); | ||||
|         } | ||||
| async fn save_icon(path: &str, icon: Vec<u8>) { | ||||
|     let operator = match CONFIG.opendal_operator_for_path_type(PathType::IconCache) { | ||||
|         Ok(operator) => operator, | ||||
|         Err(e) => { | ||||
|             warn!("Unable to save icon: {e:?}"); | ||||
|             warn!("Failed to get OpenDAL operator while saving icon: {e}"); | ||||
|             return; | ||||
|         } | ||||
|     }; | ||||
|  | ||||
|     if let Err(e) = operator.write(path, icon).await { | ||||
|         warn!("Unable to save icon: {e:?}"); | ||||
|     } | ||||
| } | ||||
|  | ||||
| fn get_icon_type(bytes: &[u8]) -> Option<&'static str> { | ||||
|     fn check_svg_after_xml_declaration(bytes: &[u8]) -> Option<&'static str> { | ||||
|         // Look for SVG tag within the first 1KB | ||||
|         if let Ok(content) = std::str::from_utf8(&bytes[..bytes.len().min(1024)]) { | ||||
|             if content.contains("<svg") || content.contains("<SVG") { | ||||
|                 return Some("svg+xml"); | ||||
|             } | ||||
|         } | ||||
|         None | ||||
|     } | ||||
|  | ||||
|     match bytes { | ||||
|         [137, 80, 78, 71, ..] => Some("png"), | ||||
|         [0, 0, 1, 0, ..] => Some("x-icon"), | ||||
| @@ -586,6 +628,8 @@ fn get_icon_type(bytes: &[u8]) -> Option<&'static str> { | ||||
|         [255, 216, 255, ..] => Some("jpeg"), | ||||
|         [71, 73, 70, 56, ..] => Some("gif"), | ||||
|         [66, 77, ..] => Some("bmp"), | ||||
|         [60, 115, 118, 103, ..] => Some("svg+xml"), // Normal svg | ||||
|         [60, 63, 120, 109, 108, ..] => check_svg_after_xml_declaration(bytes), // An svg starting with <?xml | ||||
|         _ => None, | ||||
|     } | ||||
| } | ||||
| @@ -597,6 +641,12 @@ async fn stream_to_bytes_limit(res: Response, max_size: usize) -> Result<Bytes, | ||||
|     let mut buf = BytesMut::new(); | ||||
|     let mut size = 0; | ||||
|     while let Some(chunk) = stream.next().await { | ||||
|         // It is possible that there might occur UnexpectedEof errors or others | ||||
|         // This is most of the time no issue, and if there is no chunked data anymore or at all parsing the HTML will not happen anyway. | ||||
|         // Therefore if chunk is an err, just break and continue with the data be have received. | ||||
|         if chunk.is_err() { | ||||
|             break; | ||||
|         } | ||||
|         let chunk = &chunk?; | ||||
|         size += chunk.len(); | ||||
|         buf.extend(chunk); | ||||
|   | ||||
Some files were not shown because too many files have changed in this diff Show More
		Reference in New Issue
	
	Block a user