Package list golang-github-go-kit-kit / bfba5b7
Refactor extractTokenFromAuthHeader function Brian Kassouf 5 years ago
1 changed file(s) with 9 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
88 "github.com/go-kit/kit/transport/http"
99 "golang.org/x/net/context"
1010 "google.golang.org/grpc/metadata"
11 )
12
13 const (
14 BEARER string = "bearer"
15 BEARER_FORMAT string = "Bearer %s"
1116 )
1217
1318 // moves JWT token from request header to context
7075
7176 // extractTokenFromAuthHeader returns the token from the value of the Authorzation header
7277 func extractTokenFromAuthHeader(val string) (token string, ok bool) {
73 if len(val) < 8 || !strings.EqualFold(val[0:7], "BEARER ") {
78 authHeaderParts := strings.Split(val, " ")
79 if len(authHeaderParts) != 2 || strings.ToLower(authHeaderParts[0]) != BEARER {
7480 return "", false
7581 }
7682
77 return val[7:], true
83 return authHeaderParts[1], true
7884 }
7985
8086 func generateAuthHeaderFromToken(token string) string {
81 return fmt.Sprintf("Bearer %s", token)
87 return fmt.Sprintf(BEARER_FORMAT, token)
8288 }