{"record":{"id":"271c015f2482c692","repo":"gofr-dev/gofr","slug":"unable-to-generate-token","errorCode":null,"errorMessage":"Unable to generate token","messagePattern":"Unable to generate token","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"pkg/gofr/service/mock_oauth_server.go","lineNumber":63,"sourceCode":"\n\tprivateKey, err := rsa.GenerateKey(rand.Reader, privateKeyBits)\n\trequire.NoError(t, err, \"failed to generate private key, aborting\")\n\n\tserver.privateKey = privateKey\n\n\tmux := http.NewServeMux()\n\n\tmux.HandleFunc(server.tokenURL, func(w http.ResponseWriter, r *http.Request) {\n\t\terrMessage, statusCode := server.validateCredentials(r)\n\n\t\tif statusCode != http.StatusOK {\n\t\t\thttp.Error(w, errMessage, statusCode)\n\t\t\treturn\n\t\t}\n\n\t\taccessToken, err := server.generateToken(getClaims(r))\n\t\tif err != nil {\n\t\t\thttp.Error(w, \"Unable to generate token\", http.StatusInternalServerError)\n\t\t\treturn\n\t\t}\n\n\t\t// Prepare the JSON response\n\t\tw.Header().Set(\"Content-Type\", \"application/json\")\n\t\tw.Header().Set(\"Cache-Control\", \"no-store\")\n\t\tw.Header().Set(\"Pragma\", \"no-cache\")\n\n\t\ttokenResponse := map[string]any{\n\t\t\t\"access_token\": accessToken,\n\t\t\t\"token_type\":   \"Bearer\",\n\t\t\t\"expires_in\":   3600,         // Expires in 1 hour\n\t\t\t\"scope\":        \"read write\", // Mock scope\n\t\t}\n\n\t\t_ = json.NewEncoder(w).Encode(tokenResponse)\n\t})\n","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/service/mock_oauth_server.go#L45-L81","documentation":"In mock_oauth_server.go:63, if the mock server's generateToken (signing/claims marshalling) returns an error, the token endpoint responds with a 500 and the body \"Unable to generate token\". This is a server-side failure inside the test double, meaning credentials were accepted but token creation failed.","triggerScenarios":"POST to the mock token URL with valid credentials, but generateToken(getClaims(r)) fails — typically because getClaims produced malformed/unparseable claims (e.g. unsupported grant_type or a body shape the claims extractor cannot handle) or the signing step errors.","commonSituations":"Sending a client_credentials request when the mock only expects a specific form encoding; test helper changes to getClaims breaking compatibility; misconfigured token URL causing requests to hit the handler with an unexpected request shape.","solutions":["Check the form/body the client sends to the token URL matches what getClaims expects (grant_type, client_id, etc.).","Verify server.tokenURL registration matches the path the client requests, so only well-formed requests reach this handler.","Inspect generateToken for signing-key configuration errors in the test setup.","Update the mock's getClaims/generateToken to support the new request shape if your client legitimately changed."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// send the exact form the mock expects\nform := url.Values{\"grant_type\": {\"client_credentials\"}, \"client_id\": {id}, \"client_secret\": {secret}}\nif form.Get(\"grant_type\") == \"\" { return errors.New(\"grant_type required by mock getClaims\") }","typeGuard":null,"tryCatchPattern":"if resp.StatusCode == http.StatusInternalServerError && strings.Contains(body, \"Unable to generate token\") {\n\treturn fmt.Errorf(\"mock server could not build token from request claims; check getClaims expectations: %s\", body)\n}","preventionTips":["Match the token request encoding (form fields) to what the mock's getClaims parses.","Keep server.tokenURL and the client's token URL identical in test fixtures.","Pin the mock server implementation in your test helpers to avoid drift.","Log request bodies in the mock when generateToken fails."],"tags":["testing","oauth","mock-server","internal-error"],"backgroundTag":"token-generation-failed","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}