Exemplo n.º 1
0
func (this *ApiMgr) addWriter(w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	cid := vars["cid"]
	var keystr string
	if !this.decodeJsonBody(w, req, &keystr) {
		return
	}
	var pubkey *crypto.PublicIdentity
	err := transfer.DecodeString(keystr, &pubkey)
	if err != nil {
		this.sendError(w, http.StatusBadRequest, "Invalid public key")
		return
	}
	owner := this.GetOwner(cid)
	if owner == nil {
		this.sendError(w, http.StatusNotFound, "Collection invalid")
		return
	}
	if owner.Fingerprint().String() != this.Ident.Public().Fingerprint().String() {
		this.sendError(w, http.StatusUnauthorized, "You are not the owner of the collection")
		return
	}
	this.AddWriter(cid, this.Ident, pubkey)
	this.sendJson(w, "/collections/"+cid+"/writers/"+pubkey.Fingerprint().String())
}
Exemplo n.º 2
0
func TestDigest(t *testing.T) {
	hasher1 := NewHasher()
	hasher2 := NewHasher()
	hasher1.Write([]byte("Hello world"))
	hasher2.Write([]byte("Test123"))
	hash1 := hasher1.Finalize()
	hash2 := hasher2.Finalize()
	enc1, err1 := transfer.EncodeString(hash1)
	enc2, err2 := transfer.EncodeString(hash2)
	if hash1.Equal(hash2) {
		t.Fatal("Hashes match")
	}
	if err1 != nil || err2 != nil {
		t.Fatalf("Error encoding hashes")
	}
	t.Logf("Hash of 'Hello world' is: %s", enc1)
	t.Logf("Hash of 'Test123' is: %s", enc2)
	var dh1 *Digest
	err := transfer.DecodeString(enc1, &dh1)
	if err != nil {
		t.Fatalf("Error decoding hash: %s", err)
	}
	if !hash1.Equal(dh1) {
		t.Fatal("Hash didn't deserialize right")
	}
	hasher3 := NewHasher()
	hasher3.Write([]byte("Hello"))
	hasher3.Write([]byte(" "))
	hasher3.Write([]byte("world"))
	hash3 := hasher3.Finalize()
	if !hash1.Equal(hash3) {
		t.Fatal("Write alternate failed")
	}
}
Exemplo n.º 3
0
func (this *ApiMgr) decodeWho(req *http.Request) *crypto.Digest {
	vars := mux.Vars(req)
	friendStr := vars["who"]
	var fp *crypto.Digest
	err := transfer.DecodeString(friendStr, &fp)
	if err != nil {
		return nil
	}
	return fp
}
Exemplo n.º 4
0
func (this *ApiMgr) postFriends(w http.ResponseWriter, req *http.Request) {
	var json FriendJson
	if !this.decodeJsonBody(w, req, &json) {
		return
	}
	var fp *crypto.Digest
	err := transfer.DecodeString(json.Id, &fp)
	if err != nil {
		this.sendError(w, http.StatusBadRequest, "Invalid friend id")
		return
	}
	this.doPutFriend(w, req, fp, &json.SelfJson)
}
Exemplo n.º 5
0
func (this *ApiMgr) postInvite(w http.ResponseWriter, req *http.Request) {
	var invite InviteJson
	if !this.decodeJsonBody(w, req, &invite) {
		return
	}
	var fp *crypto.Digest
	err := transfer.DecodeString(invite.Friend, &fp)
	if err != nil {
		this.sendError(w, http.StatusBadRequest, "Invalid friend Id")
		return
	}
	this.Log.Printf("Processing an invite %s:%s:%v", invite.Cid, invite.Friend, invite.Remove)
	if !this.Subscribe(fp, invite.Cid, !invite.Remove) {
		this.sendError(w, http.StatusBadRequest, "Invalid friend Id")
	}
}
Exemplo n.º 6
0
func TestIdent(t *testing.T) {
	s1 := NewSecretIdentity("pass1")
	s2 := NewSecretIdentity("pass2")
	p1 := s1.Public()
	if s1.Fingerprint().Equal(s2.Fingerprint()) {
		t.Fatal("Two identies with same fingerprint")
	}
	if !s1.Fingerprint().Equal(p1.Fingerprint()) {
		t.Fatal("Public and private fingerprints don't match")
	}
	x509 := s1.X509Certificate()
	p3, err := PublicFromCert(x509)
	if err != nil {
		t.Fatalf("Error during cert parse: %s", err)
	}
	if !p3.Fingerprint().Equal(p1.Fingerprint()) {
		t.Fatal("Certificate round trip failed")
	}
	p1enc, err := transfer.EncodeString(p1)
	if err != nil {
		t.Fatalf("Unable to encode public key")
	}
	t.Logf("Public key #1: %s", p1enc)
	var p4 *PublicIdentity
	err = transfer.DecodeString(p1enc, &p4)
	if err != nil {
		t.Fatalf("Unable to decode public key")
	}
	if !p4.Fingerprint().Equal(p1.Fingerprint()) {
		t.Fatal("Round trip of public key fails")
	}
	s1l := s1.Lock()
	s1le, err := transfer.EncodeString(s1l)
	if err != nil {
		t.Fatal("Unable to write locked private key: %s", err)
	}
	t.Logf("Locked private key: %s", s1le)
	var s1ld *LockedIdentity
	err = transfer.DecodeString(s1le, &s1ld)
	if err != nil {
		t.Fatal("Unable to decode locked key")
	}
	_, err = UnlockSecretIdentity(s1ld, "badpass")
	if err == nil {
		t.Fatal("Was able to unlock secret id with the wrong key")
	}
	t.Logf("Tried to use bad password, got: %s", err)
	s3, err := UnlockSecretIdentity(s1ld, "pass1")
	if err != nil {
		t.Fatalf("Unable to unlock id with correct password: %s", err)
	}
	if !s1.Fingerprint().Equal(s3.Fingerprint()) {
		t.Fatal("Unlocked new private key doesn't match original")
	}
	hasher := NewHasher()
	hasher.Write([]byte("Hello world"))
	digest := hasher.Finalize()
	sig2 := s2.Sign(digest)
	sig3 := s3.Sign(digest)
	sig3s, err := transfer.EncodeString(sig3)
	if err != nil {
		t.Fatalf("Unable to encode signature: %s", err)
	}
	t.Logf("Here is a signature: %s", sig3s)
	var sig4 *Signature
	err = transfer.DecodeString(sig3s, &sig4)
	if p1.Verify(digest, sig2) {
		t.Fatal("Bad signature worked")
	}
	if !p1.Verify(digest, sig4) {
		t.Fatal("Good signature failed")
	}
}