Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Documentation

on:
push:
branches: ['main']
paths:
- 'README.md'
- 'src/README-NUGET.md'
- 'docs-site/content/**'
- 'scripts/validate-docs.py'
- '.github/workflows/docs.yml'
pull_request:
branches: ['main']
paths:
- 'README.md'
- 'src/README-NUGET.md'
- 'docs-site/content/**'
- 'scripts/validate-docs.py'
- '.github/workflows/docs.yml'

permissions:
contents: read

jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- name: Validate links and navigation
run: python3 scripts/validate-docs.py
21 changes: 18 additions & 3 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,40 @@ jobs:
run: yarn check
- name: Run Vitest unit tests
run: yarn vitest run --project unit
- name: Build dashboard
run: yarn workspace zonetree-analytics-dashboard build
- name: Publish dashboard UI
run: yarn workspace zonetree-analytics-dashboard publish
- name: Upload dashboard UI
if: matrix.os == 'ubuntu-latest'
uses: actions/upload-artifact@v7
with:
name: dashboard-ui
path: apps/ZoneTree.Analytics.Dashboard/ui/dist/publish/
if-no-files-found: error

build:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v7
- name: Setup .NET
uses: actions/setup-dotnet@v6
with:
dotnet-version: |
11.0.x
10.0.x
9.0.x
8.0.x
- name: Display dotnet version
run: dotnet --version
- name: Verify native ARM64 on macOS
if: runner.os == 'macOS'
run: |
test "$(uname -m)" = arm64
sdk_info=$(dotnet --info)
echo "$sdk_info"
grep -q 'RID: *osx-arm64' <<< "$sdk_info"
- name: Restore dependencies
working-directory: ./src
run: dotnet restore
Expand Down
114 changes: 105 additions & 9 deletions .github/workflows/publish-nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,83 @@ on:
name:
description: 'When you press run workflow, the nuget packages will be published.'
default: 'I understand.'

concurrency:
group: publish-nuget

defaults:
run:
shell: bash

jobs:
build:
publish:
runs-on: ubuntu-latest
timeout-minutes: 45
permissions:
contents: read
contents: write # Create the release and its tag after both packages are published.
id-token: write # Trusted publishing: lets NuGet/login request a GitHub OIDC token.
steps:
- uses: actions/checkout@v7
- name: Setup .NET
uses: actions/setup-dotnet@v6
with:
dotnet-version: |
11.0.x
10.0.x
9.0.x
8.0.x
- name: Display dotnet version
run: dotnet --version
- name: Check release version and availability
id: version
run: |
version=$(dotnet msbuild src/ZoneTree.Analytics/ZoneTree.Analytics.csproj -nologo -getProperty:PackageVersion)
aspnet_version=$(dotnet msbuild src/ZoneTree.Analytics.AspNetCore/ZoneTree.Analytics.AspNetCore.csproj -nologo -getProperty:PackageVersion)
if [[ -z $version || $version != "$aspnet_version" ]]; then
echo "::error::Both packages must have the same PackageVersion ($version and $aspnet_version). Check src/Directory.Build.props."
exit 1
fi
# NuGet removes build metadata and an optional zero revision from package versions.
version=${version%%+*}
if [[ $version =~ ^([0-9]+\.[0-9]+\.[0-9]+)\.0(-.*)?$ ]]; then
version=${BASH_REMATCH[1]}${BASH_REMATCH[2]}
fi

tag="v$version"
existing_tag=$(git ls-remote --refs --tags origin "refs/tags/$tag")
if [[ -n $existing_tag ]]; then
echo "::error::$tag already exists. Raise VersionPrefix in src/Directory.Build.props."
exit 1
fi

scratch=$(mktemp -d)
trap 'rm -rf "$scratch"' EXIT
missing=()
for package in ZoneTree.Analytics ZoneTree.Analytics.AspNetCore; do
id=${package,,}
manifest="https://api.nuget.org/v3-flatcontainer/$id/${version,,}/$id.nuspec"
status=$(curl -sS --retry 3 -o "$scratch/package.nuspec" -w '%{http_code}' "$manifest")
case "$status" in
404) missing+=("$package"); continue ;;
200) ;;
*) echo "::error::Could not check $package on nuget.org (HTTP $status)."; exit 1 ;;
esac

# Resume a partial publish only if the existing package came from this commit.
commit=$(python3 -c '
import sys
import xml.etree.ElementTree as ET
repository = ET.parse(sys.stdin).find("{*}metadata/{*}repository")
print(repository.get("commit", "") if repository is not None else "")
' < "$scratch/package.nuspec")
if [[ $commit != "$GITHUB_SHA" ]]; then
echo "::error::$package $version was published from a different commit."
exit 1
fi
echo "$package $version was already published from this commit."
done
echo "version=$version" >> "$GITHUB_OUTPUT"
echo "missing=${missing[*]}" >> "$GITHUB_OUTPUT"
- name: Restore dependencies
working-directory: ./src
run: dotnet restore
Expand All @@ -37,22 +97,58 @@ jobs:
dotnet pack ZoneTree.Analytics/ZoneTree.Analytics.csproj --configuration Release --no-build --output ../artifacts/packages
dotnet pack ZoneTree.Analytics.AspNetCore/ZoneTree.Analytics.AspNetCore.csproj --configuration Release --no-build --output ../artifacts/packages
working-directory: ./src
- name: Upload packages
uses: actions/upload-artifact@v7
with:
name: nuget-packages
path: artifacts/packages/
- name: Verify release packages
env:
PACKAGE_VERSION: ${{ steps.version.outputs.version }}
run: |
for package in ZoneTree.Analytics ZoneTree.Analytics.AspNetCore; do
test -f "artifacts/packages/$package.$PACKAGE_VERSION.nupkg"
test -f "artifacts/packages/$package.$PACKAGE_VERSION.snupkg"
done
# Exchanges the OIDC token for a temporary API key, valid for one hour, so it runs just
# before the push. Needs a trusted publishing policy on nuget.org for this workflow file.
# The user is the nuget.org organization that owns the policy and the packages. If the
# login does not match the policy, use the account that created it instead (koculu).
- name: NuGet login
id: login
if: steps.version.outputs.missing != ''
uses: NuGet/login@v1
with:
user: koculu
- name: Publish the packages to nuget.org
# Pushing a .nupkg also pushes the .snupkg symbol package next to it.
run: dotnet nuget push "artifacts/packages/*.nupkg" --api-key "$NUGET_API_KEY" --source https://api.nuget.org/v3/index.json --skip-duplicate
if: steps.version.outputs.missing != ''
# Each push also sends the corresponding .snupkg. A rejected push fails the run.
env:
NUGET_API_KEY: ${{ steps.login.outputs.NUGET_API_KEY }}
PACKAGE_VERSION: ${{ steps.version.outputs.version }}
MISSING_PACKAGES: ${{ steps.version.outputs.missing }}
run: |
read -r -a packages <<< "$MISSING_PACKAGES"
for package in "${packages[@]}"; do
dotnet nuget push "artifacts/packages/$package.$PACKAGE_VERSION.nupkg" --api-key "$NUGET_API_KEY" --source https://api.nuget.org/v3/index.json
done
- name: Upload packages
uses: actions/upload-artifact@v7
with:
name: nuget-packages
path: artifacts/packages/
overwrite: true
- name: Tag and create GitHub release
env:
GH_TOKEN: ${{ github.token }}
PACKAGE_VERSION: ${{ steps.version.outputs.version }}
run: |
tag="v$PACKAGE_VERSION"
assets=(artifacts/packages/*.nupkg artifacts/packages/*.snupkg)
if release=$(gh release view "$tag" --json isDraft,targetCommitish); then
if ! jq -e --arg sha "$GITHUB_SHA" '.isDraft and .targetCommitish == $sha' <<< "$release" >/dev/null; then
echo "::error::A release for $tag already exists and cannot be resumed."
exit 1
fi
gh release upload "$tag" "${assets[@]}" --clobber
gh release edit "$tag" --draft=false
else
flags=()
if [[ $PACKAGE_VERSION == *-* ]]; then flags+=(--prerelease); fi
gh release create "$tag" "${assets[@]}" --target "$GITHUB_SHA" --title "ZoneTree Analytics $PACKAGE_VERSION" --generate-notes "${flags[@]}"
fi
3 changes: 3 additions & 0 deletions apps/ZoneTree.Analytics.Dashboard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ HttpOnly and SameSite=Lax, marked Secure over HTTPS, and state-changing requests
origins are rejected. Serve the dashboard over HTTPS in production. Behind a reverse proxy that
terminates HTTPS, the server reads `X-Forwarded-Proto`, `X-Forwarded-Host` and `X-Forwarded-For` from
loopback proxies; list other proxy addresses with `--Dashboard:KnownProxies=10.0.0.5,10.0.0.6`.
Forward WebSocket upgrades on `/_dashboard/live` for live reports. The **Auto-refresh** setting
controls how often the server checks the selected site's reports and rolling visitor count.
If the WebSocket disconnects, visible report pages use HTTP polling until the stream recovers.
Failed sign-ins pause a user name from one address, and with a larger limit the address itself.

The host listens on all network interfaces by default. Startup logs include a clickable local
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ static class DashboardAuthentication
public const string QueryPolicy = "Query";
const string UserItem = "zt-dashboard-user";
const string SessionItem = "zt-dashboard-session";
public const string SessionClaim = "zt-dashboard-session";

public static UserRecord User(HttpContext context) => context.Items[UserItem] as UserRecord;

Expand All @@ -21,11 +22,12 @@ public static void SetUser(HttpContext context, UserRecord user, string sessionI
context.Items[SessionItem] = sessionId;
}

public static ClaimsPrincipal Principal(UserRecord user) => new(new ClaimsIdentity(
public static ClaimsPrincipal Principal(UserRecord user, string sessionId) => new(new ClaimsIdentity(
[
new Claim(ClaimTypes.NameIdentifier, user.Id),
new Claim(ClaimTypes.Name, user.UserName),
new Claim(ClaimTypes.Role, user.Role.ToString()),
new Claim(SessionClaim, sessionId),
], Scheme));

public static IServiceCollection AddDashboardAuthentication(this IServiceCollection services)
Expand Down Expand Up @@ -53,7 +55,7 @@ protected override Task<AuthenticateResult> HandleAuthenticateAsync()
return Task.FromResult(AuthenticateResult.NoResult());
}
DashboardAuthentication.SetUser(Context, user, id);
return Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(DashboardAuthentication.Principal(user), Scheme.Name)));
return Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(DashboardAuthentication.Principal(user, id), Scheme.Name)));
}

protected override async Task HandleChallengeAsync(AuthenticationProperties properties)
Expand Down
14 changes: 11 additions & 3 deletions apps/ZoneTree.Analytics.Dashboard/server/Auth/SessionStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,29 @@ public bool TryAuthenticate(string token, out string id, out UserRecord user)
id = null;
if (string.IsNullOrEmpty(token) || token.Length > 100) return false;
id = Id(token);
return TryAuthenticateId(id, out user, touch: true);
}

// The WebSocket reads this id from the authenticated upgrade principal.
internal bool TryAuthenticateId(string id, out UserRecord user, bool touch = false)
{
user = null;
if (string.IsNullOrEmpty(id)) return false;
if (!store.TryGet<SessionRecord>(Prefix + id, out var session)) return false;
var now = DateTime.UtcNow;
if (IsExpired(session, now))
{
store.Delete(Prefix + id);
if (touch) store.Delete(Prefix + id);
return false;
}
user = users.Get(session.UserId);
if (user == null || user.Disabled || user.SecurityStamp != session.SecurityStamp)
{
store.Delete(Prefix + id);
if (touch) store.Delete(Prefix + id);
user = null;
return false;
}
if (now - session.LastSeen >= TouchInterval)
if (touch && now - session.LastSeen >= TouchInterval)
{
session.LastSeen = now;
store.Put(Prefix + id, session);
Expand Down
Loading
Loading