Go (RE2)

Bearer Token (Authorization Header) in GO

Match Bearer token values from HTTP Authorization headers, capturing the raw token string.

Try it in the GO tester →

Pattern

regexGO
Bearer\s+([A-Za-z0-9\-._~+\/]+=*)   (flags: i)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`(?i)Bearer\s+([A-Za-z0-9\-._~+\/]+=*)`)
	input := `Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.abc.def`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

How the pattern works

Bearer\s+ matches the scheme keyword (case-insensitive via i flag) and required whitespace. ([A-Za-z0-9\-._~+\/]+=*) captures the token value using the set of characters allowed in OAuth 2.0 Bearer tokens, with optional trailing = padding.

Examples

Input

Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.abc.def

Matches

  • Bearer eyJhbGciOiJIUzI1NiJ9.abc.def

Input

bearer some_token_value==

Matches

  • bearer some_token_value==

Input

Basic dXNlcjpwYXNz

No match

Same pattern, other engines

← Back to Bearer Token (Authorization Header) overview (all engines)