From 9c5ec69dceb7f6fc9642359d395b885715a40da9 Mon Sep 17 00:00:00 2001 From: siovanus Date: Thu, 9 Jul 2020 17:59:07 +0800 Subject: [PATCH] update verify date (#115) --- cred.go | 14 +++++++++----- cred_jwt.go | 19 ++++++++++++++----- cred_jwt_test.go | 9 +++++++-- cred_test.go | 9 +++++++-- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/cred.go b/cred.go index 0a36ba7..df3c2f9 100644 --- a/cred.go +++ b/cred.go @@ -311,24 +311,28 @@ func (this *Credential) VerifyCredibleOntId(credibleOntIds []string, credential return fmt.Errorf("VerifyCredibleOntId failed") } -func (this *Credential) VerifyDate(credential *VerifiableCredential) error { +func (this *Credential) VerifyExpirationDate(credential *VerifiableCredential) error { now := time.Now().UTC() if credential.ExpirationDate != "" { expirationDate, err := time.Parse("2006-01-02T15:04:05Z", credential.ExpirationDate) if err != nil { - return fmt.Errorf("VerifyDate error: %s", err) + return fmt.Errorf("VerifyExpirationDate error: %s", err) } if now.Unix() > expirationDate.Unix() { - return fmt.Errorf("VerifyDate expirationDate failed") + return fmt.Errorf("VerifyExpirationDate expirationDate failed") } } + return nil +} +func (this *Credential) VerifyIssuanceDate(credential *VerifiableCredential) error { + now := time.Now().UTC() issuanceDate, err := time.Parse("2006-01-02T15:04:05Z", credential.IssuanceDate) if err != nil { - return fmt.Errorf("VerifyDate error: %s", err) + return fmt.Errorf("VerifyIssuanceDate error: %s", err) } if now.Unix() < issuanceDate.Unix() { - return fmt.Errorf("VerifyDate issuanceDate failed") + return fmt.Errorf("VerifyIssuanceDate issuanceDate failed") } return nil } diff --git a/cred_jwt.go b/cred_jwt.go index f2bbdb4..e0e29fa 100644 --- a/cred_jwt.go +++ b/cred_jwt.go @@ -185,27 +185,36 @@ func (this *Credential) VerifyJWTCredibleOntId(credibleOntIds []string, credenti return fmt.Errorf("VerifyJWTCredibleOntId failed") } -func (this *Credential) VerifyJWTDate(credential string) error { +func (this *Credential) VerifyJWTExpirationDate(credential string) error { JWTCredential, err := DeserializeJWT(credential) if err != nil { - return fmt.Errorf("VerifyJWTDate, DeserializeJWT error: %s", err) + return fmt.Errorf("VerifyJWTExpirationDate, DeserializeJWT error: %s", err) } now := time.Now().UTC() if JWTCredential.Payload.Exp != 0 { if now.Unix() > JWTCredential.Payload.Exp { - return fmt.Errorf("VerifyJWTDate expirationDate failed") + return fmt.Errorf("VerifyJWTExpirationDate expirationDate failed") } } + return nil +} +func (this *Credential) VerifyJWTIssuanceDate(credential string) error { + JWTCredential, err := DeserializeJWT(credential) + if err != nil { + return fmt.Errorf("VerifyJWTIssuanceDate, DeserializeJWT error: %s", err) + } + + now := time.Now().UTC() if JWTCredential.Payload.Nbf != 0 { if now.Unix() < JWTCredential.Payload.Nbf { - return fmt.Errorf("VerifyJWTDate issuanceDate nbf failed") + return fmt.Errorf("VerifyJWTIssuanceDate issuanceDate nbf failed") } } if JWTCredential.Payload.Iat != 0 { if now.Unix() < JWTCredential.Payload.Iat { - return fmt.Errorf("VerifyJWTDate issuanceDate iat failed") + return fmt.Errorf("VerifyJWTIssuanceDate issuanceDate iat failed") } } return nil diff --git a/cred_jwt_test.go b/cred_jwt_test.go index 7f65a62..998e9a8 100644 --- a/cred_jwt_test.go +++ b/cred_jwt_test.go @@ -105,9 +105,14 @@ func TestJWTCredential(t *testing.T) { t.Errorf("TestJWTCredential testOntSdk.Credential.VerifyCredibleOntId error:%s", err) return } - err = testOntSdk.Credential.VerifyJWTDate(s) + err = testOntSdk.Credential.VerifyJWTIssuanceDate(s) if err != nil { - t.Errorf("TestJWTCredential testOntSdk.Credential.VerifyDate error:%s", err) + t.Errorf("TestJWTCredential testOntSdk.Credential.VerifyJWTIssuanceDate error:%s", err) + return + } + err = testOntSdk.Credential.VerifyJWTExpirationDate(s) + if err != nil { + t.Errorf("TestJWTCredential testOntSdk.Credential.VerifyJWTExpirationDate error:%s", err) return } err = testOntSdk.Credential.VerifyJWTIssuerSignature(s) diff --git a/cred_test.go b/cred_test.go index 73d5afb..88bda81 100644 --- a/cred_test.go +++ b/cred_test.go @@ -112,9 +112,14 @@ func TestCredential(t *testing.T) { t.Errorf("TestCredential testOntSdk.Credential.VerifyCredibleOntId error:%s", err) return } - err = testOntSdk.Credential.VerifyDate(credential) + err = testOntSdk.Credential.VerifyIssuanceDate(credential) if err != nil { - t.Errorf("TestCredential testOntSdk.Credential.VerifyDate error:%s", err) + t.Errorf("TestCredential testOntSdk.Credential.VerifyIssuanceDate error:%s", err) + return + } + err = testOntSdk.Credential.VerifyExpirationDate(credential) + if err != nil { + t.Errorf("TestCredential testOntSdk.Credential.VerifyExpirationDate error:%s", err) return } err = testOntSdk.Credential.VerifyIssuerSignature(credential)