gofr-dev/gofr · error
Unable to generate token
Error message
Unable to generate token
What it means
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.
Source
Thrown at pkg/gofr/service/mock_oauth_server.go:63
privateKey, err := rsa.GenerateKey(rand.Reader, privateKeyBits)
require.NoError(t, err, "failed to generate private key, aborting")
server.privateKey = privateKey
mux := http.NewServeMux()
mux.HandleFunc(server.tokenURL, func(w http.ResponseWriter, r *http.Request) {
errMessage, statusCode := server.validateCredentials(r)
if statusCode != http.StatusOK {
http.Error(w, errMessage, statusCode)
return
}
accessToken, err := server.generateToken(getClaims(r))
if err != nil {
http.Error(w, "Unable to generate token", http.StatusInternalServerError)
return
}
// Prepare the JSON response
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Pragma", "no-cache")
tokenResponse := map[string]any{
"access_token": accessToken,
"token_type": "Bearer",
"expires_in": 3600, // Expires in 1 hour
"scope": "read write", // Mock scope
}
_ = json.NewEncoder(w).Encode(tokenResponse)
})
View on GitHub (pinned to 187eb24962)
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.
Defensive patterns
Strategy: try-catch
Validate before calling
// send the exact form the mock expects
form := url.Values{"grant_type": {"client_credentials"}, "client_id": {id}, "client_secret": {secret}}
if form.Get("grant_type") == "" { return errors.New("grant_type required by mock getClaims") } Try / catch
if resp.StatusCode == http.StatusInternalServerError && strings.Contains(body, "Unable to generate token") {
return fmt.Errorf("mock server could not build token from request claims; check getClaims expectations: %s", body)
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- errMessage (dynamic credential validation error)
- require non-empty provider
- invalid interval, require a value greater than 1 second
- modulus is empty
- public exponent is empty
AI-assisted analysis of gofr-dev/gofr@187eb24962 (2026-09-01).
Data as JSON: /api/errors/271c015f2482c692.
Report an issue: GitHub.