File: request.go

package info (click to toggle)
golang-github-dgrijalva-jwt-go-v3 3.0.0%2Bv3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch, stretch-backports
  • size: 372 kB
  • ctags: 340
  • sloc: makefile: 5
file content (24 lines) | stat: -rw-r--r-- 929 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package request

import (
	"github.com/dgrijalva/jwt-go-v3"
	"net/http"
)

// Extract and parse 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.
func ParseFromRequest(req *http.Request, extractor Extractor, keyFunc jwt.Keyfunc) (token *jwt.Token, err error) {
	return ParseFromRequestWithClaims(req, extractor, jwt.MapClaims{}, keyFunc)
}

// ParseFromRequest but with custom Claims type
func ParseFromRequestWithClaims(req *http.Request, extractor Extractor, claims jwt.Claims, keyFunc jwt.Keyfunc) (token *jwt.Token, err error) {
	// Extract token from request
	if tokStr, err := extractor.ExtractToken(req); err == nil {
		return jwt.ParseWithClaims(tokStr, claims, keyFunc)
	} else {
		return nil, err
	}
}