Skip to content

Instantly share code, notes, and snippets.

@bodhi
Created July 6, 2020 03:09
Show Gist options
  • Save bodhi/a7bd835e6fe0c7b3181747cf3159fc79 to your computer and use it in GitHub Desktop.
Save bodhi/a7bd835e6fe0c7b3181747cf3159fc79 to your computer and use it in GitHub Desktop.
Paseto scratch
import (
"fmt"
"time"
paseto "github.com/o1egl/paseto/v2"
)
func main() {
key := []byte("YELLOW SUBMARINE, BLACK WIZARDRY")
claims := paseto.JSONToken{
Audience: "test",
IssuedAt: time.Now(),
Expiration: time.Now().Add(10 * time.Second),
}
fmt.Println(claims)
token, err := paseto.Encrypt(key, claims, nil)
fmt.Println(token)
var decrypted paseto.JSONToken
err = paseto.Decrypt(token, key, &decrypted, nil)
fmt.Println("decrypted", err, decrypted)
err = decrypted.Validate(paseto.ForAudience("test"),
paseto.ValidAt(time.Now()))
fmt.Println("validated", err)
err = decrypted.Validate(paseto.ForAudience("test2"),
paseto.ValidAt(time.Now()))
fmt.Println("validated wrong audience", err)
err = decrypted.Validate(paseto.ForAudience("test"),
paseto.ValidAt(time.Now().Add(30*time.Second)))
fmt.Println("validated expired", err)
err = decrypted.Validate(paseto.ForAudience("test2"),
paseto.ValidAt(time.Now().Add(30*time.Second)))
fmt.Println("validated expired/wrong audience", err)
}
@bodhi
Copy link
Author

bodhi commented Jul 6, 2020

$ go run main.go
{test    2020-07-06 13:08:51.721898 +1000 AEST m=+10.000284824 2020-07-06 13:08:41.721897 +1000 AEST m=+0.000283411 0001-01-01 00:00:00 +0000 UTC map[]}
v2.local.Vnryy5FszZMab-tpJUj2qrBxhDKYGAgW1z4JB-x1Sz5xFvYNFUzb1CIfjNML198wN_CzklWS-35CoKLl-c86b5CKH1Osrsa-JXe5aHCNtk8DRTgFjOkmSMDkrLfKTHSkQ8xpnt1zFPU5j4V4puRCtyCRQM_SQqjgdlA.bnVsbA
decrypted <nil> {test    2020-07-06 13:08:51 +1000 AEST 2020-07-06 13:08:41 +1000 AEST 0001-01-01 00:00:00 +0000 UTC map[aud:test exp:2020-07-06T13:08:51+10:00 iat:2020-07-06T13:08:41+10:00]}
validated <nil>
validated wrong audience token was not intended for "test2" audience: token validation error
validated expired token has expired: token validation error
validated expired/wrong audience token was not intended for "test2" audience: token validation error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment