Compare commits

..
57 changed files with 2660 additions and 151 deletions
@@ -0,0 +1,194 @@
name: ArtCraft Linux release
on:
push:
branches: [release, artcraft-release, artcraft-linux]
workflow_dispatch:
permissions:
contents: read
concurrency:
group: linux-release-${{ github.ref }}
cancel-in-progress: false
jobs:
packaging-check:
runs-on: ubuntu-22.04
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- uses: storytold/craft-common-actions/actions/install-apt-packages@main
with:
packages: rpm jq binutils zsync
- run: packaging/linux/package-test.sh
build:
strategy:
fail-fast: false
matrix:
include:
- {arch: x86_64, runner: ubuntu-22.04, target: x86_64-unknown-linux-gnu, bundles: "deb,rpm,appimage"}
- {arch: aarch64, runner: ubuntu-22.04-arm, target: aarch64-unknown-linux-gnu, bundles: "deb,rpm,appimage"}
- {arch: riscv64, runner: ubuntu-24.04, target: riscv64gc-unknown-linux-gnu, bundles: "deb,rpm"}
runs-on: ${{ matrix.runner }}
timeout-minutes: 90
env:
SQLX_OFFLINE: 'true'
VITE_ENVIRONMENT_TYPE: production
TAURI_FRONTEND_PATH: ${{ github.workspace }}/frontend
TAURI_APP_PATH: ${{ github.workspace }}/crates/desktop/artcraft
steps:
- uses: actions/checkout@v4
- uses: storytold/craft-common-actions/actions/install-apt-packages@main
with:
packages: libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf libssl-dev libclang-dev libasound2-dev libxdo-dev libdbus-1-dev build-essential clang file jq perl cmake nasm rpm zsync gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-libav
- uses: storytold/craft-common-actions/actions/install-riscv64-cross@main
if: matrix.arch == 'riscv64'
- name: Remove conflicting host Pango development files
if: matrix.arch == 'riscv64'
# Ubuntu's Pango GIR differs by architecture despite being in a shared path.
# Only the target GTK development packages are needed for this cross build.
run: sudo apt-get remove -y libpango1.0-dev:amd64
- name: Install RISC-V WebKitGTK dependencies
if: matrix.arch == 'riscv64'
uses: storytold/craft-common-actions/actions/install-apt-packages@main
with:
packages: libwebkit2gtk-4.1-dev:riscv64 libappindicator3-dev:riscv64 libssl-dev:riscv64 libxdo-dev:riscv64 libdbus-1-dev:riscv64 g++-riscv64-linux-gnu
- name: Configure RISC-V cross compilation
if: matrix.arch == 'riscv64'
run: |
cat >> "$GITHUB_ENV" <<'ENV'
CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_GNU_LINKER=riscv64-linux-gnu-gcc
CC_riscv64gc_unknown_linux_gnu=riscv64-linux-gnu-gcc
CXX_riscv64gc_unknown_linux_gnu=riscv64-linux-gnu-g++
AR_riscv64gc_unknown_linux_gnu=riscv64-linux-gnu-ar
PKG_CONFIG_ALLOW_CROSS=1
PKG_CONFIG_PATH_riscv64gc_unknown_linux_gnu=/usr/lib/riscv64-linux-gnu/pkgconfig:/usr/share/pkgconfig
PKG_CONFIG_LIBDIR_riscv64gc_unknown_linux_gnu=/usr/lib/riscv64-linux-gnu/pkgconfig:/usr/share/pkgconfig
ENV
echo "CMAKE_TOOLCHAIN_FILE_riscv64gc_unknown_linux_gnu=$GITHUB_WORKSPACE/packaging/linux/riscv64.cmake" >> "$GITHUB_ENV"
- uses: actions/setup-node@v4
with:
node-version: '22'
cache: npm
cache-dependency-path: frontend/package-lock.json
- uses: dtolnay/rust-toolchain@683b588c487a69d01f6c6ddf40f036ff682cc2c8 # v1
with:
toolchain: stable
targets: ${{ matrix.target }}
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
with:
key: tauri-linux-${{ matrix.arch }}
cache-on-failure: true
- run: npm ci --ignore-scripts
working-directory: frontend
- run: ./node_modules/.bin/nx run artcraft:build
working-directory: frontend
- uses: tauri-apps/tauri-action@fce9c6108b31ea247710505d3aaaa893ee6768d4 # v0
with:
projectPath: crates/desktop/artcraft
args: --target ${{ matrix.target }} --bundles ${{ matrix.bundles }} --config ${{ github.workspace }}/packaging/linux/tauri.conf.json
- name: Package and verify Linux outputs
env:
ARCH: ${{ matrix.arch }}
TARGET: ${{ matrix.target }}
run: packaging/linux/package.sh "$ARCH" "$TARGET"
- uses: actions/upload-artifact@v4
with:
name: release-linux-${{ matrix.arch }}
path: dist/release/*
if-no-files-found: error
arch:
if: always() && !cancelled()
needs: build
runs-on: ubuntu-24.04
strategy:
matrix:
arch: [x86_64, aarch64]
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: release-linux-${{ matrix.arch }}
path: dist/linux
- name: Find tarball
id: tarball
env:
ARCH: ${{ matrix.arch }}
run: |
shopt -s nullglob
files=(dist/linux/artcraft-*-linux-"$ARCH".tar.gz)
test "${#files[@]}" -eq 1
echo "path=${files[0]}" >> "$GITHUB_OUTPUT"
- uses: storytold/craft-common-actions/actions/arch-package@9d6790582b4997aa2120f4413cdb55c0b6f77fae # extra-depends support (#161)
with:
tarball: ${{ steps.tarball.outputs.path }}
arch: ${{ matrix.arch }}
extra-depends: webkit2gtk-4.1 gtk3 openssl
- name: Verify Arch runtime dependencies
run: |
for package in dist/release/*.pkg.tar.zst; do
tar --zstd -xOf "$package" .PKGINFO | grep -Fx 'depend = webkit2gtk-4.1'
done
- uses: actions/upload-artifact@v4
with:
name: release-arch-${{ matrix.arch }}
path: dist/release/*.pkg.tar.zst
if-no-files-found: error
flatpak:
if: always() && !cancelled()
needs: build
runs-on: ${{ matrix.runner }}
permissions:
contents: read
packages: write
strategy:
fail-fast: false
matrix:
include:
- {arch: x86_64, runner: ubuntu-24.04}
- {arch: aarch64, runner: ubuntu-24.04-arm}
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: release-linux-${{ matrix.arch }}
path: dist/linux
- uses: storytold/craft-common-actions/actions/install-apt-packages@main
with:
packages: flatpak flatpak-builder elfutils librsvg2-common skopeo
- name: Allow Flatpak build sandboxes
run: sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
- uses: storytold/craft-common-actions/actions/import-signing-key@main
with:
key: ${{ secrets.LINUX_SIGNING_KEY }}
- name: Build Flatpak and publish OCI
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: packaging/linux/flatpak.sh
- uses: actions/upload-artifact@v4
with:
name: release-flatpak-${{ matrix.arch }}
path: |
dist/release/*.flatpak
dist/release/*.oci-manifest.json
if-no-files-found: error
release:
needs: [packaging-check, build, arch, flatpak]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- name: Read version
id: version
run: echo "value=$(jq -r .version crates/desktop/artcraft/tauri.conf.json)" >> "$GITHUB_OUTPUT"
- name: Require signing key
env:
SIGNING_KEY: ${{ secrets.LINUX_SIGNING_KEY }}
run: test -n "$SIGNING_KEY"
- uses: storytold/craft-common-actions/actions/draft-release@main
with:
version: ${{ steps.version.outputs.value }}
app-title: ArtCraft
tag-prefix: artcraft-v
retain: "true"
allow-published: "true"
github-token: ${{ secrets.GITHUB_TOKEN }}
signing-key: ${{ secrets.LINUX_SIGNING_KEY }}
+10 -8
View File
@@ -84,7 +84,7 @@ jobs:
echo "Starting certificate import process..."
# Create certificate file
echo $APPLE_CERTIFICATE | base64 --decode > certificate.p12
echo "$APPLE_CERTIFICATE" | base64 --decode > certificate.p12
# Debug: Check if certificate file exists and has content
echo "Certificate file info:"
@@ -93,25 +93,25 @@ jobs:
# Debug: Check certificate contents without password (just structure)
echo "Certificate structure (may show error if password protected):"
openssl pkcs12 -info -in certificate.p12 -passin pass:$APPLE_CERTIFICATE_PASSWORD -noout || echo "Could not read certificate structure"
openssl pkcs12 -info -in certificate.p12 -passin pass:"$APPLE_CERTIFICATE_PASSWORD" -noout || echo "Could not read certificate structure"
# Debug: Try to list what's in the p12 file
echo "Attempting to list certificate contents:"
security cms -D -i certificate.p12 || echo "Not a CMS file, trying PKCS12..."
openssl pkcs12 -in certificate.p12 -passin pass:$APPLE_CERTIFICATE_PASSWORD -info -nodes 2>&1 | head -20 || echo "Failed to read PKCS12 contents"
openssl pkcs12 -in certificate.p12 -passin pass:"$APPLE_CERTIFICATE_PASSWORD" -info -nodes 2>&1 | head -20 || echo "Failed to read PKCS12 contents"
# Debug: Extract certificate details to verify type and expiration
echo "Extracting certificate details..."
# Extract just the certificate portion and examine it
openssl pkcs12 -in certificate.p12 -passin pass:$APPLE_CERTIFICATE_PASSWORD -nokeys 2>/dev/null | openssl x509 -text -noout | grep -E "(Subject:|Issuer:|Not Before|Not After)" || echo "Failed to parse certificate details"
openssl pkcs12 -in certificate.p12 -passin pass:"$APPLE_CERTIFICATE_PASSWORD" -nokeys 2>/dev/null | openssl x509 -text -noout | grep -E "(Subject:|Issuer:|Not Before|Not After)" || echo "Failed to parse certificate details"
# Also try to see what type of certificate this is
echo "Certificate type check:"
openssl pkcs12 -in certificate.p12 -passin pass:$APPLE_CERTIFICATE_PASSWORD -nokeys 2>/dev/null | openssl x509 -text -noout | grep -E "(CN=|OU=)" || echo "Could not determine certificate type"
openssl pkcs12 -in certificate.p12 -passin pass:"$APPLE_CERTIFICATE_PASSWORD" -nokeys 2>/dev/null | openssl x509 -text -noout | grep -E "(CN=|OU=)" || echo "Could not determine certificate type"
# Debug: Check if private key exists
echo "Checking for private key..."
openssl pkcs12 -in certificate.p12 -passin pass:$APPLE_CERTIFICATE_PASSWORD -nocerts -nodes 2>&1 | grep -E "(PRIVATE KEY|RSA KEY)" | head -5 || echo "No private key found"
openssl pkcs12 -in certificate.p12 -passin pass:"$APPLE_CERTIFICATE_PASSWORD" -nocerts -nodes 2>&1 | grep -E "(PRIVATE KEY|RSA KEY)" | head -5 || echo "No private key found"
# Create temporary keychain
security create-keychain -p "$KEYCHAIN_PASSWORD" build.keychain
@@ -139,6 +139,7 @@ jobs:
security unlock-keychain -p "$KEYCHAIN_PASSWORD" build.keychain
# Add the keychain to the search list
# shellcheck disable=SC2046
security list-keychains -d user -s build.keychain $(security list-keychains -d user | sed 's/^[[:space:]]*"//;s/"$//')
# List all certificates to debug
@@ -181,11 +182,11 @@ jobs:
security find-certificate -c "Learning Machines" build.keychain -p | openssl x509 -text -noout | grep -E "(Subject:|CN=)" || echo "Could not parse certificate"
# Set the correct signing identity
echo "APPLE_SIGNING_IDENTITY=Developer ID Application: Learning Machines LLC (DJ6XS33FX8)" >> $GITHUB_ENV
echo "APPLE_SIGNING_IDENTITY=Developer ID Application: Learning Machines LLC (DJ6XS33FX8)" >> "$GITHUB_ENV"
echo "Proceeding with identity: Developer ID Application: Learning Machines LLC (DJ6XS33FX8)"
# Also set the team ID
echo "APPLE_DEVELOPMENT_TEAM=DJ6XS33FX8" >> $GITHUB_ENV
echo "APPLE_DEVELOPMENT_TEAM=DJ6XS33FX8" >> "$GITHUB_ENV"
- name: Pre-build frontend to avoid Tauri build issues
run: |
@@ -200,6 +201,7 @@ jobs:
npx nx run artcraft:build
echo "Checking build output..."
# shellcheck disable=SC2012
ls -la apps/artcraft/dist/ | head -10
# Update Tauri config to skip the build (since we already built)
@@ -44,6 +44,7 @@ jobs:
node-version: lts/*
- name: install frontend dependencies
shell: pwsh
run: |
# Navigate to the frontend directory to install dependencies
cd ./frontend
@@ -63,6 +64,7 @@ jobs:
npm list --depth=0 | findstr nx
- name: Pre-build frontend to avoid Tauri build issues
shell: pwsh
run: |
# Build the frontend app from the correct directory
cd ./frontend
+24
View File
@@ -0,0 +1,24 @@
# Builds and tests the workspace on FreeBSD, inside a FreeBSD VM on a Linux runner
# (GitHub has no FreeBSD runners).
name: FreeBSD
# The VM build is slow, so it runs nightly on main and on pushes to `release`, not on pull requests.
on:
schedule:
- cron: "27 4 * * *"
push:
branches: [release]
workflow_dispatch:
concurrency:
group: freebsd-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: read
jobs:
freebsd:
# Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos.
uses: storytold/craft-common-actions/.github/workflows/freebsd.yml@main
+23
View File
@@ -0,0 +1,23 @@
# Quick static checks for the release pipeline (seconds, no Rust build): workflow syntax,
# packaging shell/PowerShell scripts, and the Linux desktop/AppStream/Flatpak metadata.
name: Packaging lint
on:
push:
branches: [main, release]
paths: [".github/workflows/**", "packaging/**"]
pull_request:
paths: [".github/workflows/**", "packaging/**"]
workflow_dispatch:
permissions:
contents: read
concurrency:
group: packaging-lint-${{ github.ref }}
cancel-in-progress: true
jobs:
lint:
# Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos.
uses: storytold/craft-common-actions/.github/workflows/packaging-lint.yml@main
+1
View File
@@ -44,6 +44,7 @@ explicitly authorizes a cache cleanup.
- Prefer `use` imports over inline fully-qualified paths; only qualify inline for true one-offs or std collisions
- TypeScript with Nx, React, Vite, Zustand, and Three.js
- Use two spaces for indentation
- Each Tauri command belongs in its own file module, named after the command. Keep shared state, types, and helpers in separate support modules; see the [Storyteller command conventions](crates/desktop/artcraft/src/services/storyteller/commands/AGENTS.md).
### File Layout
@@ -0,0 +1,73 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
#[derive(Deserialize, Serialize, ToSchema)]
pub struct CreateLoginChallengeRequest {}
#[derive(Deserialize, Serialize, ToSchema)]
pub struct ReviewLoginChallengeRequest {
pub approval_token: String,
}
#[derive(Deserialize, Serialize, ToSchema)]
pub struct DecideLoginChallengeRequest {
pub approval_token: String,
pub approve: bool,
}
#[derive(Deserialize, Serialize, ToSchema)]
pub struct PollLoginChallengeRequest {
pub device_token: String,
}
#[derive(Deserialize, Serialize, ToSchema)]
pub struct CreateLoginChallengeResponse {
pub success: bool,
pub device_token: String,
pub verification_url: String,
pub confirmation_code: String,
pub expires_at: DateTime<Utc>,
pub poll_interval_seconds: u32,
}
#[derive(Deserialize, Serialize, ToSchema)]
pub struct ReviewLoginChallengeResponse {
pub success: bool,
pub status: LoginChallengeState,
pub maybe_failure_type: Option<LoginChallengeFailure>,
pub confirmation_code: String,
pub requesting_ip: String,
pub expires_at: DateTime<Utc>,
pub username: String,
}
#[derive(Deserialize, Serialize, ToSchema)]
pub struct LoginChallengeResponse {
pub success: bool,
pub status: LoginChallengeState,
pub maybe_failure_type: Option<LoginChallengeFailure>,
/// Present only to the device after the redemption transaction commits.
#[serde(skip_serializing_if = "Option::is_none")]
pub maybe_signed_session: Option<String>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum LoginChallengeState {
Pending,
Approved,
Redeemed,
Failed,
#[serde(other)]
Unknown,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum LoginChallengeFailure {
UserDeclined,
Expired,
#[serde(other)]
Unknown,
}
@@ -2,4 +2,5 @@ pub mod change_password;
pub mod edit_email;
pub mod edit_username;
pub mod login;
pub mod session_info;
pub mod session_info;
pub mod login_challenges;
@@ -1,11 +1,12 @@
use crate::common::responses::user_details_light::UserDetailsLight;
use enums::by_table::users::user_feature_flag::UserFeatureFlag;
use serde_derive::Serialize;
use serde_derive::{Deserialize, Serialize};
use serde::{Deserialize as DeserializeTrait, Deserializer};
use std::collections::BTreeSet;
use tokens::tokens::users::UserToken;
use utoipa::ToSchema;
#[derive(Serialize, Copy, Clone, ToSchema)]
#[derive(Serialize, Deserialize, Copy, Clone, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum FakeYouPlan {
Free,
@@ -14,7 +15,7 @@ pub enum FakeYouPlan {
Pro,
}
#[derive(Serialize, Copy, Clone, ToSchema)]
#[derive(Serialize, Deserialize, Copy, Clone, ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum StorytellerStreamPlan {
Free,
@@ -23,7 +24,7 @@ pub enum StorytellerStreamPlan {
Pro,
}
#[derive(Serialize, ToSchema)]
#[derive(Serialize, Deserialize, ToSchema)]
pub struct SessionUserInfo {
pub core_info: UserDetailsLight,
@@ -41,6 +42,7 @@ pub struct SessionUserInfo {
/// Collection of feature / rollout flags
/// This is the proper place to detect if a user has access to some rollout (non-paywall) feature.
/// NB: The BTreeSet maintains order so React doesn't introduce re-render state bugs when order changes
#[serde(default, deserialize_with = "deserialize_known_feature_flags")]
pub maybe_feature_flags: BTreeSet<UserFeatureFlag>,
// Premium plans:
@@ -76,7 +78,7 @@ pub struct SessionUserInfo {
pub can_delete_users: bool,
}
#[derive(Serialize, ToSchema)]
#[derive(Serialize, Deserialize, ToSchema)]
pub struct SessionOnboardingState {
/// If true, the user hasn't set their email.
pub email_not_set: bool,
@@ -91,9 +93,18 @@ pub struct SessionOnboardingState {
pub username_not_customized: bool,
}
#[derive(Serialize, ToSchema)]
#[derive(Serialize, Deserialize, ToSchema)]
pub struct SessionInfoSuccessResponse {
pub success: bool,
pub logged_in: bool,
pub user: Option<SessionUserInfo>,
}
// Server rollouts may introduce flags before a desktop release understands them.
// Ignore only unknown flag names; malformed values and authentication fields
// still fail decoding. Unknown flags never grant a client-side capability.
fn deserialize_known_feature_flags<'de, D>(deserializer: D) -> Result<BTreeSet<UserFeatureFlag>, D::Error>
where D: Deserializer<'de> {
let names = Vec::<String>::deserialize(deserializer)?;
Ok(names.into_iter().filter_map(|name| UserFeatureFlag::from_str(&name).ok()).collect())
}
@@ -0,0 +1,27 @@
# artcraft_client
Rust HTTP endpoint bindings used by the ArtCraft Tauri desktop app.
## Endpoint layout
- Each HTTP endpoint binding must live in its own descriptively named `.rs` file.
This applies to both free functions and methods implemented on a shared client.
Do not combine several routes into a catch-all file such as `login_challenges.rs`
or `password_auth.rs`.
- Group related bindings in a directory module. Keep `mod.rs` for module declarations;
it must not contain endpoint implementations. For example:
`endpoints/users/login_challenges/create_login_challenge.rs` and
`endpoints/users/login_challenges/poll_login_challenge.rs`.
- Keep each endpoint's route constant, request construction, and endpoint-specific
request/response types in its file. Keep shared wire types in `artcraft_api_defs`
or a dedicated shared type module; do not duplicate them across endpoint files.
- Put shared HTTP transport, client setup, and common error handling in a separate
utility module. A transport module must not accumulate endpoint bindings.
- Update callers to import types from their new modules when splitting bindings.
Preserve request methods, paths, headers, error behavior, and cookie handling.
## Validation
Run `cargo check --offline -p artcraft_client` for client changes. For native login
bindings, also run `SQLX_OFFLINE=true cargo test --offline -p artcraft --lib
login_bridge_tests`; these tests use local HTTP fixtures and temporary cookie jars.
@@ -7,3 +7,4 @@ pub mod media_files;
pub mod prompts;
pub mod stripe_artcraft;
pub mod subscriptions;
pub mod users;
@@ -0,0 +1,16 @@
use artcraft_api_defs::users::login_challenges::{CreateLoginChallengeRequest, CreateLoginChallengeResponse};
use reqwest::Method;
use crate::utils::login_challenge_client::{LoginChallengeClient, LoginChallengeClientError};
pub const CREATE_PATH: &str = "/v1/login_challenges/create";
impl LoginChallengeClient {
pub async fn create(&self) -> Result<CreateLoginChallengeResponse, LoginChallengeClientError> {
let request = self
.request(Method::POST, CREATE_PATH)
.json(&CreateLoginChallengeRequest {});
let (result, _) = self.send(request, CREATE_PATH).await?;
Ok(result)
}
}
@@ -0,0 +1,2 @@
pub mod create_login_challenge;
pub mod poll_login_challenge;
@@ -0,0 +1,30 @@
use artcraft_api_defs::users::login_challenges::{LoginChallengeResponse, PollLoginChallengeRequest};
use reqwest::Method;
use crate::utils::login_challenge_client::{LoginChallengeClient, LoginChallengeClientError};
pub const POLL_PATH: &str = "/v1/login_challenges/poll";
// Deliberately no Debug/Serialize: the wire result contains a bearer credential.
pub struct PollLoginChallengeResult {
pub response: LoginChallengeResponse,
pub session_set_cookie: Option<String>,
}
impl LoginChallengeClient {
pub async fn poll(
&self,
device_token: &str,
) -> Result<PollLoginChallengeResult, LoginChallengeClientError> {
let request = self
.request(Method::POST, POLL_PATH)
.json(&PollLoginChallengeRequest {
device_token: device_token.to_owned(),
});
let (response, session_set_cookie) = self.send(request, POLL_PATH).await?;
Ok(PollLoginChallengeResult {
response,
session_set_cookie,
})
}
}
@@ -0,0 +1,5 @@
pub mod login_challenges;
pub mod password_auth_response;
pub mod password_login;
pub mod password_signup;
pub mod session_info;
@@ -0,0 +1,8 @@
use serde::Deserialize;
// No Debug: the response contains a bearer credential.
#[derive(Deserialize)]
pub struct PasswordAuthResponse {
pub success: bool,
pub signed_session: Option<String>,
}
@@ -0,0 +1,20 @@
use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::utils::login_challenge_client::{LoginChallengeClient, LoginChallengeClientError};
use super::password_auth_response::PasswordAuthResponse;
pub const LOGIN_PATH: &str = "/v1/login";
// No Debug: the request contains credentials.
#[derive(Deserialize, Serialize)]
pub struct PasswordLoginRequest {
pub username_or_email: String,
pub password: String,
}
impl LoginChallengeClient {
pub async fn password_login(&self, request: &PasswordLoginRequest) -> Result<(PasswordAuthResponse, Option<String>), LoginChallengeClientError> {
self.send(self.request(Method::POST, LOGIN_PATH).json(request), LOGIN_PATH).await
}
}
@@ -0,0 +1,23 @@
use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::utils::login_challenge_client::{LoginChallengeClient, LoginChallengeClientError};
use super::password_auth_response::PasswordAuthResponse;
pub const SIGNUP_PATH: &str = "/v1/create_account";
// No Debug: the request contains credentials.
#[derive(Deserialize, Serialize)]
pub struct PasswordSignupRequest {
pub username: String,
pub email_address: String,
pub password: String,
pub password_confirmation: String,
pub signup_source: String,
}
impl LoginChallengeClient {
pub async fn password_signup(&self, request: &PasswordSignupRequest) -> Result<(PasswordAuthResponse, Option<String>), LoginChallengeClientError> {
self.send(self.request(Method::POST, SIGNUP_PATH).json(request), SIGNUP_PATH).await
}
}
@@ -0,0 +1,19 @@
use artcraft_api_defs::users::session_info::SessionInfoSuccessResponse;
use reqwest::Method;
use crate::utils::login_challenge_client::{LoginChallengeClient, LoginChallengeClientError};
pub const SESSION_PATH: &str = "/v1/session";
impl LoginChallengeClient {
pub async fn session(
&self,
signed_session: &str,
) -> Result<SessionInfoSuccessResponse, LoginChallengeClientError> {
let request = self
.request(Method::GET, SESSION_PATH)
.header("Cookie", format!("session={signed_session}"));
let (result, _) = self.send(request, SESSION_PATH).await?;
Ok(result)
}
}
@@ -0,0 +1,151 @@
use std::time::Duration;
use log::{info, warn};
use reqwest::header::SET_COOKIE;
use reqwest::{Client, Method, RequestBuilder};
use serde::de::DeserializeOwned;
use url::Url;
use crate::utils::api_host::ApiHost;
use crate::utils::constants::USER_AGENT;
/// Native transport for the session bridge. No request/response bodies, cookies,
/// or challenge credentials are logged, even at debug level. Never follows redirects.
#[derive(Clone)]
pub struct LoginChallengeClient {
client: Client,
api_host: ApiHost,
}
#[derive(Debug)]
pub struct LoginChallengeClientError {
pub status: Option<u16>,
pub message: &'static str,
}
impl LoginChallengeClient {
pub fn new(api_host: &ApiHost) -> Result<Self, LoginChallengeClientError> {
let mut builder = Client::builder();
if matches!(api_host, ApiHost::Localhost { .. }) {
builder = builder.no_proxy();
}
let client = builder
.timeout(Duration::from_secs(15))
.redirect(reqwest::redirect::Policy::none())
.user_agent(USER_AGENT)
.build()
.map_err(|_| LoginChallengeClientError::invalid("Unable to initialize login client"))?;
Ok(Self {
client,
api_host: api_host.clone(),
})
}
pub fn api_origin(&self) -> String {
self.api_host.to_api_hostname_and_scheme()
}
pub fn api_url(&self) -> Url {
Url::parse(&format!("{}/", self.api_origin())).expect("ApiHost must produce a valid origin")
}
pub fn allows_verification_url(&self, value: &str) -> bool {
let Ok(url) = Url::parse(value) else {
return false;
};
let origin_allowed = match self.api_host {
ApiHost::Localhost { .. } => ["http://localhost:4200", "http://127.0.0.1:4200", "http://localhost:4201", "http://127.0.0.1:4201"]
.contains(&url.origin().ascii_serialization().as_str()),
ApiHost::Storyteller => ["https://app.getartcraft.com", "https://getartcraft.com", "https://www.getartcraft.com"]
.contains(&url.origin().ascii_serialization().as_str()),
ApiHost::FakeYou => false,
};
origin_allowed
&& url.username().is_empty()
&& url.password().is_none()
&& url.path() == "/login/desktop"
&& url.query().is_none()
&& url
.fragment()
.and_then(|f| f.strip_prefix("approval_token="))
.map(|s| {
s.len() == 43
&& s
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-')
})
.unwrap_or(false)
}
pub(crate) fn request(&self, method: Method, path: &str) -> RequestBuilder {
self
.client
.request(method, format!("{}{path}", self.api_origin()))
.header("Accept", "application/json")
}
pub(crate) async fn send<T: DeserializeOwned>(
&self,
request: RequestBuilder,
path: &str,
) -> Result<(T, Option<String>), LoginChallengeClientError> {
let origin = self.api_origin();
info!("Website login request: origin={} path={}", origin, path);
let response = request.send().await.map_err(|_| {
warn!(
"Website login network failure: origin={} path={}",
origin, path
);
LoginChallengeClientError::invalid("Unable to reach the login server")
})?;
let status = response.status();
if !status.is_success() {
warn!(
"Website login HTTP failure: origin={} path={} status={}",
origin,
path,
status.as_u16()
);
return Err(LoginChallengeClientError {
status: Some(status.as_u16()),
message: "Login server rejected the request",
});
}
let session_cookie = response
.headers()
.get_all(SET_COOKIE)
.iter()
.filter_map(|v| v.to_str().ok())
.find(|v| {
cookie::Cookie::parse(*v)
.map(|c| c.name() == "session")
.unwrap_or(false)
})
.map(str::to_owned);
let body = response.json().await.map_err(|_| {
warn!(
"Website login invalid response: origin={} path={}",
origin, path
);
LoginChallengeClientError::invalid("Invalid response from login server")
})?;
Ok((body, session_cookie))
}
}
impl LoginChallengeClientError {
pub fn invalid(message: &'static str) -> Self {
Self {
status: None,
message,
}
}
}
impl std::fmt::Display for LoginChallengeClientError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} (HTTP {:?})", self.message, self.status)
}
}
impl std::error::Error for LoginChallengeClientError {}
@@ -7,3 +7,4 @@ pub (crate) mod http_get_anonymous;
pub mod api_host;
pub mod constants;
pub mod status_codes;
pub mod login_challenge_client;
@@ -1,10 +1,10 @@
use crate::core::state::app_env_configs::app_env_configs::AppEnvConfigs;
use crate::core::state::app_startup_time::AppStartupTime;
use crate::services::storyteller::state::storyteller_credential_manager::StorytellerCredentialManager;
use anyhow::anyhow;
use chrono::TimeDelta;
use errors::AnyhowResult;
use log::{error, info};
use once_cell::sync::Lazy;
use reqwest::Url;
use artcraft_client::credentials::storyteller_avt_cookie::StorytellerAvtCookie;
use artcraft_client::credentials::storyteller_credential_set::StorytellerCredentialSet;
@@ -13,24 +13,13 @@ use tauri::{AppHandle, Manager};
use tauri_plugin_http::reqwest_cookie_store::CookieStore;
use tauri_plugin_http::Http;
const STORYTELLER_ROOT_COOKIE_URL_STR: &str = "https://api.storyteller.ai/";
static STORYTELLER_ROOT_COOKIE_URL: Lazy<Url> = Lazy::new(|| {
Url::parse(STORYTELLER_ROOT_COOKIE_URL_STR).expect("URL should parse")
});
const AVT_COOKIE_NAME : &str = "visitor";
const SESSION_COOKIE_NAME : &str = "session";
const AVT_COOKIE_NAME: &str = "visitor";
const SESSION_COOKIE_NAME: &str = "session";
/// There's some kind of race condition that we need to wait for before inspecting cookies.
const RACE_CONDITION_WAIT_TIME: TimeDelta = TimeDelta::milliseconds(5000);
pub async fn persist_storyteller_cookies_task(
app: &AppHandle,
storyteller_credential_manager: &StorytellerCredentialManager,
app_startup_time: &AppStartupTime,
) -> AnyhowResult<()> {
pub async fn persist_storyteller_cookies_task(app: &AppHandle, storyteller_credential_manager: &StorytellerCredentialManager, app_startup_time: &AppStartupTime) -> AnyhowResult<()> {
//if app_startup_time.time_delta_since() < RACE_CONDITION_WAIT_TIME {
// // NB: There's an issue when the "main window thread" inquires about the
// // webview cookies shortly after app startup. When it attempts to dump the
@@ -41,33 +30,30 @@ pub async fn persist_storyteller_cookies_task(
//}
let maybe_http = app.try_state::<Http>();
let http = match maybe_http {
None => {
error!("No HTTP plugin found");
return Err(anyhow!("No HTTP plugin found"));
}
},
Some(http) => http,
};
match http.cookies_jar.store.lock() {
Err(err) => {
error!("Failed to lock cookie jar: {:?}", err);
return Err(anyhow!("Failed to lock cookie jar"));
}
},
Ok(cookie_jar) => {
sync_tauri_credentials(&cookie_jar, storyteller_credential_manager)?;
}
sync_tauri_credentials(&cookie_jar, storyteller_credential_manager, &storyteller_cookie_url(app)?)?;
},
}
Ok(())
}
fn sync_tauri_credentials(
cookie_store: &CookieStore,
storyteller_credential_manager: &StorytellerCredentialManager,
) -> AnyhowResult<()> {
let current_http_plugin_credentials = get_credentials_from_cookie_store(cookie_store)?;
pub(crate) fn sync_tauri_credentials(cookie_store: &CookieStore, storyteller_credential_manager: &StorytellerCredentialManager, api_url: &Url) -> AnyhowResult<()> {
let current_http_plugin_credentials = get_credentials_from_cookie_store(cookie_store, api_url)?;
let mut replace_credentials = true;
@@ -78,31 +64,80 @@ fn sync_tauri_credentials(
replace_credentials = false;
}
}
if replace_credentials {
info!("Syncing ArtCraft credentials ...");
storyteller_credential_manager.set_credentials(&current_http_plugin_credentials)?;
// NB: tauri-plugin-http stores the credentials on disk, so we can defer to that for now.
//storyteller_credential_manager.persist_all_to_disk()?;
}
Ok(())
}
pub fn get_credentials_from_cookie_store(cookie_jar: &CookieStore) -> AnyhowResult<StorytellerCredentialSet> {
pub fn get_credentials_from_cookie_store(cookie_jar: &CookieStore, api_url: &Url) -> AnyhowResult<StorytellerCredentialSet> {
let mut avt_cookie = None;
let mut session_cookie = None;
for cookie in cookie_jar.iter_unexpired() {
if let Some(avt) = StorytellerAvtCookie::maybe_from_cookie(&cookie) {
avt_cookie = Some(avt);
} else if let Some(session) = StorytellerSessionCookie::maybe_from_cookie(&cookie) {
session_cookie = Some(session);
// Only cookies applicable to our API origin can become ArtCraft credentials.
for (name, value) in cookie_jar.get_request_values(api_url) {
match name {
AVT_COOKIE_NAME => avt_cookie = Some(StorytellerAvtCookie::new(value.to_owned())),
SESSION_COOKIE_NAME => session_cookie = Some(StorytellerSessionCookie::new(value.to_owned())),
_ => {},
}
}
Ok(StorytellerCredentialSet::initialize(
avt_cookie,
session_cookie,
))
Ok(StorytellerCredentialSet::initialize(avt_cookie, session_cookie))
}
pub(crate) fn storyteller_cookie_url(app: &AppHandle) -> AnyhowResult<Url> {
let config = app.try_state::<AppEnvConfigs>().ok_or_else(|| anyhow!("API configuration unavailable"))?;
Ok(Url::parse(&format!("{}/", config.storyteller_host.to_api_hostname_and_scheme()))?)
}
#[cfg(test)]
mod login_bridge_tests {
use super::*;
use crate::core::state::data_dir::app_data_root::AppDataRoot;
use tauri_plugin_http::reqwest_cookie_store::RawCookie;
#[test]
fn approved_response_cookie_reaches_native_credentials() {
let directory = tempfile::tempdir().unwrap();
let root = AppDataRoot::create_existing(directory.path()).unwrap();
let manager = StorytellerCredentialManager::initialize_empty(&root);
let mut jar = CookieStore::default();
let url = Url::parse("https://api.storyteller.ai/v1/login_challenges/poll").unwrap();
jar.store_response_cookies([RawCookie::parse("session=signed_backend_cookie; Secure; HttpOnly; Path=/; Max-Age=3600").unwrap().into_owned(), RawCookie::parse("visitor=existing_visitor; Secure; Path=/; Max-Age=3600").unwrap().into_owned()].into_iter(), &url);
sync_tauri_credentials(&jar, &manager, &url).unwrap();
let credentials = manager.get_credentials_required().unwrap();
assert_eq!(credentials.session.unwrap().as_str(), "signed_backend_cookie");
assert_eq!(credentials.avt.unwrap().as_str(), "existing_visitor");
// Retrying the same successful poll is idempotent for native credentials too.
sync_tauri_credentials(&jar, &manager, &url).unwrap();
assert_eq!(manager.get_credentials_required().unwrap().session.unwrap().as_str(), "signed_backend_cookie");
}
#[test]
fn unrelated_and_expired_cookies_cannot_become_artcraft_sessions() {
let url = Url::parse("https://api.storyteller.ai/").unwrap();
let mut jar = CookieStore::default();
jar.store_response_cookies([RawCookie::parse("session=unrelated; Path=/; Max-Age=3600").unwrap().into_owned()].into_iter(), &Url::parse("https://unrelated.example/").unwrap());
assert!(get_credentials_from_cookie_store(&jar, &url).unwrap().session.is_none());
jar.store_response_cookies([RawCookie::parse("session=expired; Secure; Path=/; Max-Age=0").unwrap().into_owned()].into_iter(), &url);
assert!(get_credentials_from_cookie_store(&jar, &url).unwrap().session.is_none());
}
#[test]
fn configured_local_api_does_not_import_production_cookies() {
let production = Url::parse("https://api.storyteller.ai/").unwrap();
let local = Url::parse("http://localhost:12345/").unwrap();
let mut jar = CookieStore::default();
jar.store_response_cookies([RawCookie::parse("session=production; Secure; Path=/").unwrap().into_owned()].into_iter(), &production);
assert!(get_credentials_from_cookie_store(&jar, &local).unwrap().session.is_none());
jar.store_response_cookies([RawCookie::parse("session=development; Path=/").unwrap().into_owned()].into_iter(), &local);
assert_eq!(get_credentials_from_cookie_store(&jar, &local).unwrap().session.unwrap().as_str(), "development");
assert_eq!(get_credentials_from_cookie_store(&jar, &production).unwrap().session.unwrap().as_str(), "production");
}
}
+14
View File
@@ -63,6 +63,13 @@ use crate::services::sora::commands::sora_logout_command::sora_logout_command;
use crate::services::sora::state::sora_credential_manager::SoraCredentialManager;
use crate::services::sora::state::sora_task_queue::SoraTaskQueue;
use crate::services::sora::threads::sora_task_polling::sora_task_polling_thread::sora_task_polling_thread;
use crate::services::storyteller::commands::login_bridge::DesktopLoginBridgeState;
use crate::services::storyteller::commands::storyteller_cancel_login_challenge_command::storyteller_cancel_login_challenge_command;
use crate::services::storyteller::commands::storyteller_create_login_challenge_command::storyteller_create_login_challenge_command;
use crate::services::storyteller::commands::storyteller_get_login_session_command::storyteller_get_login_session_command;
use crate::services::storyteller::commands::storyteller_password_login_command::storyteller_password_login_command;
use crate::services::storyteller::commands::storyteller_password_signup_command::storyteller_password_signup_command;
use crate::services::storyteller::commands::storyteller_poll_login_challenge_command::storyteller_poll_login_challenge_command;
use crate::services::storyteller::commands::storyteller_get_credits_command::storyteller_get_credits_command;
use crate::services::storyteller::commands::storyteller_get_subscription_command::storyteller_get_subscription_command;
use crate::services::storyteller::commands::storyteller_purge_credentials_command::storyteller_purge_credentials_command;
@@ -201,6 +208,7 @@ pub fn run() {
})
.manage(app_data_root)
.manage(app_env_configs)
.manage(DesktopLoginBridgeState::default())
.manage(app_preferences)
.manage(artcraft_platform_info)
.manage(artcraft_usage_tracker)
@@ -259,6 +267,12 @@ pub fn run() {
set_provider_order_command,
sora_get_credential_info_command,
sora_logout_command,
storyteller_get_login_session_command,
storyteller_password_login_command,
storyteller_password_signup_command,
storyteller_create_login_challenge_command,
storyteller_poll_login_challenge_command,
storyteller_cancel_login_challenge_command,
storyteller_get_credits_command,
storyteller_get_subscription_command,
storyteller_open_credits_purchase_command,
@@ -0,0 +1,17 @@
# Tauri Commands
- Put each `#[tauri::command]` function in its own `.rs` file module named after
that command, such as `storyteller_create_login_challenge_command.rs`. Do not
collect multiple commands in a catch-all `*_commands.rs` file.
- Keep `mod.rs` for module declarations. Put shared state, response/error types,
and implementation helpers in separate support modules, with the narrowest
visibility needed by their callers.
- Update the imports used by `tauri::generate_handler!` when moving commands.
Preserve IPC command names and arguments unless changing that contract is
explicitly part of the task.
- Authentication HTTP requests run in Rust through `artcraft_client` endpoint
bindings. The frontend invokes Tauri commands; it does not call the API
directly, and desktop commands do not connect to the backend MySQL database.
- For login bridge changes, run the existing native fixture tests with
`SQLX_OFFLINE=true cargo test --offline -p artcraft --lib login_bridge_tests`.
These tests use loopback HTTP servers and temporary cookie stores.
@@ -0,0 +1,340 @@
use std::collections::HashMap;
use std::sync::Arc;
use artcraft_api_defs::users::login_challenges::{LoginChallengeFailure, LoginChallengeState};
use artcraft_api_defs::users::session_info::SessionUserInfo;
use artcraft_client::endpoints::users::password_login::PasswordLoginRequest;
use artcraft_client::endpoints::users::password_signup::PasswordSignupRequest;
use artcraft_client::utils::api_host::ApiHost;
use artcraft_client::utils::login_challenge_client::{
LoginChallengeClient, LoginChallengeClientError,
};
use chrono::{DateTime, Utc};
use log::{info, warn};
use serde::Serialize;
use tauri::{AppHandle, Manager};
use tauri_plugin_http::reqwest_cookie_store::{CookieStoreMutex, RawCookie};
use tauri_plugin_http::Http;
use tokio::sync::Mutex;
use uuid_utils::uuid::generate_random_uuid;
use crate::core::threads::main_window_thread::persist_storyteller_cookies_task::get_credentials_from_cookie_store;
use crate::services::storyteller::state::storyteller_credential_manager::StorytellerCredentialManager;
const MAX_PENDING_CHALLENGES: usize = 8;
/// All bearer credentials stay in Rust. The UI holds only a local lookup handle.
#[derive(Default)]
pub struct DesktopLoginBridgeState {
pending: Mutex<HashMap<String, PendingChallenge>>,
}
#[derive(Clone)]
struct PendingChallenge {
device_token: String,
expires_at: DateTime<Utc>,
client: LoginChallengeClient,
}
#[derive(Serialize)]
pub struct DesktopLoginChallenge {
pub challenge_id: String,
pub verification_url: String,
pub confirmation_code: String,
pub expires_at: DateTime<Utc>,
pub poll_interval_seconds: u32,
}
#[derive(Serialize)]
pub struct DesktopLoginOutcome {
pub status: LoginChallengeState,
pub maybe_failure_type: Option<LoginChallengeFailure>,
pub maybe_user: Option<SessionUserInfo>,
}
#[derive(Debug, Serialize)]
pub struct DesktopLoginError {
pub status: Option<u16>,
pub message: String,
pub retryable: bool,
}
impl DesktopLoginError {
fn local(message: &str) -> Self {
Self {
status: None,
message: message.to_owned(),
retryable: false,
}
}
pub(super) fn from_client(origin: &str, error: LoginChallengeClientError) -> Self {
warn!(
"Website login failed: origin={} status={:?} reason={}",
origin, error.status, error.message
);
Self {
retryable: error
.status
.map(|s| s == 429 || s >= 500)
.unwrap_or(error.message == "Unable to reach the login server"),
status: error.status,
message: format!("{}: {} (HTTP {:?})", origin, error.message, error.status),
}
}
}
pub(super) async fn current_login_session(
client: &LoginChallengeClient,
jar: &Arc<CookieStoreMutex>,
) -> Result<Option<SessionUserInfo>, DesktopLoginError> {
let credentials = {
let store = jar
.store
.lock()
.map_err(|_| DesktopLoginError::local("HTTP cookie store unavailable"))?;
get_credentials_from_cookie_store(&store, &client.api_url())
.map_err(|_| DesktopLoginError::local("Unable to read session cookie"))?
};
let Some(signed) = credentials.session else {
return Ok(None);
};
let session = match client.session(signed.as_str()).await {
Ok(session) => session,
Err(error) if error.status == Some(401) => return Ok(None),
Err(error) => return Err(DesktopLoginError::from_client(&client.api_origin(), error)),
};
Ok(
session
.user
.filter(|_| session.success && session.logged_in),
)
}
pub(super) async fn password_auth(
host: &ApiHost,
manager: &StorytellerCredentialManager,
jar: &Arc<CookieStoreMutex>,
login: Option<PasswordLoginRequest>,
signup: Option<PasswordSignupRequest>,
) -> Result<SessionUserInfo, DesktopLoginError> {
let client = LoginChallengeClient::new(host)
.map_err(|e| DesktopLoginError::from_client(&host.to_api_hostname_and_scheme(), e))?;
let response = match (login, signup) {
(Some(request), None) => client.password_login(&request).await,
(None, Some(request)) => client.password_signup(&request).await,
_ => return Err(DesktopLoginError::local("Invalid authentication request")),
}
.map_err(|e| DesktopLoginError::from_client(&client.api_origin(), e))?;
let (response, cookie) = response;
if !response.success {
return Err(DesktopLoginError::local(
"Sign-in failed. Check your account details and try again.",
));
}
let signed = response
.signed_session
.ok_or_else(|| DesktopLoginError::local("Login server did not issue a session"))?;
let cookie =
cookie.ok_or_else(|| DesktopLoginError::local("Login server did not set a session cookie"))?;
let (cookie, user) = verify_session(&client, &signed, cookie).await?;
install_session_cookie(&client, &signed, cookie, manager, jar)?;
persist_session_cookie(jar).await?;
Ok(user)
}
pub(super) fn http_cookie_jar(app: &AppHandle) -> Result<Arc<CookieStoreMutex>, DesktopLoginError> {
Ok(
app
.try_state::<Http>()
.ok_or_else(|| DesktopLoginError::local("HTTP cookie store unavailable"))?
.cookies_jar
.clone(),
)
}
pub(super) async fn begin_challenge(
host: &ApiHost,
state: &DesktopLoginBridgeState,
) -> Result<DesktopLoginChallenge, DesktopLoginError> {
let client = LoginChallengeClient::new(host)
.map_err(|e| DesktopLoginError::from_client(&host.to_api_hostname_and_scheme(), e))?;
let created = client
.create()
.await
.map_err(|e| DesktopLoginError::from_client(&client.api_origin(), e))?;
if !created.success
|| !client.allows_verification_url(&created.verification_url)
|| created.device_token.len() != 43
|| created.expires_at <= Utc::now()
{
return Err(DesktopLoginError::local(
"Invalid login challenge or website/API environment mismatch",
));
}
let mut pending = state.pending.lock().await;
pending.retain(|_, value| value.expires_at > Utc::now());
if pending.len() >= MAX_PENDING_CHALLENGES {
return Err(DesktopLoginError::local("Too many pending login attempts"));
}
let challenge_id = generate_random_uuid();
pending.insert(
challenge_id.clone(),
PendingChallenge {
device_token: created.device_token,
expires_at: created.expires_at,
client,
},
);
Ok(DesktopLoginChallenge {
challenge_id,
verification_url: created.verification_url,
confirmation_code: created.confirmation_code,
expires_at: created.expires_at,
poll_interval_seconds: created.poll_interval_seconds.max(5),
})
}
pub(super) async fn poll_challenge(
state: &DesktopLoginBridgeState,
manager: &StorytellerCredentialManager,
jar: &Arc<CookieStoreMutex>,
challenge_id: &str,
) -> Result<DesktopLoginOutcome, DesktopLoginError> {
let challenge = {
let mut pending = state.pending.lock().await;
let challenge = pending.get(challenge_id).cloned().ok_or_else(|| {
DesktopLoginError::local("Login attempt was cancelled or is no longer available")
})?;
if challenge.expires_at <= Utc::now() {
pending.remove(challenge_id);
return Ok(expired());
}
challenge
};
let origin = challenge.client.api_origin();
let polled = challenge
.client
.poll(&challenge.device_token)
.await
.map_err(|e| DesktopLoginError::from_client(&origin, e))?;
if !polled.response.success || polled.response.status == LoginChallengeState::Unknown {
return Err(DesktopLoginError::local("Unrecognized login response"));
}
if polled.response.status != LoginChallengeState::Redeemed {
if polled.response.status == LoginChallengeState::Failed {
state.pending.lock().await.remove(challenge_id);
}
return Ok(DesktopLoginOutcome {
status: polled.response.status,
maybe_failure_type: polled.response.maybe_failure_type,
maybe_user: None,
});
}
let signed = polled
.response
.maybe_signed_session
.ok_or_else(|| DesktopLoginError::local("Login server did not issue a session"))?;
let set_cookie = polled
.session_set_cookie
.ok_or_else(|| DesktopLoginError::local("Login server did not set a session cookie"))?;
let (cookie, user) = verify_session(&challenge.client, &signed, set_cookie).await?;
{
let pending = state.pending.lock().await;
// A cancellation during either HTTP request must prevent local installation.
if !pending.contains_key(challenge_id) {
return Err(DesktopLoginError::local("Login attempt was cancelled"));
}
if challenge.expires_at <= Utc::now() {
return Ok(expired());
}
install_session_cookie(&challenge.client, &signed, cookie, manager, jar)?;
}
// Keep the challenge for idempotent IPC retries; it expires on its original deadline.
persist_session_cookie(jar).await?;
info!("Website login complete: origin={}", origin);
Ok(DesktopLoginOutcome {
status: LoginChallengeState::Redeemed,
maybe_failure_type: None,
maybe_user: Some(user),
})
}
pub(super) async fn cancel_challenge(state: &DesktopLoginBridgeState, challenge_id: &str) {
state.pending.lock().await.remove(challenge_id);
}
async fn verify_session(
client: &LoginChallengeClient,
signed: &str,
set_cookie: String,
) -> Result<(RawCookie<'static>, SessionUserInfo), DesktopLoginError> {
let cookie = RawCookie::parse(set_cookie)
.map_err(|_| DesktopLoginError::local("Invalid session cookie"))?
.into_owned();
if cookie.name() != "session" || cookie.value() != signed {
return Err(DesktopLoginError::local(
"Session cookie did not match the redeemed session",
));
}
// Validate on the SAME native API host before publishing any new credentials.
let session = client
.session(&signed)
.await
.map_err(|e| DesktopLoginError::from_client(&client.api_origin(), e))?;
let user = session
.user
.filter(|_| session.success && session.logged_in)
.ok_or_else(|| DesktopLoginError::local("The redeemed session could not authenticate"))?;
Ok((cookie, user))
}
fn install_session_cookie(
client: &LoginChallengeClient,
signed: &str,
cookie: RawCookie<'static>,
manager: &StorytellerCredentialManager,
jar: &Arc<CookieStoreMutex>,
) -> Result<(), DesktopLoginError> {
let mut store = jar
.store
.lock()
.map_err(|_| DesktopLoginError::local("HTTP cookie store unavailable"))?;
let mut candidate = store.clone();
candidate.store_response_cookies([cookie].into_iter(), &client.api_url());
let credentials = get_credentials_from_cookie_store(&candidate, &client.api_url())
.map_err(|_| DesktopLoginError::local("Unable to read session cookie"))?;
if credentials.session.as_ref().map(|c| c.as_str()) != Some(signed) {
return Err(DesktopLoginError::local(
"Session cookie does not apply to the configured API host",
));
}
manager
.set_credentials(&credentials)
.map_err(|_| DesktopLoginError::local("Unable to store native login credentials"))?;
*store = candidate;
Ok(())
}
async fn persist_session_cookie(jar: &Arc<CookieStoreMutex>) -> Result<(), DesktopLoginError> {
let saved = jar
.request_save()
.map_err(|_| DesktopLoginError::local("Unable to persist login cookie"))?;
tokio::task::spawn_blocking(move || saved.recv())
.await
.map_err(|_| DesktopLoginError::local("Unable to persist login cookie"))?
.map_err(|_| DesktopLoginError::local("Unable to persist login cookie"))?;
Ok(())
}
fn expired() -> DesktopLoginOutcome {
DesktopLoginOutcome {
status: LoginChallengeState::Failed,
maybe_failure_type: Some(LoginChallengeFailure::Expired),
maybe_user: None,
}
}
#[cfg(test)]
#[path = "storyteller_login_bridge_tests.rs"]
mod login_bridge_tests;
@@ -2,4 +2,11 @@ pub mod storyteller_purge_credentials_command;
pub mod storyteller_get_credits_command;
pub mod storyteller_get_subscription_command;
pub mod stripe_checkout;
pub mod stripe_customer_portal;
pub mod stripe_customer_portal;
pub(crate) mod login_bridge;
pub mod storyteller_cancel_login_challenge_command;
pub mod storyteller_create_login_challenge_command;
pub mod storyteller_get_login_session_command;
pub mod storyteller_password_login_command;
pub mod storyteller_password_signup_command;
pub mod storyteller_poll_login_challenge_command;
@@ -0,0 +1,14 @@
use tauri::State;
use crate::services::storyteller::commands::login_bridge::{
cancel_challenge, DesktopLoginBridgeState, DesktopLoginError,
};
#[tauri::command]
pub async fn storyteller_cancel_login_challenge_command(
state: State<'_, DesktopLoginBridgeState>,
challenge_id: String,
) -> Result<(), DesktopLoginError> {
cancel_challenge(&state, &challenge_id).await;
Ok(())
}
@@ -0,0 +1,14 @@
use tauri::State;
use crate::core::state::app_env_configs::app_env_configs::AppEnvConfigs;
use crate::services::storyteller::commands::login_bridge::{
begin_challenge, DesktopLoginBridgeState, DesktopLoginChallenge, DesktopLoginError,
};
#[tauri::command]
pub async fn storyteller_create_login_challenge_command(
config: State<'_, AppEnvConfigs>,
state: State<'_, DesktopLoginBridgeState>,
) -> Result<DesktopLoginChallenge, DesktopLoginError> {
begin_challenge(&config.storyteller_host, &state).await
}
@@ -0,0 +1,19 @@
use artcraft_api_defs::users::session_info::SessionUserInfo;
use artcraft_client::utils::login_challenge_client::LoginChallengeClient;
use tauri::{AppHandle, State};
use crate::core::state::app_env_configs::app_env_configs::AppEnvConfigs;
use crate::services::storyteller::commands::login_bridge::{
current_login_session, http_cookie_jar, DesktopLoginError,
};
#[tauri::command]
pub async fn storyteller_get_login_session_command(
app: AppHandle,
config: State<'_, AppEnvConfigs>,
) -> Result<Option<SessionUserInfo>, DesktopLoginError> {
let client = LoginChallengeClient::new(&config.storyteller_host).map_err(|error| {
DesktopLoginError::from_client(&config.storyteller_host.to_api_hostname_and_scheme(), error)
})?;
current_login_session(&client, &http_cookie_jar(&app)?).await
}
@@ -0,0 +1,436 @@
//! Commands' native implementation + real ArtCraft bindings + loopback HTTP.
//! All listeners bind to an ephemeral local port; no production host is contacted.
use super::*;
use crate::core::state::data_dir::app_data_root::AppDataRoot;
use artcraft_client::credentials::storyteller_avt_cookie::StorytellerAvtCookie;
use artcraft_client::credentials::storyteller_credential_set::StorytellerCredentialSet;
use serde_json::{json, Value};
use std::io::{Read, Write};
use std::net::TcpListener;
use std::thread;
use std::time::Duration;
use tauri_plugin_http::reqwest_cookie_store::CookieStore;
const SIGNED: &str = "signed_native_session_fixture";
const DEVICE: &str = "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD";
#[tokio::test]
async fn approved_session_is_verified_installed_persisted_and_retried_entirely_in_rust() {
let server = server(vec![
create(),
pending(),
redeemed(),
session(),
redeemed(),
session(),
]);
let h = harness();
// Native installation must preserve visitor cookies and avoid importing other origins.
h.jar.store.lock().unwrap().store_response_cookies(
[RawCookie::parse("visitor=existing; Path=/; Max-Age=3600")
.unwrap()
.into_owned()]
.into_iter(),
&server_url(&server),
);
h.manager
.set_credentials(&StorytellerCredentialSet::initialize_with_just_avt(
StorytellerAvtCookie::new("existing".into()),
))
.unwrap();
let created = begin_challenge(&server.host, &h.state).await.unwrap();
let public = serde_json::to_string(&created).unwrap();
assert!(!public.contains(DEVICE));
assert!(!public.contains("device_token"));
assert!(h
.manager
.get_credentials_required()
.unwrap()
.session
.is_none());
let wait = poll_challenge(&h.state, &h.manager, &h.jar, &created.challenge_id)
.await
.unwrap();
assert_eq!(wait.status, LoginChallengeState::Pending);
assert!(h
.manager
.get_credentials_required()
.unwrap()
.session
.is_none());
for _ in 0..2 {
let result = poll_challenge(&h.state, &h.manager, &h.jar, &created.challenge_id)
.await
.unwrap();
assert_eq!(result.status, LoginChallengeState::Redeemed);
assert_eq!(result.maybe_user.as_ref().unwrap().username, "google_user");
// This was the real failure: use_qt exists on the server, not this desktop.
let flags = &result.maybe_user.as_ref().unwrap().maybe_feature_flags;
assert_eq!(flags.len(), 1);
assert_eq!(flags.first().unwrap().to_str(), "studio");
assert!(!serde_json::to_string(&result).unwrap().contains(SIGNED));
let credentials = h.manager.get_credentials_required().unwrap();
assert_eq!(credentials.session.unwrap().as_str(), SIGNED);
assert_eq!(credentials.avt.unwrap().as_str(), "existing");
assert!(std::fs::read_to_string(&h.jar.path)
.unwrap()
.contains(SIGNED));
}
server.thread.join().unwrap();
}
#[tokio::test]
async fn rejection_and_timeout_never_install_a_session() {
for expired in [false, true] {
let mut steps = vec![create()];
if !expired {
steps.push(step(
"/v1/login_challenges/poll",
json!({"success":true,"status":"failed","maybe_failure_type":"user_declined"}),
));
}
let server = server(steps);
let h = harness();
let created = begin_challenge(&server.host, &h.state).await.unwrap();
if expired {
h.state
.pending
.lock()
.await
.get_mut(&created.challenge_id)
.unwrap()
.expires_at = Utc::now();
}
let result = poll_challenge(&h.state, &h.manager, &h.jar, &created.challenge_id)
.await
.unwrap();
assert_eq!(result.status, LoginChallengeState::Failed);
assert_eq!(
result.maybe_failure_type,
Some(if expired {
LoginChallengeFailure::Expired
} else {
LoginChallengeFailure::UserDeclined
})
);
assert!(h.manager.get_credentials().unwrap().is_none());
assert!(h
.jar
.store
.lock()
.unwrap()
.iter_unexpired()
.next()
.is_none());
server.thread.join().unwrap();
}
}
#[tokio::test]
async fn invalid_downstream_session_is_not_installed_and_error_has_host_without_credentials() {
let mut invalid = session();
invalid.status = 401;
invalid.body = json!({"echo": SIGNED});
let server = server(vec![create(), redeemed(), invalid]);
let h = harness();
let created = begin_challenge(&server.host, &h.state).await.unwrap();
let error = poll_challenge(&h.state, &h.manager, &h.jar, &created.challenge_id)
.await
.err()
.unwrap();
assert_eq!(error.status, Some(401));
assert!(!error.retryable);
assert!(error
.message
.contains(&server.host.to_api_hostname_and_scheme()));
assert!(!format!("{error:?}").contains(SIGNED));
assert!(h.manager.get_credentials().unwrap().is_none());
assert!(h
.jar
.store
.lock()
.unwrap()
.iter_unexpired()
.next()
.is_none());
server.thread.join().unwrap();
}
#[tokio::test]
async fn cancelling_during_http_prevents_late_cookie_installation() {
let mut delayed = redeemed();
delayed.delay = Duration::from_millis(150);
let server = server(vec![create(), delayed, session()]);
let h = harness();
let created = begin_challenge(&server.host, &h.state).await.unwrap();
let (result, _) = tokio::join!(
poll_challenge(&h.state, &h.manager, &h.jar, &created.challenge_id),
async {
tokio::time::sleep(Duration::from_millis(30)).await;
cancel_challenge(&h.state, &created.challenge_id).await;
}
);
assert!(result.is_err());
assert!(h.manager.get_credentials().unwrap().is_none());
assert!(h
.jar
.store
.lock()
.unwrap()
.iter_unexpired()
.next()
.is_none());
server.thread.join().unwrap();
}
#[tokio::test]
async fn unknown_states_and_redirects_fail_closed() {
for redirect in [false, true] {
let mut result = step(
"/v1/login_challenges/poll",
json!({"success":true,"status":"future_state","maybe_signed_session":SIGNED}),
);
if redirect {
result.status = 302;
result.headers = "Location: https://must-never-be-contacted.invalid/\r\n".into();
}
let server = server(vec![create(), result]);
let h = harness();
let created = begin_challenge(&server.host, &h.state).await.unwrap();
let error = poll_challenge(&h.state, &h.manager, &h.jar, &created.challenge_id)
.await
.err()
.unwrap();
assert!(!error.retryable);
if redirect {
assert_eq!(error.status, Some(302));
}
assert!(h.manager.get_credentials().unwrap().is_none());
server.thread.join().unwrap();
}
}
#[test]
fn website_origin_must_match_native_api_environment() {
let local = LoginChallengeClient::new(&ApiHost::Localhost { port: 12345 }).unwrap();
let prod = LoginChallengeClient::new(&ApiHost::Storyteller).unwrap();
let fragment = format!("#approval_token={}", "A".repeat(43));
assert!(local.allows_verification_url(&format!("http://localhost:4201/login/desktop{fragment}")));
assert!(!local.allows_verification_url(&format!(
"https://app.getartcraft.com/login/desktop{fragment}"
)));
assert!(prod.allows_verification_url(&format!(
"https://app.getartcraft.com/login/desktop{fragment}"
)));
assert!(!prod.allows_verification_url(&format!("http://localhost:4201/login/desktop{fragment}")));
}
#[tokio::test]
async fn malformed_session_data_still_cannot_install_credentials() {
for (field, value) in [("maybe_feature_flags", json!([42])), ("username", Value::Null)] {
let mut invalid = session();
invalid.body["user"][field] = value;
let server = server(vec![create(), redeemed(), invalid]);
let h = harness();
let created = begin_challenge(&server.host, &h.state).await.unwrap();
assert!(poll_challenge(&h.state, &h.manager, &h.jar, &created.challenge_id).await.is_err());
assert!(h.manager.get_credentials().unwrap().is_none());
assert!(h.jar.store.lock().unwrap().iter_unexpired().next().is_none());
server.thread.join().unwrap();
}
}
#[tokio::test]
async fn password_login_and_signup_verify_persist_and_recheck_via_native_api() {
for signup in [false, true] {
let mut auth = redeemed();
auth.path = if signup { "/v1/create_account" } else { "/v1/login" };
auth.body = json!({"success":true,"signed_session":SIGNED});
let server = server(vec![auth, session(), session()]);
let h = harness();
let (login, signup) = if signup {
(None, Some(PasswordSignupRequest { username: "google_user".into(), email_address: "user@example.test".into(), password: "fixture_password".into(), password_confirmation: "fixture_password".into(), signup_source: "artcraft".into() }))
} else {
(Some(PasswordLoginRequest { username_or_email: "google_user".into(), password: "fixture_password".into() }), None)
};
let user = password_auth(&server.host, &h.manager, &h.jar, login, signup).await.unwrap();
assert_eq!(user.username, "google_user");
assert_eq!(h.manager.get_credentials_required().unwrap().session.unwrap().as_str(), SIGNED);
assert!(std::fs::read_to_string(&h.jar.path).unwrap().contains(SIGNED));
let client = LoginChallengeClient::new(&server.host).unwrap();
assert_eq!(current_login_session(&client, &h.jar).await.unwrap().unwrap().username, "google_user");
server.thread.join().unwrap();
}
}
#[tokio::test]
async fn empty_native_cookie_jar_needs_no_session_http_request() {
let h = harness();
let client = LoginChallengeClient::new(&ApiHost::Localhost { port: 1 }).unwrap();
assert!(current_login_session(&client, &h.jar).await.unwrap().is_none());
}
struct Harness {
_directory: tempfile::TempDir,
state: DesktopLoginBridgeState,
manager: StorytellerCredentialManager,
jar: Arc<CookieStoreMutex>,
}
struct Server {
host: ApiHost,
thread: thread::JoinHandle<()>,
}
struct Step {
path: &'static str,
status: u16,
body: Value,
headers: String,
delay: Duration,
}
fn harness() -> Harness {
let directory = tempfile::tempdir().unwrap();
let root = AppDataRoot::create_existing(directory.path()).unwrap();
let manager = StorytellerCredentialManager::initialize_empty(&root);
let jar = Arc::new(CookieStoreMutex::new(
directory.path().join("cookies.json"),
CookieStore::default(),
));
Harness {
_directory: directory,
state: DesktopLoginBridgeState::default(),
manager,
jar,
}
}
fn server_url(server: &Server) -> reqwest::Url {
reqwest::Url::parse(&format!("{}/", server.host.to_api_hostname_and_scheme())).unwrap()
}
fn server(steps: Vec<Step>) -> Server {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
let thread = thread::spawn(move || {
for step in steps {
let (mut stream, _) = listener.accept().unwrap();
stream
.set_read_timeout(Some(Duration::from_secs(10)))
.unwrap();
let mut data = Vec::new();
let mut buffer = [0; 4096];
loop {
let count = stream.read(&mut buffer).unwrap();
assert!(count > 0);
data.extend_from_slice(&buffer[..count]);
if let Some(end) = data.windows(4).position(|v| v == b"\r\n\r\n") {
let headers = String::from_utf8_lossy(&data[..end]).to_lowercase();
let length: usize = headers
.lines()
.find_map(|s| s.strip_prefix("content-length:"))
.map(|s| s.trim().parse().unwrap())
.unwrap_or(0);
if data.len() >= end + 4 + length {
break;
}
}
}
let request = String::from_utf8(data).unwrap();
assert_eq!(request.split_whitespace().nth(1), Some(step.path));
assert!(request
.to_lowercase()
.contains(&format!("host: localhost:{port}")));
if step.path == "/v1/session" {
assert!(request.starts_with("GET "));
assert!(request.contains(&format!("session={SIGNED}")));
} else {
assert!(request.starts_with("POST "));
assert!(!request.to_lowercase().contains("cookie:"));
if step.path.ends_with("/poll") {
let body: Value =
serde_json::from_str(request.split_once("\r\n\r\n").unwrap().1).unwrap();
assert_eq!(body, json!({"device_token":DEVICE}));
}
}
thread::sleep(step.delay);
let body = step.body.to_string();
write!(stream, "HTTP/1.1 {} Test\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n{}\r\n{}", step.status, body.len(), step.headers, body).unwrap();
}
});
Server {
host: ApiHost::Localhost { port: port.into() },
thread,
}
}
fn step(path: &'static str, body: Value) -> Step {
Step {
path,
body,
status: 200,
headers: String::new(),
delay: Duration::ZERO,
}
}
fn create() -> Step {
step(
"/v1/login_challenges/create",
json!({"success":true,"device_token":DEVICE,"verification_url":format!("http://localhost:4201/login/desktop#approval_token={}","A".repeat(43)),"confirmation_code":"WDJBMJHT","expires_at":Utc::now()+chrono::Duration::minutes(20),"poll_interval_seconds":5}),
)
}
fn pending() -> Step {
step(
"/v1/login_challenges/poll",
json!({"success":true,"status":"pending","maybe_failure_type":null}),
)
}
fn redeemed() -> Step {
let mut response = step(
"/v1/login_challenges/poll",
json!({"success":true,"status":"redeemed","maybe_failure_type":null,"maybe_signed_session":SIGNED}),
);
response.headers = format!("Set-Cookie: session={SIGNED}; HttpOnly; Path=/; Max-Age=3600\r\n");
response
}
fn session() -> Step {
let mut user = json!({
"user_token":"u_native_test", "username":"google_user", "display_name":"Google User", "email_gravatar_hash":"fixture",
"core_info":{"user_token":"u_native_test","username":"google_user","display_name":"Google User","gravatar_hash":"fixture","default_avatar":{"image_index":1,"color_index":1}},
"onboarding":{"email_not_set":false,"email_not_confirmed":false,"password_not_set":true,"username_not_customized":false},
"maybe_feature_flags":["studio", "use_qt", "future_server_flag"],"fakeyou_plan":"free","storyteller_stream_plan":"free"
});
for field in [
"can_access_studio",
"can_use_tts",
"can_use_w2l",
"can_delete_own_tts_results",
"can_delete_own_w2l_results",
"can_delete_own_account",
"can_upload_tts_models",
"can_upload_w2l_templates",
"can_delete_own_tts_models",
"can_delete_own_w2l_templates",
"can_approve_w2l_templates",
"can_edit_other_users_profiles",
"can_edit_other_users_tts_models",
"can_edit_other_users_w2l_templates",
"can_delete_other_users_tts_models",
"can_delete_other_users_w2l_templates",
"can_delete_other_users_tts_results",
"can_delete_other_users_w2l_results",
"can_ban_users",
"can_delete_users",
] {
user[field] = json!(false);
}
step(
"/v1/session",
json!({"success":true,"logged_in":true,"user":user}),
)
}
@@ -0,0 +1,26 @@
use artcraft_api_defs::users::session_info::SessionUserInfo;
use artcraft_client::endpoints::users::password_login::PasswordLoginRequest;
use tauri::{AppHandle, State};
use crate::core::state::app_env_configs::app_env_configs::AppEnvConfigs;
use crate::services::storyteller::commands::login_bridge::{
http_cookie_jar, password_auth, DesktopLoginError,
};
use crate::services::storyteller::state::storyteller_credential_manager::StorytellerCredentialManager;
#[tauri::command]
pub async fn storyteller_password_login_command(
app: AppHandle,
config: State<'_, AppEnvConfigs>,
manager: State<'_, StorytellerCredentialManager>,
request: PasswordLoginRequest,
) -> Result<SessionUserInfo, DesktopLoginError> {
password_auth(
&config.storyteller_host,
&manager,
&http_cookie_jar(&app)?,
Some(request),
None,
)
.await
}
@@ -0,0 +1,26 @@
use artcraft_api_defs::users::session_info::SessionUserInfo;
use artcraft_client::endpoints::users::password_signup::PasswordSignupRequest;
use tauri::{AppHandle, State};
use crate::core::state::app_env_configs::app_env_configs::AppEnvConfigs;
use crate::services::storyteller::commands::login_bridge::{
http_cookie_jar, password_auth, DesktopLoginError,
};
use crate::services::storyteller::state::storyteller_credential_manager::StorytellerCredentialManager;
#[tauri::command]
pub async fn storyteller_password_signup_command(
app: AppHandle,
config: State<'_, AppEnvConfigs>,
manager: State<'_, StorytellerCredentialManager>,
request: PasswordSignupRequest,
) -> Result<SessionUserInfo, DesktopLoginError> {
password_auth(
&config.storyteller_host,
&manager,
&http_cookie_jar(&app)?,
None,
Some(request),
)
.await
}
@@ -0,0 +1,17 @@
use tauri::{AppHandle, State};
use crate::services::storyteller::commands::login_bridge::{
http_cookie_jar, poll_challenge, DesktopLoginBridgeState, DesktopLoginError, DesktopLoginOutcome,
};
use crate::services::storyteller::state::storyteller_credential_manager::StorytellerCredentialManager;
#[tauri::command]
pub async fn storyteller_poll_login_challenge_command(
app: AppHandle,
state: State<'_, DesktopLoginBridgeState>,
manager: State<'_, StorytellerCredentialManager>,
challenge_id: String,
) -> Result<DesktopLoginOutcome, DesktopLoginError> {
let jar = http_cookie_jar(&app)?;
poll_challenge(&state, &manager, &jar, &challenge_id).await
}
@@ -3,9 +3,10 @@ use crate::core::state::artcraft_platform_info::ArtcraftPlatformInfo;
use crate::core::state::artcraft_usage_tracker::artcraft_usage_tracker::ArtcraftUsageTracker;
use crate::services::storyteller::state::storyteller_credential_manager::StorytellerCredentialManager;
use anyhow::anyhow;
use artcraft_api_defs::analytics::log_active_user::LogAppActiveUserRequest;
use artcraft_api_defs::analytics::log_active_user::{LogAppActiveUserRequest, LOG_ACTIVE_USER_V2_PATH};
use artcraft_client::endpoints::analytics::log_active_user_v2::log_active_user_v2;
use artcraft_client::error::api_error::ApiError;
use artcraft_client::credentials::storyteller_credential_set::StorytellerCredentialSet;
use artcraft_client::error::storyteller_error::StorytellerError;
use errors::AnyhowResult;
use log::{debug, error, info};
@@ -38,7 +39,7 @@ pub async fn storyteller_activity_thread(
&app_session_token,
).await;
if let Err(err) = res {
error!("An error occurred: {:?}", err);
error!("Activity request failed: origin={} path={} error={:?}", app_env_configs.storyteller_host.to_api_hostname_and_scheme(), LOG_ACTIVE_USER_V2_PATH, err);
}
// NB: Sleep if an error occurs.
tokio::time::sleep(std::time::Duration::from_millis(ERROR_SLEEP_MILLIS)).await;
@@ -62,7 +63,7 @@ async fn polling_loop(
continue;
}
Some(creds) => {
if creds.is_empty() {
if !has_activity_session(&creds) {
tokio::time::sleep(std::time::Duration::from_millis(5_000)).await;
continue;
}
@@ -123,7 +124,31 @@ async fn polling_loop(
};
// Wait at least a minute, no matter what the server tells us.
let wait_millis = std::cmp::min(wait_millis, 60_000);
let wait_millis = std::cmp::max(wait_millis, 60_000);
tokio::time::sleep(std::time::Duration::from_millis(wait_millis)).await;
}
}
// Anonymous visitor tracking is not authentication for active_user_v2.
fn has_activity_session(creds: &StorytellerCredentialSet) -> bool {
creds.session.as_ref().map(|session| !session.as_str().is_empty()).unwrap_or(false)
}
#[cfg(test)]
mod login_bridge_tests {
use super::has_activity_session;
use artcraft_client::credentials::storyteller_credential_set::StorytellerCredentialSet;
use artcraft_client::credentials::storyteller_avt_cookie::StorytellerAvtCookie;
use artcraft_client::credentials::storyteller_session_cookie::StorytellerSessionCookie;
#[test]
fn anonymous_visitor_cookie_does_not_authorize_activity_requests() {
assert!(!has_activity_session(&StorytellerCredentialSet::empty()));
let visitor_only = StorytellerCredentialSet::initialize_with_just_avt(StorytellerAvtCookie::new("visitor_fixture".into()));
assert!(!has_activity_session(&visitor_only));
let empty_session = StorytellerCredentialSet::initialize_with_just_cookie(StorytellerSessionCookie::new(String::new()));
assert!(!has_activity_session(&empty_session));
let signed_in = StorytellerCredentialSet::initialize_with_just_cookie(StorytellerSessionCookie::new("signed_session_fixture".into()));
assert!(has_activity_session(&signed_in));
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
/// The version of the ArtCraft desktop app reported in the UI and via Analytics.
/// This should match the CI builds.
pub const ARTCRAFT_VERSION: &str = "0.39.0";
pub const ARTCRAFT_VERSION: &str = "0.41.0";
+1 -1
View File
@@ -1,7 +1,7 @@
{
"$schema": "https://schema.tauri.app/config/2.0.0-rc",
"productName": "ArtCraft",
"version": "0.39.0",
"version": "0.41.0",
"identifier": "ai.artcraft.app",
"build": {
"frontendDist": "../../../frontend/apps/artcraft/dist",
+1 -1
View File
@@ -1,7 +1,7 @@
{
"$schema": "https://schema.tauri.app/config/2.0.0-rc",
"productName": "ArtCraft",
"version": "0.39.0",
"version": "0.41.0",
"identifier": "ai.artcraft.app",
"build": {
"frontendDist": "../../../frontend/apps/artcraft/dist",
+29 -14
View File
@@ -1,7 +1,20 @@
use std::env;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
pub fn main() {
println!("cargo:rerun-if-env-changed=DATABASE_URL");
println!("cargo:rerun-if-env-changed=SQLX_OFFLINE");
if let Ok(url) = env::var("DATABASE_URL") {
println!("cargo:rustc-env=DATABASE_URL={}", url);
return;
}
if env::var("SQLX_OFFLINE").as_deref() == Ok("true") || env::var("SQLX_OFFLINE").as_deref() == Ok("1") {
println!("cargo:rustc-env=SQLX_OFFLINE=true");
return;
}
// NB: This should help JetBrains' RustRover from highlighting failing query macros,
// but we do not want to interfere with the following:
//
@@ -14,27 +27,29 @@ pub fn main() {
Some("unix") => unix_temp_database_pathing(),
Some("windows") => windows_temp_database_pathing(),
_ => {
println!("cargo:warning=Unsupported target family, using Unix temp database pathing.");
unix_temp_database_pathing();
println!("cargo:rustc-env=SQLX_OFFLINE=true");
},
}
}
fn unix_temp_database_pathing() {
println!("cargo:rustc-env=DATABASE_URL=sqlite:/tmp/tasks.sqlite");
println!("cargo:rerun-if-changed=/tmp/tasks.sqlite");
if Path::new("/tmp/tasks.sqlite").exists() {
println!("cargo:rustc-env=DATABASE_URL=sqlite:/tmp/tasks.sqlite");
} else {
println!("cargo:rustc-env=SQLX_OFFLINE=true");
}
}
fn windows_temp_database_pathing() {
if let Ok(local_app_data) = env::var("LOCALAPPDATA") {
let path = PathBuf::from(local_app_data);
let path = path.join("Temp\\tasks.sqlite");
let path = path
.to_str()
.expect("path should be valid")
.to_string();
println!("cargo:warning=LocalAppData path: {}", path);
println!("cargo:rustc-env=DATABASE_URL=sqlite:{}", path);
} else {
panic!("LOCALAPPDATA environment variable not set");
let path = PathBuf::from(local_app_data).join("Temp\\tasks.sqlite");
if path.exists() {
let path_str = path.to_str().expect("path should be valid");
println!("cargo:warning=LocalAppData path: {}", path_str);
println!("cargo:rustc-env=DATABASE_URL=sqlite:{}", path_str);
return;
}
}
println!("cargo:rustc-env=SQLX_OFFLINE=true");
}
@@ -0,0 +1,167 @@
import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { DesktopLoginBridge } from "./DesktopLoginBridge";
const { native, forbiddenFetch } = vi.hoisted(() => ({ native: vi.fn(), forbiddenFetch: vi.fn(() => { throw new Error("JavaScript must never call HTTP for login"); }) }));
vi.mock("@tauri-apps/api/core", () => ({ invoke: native }));
vi.mock("@storyteller/tauri-utils", () => ({ FetchProxy: forbiddenFetch }));
vi.mock("@storyteller/api", () => { throw new Error("Login UI must only import API types"); });
vi.mock("qrcode.react", () => ({ QRCodeSVG: ({ value }: { value: string }) => <svg role="img" aria-label="Login QR" data-value={value} /> }));
const CHALLENGE_ID = "local-handle";
const APPROVAL_URL = `https://app.getartcraft.com/login/desktop#approval_token=${"A".repeat(43)}`;
const USER = { user_token: "user_fixture", username: "google_user" };
let outcome: Record<string, unknown>;
let expiresIn: number;
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-09-25T12:00:00Z"));
vi.stubGlobal("fetch", forbiddenFetch);
localStorage.clear();
native.mockReset();
forbiddenFetch.mockClear();
expiresIn = 1_200_000;
outcome = { status: "pending", maybe_failure_type: null, maybe_user: null };
native.mockImplementation(async (command: string) => {
if (command === "storyteller_create_login_challenge_command") return {
challenge_id: CHALLENGE_ID, verification_url: APPROVAL_URL,
confirmation_code: "WDJBMJHT", expires_at: new Date(Date.now() + expiresIn).toISOString(), poll_interval_seconds: 5,
};
if (command === "storyteller_poll_login_challenge_command") return outcome;
if (command === "storyteller_cancel_login_challenge_command" || command === "plugin:opener|open_url") return;
throw new Error(`Unexpected native command: ${command}`);
});
});
afterEach(() => {
cleanup();
expect(forbiddenFetch).not.toHaveBeenCalled();
expect(localStorage.getItem("artcraft_signed_session")).toBeNull();
vi.unstubAllGlobals();
vi.useRealTimers();
});
describe("desktop native login bridge integration", () => {
it("creates through IPC and opens the native browser with the matching QR/code", async () => {
render(<DesktopLoginBridge onSuccess={vi.fn()} />);
await start("Login with Website");
expect(native).toHaveBeenCalledWith("storyteller_create_login_challenge_command");
expect(native).toHaveBeenCalledWith("plugin:opener|open_url", { url: APPROVAL_URL });
expect(screen.getByRole("img", { name: "Login QR" }).getAttribute("data-value")).toBe(APPROVAL_URL);
expect(screen.getByText("WDJB-MJHT")).toBeTruthy();
});
it("polls with only a local handle and accepts the natively verified user without seeing credentials", async () => {
const success = vi.fn();
render(<DesktopLoginBridge onSuccess={success} />);
await start("Scan to Login");
await tick(5000);
expect(success).not.toHaveBeenCalled();
outcome = { status: "redeemed", maybe_failure_type: null, maybe_user: USER };
await tick(5000);
expect(native).toHaveBeenCalledWith("storyteller_poll_login_challenge_command", { challengeId: CHALLENGE_ID });
expect(success).toHaveBeenCalledWith(USER);
expect(native).toHaveBeenCalledWith("storyteller_cancel_login_challenge_command", { challengeId: CHALLENGE_ID });
const count = calls("storyteller_poll_login_challenge_command");
await tick(30_000);
expect(calls("storyteller_poll_login_challenge_command")).toBe(count);
});
it("stops after decline", async () => {
const success = vi.fn();
outcome = { status: "failed", maybe_failure_type: "user_declined" };
render(<DesktopLoginBridge onSuccess={success} />);
await start("Scan to Login");
await tick(5000);
expect(screen.getByText("Login was declined on the website.")).toBeTruthy();
expect(success).not.toHaveBeenCalled();
await tick(60_000);
expect(calls("storyteller_poll_login_challenge_command")).toBe(1);
});
it("expires locally and cancels the native attempt without creating another", async () => {
expiresIn = 5000;
render(<DesktopLoginBridge onSuccess={vi.fn()} />);
await start("Scan to Login");
await tick(5000);
expect(screen.getByText("Login request expired. Start a new request.")).toBeTruthy();
expect(calls("storyteller_create_login_challenge_command")).toBe(1);
expect(calls("storyteller_poll_login_challenge_command")).toBe(0);
expect(calls("storyteller_cancel_login_challenge_command")).toBe(1);
});
it("backs off a native network failure and retries the same local handle", async () => {
render(<DesktopLoginBridge onSuccess={vi.fn()} />);
await start("Scan to Login");
native.mockRejectedValueOnce({ message: "Disconnected", status: null, retryable: true });
await tick(5000);
expect(screen.getByText("Connection interrupted. Retrying…")).toBeTruthy();
await tick(9999);
expect(calls("storyteller_poll_login_challenge_command")).toBe(1);
await tick(1);
expect(calls("storyteller_poll_login_challenge_command")).toBe(2);
});
it("cancels on unmount and ignores a late command result", async () => {
let resolve: (value: unknown) => void = () => {};
const success = vi.fn();
const view = render(<DesktopLoginBridge onSuccess={success} />);
await start("Scan to Login");
native.mockImplementationOnce(() => new Promise((done) => { resolve = done; }));
await tick(5000);
view.unmount();
expect(native).toHaveBeenCalledWith("storyteller_cancel_login_challenge_command", { challengeId: CHALLENGE_ID });
await act(async () => { resolve({ status: "redeemed", maybe_user: USER }); });
expect(success).not.toHaveBeenCalled();
});
it("ignores an in-flight redemption after Back and can start a new challenge", async () => {
let resolve: (value: unknown) => void = () => {};
const success = vi.fn();
render(<DesktopLoginBridge onSuccess={success} />);
await start("Scan to Login");
native.mockImplementationOnce(() => new Promise((done) => { resolve = done; }));
await tick(5000);
await start("Back");
expect(native).toHaveBeenCalledWith("storyteller_cancel_login_challenge_command", { challengeId: CHALLENGE_ID });
await act(async () => { resolve({ status: "redeemed", maybe_user: USER }); });
expect(success).not.toHaveBeenCalled();
expect(screen.queryByRole("img", { name: "Login QR" })).toBeNull();
await tick(30_000);
expect(calls("storyteller_poll_login_challenge_command")).toBe(1);
await start("Scan to Login");
expect(screen.getByRole("img", { name: "Login QR" })).toBeTruthy();
outcome = { status: "redeemed", maybe_user: USER };
await tick(5000);
expect(success).toHaveBeenCalledExactlyOnceWith(USER);
});
it("fails closed on a future status", async () => {
outcome = { status: "future_state", maybe_user: USER };
render(<DesktopLoginBridge onSuccess={vi.fn()} />);
await start("Scan to Login");
await tick(5000);
expect(screen.getByText("This login request could not be completed. Start a new request.")).toBeTruthy();
});
it("shows the native host/status diagnostic and stops on terminal HTTP errors", async () => {
render(<DesktopLoginBridge onSuccess={vi.fn()} />);
await start("Scan to Login");
native.mockRejectedValueOnce({ message: "http://localhost:12345: Login server rejected the request (HTTP 401)", status: 401, retryable: false });
await tick(5000);
expect(screen.getByText(/localhost:12345.*401/)).toBeTruthy();
await tick(60_000);
expect(calls("storyteller_poll_login_challenge_command")).toBe(1);
});
});
async function start(label: string) {
await act(async () => { fireEvent.click(screen.getByRole("button", { name: label })); });
}
async function tick(ms: number) {
await act(async () => { await vi.advanceTimersByTimeAsync(ms); });
}
function calls(command: string) {
return native.mock.calls.filter(([name]) => name === command).length;
}
@@ -0,0 +1,134 @@
import { useEffect, useRef, useState } from "react";
import { QRCodeSVG } from "qrcode.react";
import { invoke } from "@tauri-apps/api/core";
import { ArrowLeftIcon } from "lucide-react";
import type { UserInfo } from "@storyteller/api";
import { createDesktopLoginChallenge, pollDesktopLoginChallenge, cancelDesktopLoginChallenge, isDesktopLoginError, type DesktopLoginChallenge } from "./NativeLoginBridge";
const OpenUrl = (url: string) => invoke("plugin:opener|open_url", { url });
export function DesktopLoginBridge({ onSuccess, onStart, onActiveChange }: { onSuccess: (user: UserInfo) => void; onStart?: () => void; onActiveChange?: (active: boolean) => void }) {
const [challenge, setChallenge] = useState<DesktopLoginChallenge | null>(null);
const [busy, setBusy] = useState(false);
const [message, setMessage] = useState("");
const [now, setNow] = useState(Date.now);
const generation = useRef(0);
const success = useRef(onSuccess);
success.current = onSuccess;
const isActive = busy || challenge !== null;
useEffect(() => { onActiveChange?.(isActive); }, [isActive, onActiveChange]);
useEffect(() => () => { generation.current += 1; }, []);
useEffect(() => {
if (!challenge) return;
const timer = window.setInterval(() => setNow(Date.now()), 1000);
return () => window.clearInterval(timer);
}, [challenge]);
useEffect(() => {
if (!challenge) return;
let active = true;
const current = generation.current;
let timer: ReturnType<typeof setTimeout>;
let delay = Math.max(5, challenge.poll_interval_seconds) * 1000;
const deadline = Date.parse(challenge.expires_at);
const finish = (text: string) => {
setChallenge(null);
setMessage(text);
};
const poll = async () => {
if (!active || current !== generation.current) return;
if (Date.now() >= deadline) { finish("Login request expired. Start a new request."); return; }
try {
const result = await pollDesktopLoginChallenge(challenge.challenge_id);
if (!active || current !== generation.current) return;
if (result.status === "redeemed") {
if (!result.maybe_user) throw new Error("Native session verification did not return a user");
active = false;
setChallenge(null);
setMessage("Signed in successfully.");
success.current(result.maybe_user);
return;
}
if (result.status === "failed") {
finish(result.maybe_failure_type === "user_declined" ? "Login was declined on the website." : result.maybe_failure_type === "expired" ? "Login request expired. Start a new request." : "Login failed. Start a new request.");
return;
}
if (!["pending", "approved"].includes(result.status)) {
finish("This login request could not be completed. Start a new request.");
return;
}
delay = Math.max(5, challenge.poll_interval_seconds) * 1000;
setMessage("Waiting for your confirmation on the website…");
} catch (error) {
if (!active || current !== generation.current) return;
if (isDesktopLoginError(error) && !error.retryable) {
finish(error.message);
return;
}
delay = Math.min(delay * 2, 30_000);
setMessage("Connection interrupted. Retrying…");
}
if (active) timer = setTimeout(poll, Math.min(delay, Math.max(0, deadline - Date.now())));
};
timer = setTimeout(poll, Math.min(delay, Math.max(0, deadline - Date.now())));
return () => {
active = false;
clearTimeout(timer);
void cancelDesktopLoginChallenge(challenge.challenge_id).catch(() => {});
};
}, [challenge]);
const start = async (openBrowser: boolean) => {
if (busy) return;
onStart?.();
const current = ++generation.current;
setBusy(true);
setMessage("");
try {
const created = await createDesktopLoginChallenge();
if (current !== generation.current) {
void cancelDesktopLoginChallenge(created.challenge_id).catch(() => {});
return;
}
// Rust validates the approval URL against the configured API environment.
setNow(Date.now());
setChallenge(created);
setMessage("Waiting for your confirmation on the website…");
if (openBrowser) await OpenUrl(created.verification_url);
} catch (error) {
if (current === generation.current) setMessage(isDesktopLoginError(error) ? error.message : "Unable to open website login. Try again or scan the QR code.");
} finally {
if (current === generation.current) setBusy(false);
}
};
const back = () => {
generation.current += 1;
setChallenge(null);
setBusy(false);
setMessage("");
};
const remaining = challenge ? Math.max(0, Math.ceil((Date.parse(challenge.expires_at) - now) / 1000)) : 0;
return <section aria-label="Website login" className="mb-6 rounded-xl border border-white/15 p-4 text-center">
{isActive && <div className="mb-4 text-left">
<button type="button" className="inline-flex items-center gap-2 text-sm text-white/70 hover:text-white" onClick={back}>
<ArrowLeftIcon size={16} aria-hidden="true" /> Back
</button>
</div>}
{challenge ? <>
<p className="mb-3 text-sm text-white/70">Scan with your phone or approve in your browser.</p>
<QRCodeSVG value={challenge.verification_url} size={192} marginSize={4} title="Scan to approve desktop login" className="mx-auto rounded-lg" />
<p className="my-3 font-mono text-2xl tracking-widest">{challenge.confirmation_code.slice(0, 4)}-{challenge.confirmation_code.slice(4)}</p>
<p className="mb-3 text-xs text-white/60">Verify this code on the website. Expires in {Math.floor(remaining / 60)}:{String(remaining % 60).padStart(2, "0")}</p>
<button type="button" className="rounded-lg border border-white/30 px-4 py-2" onClick={() => OpenUrl(challenge.verification_url).catch(() => setMessage("Unable to open the browser. Scan the QR code instead."))}>Open website</button>
</> : busy ? <p className="text-sm text-white/70" role="status">Preparing login…</p> : <div className="flex flex-col gap-2">
<button type="button" className="rounded-lg bg-white px-4 py-3 font-medium text-black" onClick={() => start(true)}>Login with Website</button>
<button type="button" className="rounded-lg border border-white/30 px-4 py-2" onClick={() => start(false)}>Scan to Login</button>
<p className="text-xs text-white/50">Use Google or any account already signed in on the website.</p>
</div>}
{message && <p className="mt-3 text-sm text-white/70" role="status">{message}</p>}
</section>;
}
@@ -0,0 +1,86 @@
.panel {
position: relative;
isolation: isolate;
display: flex;
width: 100%;
flex-direction: column;
align-items: center;
overflow: hidden;
padding: 56px 32px;
text-align: center;
background: radial-gradient(ellipse at 50% 0%, rgb(94 234 212 / 9%), transparent 70%);
animation: arrive 450ms ease-out both;
}
.glow {
position: absolute;
z-index: -1;
top: -52px;
left: calc(50% - 144px);
width: 288px;
height: 288px;
border-radius: 50%;
background: radial-gradient(circle, rgb(94 234 212 / 24%), rgb(129 140 248 / 12%) 45%, transparent 70%);
filter: blur(24px);
pointer-events: none;
animation: glow 2400ms ease-in-out infinite;
}
.badge {
display: flex;
width: 76px;
height: 76px;
align-items: center;
justify-content: center;
margin-bottom: 28px;
border: 1px solid rgb(153 246 228 / 35%);
border-radius: 50%;
color: #b4f7de;
background: linear-gradient(145deg, rgb(153 246 228 / 16%), rgb(94 234 212 / 4%));
box-shadow: 0 0 0 8px rgb(94 234 212 / 4%), 0 0 44px rgb(94 234 212 / 18%), inset 0 1px 0 rgb(255 255 255 / 12%);
}
.heading {
margin: 0;
max-width: 100%;
color: rgb(255 255 255 / 80%);
font-size: 18px;
font-weight: 500;
line-height: 1.5;
}
.username {
display: block;
margin-top: 4px;
overflow-wrap: anywhere;
color: #b4f7de;
background: linear-gradient(100deg, #d4ffec, #99f6e4 55%, #c7d2fe);
background-clip: text;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
font-size: 30px;
font-weight: 600;
letter-spacing: -0.025em;
line-height: 1.25;
}
.caption {
margin: 14px 0 0;
color: rgb(255 255 255 / 50%);
font-size: 14px;
line-height: 1.5;
}
@keyframes arrive {
from { opacity: 0; transform: translateY(8px) scale(0.98); }
to { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes glow {
0%, 100% { opacity: 0.7; transform: scale(0.95); }
50% { opacity: 1; transform: scale(1.06); }
}
@media (prefers-reduced-motion: reduce) {
.panel, .glow { animation: none; }
}
@@ -0,0 +1,18 @@
import { CheckIcon } from "lucide-react";
import styles from "./LoginSuccess.module.css";
export function LoginSuccess({ username }: { username: string }) {
return (
<div className={styles.panel} role="status" aria-live="polite" aria-atomic="true">
<div className={styles.glow} aria-hidden="true" />
<div className={styles.badge} aria-hidden="true">
<CheckIcon size={34} strokeWidth={2} />
</div>
<h2 className={styles.heading}>
Logged in as{" "}
<span className={styles.username}>{username}</span>
</h2>
<p className={styles.caption}>You're ready to create.</p>
</div>
);
}
@@ -0,0 +1,38 @@
import { invoke } from "@tauri-apps/api/core";
import type { UserInfo } from "@storyteller/api";
export interface DesktopLoginChallenge {
challenge_id: string;
verification_url: string;
confirmation_code: string;
expires_at: string;
poll_interval_seconds: number;
}
export interface DesktopLoginOutcome {
status: string;
maybe_failure_type: string | null;
maybe_user: UserInfo | null;
}
export interface DesktopLoginError {
status: number | null;
message: string;
retryable: boolean;
}
export const createDesktopLoginChallenge = () => invoke<DesktopLoginChallenge>("storyteller_create_login_challenge_command");
export const pollDesktopLoginChallenge = (challengeId: string) => invoke<DesktopLoginOutcome>("storyteller_poll_login_challenge_command", { challengeId });
export const cancelDesktopLoginChallenge = (challengeId: string) => invoke<void>("storyteller_cancel_login_challenge_command", { challengeId });
export function isDesktopLoginError(error: unknown): error is DesktopLoginError {
return typeof error === "object" && error !== null && "message" in error && "retryable" in error;
}
export const getNativeLoginSession = () => invoke<UserInfo | null>("storyteller_get_login_session_command");
export const passwordLogin = (usernameOrEmail: string, password: string) => invoke<UserInfo>("storyteller_password_login_command", {
request: { username_or_email: usernameOrEmail, password },
});
export const passwordSignup = (username: string, email: string, password: string, passwordConfirmation: string) => invoke<UserInfo>("storyteller_password_signup_command", {
request: { username, email_address: email, password, password_confirmation: passwordConfirmation, signup_source: "artcraft" },
});
@@ -1,12 +1,182 @@
import { render } from '@testing-library/react';
import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { LoginModal } from "./login-modal";
import { useLoginModalStore } from "./useLoginModalStore";
import LoginModal from './login-modal';
const { native, forbiddenFetch } = vi.hoisted(() => ({ native: vi.fn(), forbiddenFetch: vi.fn(() => { throw new Error("Authentication HTTP must run in Rust"); }) }));
vi.mock("@tauri-apps/api/core", () => ({ invoke: native }));
vi.mock("@storyteller/api", () => { throw new Error("Authentication UI must not import API clients"); });
vi.mock("@storyteller/tauri-utils", () => ({ FetchProxy: forbiddenFetch }));
vi.mock("@headlessui/react", () => ({ Transition: ({ show, children }: any) => show ? children : null, TransitionChild: ({ children }: any) => children }));
vi.mock("@storyteller/icons", () => ({ DynamicIcon: () => null, DiscordIcon: () => null }));
vi.mock("@storyteller/ui-button", () => ({ Button: ({ children, variant, icon, iconFlip, ...props }: any) => <button {...props}>{children}</button> }));
vi.mock("@storyteller/ui-input", () => ({ Input: ({ inputClassName, ...props }: any) => <input {...props} /> }));
vi.mock("qrcode.react", () => ({ QRCodeSVG: () => <svg role="img" aria-label="Login QR" /> }));
describe('LoginModal', () => {
it('should render successfully', () => {
const { baseElement } = render(<LoginModal />);
expect(baseElement).toBeTruthy();
const USER = { username: "google_user", user_token: "u_test" };
beforeEach(() => {
vi.useFakeTimers();
vi.stubGlobal("fetch", forbiddenFetch);
forbiddenFetch.mockClear();
localStorage.clear();
useLoginModalStore.setState({ isOpen: true, recheckTrigger: 0 });
native.mockReset().mockImplementation(async (command: string) => {
if (command === "storyteller_get_login_session_command") return null;
if (command === "storyteller_create_login_challenge_command") return {
challenge_id: "native_handle", verification_url: `https://app.getartcraft.com/login/desktop#approval_token=${"A".repeat(43)}`,
confirmation_code: "WDJBMJHT", expires_at: new Date(Date.now() + 1_200_000).toISOString(), poll_interval_seconds: 5,
};
if (command === "storyteller_poll_login_challenge_command") return { status: "redeemed", maybe_user: USER };
if (command === "storyteller_password_login_command" || command === "storyteller_password_signup_command") return USER;
if (command === "storyteller_cancel_login_challenge_command" || command === "plugin:opener|open_url") return;
throw new Error(`Unexpected command: ${command}`);
});
});
afterEach(() => {
cleanup();
expect(forbiddenFetch).not.toHaveBeenCalled();
expect(localStorage.getItem("artcraft_signed_session")).toBeNull();
vi.unstubAllGlobals();
vi.useRealTimers();
});
describe("native login modal integration", () => {
it("shows website and QR login only on the login screen", async () => {
await act(async () => { render(<LoginModal />); });
expect(screen.getByText("Create your account")).toBeTruthy();
expect(screen.queryByRole("button", { name: "Login with Website" })).toBeNull();
expect(screen.queryByRole("button", { name: "Scan to Login" })).toBeNull();
fireEvent.click(screen.getByRole("button", { name: "Log in" }));
expect(screen.getByRole("button", { name: "Login with Website" })).toBeTruthy();
expect(screen.getByRole("button", { name: "Scan to Login" })).toBeTruthy();
fireEvent.click(screen.getByRole("button", { name: "Sign up" }));
expect(screen.queryByRole("button", { name: "Login with Website" })).toBeNull();
});
it.each(["Login with Website", "Scan to Login"])("hides password login during %s and restores it with Back", async (label) => {
const success = vi.fn();
await act(async () => { render(<LoginModal isSignUp={false} onArtCraftAuthSuccess={success} />); });
expect(screen.getByPlaceholderText("you@example.com or username")).toBeTruthy();
await act(async () => { fireEvent.click(screen.getByRole("button", { name: label })); });
expect(screen.getByRole("img", { name: "Login QR" })).toBeTruthy();
expect(screen.queryByPlaceholderText("you@example.com or username")).toBeNull();
expect(screen.queryByPlaceholderText("Min. 8 characters")).toBeNull();
expect(screen.queryByRole("button", { name: "Sign up" })).toBeNull();
await act(async () => { fireEvent.click(screen.getByRole("button", { name: "Back" })); });
expect(screen.getByPlaceholderText("you@example.com or username")).toBeTruthy();
expect(screen.getByPlaceholderText("Min. 8 characters")).toBeTruthy();
expect(screen.getByRole("button", { name: "Login with Website" })).toBeTruthy();
expect(screen.getByRole("button", { name: "Scan to Login" })).toBeTruthy();
expect(screen.queryByRole("img", { name: "Login QR" })).toBeNull();
expect(native).toHaveBeenCalledWith("storyteller_cancel_login_challenge_command", { challengeId: "native_handle" });
await act(async () => { await vi.advanceTimersByTimeAsync(30_000); });
expect(native.mock.calls.filter(([command]) => command === "storyteller_poll_login_challenge_command")).toHaveLength(0);
expect(success).not.toHaveBeenCalled();
});
it("hides password login while preparing a challenge and cancels a late creation after Back", async () => {
let resolve: (value: unknown) => void = () => {};
await act(async () => { render(<LoginModal isSignUp={false} />); });
native.mockImplementationOnce(() => new Promise((done) => { resolve = done; }));
await act(async () => { fireEvent.click(screen.getByRole("button", { name: "Login with Website" })); });
expect(screen.getByText("Preparing login…")).toBeTruthy();
expect(screen.queryByPlaceholderText("Min. 8 characters")).toBeNull();
await act(async () => { fireEvent.click(screen.getByRole("button", { name: "Back" })); });
await act(async () => { resolve({ challenge_id: "late_handle" }); });
expect(screen.getByPlaceholderText("Min. 8 characters")).toBeTruthy();
expect(screen.queryByRole("img", { name: "Login QR" })).toBeNull();
expect(native).toHaveBeenCalledWith("storyteller_cancel_login_challenge_command", { challengeId: "late_handle" });
expect(native.mock.calls.filter(([command]) => command === "plugin:opener|open_url")).toHaveLength(0);
});
it.each([true, false])("keeps the completed native login when a stale session check resolves (before completion: %s)", async (beforeCompletion) => {
let resolveSession: (value: unknown) => void = () => {};
native.mockImplementationOnce(() => new Promise((resolve) => { resolveSession = resolve; }));
const success = vi.fn();
await act(async () => { render(<LoginModal isSignUp={false} onArtCraftAuthSuccess={success} />); });
await act(async () => { fireEvent.click(screen.getByRole("button", { name: "Scan to Login" })); });
if (beforeCompletion) await act(async () => { resolveSession(null); });
expect(screen.getByRole("img", { name: "Login QR" })).toBeTruthy();
await act(async () => { await vi.advanceTimersByTimeAsync(5000); });
expect(success).toHaveBeenCalledExactlyOnceWith(USER);
if (!beforeCompletion) await act(async () => { resolveSession(null); });
expect(screen.getByRole("heading", { name: "Logged in as google_user" })).toBeTruthy();
expect(screen.queryByRole("img", { name: "Login QR" })).toBeNull();
expect(useLoginModalStore.getState().isOpen).toBe(true);
await act(async () => { await vi.advanceTimersByTimeAsync(2999); });
expect(screen.getByRole("heading", { name: "Logged in as google_user" })).toBeTruthy();
await act(async () => { await vi.advanceTimersByTimeAsync(1); });
expect(useLoginModalStore.getState().isOpen).toBe(false);
expect(screen.queryByRole("heading", { name: "Logged in as google_user" })).toBeNull();
expect(screen.queryByText("Create your account")).toBeNull();
expect(native).toHaveBeenCalledWith("storyteller_poll_login_challenge_command", { challengeId: "native_handle" });
});
it.each([false, true])("submits password authentication only through Rust (signup: %s)", async (signup) => {
const success = vi.fn();
await act(async () => { render(<LoginModal isSignUp={signup} onArtCraftAuthSuccess={success} />); });
if (signup) {
fireEvent.change(screen.getByPlaceholderText("Username"), { target: { value: "native_user" } });
fireEvent.change(screen.getByPlaceholderText("you@example.com"), { target: { value: "user@example.com" } });
fireEvent.change(screen.getByPlaceholderText("Re-enter password"), { target: { value: "password123" } });
} else {
fireEvent.change(screen.getByPlaceholderText("you@example.com or username"), { target: { value: "native_user" } });
}
fireEvent.change(screen.getByPlaceholderText("Min. 8 characters"), { target: { value: "password123" } });
const submit = screen.getAllByRole("button", { name: signup ? "Sign up" : "Log in" }).find((button) => button.getAttribute("type") === "submit")!;
await act(async () => { fireEvent.click(submit); });
expect(success).toHaveBeenCalledExactlyOnceWith(USER);
expect(native).toHaveBeenCalledWith(signup ? "storyteller_password_signup_command" : "storyteller_password_login_command", {
request: signup ? { username: "native_user", email_address: "user@example.com", password: "password123", password_confirmation: "password123", signup_source: "artcraft" } : { username_or_email: "native_user", password: "password123" },
});
if (!signup) {
expect(screen.getByRole("heading", { name: "Logged in as google_user" })).toBeTruthy();
expect(screen.queryByText("Join Our Community")).toBeNull();
await act(async () => { await vi.advanceTimersByTimeAsync(3000); });
expect(useLoginModalStore.getState().isOpen).toBe(false);
}
});
it("dismisses the confirmation once even when callbacks change", async () => {
const firstClose = vi.fn();
const latestClose = vi.fn();
const success = vi.fn();
const view = render(<LoginModal isSignUp={false} onClose={firstClose} onArtCraftAuthSuccess={success} />);
await act(async () => {});
await act(async () => { fireEvent.click(screen.getByRole("button", { name: "Login with Website" })); });
await act(async () => { await vi.advanceTimersByTimeAsync(5000); });
expect(screen.getByRole("status").textContent).toContain("Logged in as google_user");
await act(async () => { await vi.advanceTimersByTimeAsync(1500); });
view.rerender(<LoginModal isSignUp={false} onClose={latestClose} onArtCraftAuthSuccess={success} />);
await act(async () => { await vi.advanceTimersByTimeAsync(1500); });
expect(useLoginModalStore.getState().isOpen).toBe(false);
expect(firstClose).not.toHaveBeenCalled();
expect(latestClose).toHaveBeenCalledTimes(1);
expect(success).toHaveBeenCalledExactlyOnceWith(USER);
await act(async () => { await vi.advanceTimersByTimeAsync(10_000); });
expect(latestClose).toHaveBeenCalledTimes(1);
});
it("cleans up the confirmation timer on unmount", async () => {
const onClose = vi.fn();
const view = render(<LoginModal isSignUp={false} onClose={onClose} />);
await act(async () => {});
await act(async () => { fireEvent.click(screen.getByRole("button", { name: "Scan to Login" })); });
await act(async () => { await vi.advanceTimersByTimeAsync(5000); });
expect(screen.getByRole("heading", { name: "Logged in as google_user" })).toBeTruthy();
view.unmount();
await act(async () => { await vi.advanceTimersByTimeAsync(10_000); });
expect(onClose).not.toHaveBeenCalled();
});
it("restores an existing session without showing the login confirmation", async () => {
native.mockResolvedValueOnce(USER);
const success = vi.fn();
await act(async () => { render(<LoginModal onArtCraftAuthSuccess={success} />); });
expect(success).toHaveBeenCalledExactlyOnceWith(USER);
expect(useLoginModalStore.getState().isOpen).toBe(false);
expect(screen.queryByRole("heading", { name: "Logged in as google_user" })).toBeNull();
});
});
@@ -1,17 +1,19 @@
import { Button } from "@storyteller/ui-button";
import { Transition, TransitionChild } from "@headlessui/react";
import { useState, useEffect } from "react";
import { useState, useEffect, useRef } from "react";
import { ArrowRightIcon } from "lucide-react";
import { DiscordIcon } from "@storyteller/icons";
import type { UserInfo } from "@storyteller/api";
import { DesktopLoginBridge } from "./DesktopLoginBridge";
import { LoginSuccess } from "./LoginSuccess";
import { ArtCraftSignUp } from "./artcraft-signup";
import { UsersApi } from "@storyteller/api";
import { getNativeLoginSession, passwordLogin, passwordSignup, isDesktopLoginError } from "./NativeLoginBridge";
import { useLoginModalStore } from "./useLoginModalStore";
const SIGNUP_SOURCE_ARTCRAFT = "artcraft";
// Webapp auth-showcase video (swap by passing `videoUrl`).
const DEFAULT_SHOWCASE_VIDEO =
"https://player.vimeo.com/video/1169289718?background=1&autoplay=1&loop=1&muted=1";
const LOGIN_SUCCESS_DURATION_MS = 3000;
interface LoginModalProps {
onClose?: () => void;
@@ -40,39 +42,56 @@ export function LoginModal({
const [errorMessage, setErrorMessage] = useState("");
const [showDiscord, setShowDiscord] = useState(false);
const [showSuccess, setShowSuccess] = useState(false);
const [isChallengeActive, setIsChallengeActive] = useState(false);
const [loggedInUsername, setLoggedInUsername] = useState<string | null>(null);
const checkArtCraftLogin = async () => {
const usersApi = new UsersApi();
const session = await usersApi.GetSession();
const loggedIn = session.data?.loggedIn;
return loggedIn;
};
const authGeneration = useRef(0);
const authSuccess = useRef(onArtCraftAuthSuccess);
authSuccess.current = onArtCraftAuthSuccess;
const afterClose = useRef(onClose);
afterClose.current = onClose;
// Check session on mount and when recheckTrigger changes.
// A stale startup/recheck response must never reopen the modal after login.
useEffect(() => {
checkArtCraftLogin().then((loggedIn) => {
if (loggedIn) {
const generation = ++authGeneration.current;
let active = true;
getNativeLoginSession().then((user) => {
if (!active || generation !== authGeneration.current) return;
if (user) {
setIsLoggedInArtCraft(true);
authSuccess.current?.(user);
closeModal();
} else {
// Reset modal state to initial values.
setIsLoading(false);
setIsSignUp(initialIsSignUp);
setErrorMessage("");
setShowDiscord(false);
setShowSuccess(false);
setLoggedInUsername(null);
setIsLoggedInArtCraft(false);
const { openModal } = useLoginModalStore.getState();
openModal();
useLoginModalStore.getState().openModal();
}
}).catch((error) => {
if (!active || generation !== authGeneration.current) return;
setErrorMessage(isDesktopLoginError(error) ? error.message : "Unable to check your account. Please sign in again.");
useLoginModalStore.getState().openModal();
});
return () => { active = false; };
}, [recheckTrigger, closeModal, initialIsSignUp]);
useEffect(() => {
if (onOpenChange) onOpenChange(isOpen);
}, [isOpen, onOpenChange]);
useEffect(() => {
if (loggedInUsername === null || !isOpen) return;
const timer = window.setTimeout(() => {
closeModal();
afterClose.current?.();
}, LOGIN_SUCCESS_DURATION_MS);
return () => window.clearTimeout(timer);
}, [loggedInUsername, isOpen, closeModal]);
const handleClose = () => {
closeModal();
onClose?.();
@@ -84,6 +103,15 @@ export function LoginModal({
setShowSuccess(true);
};
const handleLoginSuccess = (user: UserInfo) => {
authGeneration.current += 1;
setIsLoading(false);
setIsLoggedInArtCraft(true);
setIsChallengeActive(false);
setLoggedInUsername(user.username);
authSuccess.current?.(user);
};
const handleAuthSubmit = async (
username: string,
email: string,
@@ -91,56 +119,24 @@ export function LoginModal({
passwordConfirmation: string
) => {
setIsLoading(true);
const usersApi = new UsersApi();
setErrorMessage("");
const generation = ++authGeneration.current;
try {
let signupResponse, loginResponse;
const user = isSignUp
? await passwordSignup(username, email, password, passwordConfirmation)
: await passwordLogin(username || email, password);
if (generation !== authGeneration.current) return;
if (isSignUp) {
signupResponse = await usersApi.Signup({
username,
email,
password,
passwordConfirmation,
signupSource: SIGNUP_SOURCE_ARTCRAFT,
});
if (!signupResponse.success) {
setErrorMessage(
signupResponse.errorMessage || "Signup failed, please try again."
);
setIsLoading(false);
return;
}
loginResponse = await usersApi.Login({
usernameOrEmail: username || email,
password,
});
setIsLoggedInArtCraft(true);
authSuccess.current?.(user);
setShowDiscord(true);
} else {
loginResponse = await usersApi.Login({
usernameOrEmail: username || email,
password,
});
handleLoginSuccess(user);
}
if (!loginResponse.success) {
setErrorMessage(
loginResponse.errorMessage || "Login failed, please try again."
);
setIsLoading(false);
return;
}
setIsLoggedInArtCraft(true);
if (onArtCraftAuthSuccess) {
const session = await usersApi.GetSession();
const userInfo = session.data?.user;
if (userInfo) onArtCraftAuthSuccess(userInfo);
}
setShowDiscord(true); // Onboarding step after successful auth.
} catch (e) {
console.error(e);
setErrorMessage("An unexpected error occurred. Please try again.");
} catch (error) {
if (generation === authGeneration.current) setErrorMessage(isDesktopLoginError(error) ? error.message : "An unexpected error occurred. Please try again.");
} finally {
setIsLoading(false);
if (generation === authGeneration.current) setIsLoading(false);
}
};
@@ -204,7 +200,7 @@ export function LoginModal({
const inOnboarding = showDiscord || showSuccess;
return (
<Transition appear show={isOpen}>
<Transition appear show={isOpen} afterLeave={() => setLoggedInUsername(null)}>
<div className="fixed inset-0 z-[100]">
<TransitionChild
enter="ease-out duration-300"
@@ -226,10 +222,12 @@ export function LoginModal({
leaveTo="opacity-0 scale-95"
>
<div
className="relative flex w-full max-w-5xl overflow-hidden rounded-3xl border border-white/[4%] bg-[#1C1C20] text-white shadow-2xl lg:min-h-[640px]"
className={`relative flex w-full overflow-hidden rounded-3xl border bg-[#1C1C20] text-white shadow-2xl ${loggedInUsername !== null ? "max-w-md border-emerald-200/20" : "max-w-5xl border-white/[4%] lg:min-h-[640px]"}`}
onClick={(e) => e.stopPropagation()}
>
{inOnboarding ? (
{loggedInUsername !== null ? (
<LoginSuccess username={loggedInUsername} />
) : inOnboarding ? (
renderOnboarding()
) : (
<>
@@ -254,13 +252,14 @@ export function LoginModal({
</p>
</div>
<ArtCraftSignUp
{!isSignUp && <DesktopLoginBridge onActiveChange={setIsChallengeActive} onStart={() => { authGeneration.current += 1; }} onSuccess={handleLoginSuccess} />}
{!isChallengeActive && <ArtCraftSignUp
onSubmit={handleAuthSubmit}
isSignUp={isSignUp}
onToggleMode={() => setIsSignUp((prev) => !prev)}
errorMessage={errorMessage}
isLoading={isLoading}
/>
/>}
</div>
</div>
@@ -54,6 +54,9 @@
},
{
"path": "../input/tsconfig.lib.json"
},
{
"path": "../../tauri-api/tsconfig.lib.json"
}
]
}
+10
View File
@@ -49,6 +49,7 @@
"mediabunny": "^1.29.1",
"opencut-wasm": "^0.2.10",
"posthog-js": "^1.141.0",
"qrcode.react": "4.2.0",
"radix-ui": "^1.4.3",
"react": "18.3.1",
"react-copy-to-clipboard": "^5.1.0",
@@ -20479,6 +20480,15 @@
"node": ">=6"
}
},
"node_modules/qrcode.react": {
"version": "4.2.0",
"resolved": "https://registry.npmjs.org/qrcode.react/-/qrcode.react-4.2.0.tgz",
"integrity": "sha512-QpgqWi8rD9DsS9EP3z7BT+5lY5SFhsqGjpgW5DY/i3mK4M9DTBNz3ErMi8BWYEfI3L0d8GIbGmcdFAS1uIRGjA==",
"license": "ISC",
"peerDependencies": {
"react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
}
},
"node_modules/qs": {
"version": "6.15.2",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.15.2.tgz",
+1
View File
@@ -38,6 +38,7 @@
"mediabunny": "^1.29.1",
"opencut-wasm": "^0.2.10",
"posthog-js": "^1.141.0",
"qrcode.react": "4.2.0",
"radix-ui": "^1.4.3",
"react": "18.3.1",
"react-copy-to-clipboard": "^5.1.0",
+36
View File
@@ -0,0 +1,36 @@
# ArtCraft Studio Linux releases
The Linux workflow builds every package before the release job signs and uploads them to the
existing `artcraft-v<version>` draft, retaining Windows and macOS assets. The version comes from
`crates/desktop/artcraft/tauri.conf.json`.
| Architecture | Outputs | Builder |
|--------------|---------------------------------------------------------------|----------------------------|
| x86_64 | AppImage, DEB, RPM, tar.gz, Arch pkg.tar.zst, Flatpak bundle, OCI | Ubuntu 22.04 native |
| aarch64 | AppImage, DEB, RPM, tar.gz, Arch pkg.tar.zst, Flatpak bundle, OCI | Ubuntu 22.04 ARM native |
| riscv64 | DEB, RPM, tar.gz | Ubuntu 24.04 cross compiler |
Native AppImages also include zsync delta-update metadata.
Flatpak uses native Ubuntu 24.04 runners and GNOME 51 for GTK3/WebKitGTK 4.1. Its build checks
for missing ELF dependencies inside the sandbox. RISC-V packages require Ubuntu 24.04-era
libraries; x86_64 and ARM64 use Ubuntu 22.04 as their build baseline. The suite does not publish
RISC-V AppImages, Arch packages or Flatpaks.
`package.sh <architecture> <rust-target>` normalizes Tauri's DEB/RPM/AppImage filenames and
extracts the DEB's `/usr` tree into the suite's `bin/` and `share/` tarball layout. Shared Craft
Arch packaging consumes that tarball, with WebKitGTK, GTK3 and OpenSSL dependencies added.
`flatpak.sh` consumes the same tarball, builds the bundle, and exports the OCI layout.
Release runs import `LINUX_SIGNING_KEY` for Flatpak commit signatures and OCI publishing. The final shared
`draft-release` action signs RPMs, DEBs, Arch packages, AppImages, tarballs, OCI manifests and
checksums. Only a stable version on `release` or `artcraft-release` moves the OCI stable architecture tags.
Like the macOS and Windows release workflows, Linux release packaging runs on release branches
(`release`, `artcraft-release`, `artcraft-linux`) and workflow dispatch rather than on pull requests.
APT, RPM and pacman repositories are generated by `artcraft-services` from the release assets
and checksums. Its release catalog must be updated only after the signed release is published.
Installing a DEB manually is separate from publishing that DEB in the website's APT repository.
A successful build proves artifact production and metadata/dependency checks. It does not replace
installation and interactive testing on physical RISC-V, ARM64 and x86_64 desktops.
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop-application">
<id>ai.artcraft.app</id>
<name>ArtCraft Studio</name>
<summary>Create AI images and video</summary>
<metadata_license>MIT</metadata_license>
<project_license>MIT OR Apache-2.0</project_license>
<description><p>ArtCraft Studio is a desktop workspace for generating AI images and video with connected AI providers.</p></description>
<launchable type="desktop-id">ai.artcraft.app.desktop</launchable>
<url type="homepage">https://getartcraft.com</url>
<developer id="ai.artcraft"><name>ArtCraft</name></developer>
<categories><category>Graphics</category><category>AudioVideo</category></categories>
<provides><binary>artcraft</binary></provides>
</component>
+40
View File
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
set -euo pipefail
arch="$(uname -m)"
version="$(jq -r .version crates/desktop/artcraft/tauri.conf.json)"
base="artcraft-$version-linux-$arch"
work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT
mkdir -p "$work/stage" dist/release
tar -xzf "dist/linux/$base.tar.gz" --strip-components=1 -C "$work/stage"
cp packaging/linux/flatpak.yml "$work/manifest.yml"
flatpak remote-add --user --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
sign=()
keys=()
if [[ -n "${SIGNING_FPR:-}" ]]; then
sign=(--gpg-sign="$SIGNING_FPR" --gpg-homedir="$GNUPGHOME")
gpg --batch --armor --export "$SIGNING_FPR" > "$work/public.asc"
keys=(--gpg-keys="$work/public.asc")
fi
flatpak-builder --user --install-deps-from=flathub --disable-rofiles-fuse --force-clean \
--default-branch=stable --repo="$work/repo" "${sign[@]}" "$work/build" "$work/manifest.yml"
# Fail on missing WebKitGTK or other runtime libraries rather than shipping an unloadable binary.
# shellcheck disable=SC2016 # Expand these variables inside the Flatpak runtime shell.
flatpak build --runtime "$work/build" sh -c '
dependencies=$(ldd /app/bin/artcraft) || exit 1
printf "%s\n" "$dependencies"
case "$dependencies" in *"not found"*) exit 1 ;; esac
'
flatpak build-bundle --runtime-repo=https://dl.flathub.org/repo/flathub.flatpakrepo \
"${keys[@]}" "$work/repo" "dist/release/$base.flatpak" ai.artcraft.app stable
flatpak build-bundle --oci "$work/repo" "dist/release/$base-oci" ai.artcraft.app stable
skopeo inspect "oci:dist/release/$base-oci" >/dev/null
if [[ -n "${SIGNING_FPR:-}" ]]; then
registry="ghcr.io/${GITHUB_REPOSITORY,,}"
creds="$GITHUB_ACTOR:$GITHUB_TOKEN"
skopeo copy --format oci --dest-creds "$creds" "oci:dist/release/$base-oci" "docker://$registry:$version-$arch"
if [[ "$version" != *-* && ( "$GITHUB_REF" == refs/heads/release || "$GITHUB_REF" == refs/heads/artcraft-release ) ]]; then
skopeo copy --format oci --dest-creds "$creds" "oci:dist/release/$base-oci" "docker://$registry:stable-$arch"
fi
skopeo inspect --raw --creds "$creds" "docker://$registry:$version-$arch" >"dist/release/$base.oci-manifest.json"
fi
+25
View File
@@ -0,0 +1,25 @@
app-id: ai.artcraft.app
runtime: org.gnome.Platform
runtime-version: '51'
sdk: org.gnome.Sdk
command: artcraft
finish-args:
- --socket=wayland
- --socket=fallback-x11
- --socket=pulseaudio
- --share=ipc
- --share=network
- --device=dri
- --filesystem=xdg-pictures
- --filesystem=xdg-videos
- --filesystem=xdg-download
modules:
- name: artcraft
buildsystem: simple
build-commands:
- cp -a . /app/
- install -Dm644 /app/share/icons/hicolor/128x128/apps/artcraft.png /app/share/icons/hicolor/128x128/apps/ai.artcraft.app.png
- sed -i 's/^Icon=.*/Icon=ai.artcraft.app/' /app/share/applications/ai.artcraft.app.desktop
sources:
- type: dir
path: stage
+50
View File
@@ -0,0 +1,50 @@
#!/usr/bin/env bash
# Integration fixture: real DEB/RPM archives, with no Rust compilation or signing key.
set -euo pipefail
script="$(realpath "$(dirname "$0")/package.sh")"
work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT
cd "$work"
mkdir -p crates/desktop/artcraft target/x86_64-unknown-linux-gnu/release/bundle/{deb,rpm,appimage}
printf '{"version":"0.42.0"}\n' > crates/desktop/artcraft/tauri.conf.json
mkdir -p stage/{DEBIAN,usr/bin,usr/share/applications}
cp /bin/true stage/usr/bin/artcraft
printf '[Desktop Entry]\nType=Application\nName=ArtCraft\nExec=artcraft\n' > stage/usr/share/applications/ArtCraft.desktop
cat > stage/DEBIAN/control <<'CONTROL'
Package: artcraft
Version: 0.42.0
Architecture: amd64
Maintainer: Fixture <fixture@example.invalid>
Description: Packaging fixture
CONTROL
dpkg-deb --build stage target/x86_64-unknown-linux-gnu/release/bundle/deb/ArtCraft.deb
mkdir -p rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
cat > rpmbuild/SPECS/artcraft.spec <<'SPEC'
Name: artcraft
Version: 0.42.0
Release: 1
Summary: Packaging fixture
License: MIT
%description
Packaging fixture
%install
mkdir -p %{buildroot}/usr/bin
cp /bin/true %{buildroot}/usr/bin/artcraft
%files
/usr/bin/artcraft
SPEC
rpmbuild --define "_topdir $work/rpmbuild" -bb rpmbuild/SPECS/artcraft.spec
find rpmbuild/RPMS -name '*.rpm' -exec cp {} target/x86_64-unknown-linux-gnu/release/bundle/rpm/ \;
printf 'appimage fixture\n' > target/x86_64-unknown-linux-gnu/release/bundle/appimage/ArtCraft.AppImage
"$script" x86_64 x86_64-unknown-linux-gnu
for format in deb rpm AppImage AppImage.zsync tar.gz; do test -s "dist/release/artcraft-0.42.0-linux-x86_64.$format"; done
mkdir unpacked
tar -xzf dist/release/artcraft-0.42.0-linux-x86_64.tar.gz -C unpacked
test -x unpacked/artcraft-0.42.0-linux-x86_64/bin/artcraft
test -f unpacked/artcraft-0.42.0-linux-x86_64/share/applications/ai.artcraft.app.desktop
rm target/x86_64-unknown-linux-gnu/release/bundle/rpm/*.rpm
if "$script" x86_64 x86_64-unknown-linux-gnu; then
echo 'Packaging accepted a missing RPM' >&2
exit 1
fi
echo 'Package integration checks passed'
+54
View File
@@ -0,0 +1,54 @@
#!/usr/bin/env bash
# Normalize Tauri packages and derive the suite's bin/share tarball from its Debian package.
set -euo pipefail
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
arch="${1:?architecture required}"
target="${2:?Rust target required}"
version="$(jq -r .version crates/desktop/artcraft/tauri.conf.json)"
case "$arch" in x86_64|aarch64|riscv64) ;; *) exit 2 ;; esac
base="artcraft-$version-linux-$arch"
bundle="target/$target/release/bundle"
mkdir -p dist/release
for format in deb rpm AppImage; do
mapfile -t files < <(find "$bundle" -type f -name "*.$format")
if [[ "$arch" == riscv64 && "$format" == AppImage ]]; then continue; fi
[[ ${#files[@]} == 1 ]] || { echo "Expected one $format in $bundle, found ${#files[@]}" >&2; exit 1; }
cp "${files[0]}" "dist/release/$base.$format"
done
if [[ "$arch" != riscv64 ]]; then
zsyncmake -u "https://github.com/storytold/artcraft/releases/download/artcraft-v$version/$base.AppImage" \
-o "dist/release/$base.AppImage.zsync" "dist/release/$base.AppImage"
fi
work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT
dpkg-deb -x "dist/release/$base.deb" "$work/deb"
mkdir -p "$work/$base"
cp -a "$work/deb/usr/." "$work/$base/"
mapfile -t desktops < <(find "$work/$base/share/applications" -name '*.desktop')
[[ ${#desktops[@]} == 1 ]] || { echo 'Expected one desktop entry' >&2; exit 1; }
if [[ "${desktops[0]}" != "$work/$base/share/applications/ai.artcraft.app.desktop" ]]; then
mv "${desktops[0]}" "$work/$base/share/applications/ai.artcraft.app.desktop"
fi
install -Dm644 "$script_dir/ai.artcraft.app.metainfo.xml" "$work/$base/share/metainfo/ai.artcraft.app.metainfo.xml"
sed -i 's/^Name=.*/Name=ArtCraft Studio/' "$work/$base/share/applications/ai.artcraft.app.desktop"
test -x "$work/$base/bin/artcraft"
case "$arch" in
x86_64) machine='Advanced Micro Devices X86-64' ;;
aarch64) machine='AArch64' ;;
riscv64) machine='RISC-V' ;;
esac
readelf -h "$work/$base/bin/artcraft" | grep -F "$machine"
if [[ "$arch" == "$(uname -m)" ]]; then
dependencies="$(ldd "$work/$base/bin/artcraft")"
if grep -q 'not found' <<< "$dependencies"; then
printf '%s\n' "$dependencies" >&2
exit 1
fi
fi
# The tarball layout is consumed by the launcher, Arch packaging and Flatpak.
tar -czf "dist/release/$base.tar.gz" -C "$work" "$base"
case "$arch" in x86_64) deb_arch=amd64 ;; aarch64) deb_arch=arm64 ;; riscv64) deb_arch=riscv64 ;; esac
[[ "$(dpkg-deb -f "dist/release/$base.deb" Architecture)" == "$deb_arch" ]]
[[ "$(rpm -qp --qf '%{ARCH}' "dist/release/$base.rpm")" == "$arch" ]]
dpkg-deb --info "dist/release/$base.deb"
rpm -qp --requires "dist/release/$base.rpm"
+7
View File
@@ -0,0 +1,7 @@
# boring-sys2 has built-in ARM toolchains but no RISC-V toolchain. Set the target
# explicitly so BoringSSL does not select the x86_64 runner's assembly sources.
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR riscv64)
set(CMAKE_C_COMPILER riscv64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER riscv64-linux-gnu-g++)
set(OPENSSL_NO_ASM ON CACHE BOOL "Use BoringSSL's portable RISC-V implementation")
+15
View File
@@ -0,0 +1,15 @@
{
"build": {
"beforeBuildCommand": ""
},
"bundle": {
"category": "GraphicsAndDesign",
"shortDescription": "AI image and video creation",
"longDescription": "ArtCraft Studio is a desktop workspace for generating AI images and video.",
"linux": {
"appimage": {
"bundleMediaFramework": true
}
}
}
}
+3
View File
@@ -0,0 +1,3 @@
# Windows Packaging
ArtCraft Studio Windows release packaging assets and configuration.