- Notifications
You must be signed in to change notification settings - Fork 371
/
Copy pathrequest.go
70 lines (58 loc) · 1.9 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package request
import (
"net/http"
"github.com/golang-jwt/jwt/v4"
)
// 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 can provide options to modify parsing behavior
funcParseFromRequest(req*http.Request, extractorExtractor, keyFunc jwt.Keyfunc, options...ParseFromRequestOption) (token*jwt.Token, errerror) {
// Create basic parser struct
p:=&fromRequestParser{req, extractor, nil, nil}
// Handle options
for_, option:=rangeoptions {
option(p)
}
// Set defaults
ifp.claims==nil {
p.claims= jwt.MapClaims{}
}
ifp.parser==nil {
p.parser=&jwt.Parser{}
}
// perform extract
tokenString, err:=p.extractor.ExtractToken(req)
iferr!=nil {
returnnil, err
}
// perform parse
returnp.parser.ParseWithClaims(tokenString, p.claims, keyFunc)
}
// ParseFromRequestWithClaims is an alias for ParseFromRequest but with custom Claims type.
//
// Deprecated: use ParseFromRequest and the WithClaims option
funcParseFromRequestWithClaims(req*http.Request, extractorExtractor, claims jwt.Claims, keyFunc jwt.Keyfunc) (token*jwt.Token, errerror) {
returnParseFromRequest(req, extractor, keyFunc, WithClaims(claims))
}
typefromRequestParserstruct {
req*http.Request
extractorExtractor
claims jwt.Claims
parser*jwt.Parser
}
typeParseFromRequestOptionfunc(*fromRequestParser)
// WithClaims parses with custom claims
funcWithClaims(claims jwt.Claims) ParseFromRequestOption {
returnfunc(p*fromRequestParser) {
p.claims=claims
}
}
// WithParser parses using a custom parser
funcWithParser(parser*jwt.Parser) ParseFromRequestOption {
returnfunc(p*fromRequestParser) {
p.parser=parser
}
}