예제 #1
0
func (c SyncableObjects) ListObjects(object_key string) revel.Result {

	// connect to mongodb
	session, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}
	defer session.Close()

	// query
	dbs := session.DB("landline").C("Schemas")

	var object_type map[string]interface{}
	err = dbs.Find(bson.M{"object_key": object_key}).One(&object_type)
	if err != nil {
		revel.TRACE.Println(err)
	}

	dbc := session.DB("landline").C("SyncableObjects")

	var results []map[string]interface{}
	err = dbc.Find(bson.M{"object_type": object_key}).All(&results)
	if err != nil {
		revel.TRACE.Println(err)
	}

	revel.TRACE.Println(results)

	// decrypt each
	var object_list []map[string]interface{}
	for u, result := range results {
		revel.TRACE.Println(u)

		// object that the client does not know about
		syncable_object_map := make(map[string]interface{})
		syncable_object_map["uuid"] = result["uuid"]
		syncable_object_map["object_type"] = result["object_type"]
		syncable_object_map["time_modified_since_creation"] = result["time_modified_since_creation"]

		// decrypt
		kv_hex, err := hex.DecodeString(result["key_value_pairs"].(string))
		if err != nil {
			revel.TRACE.Println(err)
		}
		kv_plain := cryptoWrapper.Decrypt([]byte(c.Session["kek"]), kv_hex)

		// unmarshal the json
		var obj_json map[string]interface{}
		if err := json.Unmarshal(kv_plain, &obj_json); err != nil {
			panic(err)
		}

		syncable_object_map["key_value_pairs_plain"] = obj_json

		object_list = append(object_list, syncable_object_map)
	}

	return c.Render(object_type, object_key, results, object_list)
}
예제 #2
0
func (c SyncableObjects) ViewObject(object_key, uuid string) revel.Result {

	// connect to mongodb
	session, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}
	defer session.Close()

	// query types
	schemasdb := session.DB("landline").C("Schemas")
	var schema map[string]interface{}
	err = schemasdb.Find(bson.M{"object_key": object_key}).One(&schema)
	if err != nil {
		revel.TRACE.Println(err)
	}

	// query objects
	dbc := session.DB("landline").C("SyncableObjects")
	var object map[string]string
	err = dbc.Find(bson.M{"uuid": uuid, "object_type": object_key}).One(&object)
	if err != nil {
		revel.TRACE.Println(err)
	}
	revel.TRACE.Println(object)

	// decrypt key_value_pairs
	kv_hex, err := hex.DecodeString(object["key_value_pairs"])
	if err != nil {
		revel.TRACE.Println(err)
	}
	kv_plain := string(cryptoWrapper.Decrypt([]byte(c.Session["kek"]), kv_hex))
	revel.TRACE.Println(kv_plain)

	// convert string of json to json to map
	byt := []byte((kv_plain))
	var key_values map[string]interface{}
	if err := json.Unmarshal(byt, &key_values); err != nil {
		panic(err)
	}

	return c.Render(object, key_values, schema)
}
예제 #3
0
func (c SyncableObjects) CreateObjectAction(object_key string) revel.Result {

	revel.TRACE.Println(c.Params.Values)

	// build kv map
	key_values := make(map[string]interface{})

	// parse params and put into map
	for k, v := range c.Params.Values {
		if k != "object_key" {
			key_values[k] = v[0]
		}
	}
	revel.TRACE.Println(key_values)

	// convert map to json to string of json
	key_values_map, err := json.Marshal(key_values)
	if err != nil {
		revel.TRACE.Println(err)
	}
	key_values_string := string(key_values_map[:])
	revel.TRACE.Println(key_values_string)

	// encrypt json string
	kv_string_encrypted := hex.EncodeToString(cryptoWrapper.Encrypt([]byte(c.Session["kek"]), []byte(key_values_string)))
	revel.TRACE.Println(kv_string_encrypted)

	// test decrypt
	kv_hex, err := hex.DecodeString(kv_string_encrypted)
	if err != nil {
		revel.TRACE.Println(err)
	}
	kv_plain := string(cryptoWrapper.Decrypt([]byte(c.Session["kek"]), kv_hex))
	revel.TRACE.Println(kv_plain)

	// create object
	object_map := make(map[string]interface{})
	uuid, err := uuid.NewV4()
	object_map["uuid"] = uuid.String()
	object_map["object_type"] = object_key
	object_map["key_value_pairs"] = kv_string_encrypted
	object_map["time_modified_since_creation"] = float64(0)

	// connect to mongodb
	session, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}
	defer session.Close()

	// insert into db
	dbc := session.DB("landline").C("SyncableObjects")
	err = dbc.Insert(object_map)
	if err != nil {
		panic(err)
	}

	// redirect
	c.Flash.Success("Object created")
	return c.Redirect(routes.SyncableObjects.ViewObject(object_key, object_map["uuid"].(string)))
}
예제 #4
0
파일: web.go 프로젝트: LDLN/web-app
func (c Web) LoginAction(username, password string) revel.Result {

	// hashed_password
	hashed_password := cryptoWrapper.HashPassword(username, password)

	// connect to mongodb
	session, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}
	defer session.Close()

	// find user object
	dbu := session.DB("landline").C("Users")
	var result map[string]string
	err = dbu.Find(bson.M{"username": username, "hashed_password": hashed_password}).One(&result)

	if err != nil {
		revel.TRACE.Println("Username and password not found")
	} else {

		// decrypt kek
		ps := []string{password, username, salt}
		key := []byte(string([]rune(strings.Join(ps, "-"))[0:32]))
		bdec, err := hex.DecodeString(result["encrypted_kek"])
		if err != nil {
			revel.TRACE.Println(err)
			return c.Redirect(Web.LoginForm)
		}
		kek := string(cryptoWrapper.Decrypt(key, bdec))

		// decrypt rsa private
		privenc, err := hex.DecodeString(result["encrypted_rsa_private"])
		if err != nil {
			revel.TRACE.Println(err)
			return c.Redirect(Web.LoginForm)
		}
		priva := cryptoWrapper.Decrypt(key, privenc)
		priv, err := x509.ParsePKCS1PrivateKey(priva)

		revel.TRACE.Println("Login successful")
		revel.TRACE.Println(username)
		revel.TRACE.Println(kek)
		revel.TRACE.Println(priv)

		// get deployment
		dbd := session.DB("landline").C("Deployments")
		var resultd map[string]string
		err = dbd.Find(bson.M{}).One(&resultd)

		// save to session
		c.Session["kek"] = kek
		c.Session["username"] = username
		c.Session["deployment_name"] = resultd["name"]
		c.Session["deployment_unit"] = resultd["unit"]

		// redirect
		return c.Redirect(SyncableObjects.Map)
	}

	// redirect
	c.Flash.Error("Username and password not found")
	return c.Redirect(Web.LoginForm)
}