Exemple #1
0
//Invoked by dispatch to authenticate a user
func Auth(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()

	var login Login
	var pb []byte
	login.Pass = ""
	for k, v := range r.Form {
		if k == "user" {
			login.User = strings.Join(v, "")
		} else if k == "pass" {
			pb = []byte(strings.Join(v, ""))
		}
	}

	//Hash
	hash := sha256.New()
	hash.Write(pb)
	login.Pass = base64.URLEncoding.EncodeToString(hash.Sum(nil))

	if login.User != "" && login.Pass != "" && db.Validate(login.User, login.Pass) {
		fmt.Printf("[SUCCESS] %s attempted auth\n", login.User)
		token := db.NewToken(login.User)
		fmt.Fprintf(w, "%s", token)
	} else {
		fmt.Fprintf(w, "Authentication failure")
	}

}
//Invoked by dispatch to authenticate a user
func Connect(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")

	r.ParseForm()

	var login S.Login
	var pb []byte
	login.Pass = ""
	var callback string
	for k, v := range r.Form {
		if k == "user" {
			login.User = strings.Join(v, "")
		} else if k == "pass" {
			pb = []byte(strings.Join(v, ""))
		} else if k == "callback" {
			callback = strings.Join(v, "")
		}
	}

	//Hash
	hash := sha256.New()
	hash.Write(pb)
	login.Pass = base64.URLEncoding.EncodeToString(hash.Sum(nil))

	if login.User != "" && login.Pass != "" && db.Validate(login.User, login.Pass) {
		fmt.Printf("[SUCCESS] %s attempted auth\n", login.User)
		token := db.NewToken(login.User)
		fmt.Fprintf(w, callback+"(%s)", token)
	} else {
		fmt.Fprintf(w, "Authentication failure")
	}

}