mirror of https://github.com/perkeep/perkeep.git
pkg/schema: make sure claim date becomes signature time
It seems that signing a claim schema does not automatically set the claim date to the signature time, which I believe should be the case. This change adds a test to demonstrate the issue, and the fix that makes the test pass. Fixes #917 Change-Id: I55d6c75cbe3c3c4c1d6e5b69f6d26277d0d12728
This commit is contained in:
parent
1486c78dec
commit
353e1e8873
|
@ -429,7 +429,7 @@ func (bb *Builder) SetSigner(signer blob.Ref) *Builder {
|
||||||
return bb
|
return bb
|
||||||
}
|
}
|
||||||
|
|
||||||
// SignAt sets the blob builder's camliSigner field with SetSigner
|
// Sign sets the blob builder's camliSigner field with SetSigner
|
||||||
// and returns the signed JSON using the provided signer.
|
// and returns the signed JSON using the provided signer.
|
||||||
func (bb *Builder) Sign(signer *Signer) (string, error) {
|
func (bb *Builder) Sign(signer *Signer) (string, error) {
|
||||||
return bb.SignAt(signer, time.Time{})
|
return bb.SignAt(signer, time.Time{})
|
||||||
|
@ -445,6 +445,10 @@ func (bb *Builder) SignAt(signer *Signer, sigTime time.Time) (string, error) {
|
||||||
default:
|
default:
|
||||||
return "", fmt.Errorf("can't sign camliType %q", bb.Type())
|
return "", fmt.Errorf("can't sign camliType %q", bb.Type())
|
||||||
}
|
}
|
||||||
|
if sigTime.IsZero() {
|
||||||
|
sigTime = time.Now()
|
||||||
|
}
|
||||||
|
bb.SetClaimDate(sigTime)
|
||||||
return signer.SignJSON(bb.SetSigner(signer.pubref).Blob().JSON(), sigTime)
|
return signer.SignJSON(bb.SetSigner(signer.pubref).Blob().JSON(), sigTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,8 +17,10 @@ limitations under the License.
|
||||||
package schema
|
package schema
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"camlistore.org/pkg/blob"
|
"camlistore.org/pkg/blob"
|
||||||
"camlistore.org/pkg/jsonsign"
|
"camlistore.org/pkg/jsonsign"
|
||||||
|
@ -49,3 +51,40 @@ func TestSigner(t *testing.T) {
|
||||||
t.Errorf("Permanode doesn't look signed: %v", pn)
|
t.Errorf("Permanode doesn't look signed: %v", pn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestClaimDate makes sure that when we sign a schema, we set the claimDate to
|
||||||
|
// the time of the signature.
|
||||||
|
// It demonstrates that issue #917 is fixed.
|
||||||
|
func TestClaimDate(t *testing.T) {
|
||||||
|
ent, err := jsonsign.NewEntity()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
armorPub, err := jsonsign.ArmoredPublicKey(ent)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
pubRef := blob.SHA1FromString(armorPub)
|
||||||
|
sig, err := NewSigner(pubRef, strings.NewReader(armorPub), ent)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewSigner: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sigTime, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
share := NewShareRef(ShareHaveRef, true).SetShareTarget(pubRef)
|
||||||
|
signed, err := share.SignAt(sig, sigTime)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ss := &superset{}
|
||||||
|
if err := json.NewDecoder(strings.NewReader(signed)).Decode(ss); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !strings.HasPrefix(ss.ClaimDate.String(), "2006-01-02") {
|
||||||
|
t.Fatalf("wrong claimDate in superset: got %q, wanted %q", ss.ClaimDate, sigTime)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue