EqualFold Compares Schemes Case Insensitively
Compare auth scheme names with strings.EqualFold so Bearer, BEARER and bearer all route correctly; credentials themselves need a constant-time compare.
Code
package main
import (
"fmt"
"strings"
)
// authorizeScheme 只判断认证方案的类别:RFC 7235 里方案名不区分大小写
// 凭据本身不在这里比较,应交给常量时间比较函数
func authorizeScheme(header string) string {
scheme, _, _ := strings.Cut(header, " ")
switch {
case strings.EqualFold(scheme, "Bearer"):
return "token flow"
case strings.EqualFold(scheme, "Basic"):
return "basic flow"
default:
return "unsupported scheme: " + scheme
}
}
func main() {
for _, header := range []string{
"Bearer abc123",
"BEARER abc123",
"bearer abc123",
"Basic dXNlcjpwYXNz",
"ApiKey secret",
} {
fmt.Printf("%-20s -> %s\n", header, authorizeScheme(header))
}
}Inside the logic
Under the hood: the functions authorizeScheme and main.
The cycling part of this snippet is for _, header := range []string.
Points of care
- Nothing but built-in APIs — the supply chain question does not arise.
Using it
The declaration to build on: func authorizeScheme(header string) string
main calls 1 helper in sequence: authorizeScheme.
Key statements
func authorizeScheme(header string) string {
scheme, _, _ := strings.Cut(header, " ")
switch {
case strings.EqualFold(scheme, "Bearer"):
return "token flow"
case strings.EqualFold(scheme, "Basic"):
return "basic flow"
default:
return "unsupported scheme: " + scheme
}
}
for _, header := range []string{
"Bearer abc123",
"BEARER abc123",
"bearer abc123",
"Basic dXNlcjpwYXNz",
"ApiKey secret",
} {
fmt.Printf("%-20s -> %s\n", header, authorizeScheme(header))
}
Interactive Tools for This Topic
Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.Basic & Bearer Auth Header ToolEncode and decode HTTP Authorization Basic and Bearer headers locally with UTF-8 Base64 validation and automatic JWT claim and expiry inspection.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.Case ConverterConvert text between lowercase, uppercase, title, sentence, camelCase, snake_case, and kebab-case while preserving Unicode letters and numbers.
FAQ
- How do you run EqualFold Compares Schemes Case Insensitively?
- Execute it straight away — the program needs nothing beyond the runtime.
Report a problem with this exampleOpens a pre-filled issue on GitHub.