// generateTokenWithTime generates token using the given time. func generateTokenWithTime(c context.Context, user string, action string, now time.Time) (string, error) { nowTime := now.UnixNano() d := toData(user, action, nowTime) key, signed, err := signature.Sign(c, d) if err != nil { return "", err } sig, err := json.Marshal(sigData{ Key: key, Signature: signed, IssueTime: nowTime, }) if err != nil { return "", err } return base64.URLEncoding.EncodeToString(sig), nil }
func TestShouldSignAndCheck(t *testing.T) { c := context.Background() blob := []byte("blob") key, sig, err := signature.Sign(c, blob) if err != nil { t.Fatalf("Sign(_, %v)=_,_,%v; want <nil>", blob, err) } pc, err := signature.PublicCerts(c) if err != nil { t.Fatalf("PublicCerts(_)=%v; want <nil>", err) } cert := signature.X509CertByName(pc, key) if cert == nil { t.Fatalf("X509CertByName(%v, %v)=<nil>; want non nil", pc, key) } err = signature.Check(blob, cert, sig) if err != nil { t.Errorf("Check(%v, %v, %v)=%v; want <nil>", blob, cert, sig, err) } }