forked from dgrijalva/jwt-go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
hmac_example_test.go
67 lines (55 loc) · 2.06 KB
/
hmac_example_test.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
package h256only_test
import (
"encoding/hex"
"fmt"
"time"
"github.com/kevinburke/h256only"
)
func ExampleNew_hmac() {
// Load your secret key from a safe place and reuse it across multiple
// calls. (Obviously don't use this example key for anything real.) If you
// want to convert a passphrase to a key, use a suitable package like bcrypt
// or scrypt.
secretKeyBytes, err := hex.DecodeString("6368616e676520746869732070617373776f726420746f206120736563726574")
if err != nil {
panic(err)
}
var secretKey [32]byte
copy(secretKey[:], secretKeyBytes)
// Create a new token object, specifying signing method and the claims
// you would like it to contain.
token := h256only.NewWithClaims(h256only.MapClaims{
"foo": "bar",
"nbf": time.Date(2015, 10, 10, 12, 0, 0, 0, time.UTC).Unix(),
})
// Sign and get the complete encoded token as a string using the secret
tokenString, err := token.SignedString(&secretKey)
fmt.Println(tokenString, err)
// Output:
// eyJ0eXAiOiJoMjU2b25seSJ9.eyJmb28iOiJiYXIiLCJuYmYiOjE0NDQ0Nzg0MDB9.qBIRJpbvtdNgqsSHjawY-x6sB7LL2416pb5r7LIVeUI <nil>
}
// Example parsing and validating a token using the HMAC signing method
func ExampleParse_hmac() {
// sample token string taken from the New example
tokenString := "eyJ0eXAiOiJoMjU2b25seSJ9.eyJmb28iOiJiYXIiLCJuYmYiOjE0NDQ0Nzg0MDB9.qBIRJpbvtdNgqsSHjawY-x6sB7LL2416pb5r7LIVeUI"
// Load your secret key from a safe place and reuse it across multiple
// calls. (Obviously don't use this example key for anything real.) If you
// want to convert a passphrase to a key, use a suitable package like bcrypt
// or scrypt.
secretKeyBytes, err := hex.DecodeString("6368616e676520746869732070617373776f726420746f206120736563726574")
if err != nil {
panic(err)
}
var secretKey [32]byte
copy(secretKey[:], secretKeyBytes)
// Parse takes the token string and a key.
token, err := h256only.Parse(tokenString, &secretKey)
if err != nil {
fmt.Println(err)
return
}
if claims, ok := token.Claims.(h256only.MapClaims); ok {
fmt.Println(claims["foo"], claims["nbf"])
}
// Output: bar 1444478400
}