-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathtoken_extraction_test.go
More file actions
44 lines (33 loc) · 1.01 KB
/
token_extraction_test.go
File metadata and controls
44 lines (33 loc) · 1.01 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
package auth0
import (
"fmt"
"net/http"
"testing"
"gopkg.in/square/go-jose.v2/jwt"
)
var tokenRaw = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzdWJqZWN0IiwiaXNzIjoiaXNzdWVyIiwiYXVkIjoiYXVkaWVuY2UifQ.XjWtlyDjBoFDREk1WbvxriSdLve5jI7uyamzCiGdg9U"
var audience = "audience"
var issuer = "issuer"
func TestFromRequestExtraction(t *testing.T) {
headerTokenRequest, _ := http.NewRequest("", "http://localhost", nil)
headerValue := fmt.Sprintf("Bearer %s", tokenRaw)
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.Error("Invalid Claims")
t.FailNow()
}
if len(claims.Audience) != 1 || claims.Issuer == "" {
t.Error("Missing audience, issuer or subject")
return
}
if claims.Issuer != issuer || claims.Audience[0] != audience {
t.Error("Invalid issuer, audience or subject:", claims.Issuer, claims.Audience[0])
}
}