MCPcopy
hub / github.com/golang-jwt/jwt / ParseFromRequest

Function ParseFromRequest

request/request.go:15–40  ·  view source on GitHub ↗

ParseFromRequest extracts and parses a JWT token from an HTTP request. This behaves the same as Parse, but accepts a request and an extractor instead of a token string. The Extractor interface allows you to define the logic for extracting a token. Several useful implementations are provided. You

(req *http.Request, extractor Extractor, keyFunc jwt.Keyfunc, options ...ParseFromRequestOption)

Source from the content-addressed store, hash-verified

13//
14// You can provide options to modify parsing behavior
15func ParseFromRequest(req *http.Request, extractor Extractor, keyFunc jwt.Keyfunc, options ...ParseFromRequestOption) (token *jwt.Token, err error) {
16 // Create basic parser struct
17 p := &fromRequestParser{req, extractor, nil, nil}
18
19 // Handle options
20 for _, option := range options {
21 option(p)
22 }
23
24 // Set defaults
25 if p.claims == nil {
26 p.claims = jwt.MapClaims{}
27 }
28 if p.parser == nil {
29 p.parser = &jwt.Parser{}
30 }
31
32 // perform extract
33 tokenString, err := p.extractor.ExtractToken(req)
34 if err != nil {
35 return nil, err
36 }
37
38 // perform parse
39 return p.parser.ParseWithClaims(tokenString, p.claims, keyFunc)
40}
41
42// ParseFromRequestWithClaims is an alias for ParseFromRequest but with custom Claims type.
43//

Callers 2

restrictedHandlerFunction · 0.92

Calls 2

ParseWithClaimsMethod · 0.80
ExtractTokenMethod · 0.65

Tested by 1

restrictedHandlerFunction · 0.74