Exemplo n.º 1
0
Arquivo: web.go Projeto: LDLN/web-app
func createUser(username, password, skek string) bool {

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

	// encrypt kek
	ps := []string{password, username, salt}
	key := []byte(string([]rune(strings.Join(ps, "-"))[0:32]))
	pkek := []byte(skek)
	encrypted_kek := hex.EncodeToString(cryptoWrapper.Encrypt(key, pkek))

	// generate rsa keypair for user
	size := 1024
	priv, err := rsa.GenerateKey(rand.Reader, size)
	if err != nil {
		revel.TRACE.Println("failed to generate key")
	}
	if bits := priv.N.BitLen(); bits != size {
		revel.TRACE.Println("key too short (%d vs %d)", bits, size)
	}
	pub, err := x509.MarshalPKIXPublicKey(&priv.PublicKey)
	rsa_public_string := hex.EncodeToString(pub)

	revel.TRACE.Println(priv)

	// encrypt rsa private keypair
	encrypted_rsa_private := hex.EncodeToString(cryptoWrapper.Encrypt(key, x509.MarshalPKCS1PrivateKey(priv)))

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

	// save user object
	dbu := session.DB("landline").C("Users")

	user_object_map := make(map[string]string)
	user_object_map["username"] = username
	user_object_map["hashed_password"] = hashed_password
	user_object_map["encrypted_kek"] = encrypted_kek
	user_object_map["encrypted_rsa_private"] = encrypted_rsa_private
	user_object_map["rsa_public"] = rsa_public_string

	err = dbu.Insert(user_object_map)
	if err != nil {
		panic(err)
	}

	return true
}
Exemplo n.º 2
0
Arquivo: web.go Projeto: 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)
}