@@ -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
0 commit comments