Skip to content

Commit 4baf617

Browse files
committed
fix(shell): refresh claude oauth token per cli launch
1 parent ccb313e commit 4baf617

3 files changed

Lines changed: 51 additions & 4 deletions

File tree

packages/app/tests/docker-git/entrypoint-auth.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ describe("renderEntrypoint auth bridge", () => {
2828
expect(entrypoint).toContain("docker_git_upsert_ssh_env \"GH_TOKEN\" \"$EFFECTIVE_GH_TOKEN\"")
2929
expect(entrypoint).toContain("docker_git_upsert_ssh_env \"GIT_AUTH_TOKEN\" \"$EFFECTIVE_GITHUB_TOKEN\"")
3030
expect(entrypoint).toContain("GIT_CREDENTIAL_HELPER_PATH=\"/usr/local/bin/docker-git-credential-helper\"")
31+
expect(entrypoint).toContain("CLAUDE_REAL_BIN=\"/usr/local/bin/.docker-git-claude-real\"")
32+
expect(entrypoint).toContain("CLAUDE_WRAPPER_BIN=\"/usr/local/bin/claude\"")
33+
expect(entrypoint).toContain("cat <<'EOF' > \"$CLAUDE_WRAPPER_BIN\"")
34+
expect(entrypoint).toContain("CLAUDE_CONFIG_DIR=\"${CLAUDE_CONFIG_DIR:-$HOME/.claude}\"")
3135
expect(entrypoint).toContain("token=\"${GITHUB_TOKEN:-}\"")
3236
expect(entrypoint).toContain("token=\"${GH_TOKEN:-}\"")
3337
expect(entrypoint).toContain(String.raw`printf "%s\n" "password=$token"`)

packages/docker-git/tests/core/templates.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ describe("planFiles", () => {
9292
expect(entrypointSpec.contents).toContain("npm_config_store_dir")
9393
expect(entrypointSpec.contents).toContain("NPM_CONFIG_CACHE")
9494
expect(entrypointSpec.contents).toContain("YARN_CACHE_FOLDER")
95+
expect(entrypointSpec.contents).toContain("CLAUDE_REAL_BIN=\"/usr/local/bin/.docker-git-claude-real\"")
96+
expect(entrypointSpec.contents).toContain("CLAUDE_WRAPPER_BIN=\"/usr/local/bin/claude\"")
97+
expect(entrypointSpec.contents).toContain("cat <<'EOF' > \"$CLAUDE_WRAPPER_BIN\"")
98+
expect(entrypointSpec.contents).toContain('CLAUDE_CONFIG_DIR="${CLAUDE_CONFIG_DIR:-$HOME/.claude}"')
99+
expect(entrypointSpec.contents).toContain("unset CLAUDE_CODE_OAUTH_TOKEN || true")
95100
expect(entrypointSpec.contents).toContain("CLONE_CACHE_ARGS=\"--reference-if-able '$CACHE_REPO_DIR' --dissociate\"")
96101
expect(entrypointSpec.contents).toContain("[clone-cache] using mirror: $CACHE_REPO_DIR")
97102
expect(entrypointSpec.contents).toContain("git clone --progress $CLONE_CACHE_ARGS")

packages/lib/src/core/templates-entrypoint/claude.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,56 @@ export CLAUDE_CONFIG_DIR
2424
mkdir -p "$CLAUDE_CONFIG_DIR" || true
2525
2626
CLAUDE_TOKEN_FILE="$CLAUDE_CONFIG_DIR/.oauth-token"
27-
CLAUDE_CODE_OAUTH_TOKEN=""
27+
docker_git_refresh_claude_oauth_token() {
28+
local token=""
29+
if [[ -f "$CLAUDE_TOKEN_FILE" ]]; then
30+
token="$(tr -d '\r\n' < "$CLAUDE_TOKEN_FILE")"
31+
fi
32+
export CLAUDE_CODE_OAUTH_TOKEN="$token"
33+
}
34+
35+
docker_git_refresh_claude_oauth_token
36+
37+
CLAUDE_REAL_BIN="/usr/local/bin/.docker-git-claude-real"
38+
CLAUDE_WRAPPER_BIN="/usr/local/bin/claude"
39+
if command -v claude >/dev/null 2>&1; then
40+
CURRENT_CLAUDE_BIN="$(command -v claude)"
41+
if [[ "$CURRENT_CLAUDE_BIN" != "$CLAUDE_REAL_BIN" && ! -f "$CLAUDE_REAL_BIN" ]]; then
42+
mv "$CURRENT_CLAUDE_BIN" "$CLAUDE_REAL_BIN"
43+
fi
44+
if [[ -f "$CLAUDE_REAL_BIN" ]]; then
45+
cat <<'EOF' > "$CLAUDE_WRAPPER_BIN"
46+
#!/usr/bin/env bash
47+
set -euo pipefail
48+
49+
CLAUDE_REAL_BIN="/usr/local/bin/.docker-git-claude-real"
50+
CLAUDE_CONFIG_DIR="${"$"}{CLAUDE_CONFIG_DIR:-$HOME/.claude}"
51+
CLAUDE_TOKEN_FILE="$CLAUDE_CONFIG_DIR/.oauth-token"
52+
2853
if [[ -f "$CLAUDE_TOKEN_FILE" ]]; then
2954
CLAUDE_CODE_OAUTH_TOKEN="$(tr -d '\r\n' < "$CLAUDE_TOKEN_FILE")"
55+
export CLAUDE_CODE_OAUTH_TOKEN
56+
else
57+
unset CLAUDE_CODE_OAUTH_TOKEN || true
58+
fi
59+
60+
exec "$CLAUDE_REAL_BIN" "$@"
61+
EOF
62+
chmod 0755 "$CLAUDE_WRAPPER_BIN" || true
63+
fi
3064
fi
31-
export CLAUDE_CODE_OAUTH_TOKEN
3265
3366
CLAUDE_PROFILE="/etc/profile.d/claude-config.sh"
3467
printf "export CLAUDE_AUTH_LABEL=%q\n" "$CLAUDE_AUTH_LABEL" > "$CLAUDE_PROFILE"
3568
printf "export CLAUDE_CONFIG_DIR=%q\n" "$CLAUDE_CONFIG_DIR" >> "$CLAUDE_PROFILE"
36-
if [[ -n "$CLAUDE_CODE_OAUTH_TOKEN" ]]; then
37-
printf "export CLAUDE_CODE_OAUTH_TOKEN=%q\n" "$CLAUDE_CODE_OAUTH_TOKEN" >> "$CLAUDE_PROFILE"
69+
cat <<'EOF' >> "$CLAUDE_PROFILE"
70+
CLAUDE_TOKEN_FILE="${"$"}{CLAUDE_CONFIG_DIR:-$HOME/.claude}/.oauth-token"
71+
if [[ -f "$CLAUDE_TOKEN_FILE" ]]; then
72+
export CLAUDE_CODE_OAUTH_TOKEN="$(tr -d '\r\n' < "$CLAUDE_TOKEN_FILE")"
73+
else
74+
unset CLAUDE_CODE_OAUTH_TOKEN || true
3875
fi
76+
EOF
3977
chmod 0644 "$CLAUDE_PROFILE" || true
4078
4179
docker_git_upsert_ssh_env "CLAUDE_AUTH_LABEL" "$CLAUDE_AUTH_LABEL"

0 commit comments

Comments
 (0)