{"record":{"id":"479b18793f3aea76","repo":"iflytek/astron-agent","slug":"failed-to-build-authentication-url","errorCode":null,"errorMessage":"Failed to build authentication URL","messagePattern":"Failed to build authentication URL","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"console/backend/hub/src/main/java/com/iflytek/astron/console/hub/util/BotAIServiceClient.java","lineNumber":514,"sourceCode":"            mac.init(spec);\n\n            byte[] hexDigits = mac.doFinal(signatureString.getBytes(StandardCharsets.UTF_8));\n            String signature = Base64.getEncoder().encodeToString(hexDigits);\n\n            String authorization = String.format(\n                    \"hmac username=\\\"%s\\\", algorithm=\\\"%s\\\", headers=\\\"%s\\\", signature=\\\"%s\\\"\",\n                    apiKey, \"hmac-sha256\", \"host date request-line\", signature);\n\n            String authBase = Base64.getEncoder().encodeToString(authorization.getBytes(StandardCharsets.UTF_8));\n\n            return String.format(\"%s?authorization=%s&host=%s&date=%s\",\n                    requestUrl,\n                    URLEncoder.encode(authBase, StandardCharsets.UTF_8),\n                    URLEncoder.encode(host, StandardCharsets.UTF_8),\n                    URLEncoder.encode(date, StandardCharsets.UTF_8));\n\n        } catch (Exception e) {\n            throw new IllegalArgumentException(\"Failed to build authentication URL\", e);\n        }\n    }\n\n    /**\n     * Build WebSocket authentication URL (for text generation)\n     */\n    private String buildWebSocketAuthUrl(String hostUrl, String apiKey, String apiSecret)\n            throws IllegalArgumentException {\n        try {\n            URI uri = URI.create(hostUrl);\n            SimpleDateFormat format = new SimpleDateFormat(\"EEE, dd MMM yyyy HH:mm:ss z\", Locale.US);\n            format.setTimeZone(TimeZone.getTimeZone(\"GMT\"));\n            String date = format.format(new Date());\n\n            String preStr = \"host: \" + uri.getHost() + \"\\n\" +\n                    \"date: \" + date + \"\\n\" +\n                    \"GET \" + uri.getPath() + \" HTTP/1.1\";\n","sourceCodeStart":496,"sourceCodeEnd":532,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/console/backend/hub/src/main/java/com/iflytek/astron/console/hub/util/BotAIServiceClient.java#L496-L532","documentation":"buildAuthenticatedUrl constructs the HMAC-SHA256 signed URL for the image generation HTTP API (authorization/host/date query params). Any exception during URI creation, date formatting, MAC initialization, or URL encoding is wrapped as IllegalArgumentException('Failed to build authentication URL', cause). It indicates the request URL could not be signed, before any network call is made.","triggerScenarios":"requestUrl is malformed (URI.create throws or uri.getHost() returns null), apiSecret is null (NPE on getBytes), or URLEncoder/String.format fails — thrown when generateImage calls buildAuthenticatedUrl(imageHost, apiKey, apiSecret, \"POST\").","commonSituations":"Missing iFlytek platform API secret in platform account config (null apiKey/apiSecret), imageHost changed to an invalid URL, or a broken URL constant after an environment config change.","solutions":["Inspect the wrapped cause exception in the stack trace to pinpoint the failure","Verify platform account config has non-null platformApiKey and platformApiSecret","Validate the imageHost constant is a well-formed absolute URL with a host","Pre-check credentials before calling the client (fail fast with a config error instead of IllegalArgumentException)"],"exampleFix":"// before\nString requestUrl = buildAuthenticatedUrl(imageHost, config.getPlatformApiKey(), config.getPlatformApiSecret(), \"POST\");\n// after\nString apiKey = config.getPlatformApiKey();\nString apiSecret = config.getPlatformApiSecret();\nif (StrUtil.isBlank(apiKey) || StrUtil.isBlank(apiSecret)) {\n    throw new BusinessException(ResponseEnum.CONFIG_ERROR);\n}\nString requestUrl = buildAuthenticatedUrl(imageHost, apiKey, apiSecret, \"POST\");","handlingStrategy":"validation","validationCode":"if (StrUtil.isBlank(apiKey) || StrUtil.isBlank(apiSecret) || URI.create(imageHost).getHost() == null) { throw new BusinessException(ResponseEnum.CONFIG_ERROR); }","typeGuard":null,"tryCatchPattern":"try { JSONObject r = client.generateImage(uid, prompt, size); } catch (IllegalArgumentException e) { log.error(\"Auth URL build failed (config?)\", e); throw new BusinessException(ResponseEnum.CONFIG_ERROR); }","preventionTips":["Ensure platformApiKey/platformApiSecret are configured before calling the client","Keep the imageHost constant a valid absolute URL","Fail fast on blank credentials with a clear config error"],"tags":["java","hmac","authentication","url-building"],"backgroundTag":"invalid-url-format","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}