auth.go 2.5 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7
package auth

import (
	"encoding/json"
	"net/http"
	"strings"

黄孟柱 已提交
8 9 10
	"github.com/eolinker/goku-api-gateway/console/controller"
	"github.com/eolinker/goku-api-gateway/console/module/auth"
	log "github.com/eolinker/goku-api-gateway/goku-log"
E
eoLinker API Management 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
)

// 获取认证状态
func GetAuthStatus(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationNone, controller.OperationREAD)
	if e != nil {
		return
	}

	strategyID := httpRequest.PostFormValue("strategyID")

	flag, result, err := auth.GetAuthStatus(strategyID)
	if !flag {

		controller.WriteError(httpResponse, "250000", "auth", "[ERROR]The auth info of the strategy does not exist!", err)
		return
	}

	result["statusCode"] = "000000"
	result["type"] = "auth"
	res, _ := json.Marshal(result)

	httpResponse.Write(res)

	return
}

// 获取认证信息
func GetAuthInfo(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationNone, controller.OperationREAD)
	if e != nil {
		return
	}

	strategyID := httpRequest.PostFormValue("strategyID")

	flag, result, err := auth.GetAuthInfo(strategyID)
	if !flag {

		log.Debug(err)
		controller.WriteError(httpResponse, "250000", "auth", "[ERROR]The auth info of the strategy does not exist!", err)

		return
	}

	result["statusCode"] = "000000"
	result["type"] = "auth"
	res, _ := json.Marshal(result)

	httpResponse.Write(res)
	return

}

// 编辑认证信息
func EditAuthInfo(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationNone, controller.OperationEDIT)
	if e != nil {
		return
	}

	strategyID := httpRequest.PostFormValue("strategyID")
	strategyName := httpRequest.PostFormValue("strategyName")
	basicAuthList := httpRequest.PostFormValue("basicAuthList")
	apikeyList := httpRequest.PostFormValue("apiKeyList")
	jwtCredentialList := httpRequest.PostFormValue("jwtCredentialList")
	oauth2CredentialList := httpRequest.PostFormValue("oauth2CredentialList")
	delClientIDList := httpRequest.PostFormValue("deleteClientIDList")

	idList := strings.Split(delClientIDList, ",")
	flag, err := auth.EditAuthInfo(strategyID, strategyName, basicAuthList, apikeyList, jwtCredentialList, oauth2CredentialList, idList)
	if !flag {
		controller.WriteError(httpResponse, "250000", "auth", "[ERROR]Fail to edit auth!", err)

		return
	}
	controller.WriteResultInfo(httpResponse, "auth", "", nil)
}