Exemplo n.º 1
0
func CreateCredHandler(w http.ResponseWriter, r *http.Request) {
	// TODO: Prevent making the same cred a second time
	cred := &credence.Cred{}
	if err := jsonpb.Unmarshal(r.Body, cred); err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	fingerprint := len(r.URL.Query()["fingerprint"]) > 0
	confirm := len(r.URL.Query()["confirm"]) > 0

	signingUser := models.Me()

	if !confirm && helpers.StatementAlreadyMade(cred, signingUser) {
		w.WriteHeader(http.StatusConflict)
		return
	}

	if fingerprint {
		cred.AuthorFingerprint = signingUser.Fingerprint
	}

	// Set attributes
	cred.Timestamp = time.Now().Unix()
	err := helpers.SetSignature(cred)
	if err != nil {
		panic(err)
	}

	// Store in the DB
	helpers.StoreCredWithAuthor(cred, signingUser)

	// Set up the broadcaster
	broadcaster, err := goczmq.NewPush("inproc://broadcast")
	if err != nil {
		panic(err)
	}
	defer broadcaster.Destroy()

	// Create the broadcastable bytes
	msg := &credence.Message{
		Type: &credence.Message_Cred{
			Cred: cred,
		},
	}

	msgBytes, err := proto.Marshal(msg)
	if err != nil {
		log.Print(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	_, err = broadcaster.Write(msgBytes)
	if err != nil {
		panic(err)
	}

	helpers.ModelNegotiator().Negotiate(w, r, cred)
}
Exemplo n.º 2
0
func SavePublicKeyIfNeccessary() {
	privateKey, err := LoadPrivateKey()
	if err != nil {
		return
	}

	me := models.Me()
	if me.Fingerprint == nil {
		SavePublicKeyToDB(privateKey)
	}
}
Exemplo n.º 3
0
func SavePublicKeyToDB(privateKey openssl.PrivateKey) {
	publicDerBlock, err := privateKey.MarshalPKIXPublicKeyDER()
	if err != nil {
		panic(err)
	}

	fingerprint, err := openssl.SHA256(publicDerBlock)
	if err != nil {
		panic(err)
	}

	me := models.Me()
	me.PublicKey = publicDerBlock
	me.Fingerprint = fingerprint[:]
	db := models.DB()
	db.Save(&me)
	log.Print("Stored self public key in user DB")
}
Exemplo n.º 4
0
func AssertIdentity(user *models.User) *credence.IdentityAssertion {
	me := models.Me()

	identityAssertion := &credence.IdentityAssertion{
		PublicKey:   user.PublicKey,
		Name:        user.Name,
		IdentityUri: user.IdentityUri,
	}

	if HasPrivateKey() {
		identityAssertion.Fingerprint = me.Fingerprint

		bytes, err := proto.Marshal(identityAssertion)
		if err != nil {
			panic(err)
		}
		identityAssertion.Signature = SignBytes(bytes)
	}

	return identityAssertion
}