<commit_before>// Copyright (c) 2014-2016 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package api

import (
	"encoding/json"
	"github.com/bitmark-inc/bitmark-webgui/configuration"
	"github.com/bitmark-inc/bitmark-webgui/fault"
	"github.com/bitmark-inc/bitmark-webgui/services"
	"github.com/bitmark-inc/logger"
	"golang.org/x/crypto/bcrypt"
	"net/http"
	"os"
	"time"
)

type LoginResponse struct {
	Chain                string `json:"chain"`
	BitmarkCliConfigFile string `json:"bitmark_cli_config_file"`
}

// GET /api/login
func LoginStatus(w http.ResponseWriter, configuration *configuration.Configuration, log *logger.L) {

	log.Info("GET /api/login: check login status")
	loginResponse := &LoginResponse{
		Chain:                configuration.BitmarkChain,
		BitmarkCliConfigFile: configuration.BitmarkCliConfigFile,
	}
	response := &Response{
		Ok:     true,
		Result: loginResponse,
	}

	if err := writeApiResponseAndSetCookie(w, response); nil != err {
		log.Errorf("Error: %v", err)
	}
}

type loginRequset struct {
	Password string
}

// POST /api/login
func LoginBitmarkWebgui(w http.ResponseWriter, req *http.Request, configuration *configuration.Configuration, log *logger.L) {

	log.Info("POST /api/login")
	response := &Response{
		Ok:     false,
		Result: loginErr,
	}

	decoder := json.NewDecoder(req.Body)
	var request loginRequset
	err := decoder.Decode(&request)
	if nil != err {
		log.Errorf("Error: %v", err)
		if err := writeApiResponse(w, response); nil != err {
			log.Errorf("Error: %v", err)
		}
		return
	}

	if err := bcrypt.CompareHashAndPassword([]byte(configuration.Password), []byte(request.Password)); nil != err {
		log.Errorf("Login failed: %v, Host: %v, User-Agent: %v", fault.ErrWrongPassword, req.Host, req.Header.Get("User-Agent"))
		if err := writeApiResponse(w, response); nil != err {
			log.Errorf("Error: %v", err)
		}
		return
	}

	loginResponse := &LoginResponse{
		Chain:                configuration.BitmarkChain,
		BitmarkCliConfigFile: configuration.BitmarkCliConfigFile,
	}

	response.Ok = true
	response.Result = loginResponse
	if err := writeApiResponseAndSetCookie(w, response); nil != err {
		log.Errorf("Error: %v", err)
	}

	// clean request password
	request.Password = "<PASSWORD>"

}

type logoutRequset struct {
	Password             string `json:"password"`
	BitmarkPayConfigFile string `json:"bitmark_pay_config_file"`
}

// POST /api/logout
func LogoutBitmarkWebgui(w http.ResponseWriter, req *http.Request, filePath string, webguiConfiguration *configuration.Configuration, log *logger.L) {
	response := &Response{
		Ok:     false,
		Result: "logout error",
	}

	cookie := &http.Cookie{
		Name:   CookieName,
		Secure: true,
		MaxAge: -1,
	}
	http.SetCookie(w, cookie)

	response.Ok = true
	response.Result = nil

	w.Header().Set("Content-Type", "text/json; charset=utf-8")
	if b, err := json.MarshalIndent(response, "", "  "); nil != err {
		log.Errorf("Error: %v", fault.ErrJsonParseFail)
	} else {
		w.Write(b)
	}
}

// POST /api/logoutOnestep
func LogoutBitmarkWebguiOnestep(w http.ResponseWriter, req *http.Request, filePath string, webguiConfiguration *configuration.Configuration, log *logger.L) {

	log.Info("POST /api/logout")
	response := &Response{
		Ok:     false,
		Result: "logout error",
	}

	decoder := json.NewDecoder(req.Body)
	var request logoutRequset<commit_msg>bug fixed. expire correct cookie<commit_after>// Copyright (c) 2014-2016 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package api

import (
	"encoding/json"
	"github.com/bitmark-inc/bitmark-webgui/configuration"
	"github.com/bitmark-inc/bitmark-webgui/fault"
	"github.com/bitmark-inc/bitmark-webgui/services"
	"github.com/bitmark-inc/logger"
	"golang.org/x/crypto/bcrypt"
	"net/http"
	"os"
	"time"
)

type LoginResponse struct {
	Chain                string `json:"chain"`
	BitmarkCliConfigFile string `json:"bitmark_cli_config_file"`
}

// GET /api/login
func LoginStatus(w http.ResponseWriter, configuration *configuration.Configuration, log *logger.L) {

	log.Info("GET /api/login: check login status")
	loginResponse := &LoginResponse{
		Chain:                configuration.BitmarkChain,
		BitmarkCliConfigFile: configuration.BitmarkCliConfigFile,
	}
	response := &Response{
		Ok:     true,
		Result: loginResponse,
	}

	if err := writeApiResponseAndSetCookie(w, response); nil != err {
		log.Errorf("Error: %v", err)
	}
}

type loginRequset struct {
	Password string
}

// POST /api/login
func LoginBitmarkWebgui(w http.ResponseWriter, req *http.Request, configuration *configuration.Configuration, log *logger.L) {

	log.Info("POST /api/login")
	response := &Response{
		Ok:     false,
		Result: loginErr,
	}

	decoder := json.NewDecoder(req.Body)
	var request loginRequset
	err := decoder.Decode(&request)
	if nil != err {
		log.Errorf("Error: %v", err)
		if err := writeApiResponse(w, response); nil != err {
			log.Errorf("Error: %v", err)
		}
		return
	}

	if err := bcrypt.CompareHashAndPassword([]byte(configuration.Password), []byte(request.Password)); nil != err {
		log.Errorf("Login failed: %v, Host: %v, User-Agent: %v", fault.ErrWrongPassword, req.Host, req.Header.Get("User-Agent"))
		if err := writeApiResponse(w, response); nil != err {
			log.Errorf("Error: %v", err)
		}
		return
	}

	loginResponse := &LoginResponse{
		Chain:                configuration.BitmarkChain,
		BitmarkCliConfigFile: configuration.BitmarkCliConfigFile,
	}

	response.Ok = true
	response.Result = loginResponse
	if err := writeApiResponseAndSetCookie(w, response); nil != err {
		log.Errorf("Error: %v", err)
	}

	// clean request password
	request.Password = "<PASSWORD>"

}

type logoutRequset struct {
	Password             string `json:"password"`
	BitmarkPayConfigFile string `json:"bitmark_pay_config_file"`
}

// POST /api/logout
func LogoutBitmarkWebgui(w http.ResponseWriter, req *http.Request, filePath string, webguiConfiguration *configuration.Configuration, log *logger.L) {
	response := &Response{
		Ok:     false,
		Result: "logout error",
	}

	cookie := &http.Cookie{
		Name:   CookieName,
		Secure: true,
		Path:   "/",
		MaxAge: -1,
	}
	http.SetCookie(w, cookie)

	response.Ok = true
	response.Result = nil

	w.Header().Set("Content-Type", "text/json; charset=utf-8")
	if b, err := json.MarshalIndent(response, "", "  "); nil != err {
		log.Errorf("Error: %v", fault.ErrJsonParseFail)
	} else {
		w.Write(b)
	}
}

// POST /api/logoutOnestep
func LogoutBitmarkWebguiOnestep(w http.ResponseWriter, req *http.Request, filePath string, webguiConfiguration *configuration.Configuration, log *logger.L) {

	log.Info("POST /api/logout")
	response := &Response{
		Ok:     false,
		Result: "logout error",
	}

	decoder := json.NewDecoder(req.Body)
	var request logoutRequset
{'fraction_non_alphanumeric': 0.13807165744247013, 'fraction_numerical': 0.0029129041654529565, 'mean_word_length': 4.06789667896679, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5058383', 'n_tokens_mistral': 2419, 'n_tokens_neox': 2282, 'n_words': 528}