Skip to content

Commit

Permalink
update verify date (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
siovanus authored Jul 9, 2020
1 parent a3cd172 commit 9c5ec69
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
14 changes: 9 additions & 5 deletions cred.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
19 changes: 14 additions & 5 deletions cred_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions cred_jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions cred_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9c5ec69

Please sign in to comment.