Skip to content

Commit 14d25f9

Browse files
committed
Changes from running between environments
1 parent 128c3e7 commit 14d25f9

2 files changed

Lines changed: 68 additions & 40 deletions

File tree

cache/__tests__/cache-metrics-worst-case.sh

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -136,31 +136,45 @@ get_auth_token() {
136136
exit 1
137137
fi
138138

139-
# Test the token
139+
# Validate JWT format (3 parts separated by dots)
140140
log_info "Validating token..."
141-
local test_response=$(curl -s -w "\n%{http_code}" -X POST "${API_BASE}/api/create" \
142-
-H "Content-Type: application/json" \
143-
-H "Authorization: Bearer ${AUTH_TOKEN}" \
144-
-d '{"type":"TokenTest","__rerum":{"test":true}}' 2>/dev/null)
141+
if ! echo "$AUTH_TOKEN" | grep -qE '^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$'; then
142+
echo -e "${RED}ERROR: Token is not a valid JWT format${NC}"
143+
echo "Expected format: header.payload.signature"
144+
exit 1
145+
fi
145146

146-
local http_code=$(echo "$test_response" | tail -n1)
147+
# Extract and decode payload (second part of JWT)
148+
local payload=$(echo "$AUTH_TOKEN" | cut -d. -f2)
149+
# Add padding if needed for base64 decoding
150+
local padded_payload="${payload}$(printf '%*s' $((4 - ${#payload} % 4)) '' | tr ' ' '=')"
151+
local decoded_payload=$(echo "$padded_payload" | base64 -d 2>/dev/null)
147152

148-
if [ "$http_code" == "201" ]; then
149-
log_success "Token is valid"
150-
# Clean up test object
151-
local test_id=$(echo "$test_response" | head -n-1 | grep -o '"@id":"[^"]*"' | cut -d'"' -f4)
152-
if [ -n "$test_id" ]; then
153-
curl -s -X DELETE "${test_id}" \
154-
-H "Authorization: Bearer ${AUTH_TOKEN}" > /dev/null 2>&1
155-
fi
156-
elif [ "$http_code" == "401" ]; then
157-
echo -e "${RED}ERROR: Token is expired or invalid (HTTP 401)${NC}"
158-
echo "Please obtain a fresh token from: https://devstore.rerum.io/"
153+
if [ -z "$decoded_payload" ]; then
154+
echo -e "${RED}ERROR: Failed to decode JWT payload${NC}"
159155
exit 1
156+
fi
157+
158+
# Extract expiration time (exp field in seconds since epoch)
159+
local exp=$(echo "$decoded_payload" | grep -o '"exp":[0-9]*' | cut -d: -f2)
160+
161+
if [ -z "$exp" ]; then
162+
echo -e "${YELLOW}WARNING: Token does not contain 'exp' field${NC}"
163+
echo "Proceeding anyway, but token may be rejected by server..."
160164
else
161-
echo -e "${RED}ERROR: Token validation failed (HTTP $http_code)${NC}"
162-
echo "Response: $(echo "$test_response" | head -n-1)"
163-
exit 1
165+
local current_time=$(date +%s)
166+
if [ "$exp" -lt "$current_time" ]; then
167+
echo -e "${RED}ERROR: Token is expired${NC}"
168+
echo "Token expired at: $(date -d @$exp)"
169+
echo "Current time: $(date -d @$current_time)"
170+
echo "Please obtain a fresh token from: https://devstore.rerum.io/"
171+
exit 1
172+
else
173+
local time_remaining=$((exp - current_time))
174+
local hours=$((time_remaining / 3600))
175+
local minutes=$(( (time_remaining % 3600) / 60 ))
176+
log_success "Token is valid (expires in ${hours}h ${minutes}m)"
177+
fi
164178
fi
165179
}
166180

cache/__tests__/cache-metrics.sh

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -135,31 +135,45 @@ get_auth_token() {
135135
exit 1
136136
fi
137137

138-
# Test the token
138+
# Validate JWT format (3 parts separated by dots)
139139
log_info "Validating token..."
140-
local test_response=$(curl -s -w "\n%{http_code}" -X POST "${API_BASE}/api/create" \
141-
-H "Content-Type: application/json" \
142-
-H "Authorization: Bearer ${AUTH_TOKEN}" \
143-
-d '{"type":"TokenTest","__rerum":{"test":true}}' 2>/dev/null)
140+
if ! echo "$AUTH_TOKEN" | grep -qE '^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$'; then
141+
echo -e "${RED}ERROR: Token is not a valid JWT format${NC}"
142+
echo "Expected format: header.payload.signature"
143+
exit 1
144+
fi
144145

145-
local http_code=$(echo "$test_response" | tail -n1)
146+
# Extract and decode payload (second part of JWT)
147+
local payload=$(echo "$AUTH_TOKEN" | cut -d. -f2)
148+
# Add padding if needed for base64 decoding
149+
local padded_payload="${payload}$(printf '%*s' $((4 - ${#payload} % 4)) '' | tr ' ' '=')"
150+
local decoded_payload=$(echo "$padded_payload" | base64 -d 2>/dev/null)
146151

147-
if [ "$http_code" == "201" ]; then
148-
log_success "Token is valid"
149-
# Clean up test object
150-
local test_id=$(echo "$test_response" | head -n-1 | grep -o '"@id":"[^"]*"' | cut -d'"' -f4)
151-
if [ -n "$test_id" ]; then
152-
curl -s -X DELETE "${test_id}" \
153-
-H "Authorization: Bearer ${AUTH_TOKEN}" > /dev/null 2>&1
154-
fi
155-
elif [ "$http_code" == "401" ]; then
156-
echo -e "${RED}ERROR: Token is expired or invalid (HTTP 401)${NC}"
157-
echo "Please obtain a fresh token from: https://devstore.rerum.io/"
152+
if [ -z "$decoded_payload" ]; then
153+
echo -e "${RED}ERROR: Failed to decode JWT payload${NC}"
158154
exit 1
155+
fi
156+
157+
# Extract expiration time (exp field in seconds since epoch)
158+
local exp=$(echo "$decoded_payload" | grep -o '"exp":[0-9]*' | cut -d: -f2)
159+
160+
if [ -z "$exp" ]; then
161+
echo -e "${YELLOW}WARNING: Token does not contain 'exp' field${NC}"
162+
echo "Proceeding anyway, but token may be rejected by server..."
159163
else
160-
echo -e "${RED}ERROR: Token validation failed (HTTP $http_code)${NC}"
161-
echo "Response: $(echo "$test_response" | head -n-1)"
162-
exit 1
164+
local current_time=$(date +%s)
165+
if [ "$exp" -lt "$current_time" ]; then
166+
echo -e "${RED}ERROR: Token is expired${NC}"
167+
echo "Token expired at: $(date -d @$exp)"
168+
echo "Current time: $(date -d @$current_time)"
169+
echo "Please obtain a fresh token from: https://devstore.rerum.io/"
170+
exit 1
171+
else
172+
local time_remaining=$((exp - current_time))
173+
local hours=$((time_remaining / 3600))
174+
local minutes=$(( (time_remaining % 3600) / 60 ))
175+
log_success "Token is valid (expires in ${hours}h ${minutes}m)"
176+
fi
163177
fi
164178
}
165179

0 commit comments

Comments
 (0)