예제 #1
0
// Register HTTP handler
func Register(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		w.Header().Add("Allow", http.MethodPost)
		errors.Write(w, errors.InvalidMethod)
		return
	}

	dec := json.NewDecoder(r.Body)
	var af authform
	err := dec.Decode(&af)

	if err != nil || len(af.Email) == 0 || len(af.Password) == 0 {
		errors.Write(w, errors.MissingFields)
		return
	}

	user, token, timeStamp := config.CreateUser(af.Email, af.Password)
	if user == nil {
		log.Debugf("%s tried to register existing user %s\n", util.GetIP(r), af.Email)
		errors.Write(w, errors.EmailUsed)
		return
	}
	log.Debugf("%s registered %s\n", util.GetIP(r), af.Email)

	if config.GetMail().IsEnabled() {
		config.GetMail().Send(user.GetEmail(), "mauIRC account created", "account-created", map[string]interface{}{
			"SenderIP":   util.GetIP(r),
			"ServerAddr": config.GetExternalAddr(),
			"ServerIP":   config.GetAddr(),
			"Token":      token,
			"Expiry":     timeStamp.Format(time.RFC1123),
		})
	} else {
		w.WriteHeader(http.StatusOK)
	}
}
예제 #2
0
// HTTPCheck handler
func HTTPCheck(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodGet {
		w.Header().Add("Allow", http.MethodGet)
		errors.Write(w, errors.InvalidMethod)
		return
	}

	success, _ := Check(w, r)
	log.Debugf("%s checked authentication (Authenticated: %t)\n", util.GetIP(r), success)
	w.WriteHeader(http.StatusOK)
	if !success {
		w.Write([]byte("{\"authenticated\": false}"))
	} else {
		w.Write([]byte("{\"authenticated\": true}"))
	}
}