{"record":{"id":"4750b28d6a7ae764","repo":"ory/kratos","slug":"basic-auth-auth-strategy-requires-a-string-user","errorCode":null,"errorMessage":"basic_auth auth strategy requires a string user","messagePattern":"basic_auth auth strategy requires a string user","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"request/auth.go","lineNumber":47,"sourceCode":"func authStrategy(typ string, config map[string]any) (AuthStrategy, error) {\n\tswitch typ {\n\tcase \"\":\n\t\treturn NewNoopAuthStrategy(), nil\n\tcase \"api_key\":\n\t\tname, ok := config[\"name\"].(string)\n\t\tif !ok {\n\t\t\treturn nil, fmt.Errorf(\"api_key auth strategy requires a string name\")\n\t\t}\n\t\tvalue, ok := config[\"value\"].(string)\n\t\tif !ok {\n\t\t\treturn nil, fmt.Errorf(\"api_key auth strategy requires a string value\")\n\t\t}\n\t\tin, _ := config[\"in\"].(string) // in is optional\n\t\treturn NewAPIKeyStrategy(in, name, value), nil\n\tcase \"basic_auth\":\n\t\tuser, ok := config[\"user\"].(string)\n\t\tif !ok {\n\t\t\treturn nil, fmt.Errorf(\"basic_auth auth strategy requires a string user\")\n\t\t}\n\t\tpassword, ok := config[\"password\"].(string)\n\t\tif !ok {\n\t\t\treturn nil, fmt.Errorf(\"basic_auth auth strategy requires a string password\")\n\t\t}\n\t\treturn NewBasicAuthStrategy(user, password), nil\n\t}\n\n\treturn nil, fmt.Errorf(\"unsupported auth type: %s\", typ)\n}\n\nfunc NewNoopAuthStrategy() AuthStrategy {\n\treturn &noopAuthStrategy{}\n}\n\nfunc (c *noopAuthStrategy) apply(_ *retryablehttp.Request) {}\n\nfunc NewBasicAuthStrategy(user, password string) AuthStrategy {","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/ory/kratos/blob/b86338da04a040247a07f46100a86dcfb3875909/request/auth.go#L29-L65","documentation":"The auth strategy factory tried to build the basic_auth strategy, but the 'user' key in the strategy's config map is either absent or not a Go string. The type assertion config[\"user\"].(string) failed, so no strategy could be constructed.","triggerScenarios":"basic_auth auth config without a \"user\" key, or \"user\" of a non-string type (number, bool, null) — e.g. YAML user: 42 or user: true.","commonSituations":"Numeric usernames parsed as integers in YAML; partially migrated configs still carrying api_key's name/value keys; typos like \"username\" instead of \"user\".","solutions":["Add a string \"user\" field to the basic_auth config","Quote numeric-looking usernames in YAML","Rename \"username\"/\"login\" keys to exactly \"user\"","Check that \"password\" is also a string, since that check follows"],"exampleFix":"// before\nauth: {type: basic_auth, username: alice, password: \"s3cret\"}\n// after\nauth: {type: basic_auth, user: alice, password: \"s3cret\"}","handlingStrategy":"validation","validationCode":"// Go: check basic_auth user before building\nfunc validateBasicAuthUser(cfg map[string]interface{}) error {\n\tif _, ok := cfg[\"user\"].(string); !ok {\n\t\treturn errors.New(\"basic_auth requires a string user\")\n\t}\n\tif _, ok := cfg[\"password\"].(string); !ok {\n\t\treturn errors.New(\"basic_auth requires a string password\")\n\t}\n\treturn nil\n}","typeGuard":"func hasBasicAuthUser(cfg map[string]interface{}) bool {\n\t_, ok := cfg[\"user\"].(string)\n\treturn ok\n}","tryCatchPattern":"b, err := request.NewBuilder(cfg)\nif err != nil {\n\tif strings.Contains(err.Error(), \"basic_auth\") {\n\t\treturn fmt.Errorf(\"basic_auth config must have string 'user' and 'password'\")\n\t}\n\treturn err\n}","preventionTips":["Use the key \"user\", not \"username\"","Quote numeric usernames in YAML","Validate config against the Kratos JSON schema in CI","Keep auth blocks consistent when migrating between api_key and basic_auth"],"tags":["configuration","auth","basic-auth","type-mismatch"],"backgroundTag":"missing-required-config-field","analyzedSha":"b86338da04a040247a07f46100a86dcfb3875909","analyzedAt":"2026-09-07T15:58:15.934Z","contentChangedAt":"2026-09-07T15:58:15.934Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}