Skip to content

Commit

Permalink
Merge branch 'main' of github.com:bitwarden/sdk into ps/rustfmt
Browse files Browse the repository at this point in the history
# Conflicts:
#	crates/bitwarden-generators/src/passphrase.rs
#	crates/bitwarden-generators/src/password.rs
#	crates/bitwarden/src/tool/generators/username_forwarders/forwardemail.rs
  • Loading branch information
Hinton committed Jan 19, 2024
2 parents fe3e13f + b174e49 commit 051e455
Show file tree
Hide file tree
Showing 48 changed files with 1,542 additions and 689 deletions.
122 changes: 122 additions & 0 deletions .github/workflows/build-python-wheels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
---
name: Build Python Wheels

on:
pull_request:
push:
branches:
- "main"
- "rc"
- "hotfix-rc"
workflow_dispatch:

defaults:
run:
shell: bash
working-directory: languages/python

jobs:
generate_schemas:
uses: ./.github/workflows/generate_schemas.yml

setup:
name: Setup
runs-on: ubuntu-22.04
outputs:
package_version: ${{ steps.retrieve-version.outputs.package_version }}
steps:
- name: Checkout repo
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: Get Package Version
id: retrieve-version
run: |
VERSION="$(grep -o '^version = ".*"' ../../crates/bitwarden-py/Cargo.toml | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+")"
echo "package_version=$VERSION" >> $GITHUB_OUTPUT
build:
name: Building Python wheel for - ${{ matrix.settings.os }} - ${{ matrix.settings.target }}
runs-on: ${{ matrix.settings.os || 'ubuntu-latest' }}
needs:
- generate_schemas
- setup
env:
_PACKAGE_VERSION: ${{ needs.setup.outputs.package_version }}
strategy:
fail-fast: false
matrix:
settings:
- os: macos-12
target: x86_64-apple-darwin

- os: macos-12
target: aarch64-apple-darwin

- os: windows-2022
target: x86_64-pc-windows-msvc

- os: ubuntu-22.04
target: x86_64-unknown-linux-gnu

- os: ubuntu-22.04
target: aarch64-unknown-linux-gnu

steps:
- name: Checkout repo
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: Setup Node
uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4.0.0
with:
node-version: 18

- name: Install rust
uses: dtolnay/rust-toolchain@439cf607258077187679211f12aa6f19af4a0af7 # stable
with:
toolchain: stable
targets: ${{ matrix.settings.target }}

- name: Cache cargo registry
uses: Swatinem/rust-cache@3cf7f8cc28d1b4e7d01e3783be10a97d55d483c8 # v2.7.1
with:
key: ${{ matrix.settings.target }}-cargo-${{ matrix.settings.os }}

- name: Retrieve schemas
uses: actions/download-artifact@f44cd7b40bfd40b6aa1cc1b9b5b7bf03d3c67110 # v4.1.0
with:
name: schemas.py
path: ${{ github.workspace }}/languages/python/bitwarden_sdk

- name: Build wheels
if: ${{ matrix.settings.target != 'x86_64-unknown-linux-gnu' }}
uses: PyO3/maturin-action@b9e8f88fd4448fdecf5095864cdc7e39a544aa9f # v1.40.7
with:
target: ${{ matrix.settings.target }}
args: --release --find-interpreter --sdist
sccache: "true"
manylinux: "2_28" # https://github.com/pola-rs/polars/pull/12211
working-directory: ${{ github.workspace }}/languages/python

- name: Build wheels (Linux - x86_64)
if: ${{ matrix.settings.target == 'x86_64-unknown-linux-gnu' }}
uses: PyO3/maturin-action@b9e8f88fd4448fdecf5095864cdc7e39a544aa9f # v1.40.7
with:
target: ${{ matrix.settings.target }}
args: --release --find-interpreter --sdist
container: quay.io/pypa/manylinux_2_28_x86_64:2023-11-20-745eb52
sccache: "true"
manylinux: "2_28" # https://github.com/pola-rs/polars/pull/12211
working-directory: ${{ github.workspace }}/languages/python

- name: Upload wheels
uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
with:
name: bitwarden_sdk-${{ env._PACKAGE_VERSION }}-${{ matrix.settings.target }}
path: ${{ github.workspace }}/target/wheels/bitwarden_sdk*.whl

- name: Upload sdists
if: ${{ matrix.settings.target == 'x86_64-unknown-linux-gnu' }} # we only need one sdist
uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
with:
name: bitwarden_sdk-${{ env._PACKAGE_VERSION }}-sdist
path: ${{ github.workspace }}/target/wheels/bitwarden_sdk-*.tar.gz
3 changes: 2 additions & 1 deletion .github/workflows/build-rust-crates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ jobs:

package:
- bitwarden
- bitwarden-crypto
- bitwarden-api-api
- bitwarden-api-identity
- bitwarden-crypto
- bitwarden-generators

steps:
- name: Checkout
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/publish-rust-crates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ on:
required: true
default: true
type: boolean
publish_bitwarden-generators:
description: "Publish bitwarden-generators crate"
required: true
default: true
type: boolean

defaults:
run:
Expand Down Expand Up @@ -67,6 +72,7 @@ jobs:
PUBLISH_BITWARDEN_API_API: ${{ github.event.inputs.publish_bitwarden-api-api }}
PUBLISH_BITWARDEN_API_IDENTITY: ${{ github.event.inputs.publish_bitwarden-api-identity }}
PUBLISH_BITWARDEN_CRYPTO: ${{ github.event.inputs.publish_bitwarden-crypto }}
PUBLISH_BITWARDEN_GENERATORS: ${{ github.event.inputs.publish_bitwarden-generators }}
run: |
if [[ "$PUBLISH_BITWARDEN" == "false" ]] && [[ "$PUBLISH_BITWARDEN_API_API" == "false" ]] && [[ "$PUBLISH_BITWARDEN_API_IDENTITY" == "false" ]]; then
echo "==================================="
Expand Down Expand Up @@ -98,6 +104,11 @@ jobs:
PACKAGES_LIST="$PACKAGES_LIST bitwarden-crypto"
fi
if [[ "$PUBLISH_BITWARDEN_GENERATORS" == "true" ]]; then
PACKAGES_COMMAND="$PACKAGES_COMMAND -p bitwarden-generators"
PACKAGES_LIST="$PACKAGES_LIST bitwarden-generators"
fi
echo "Packages command: " $PACKAGES_COMMAND
echo "Packages list: " $PACKAGES_LIST
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/version-bump.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ on:
- bitwarden-api-api
- bitwarden-api-identity
- bitwarden-crypto
- bitwarden-generators
- bitwarden-json
- cli
- napi
Expand Down Expand Up @@ -123,6 +124,12 @@ jobs:
if: ${{ inputs.project == 'bitwarden-crypto' }}
run: cargo-set-version set-version -p bitwarden-crypto ${{ inputs.version_number }}

### bitwarden-generators

- name: Bump bitwarden-generators crate Version
if: ${{ inputs.project == 'bitwarden-generators' }}
run: cargo-set-version set-version -p bitwarden-generators ${{ inputs.version_number }}

### cli

- name: Bump cli Version
Expand Down
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/bitwarden-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ license-file = "LICENSE"
repository = "https://github.com/bitwarden/sdk"
homepage = "https://bitwarden.com"
description = """
Bitwarden Cryptographic primitives
Internal crate for the bitwarden crate. Do not use.
"""
keywords = ["bitwarden"]
edition = "2021"
Expand Down
6 changes: 6 additions & 0 deletions crates/bitwarden-crypto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Bitwarden Crypto

This is an internal crate for the Bitwarden SDK do not depend on this directly and use the
[`bitwarden`](https://crates.io/crates/bitwarden) crate instead.

This crate does not follow semantic versioning and the public interface may change at any time.
33 changes: 33 additions & 0 deletions crates/bitwarden-generators/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "bitwarden-generators"
version = "0.1.0"
authors = ["Bitwarden Inc"]
license-file = "LICENSE"
repository = "https://github.com/bitwarden/sdk"
homepage = "https://bitwarden.com"
description = """
Internal crate for the bitwarden crate. Do not use.
"""
keywords = ["bitwarden"]
edition = "2021"
rust-version = "1.57"

[features]
mobile = ["uniffi"] # Mobile-specific features

[dependencies]
bitwarden-crypto = { path = "../bitwarden-crypto", version = "=0.1.0" }
rand = ">=0.8.5, <0.9"
reqwest = { version = ">=0.11, <0.12", features = [
"json",
], default-features = false }
schemars = { version = ">=0.8.9, <0.9", features = ["uuid1", "chrono"] }
serde = { version = ">=1.0, <2.0", features = ["derive"] }
serde_json = ">=1.0.96, <2.0"
thiserror = ">=1.0.40, <2.0"
uniffi = { version = "=0.25.2", optional = true }

[dev-dependencies]
rand_chacha = "0.3.1"
tokio = { version = "1.35.1", features = ["rt", "macros"] }
wiremock = "0.5.22"
6 changes: 6 additions & 0 deletions crates/bitwarden-generators/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Bitwarden Generators

This is an internal crate for the Bitwarden SDK do not depend on this directly and use the
[`bitwarden`](https://crates.io/crates/bitwarden) crate instead.

This crate does not follow semantic versioning and the public interface may change at any time.
11 changes: 11 additions & 0 deletions crates/bitwarden-generators/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod passphrase;
pub use passphrase::{passphrase, PassphraseError, PassphraseGeneratorRequest};
mod password;
mod util;
pub use password::{password, PasswordError, PasswordGeneratorRequest};
mod username;
pub use username::{username, ForwarderServiceType, UsernameError, UsernameGeneratorRequest};
mod username_forwarders;

#[cfg(feature = "mobile")]
uniffi::setup_scaffolding!();
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ use bitwarden_crypto::EFF_LONG_WORD_LIST;
use rand::{seq::SliceRandom, Rng, RngCore};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{error::Result, util::capitalize_first_letter};
use crate::util::capitalize_first_letter;

#[derive(Debug, Error)]
pub enum PassphraseError {
#[error("'num_words' must be between {} and {}", minimum, maximum)]
InvalidNumWords { minimum: u8, maximum: u8 },
#[error("'word_separator' cannot be empty")]
EmptyWordSeparator,
}

/// Passphrase generator request options.
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
Expand Down Expand Up @@ -49,16 +58,19 @@ struct ValidPassphraseGeneratorOptions {
impl PassphraseGeneratorRequest {
/// Validates the request and returns an immutable struct with valid options to use with the
/// passphrase generator.
fn validate_options(self) -> Result<ValidPassphraseGeneratorOptions> {
fn validate_options(self) -> Result<ValidPassphraseGeneratorOptions, PassphraseError> {
// TODO: Add password generator policy checks

if !(MINIMUM_PASSPHRASE_NUM_WORDS..=MAXIMUM_PASSPHRASE_NUM_WORDS).contains(&self.num_words)
{
return Err(format!("'num_words' must be between {MINIMUM_PASSPHRASE_NUM_WORDS} and {MAXIMUM_PASSPHRASE_NUM_WORDS}").into());
return Err(PassphraseError::InvalidNumWords {
minimum: MINIMUM_PASSPHRASE_NUM_WORDS,
maximum: MAXIMUM_PASSPHRASE_NUM_WORDS,
});
}

if self.word_separator.chars().next().is_none() {
return Err("'word_separator' cannot be empty".into());
return Err(PassphraseError::EmptyWordSeparator);
};

Ok(ValidPassphraseGeneratorOptions {
Expand All @@ -70,13 +82,8 @@ impl PassphraseGeneratorRequest {
}
}

/// Implementation of the random passphrase generator. This is not accessible to the public API.
/// See [`ClientGenerator::passphrase`](crate::ClientGenerator::passphrase) for the API function.
///
/// # Arguments:
/// * `options`: Valid parameters used to generate the passphrase. To create it, use
/// [`PassphraseGeneratorRequest::validate_options`](PassphraseGeneratorRequest::validate_options).
pub(super) fn passphrase(request: PassphraseGeneratorRequest) -> Result<String> {
/// Implementation of the random passphrase generator.
pub fn passphrase(request: PassphraseGeneratorRequest) -> Result<String, PassphraseError> {
let options = request.validate_options()?;
Ok(passphrase_with_rng(rand::thread_rng(), options))
}
Expand Down
Loading

0 comments on commit 051e455

Please sign in to comment.