Exemple #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)
}
Exemple #2
0
func InfoCredHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		w.WriteHeader(http.StatusMethodNotAllowed)
		return
	}

	// TODO: This is a hack, need proper routing
	credHash := r.URL.Path[12:]

	credRecord, found := helpers.CredRecordFromCredHash(credHash)
	if !found {
		w.WriteHeader(http.StatusNotFound)
		// TODO: Pretty 404
		return
	}

	w.WriteHeader(http.StatusOK)
	helpers.ModelNegotiator().Negotiate(w, r, &credRecord)
}
Exemple #3
0
func UserDetailHandler(w http.ResponseWriter, r *http.Request) {
	// TODO: This is a hack, need proper routing
	userParam := r.URL.Path[7:]

	userId, err := strconv.ParseUint(userParam, 10, 64)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}
	db := models.DB()

	user := &models.User{}
	db.Where(&models.User{ID: uint(userId)}).First(user)
	if db.NewRecord(user) {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	identityAssertion := helpers.AssertIdentity(user)
	helpers.ModelNegotiator().Negotiate(w, r, identityAssertion)
}