forked from dschenkelman/auth0
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_extraction_test.go
More file actions
48 lines (37 loc) · 1.19 KB
/
token_extraction_test.go
File metadata and controls
48 lines (37 loc) · 1.19 KB
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
package auth0
import (
"fmt"
"net/http"
"reflect"
"testing"
"time"
"gopkg.in/square/go-jose.v2"
"gopkg.in/square/go-jose.v2/jwt"
)
func TestFromRequestExtraction(t *testing.T) {
referenceToken := getTestToken(defaultAudience, defaultIssuer, time.Now(), jose.HS256, defaultSecret)
headerTokenRequest, _ := http.NewRequest("", "http://localhost", nil)
headerValue := fmt.Sprintf("Bearer %s", referenceToken)
headerTokenRequest.Header.Add("Authorization", headerValue)
token, err := FromHeader(headerTokenRequest)
if err != nil {
t.Error(err)
return
}
claims := jwt.Claims{}
err = token.Claims([]byte("secret"), &claims)
if err != nil {
t.Errorf("Claims should be decoded correctly with default token: %q \n", err)
t.FailNow()
}
if claims.Issuer != defaultIssuer || !reflect.DeepEqual(claims.Audience, jwt.Audience(defaultAudience)) {
t.Error("Invalid issuer, audience or subject:", claims.Issuer, claims.Audience)
}
}
func TestInvalidExtract(t *testing.T) {
headerTokenRequest, _ := http.NewRequest("", "http://localhost", nil)
_, err := FromHeader(headerTokenRequest)
if err == nil {
t.Error("A request without valid Authorization header should return an error.")
}
}