原始提交
This commit is contained in:
		
							
								
								
									
										82
									
								
								util/error_utils.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								util/error_utils.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,82 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022. Gardel <sunxinao@hotmail.com> and contributors
 | 
			
		||||
 *
 | 
			
		||||
 * This program is free software: you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU Affero General Public License as published by
 | 
			
		||||
 * the Free Software Foundation, either version 3 of the License, or
 | 
			
		||||
 * (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This program is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
 * GNU Affero General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU Affero General Public License
 | 
			
		||||
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package util
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var MessageInvalidToken = "Invalid token."
 | 
			
		||||
var MessageInvalidCredentials = "Invalid credentials. Invalid username or password."
 | 
			
		||||
var MessageTokenAlreadyAssigned = "Access token already has a profile assigned."
 | 
			
		||||
var MessageAccessDenied = "Access denied."
 | 
			
		||||
var MessageProfileNotFound = "No such profile."
 | 
			
		||||
 | 
			
		||||
type YggdrasilError struct {
 | 
			
		||||
	ErrorCode    string `json:"error"`
 | 
			
		||||
	ErrorMessage string `json:"errorMessage"`
 | 
			
		||||
	Cause        string `json:"cause,omitempty"`
 | 
			
		||||
	Status       int    `json:"-"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (e YggdrasilError) Error() string {
 | 
			
		||||
	return e.ErrorMessage
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewIllegalArgumentError(msg string) (err YggdrasilError) {
 | 
			
		||||
	err.ErrorCode = "IllegalArgumentException"
 | 
			
		||||
	err.Status = http.StatusBadRequest
 | 
			
		||||
	err.ErrorMessage = msg
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewForbiddenOperationError(msg string) (err YggdrasilError) {
 | 
			
		||||
	err.ErrorCode = "ForbiddenOperationException"
 | 
			
		||||
	err.Status = http.StatusForbidden
 | 
			
		||||
	err.ErrorMessage = msg
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func HandleError(c *gin.Context, err error) {
 | 
			
		||||
	switch x := err.(type) {
 | 
			
		||||
	case YggdrasilError:
 | 
			
		||||
		if x.Status == 0 {
 | 
			
		||||
			x.Status = http.StatusForbidden
 | 
			
		||||
		}
 | 
			
		||||
		if x.Status == http.StatusNoContent {
 | 
			
		||||
			c.Status(x.Status)
 | 
			
		||||
		} else {
 | 
			
		||||
			c.AbortWithStatusJSON(x.Status, x)
 | 
			
		||||
		}
 | 
			
		||||
		break
 | 
			
		||||
	case *YggdrasilError:
 | 
			
		||||
		if x.Status == 0 {
 | 
			
		||||
			x.Status = http.StatusForbidden
 | 
			
		||||
		}
 | 
			
		||||
		if x.Status == http.StatusNoContent {
 | 
			
		||||
			c.Status(x.Status)
 | 
			
		||||
		} else {
 | 
			
		||||
			c.AbortWithStatusJSON(x.Status, x)
 | 
			
		||||
		}
 | 
			
		||||
		break
 | 
			
		||||
	default:
 | 
			
		||||
		c.AbortWithStatusJSON(http.StatusForbidden, x.Error())
 | 
			
		||||
		break
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										165
									
								
								util/http_utils.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										165
									
								
								util/http_utils.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,165 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022. Gardel <sunxinao@hotmail.com> and contributors
 | 
			
		||||
 *
 | 
			
		||||
 * This program is free software: you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU Affero General Public License as published by
 | 
			
		||||
 * the Free Software Foundation, either version 3 of the License, or
 | 
			
		||||
 * (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This program is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
 * GNU Affero General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU Affero General Public License
 | 
			
		||||
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package util
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func GetObject(url string, value interface{}) error {
 | 
			
		||||
	resp, err := http.Get(url)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
	if resp.StatusCode == http.StatusNoContent {
 | 
			
		||||
		return &YggdrasilError{
 | 
			
		||||
			Status:       http.StatusNoContent,
 | 
			
		||||
			ErrorCode:    "IllegalArgumentException",
 | 
			
		||||
			ErrorMessage: "Http No Content",
 | 
			
		||||
		}
 | 
			
		||||
	} else if resp.StatusCode/100 == 4 {
 | 
			
		||||
		decoder := json.NewDecoder(resp.Body)
 | 
			
		||||
		errResp := YggdrasilError{}
 | 
			
		||||
		err = decoder.Decode(&errResp)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		return errResp
 | 
			
		||||
	} else {
 | 
			
		||||
		body, err := ioutil.ReadAll(resp.Body)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		return json.Unmarshal(body, value)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GetForString(url string) (string, error) {
 | 
			
		||||
	resp, err := http.Get(url)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
	if resp.StatusCode == http.StatusNoContent {
 | 
			
		||||
		return "", nil
 | 
			
		||||
	} else if resp.StatusCode/100 == 4 {
 | 
			
		||||
		decoder := json.NewDecoder(resp.Body)
 | 
			
		||||
		errResp := YggdrasilError{}
 | 
			
		||||
		err = decoder.Decode(&errResp)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return "", err
 | 
			
		||||
		}
 | 
			
		||||
		return "", errResp
 | 
			
		||||
	} else {
 | 
			
		||||
		body, err := ioutil.ReadAll(resp.Body)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return "", err
 | 
			
		||||
		}
 | 
			
		||||
		return string(body), nil
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func PostObject(url string, data interface{}, result interface{}) error {
 | 
			
		||||
	buf := bytes.Buffer{}
 | 
			
		||||
	encoder := json.NewEncoder(&buf)
 | 
			
		||||
	err := encoder.Encode(data)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	resp, err := http.Post(url, "application/json", &buf)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
	if resp.StatusCode == http.StatusNoContent {
 | 
			
		||||
		return &YggdrasilError{
 | 
			
		||||
			Status:       http.StatusNoContent,
 | 
			
		||||
			ErrorCode:    "IllegalArgumentException",
 | 
			
		||||
			ErrorMessage: "Http No Content",
 | 
			
		||||
		}
 | 
			
		||||
	} else if resp.StatusCode/100 == 4 {
 | 
			
		||||
		decoder := json.NewDecoder(resp.Body)
 | 
			
		||||
		errResp := YggdrasilError{}
 | 
			
		||||
		err = decoder.Decode(&errResp)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		return errResp
 | 
			
		||||
	} else {
 | 
			
		||||
		body, err := ioutil.ReadAll(resp.Body)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		return json.Unmarshal(body, result)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func PostObjectForError(url string, data interface{}) error {
 | 
			
		||||
	buf := bytes.Buffer{}
 | 
			
		||||
	encoder := json.NewEncoder(&buf)
 | 
			
		||||
	err := encoder.Encode(data)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	resp, err := http.Post(url, "application/json", &buf)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
	if resp.StatusCode == http.StatusNoContent {
 | 
			
		||||
		return nil
 | 
			
		||||
	} else {
 | 
			
		||||
		decoder := json.NewDecoder(resp.Body)
 | 
			
		||||
		errResp := YggdrasilError{}
 | 
			
		||||
		err = decoder.Decode(&errResp)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		return errResp
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func PostForString(url string, data []byte) (string, error) {
 | 
			
		||||
	reader := bytes.NewReader(data)
 | 
			
		||||
	resp, err := http.Post(url, "application/json", reader)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
	if resp.StatusCode == http.StatusNoContent {
 | 
			
		||||
		return "", nil
 | 
			
		||||
	} else if resp.StatusCode/100 == 4 {
 | 
			
		||||
		decoder := json.NewDecoder(resp.Body)
 | 
			
		||||
		errResp := YggdrasilError{}
 | 
			
		||||
		err = decoder.Decode(&errResp)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return "", err
 | 
			
		||||
		}
 | 
			
		||||
		return "", errResp
 | 
			
		||||
	} else {
 | 
			
		||||
		body, err := ioutil.ReadAll(resp.Body)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return "", err
 | 
			
		||||
		}
 | 
			
		||||
		return string(body), nil
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										84
									
								
								util/properties_utils.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								util/properties_utils.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,84 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022. Gardel <sunxinao@hotmail.com> and contributors
 | 
			
		||||
 *
 | 
			
		||||
 * This program is free software: you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU Affero General Public License as published by
 | 
			
		||||
 * the Free Software Foundation, either version 3 of the License, or
 | 
			
		||||
 * (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This program is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
 * GNU Affero General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU Affero General Public License
 | 
			
		||||
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package util
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"crypto"
 | 
			
		||||
	"crypto/rand"
 | 
			
		||||
	"crypto/rsa"
 | 
			
		||||
	"crypto/sha1"
 | 
			
		||||
	"encoding/base64"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"log"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Property struct {
 | 
			
		||||
	Name  string
 | 
			
		||||
	Value interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type StringProperty struct {
 | 
			
		||||
	Name  string `json:"name,omitempty"`
 | 
			
		||||
	Value string `json:"value,omitempty"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PrivateKey RSA PKCS8 Private Key
 | 
			
		||||
var PrivateKey *rsa.PrivateKey
 | 
			
		||||
 | 
			
		||||
func EncodeBase64(properties ...Property) (string, error) {
 | 
			
		||||
	obj := make(map[string]interface{})
 | 
			
		||||
	for _, property := range properties {
 | 
			
		||||
		obj[property.Name] = property.Value
 | 
			
		||||
	}
 | 
			
		||||
	jsonBytes, err := json.Marshal(obj)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	return base64.StdEncoding.EncodeToString(jsonBytes), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Properties(sign bool, properties ...StringProperty) []map[string]string {
 | 
			
		||||
	list := make([]map[string]string, 0, len(properties))
 | 
			
		||||
	for _, property := range properties {
 | 
			
		||||
		obj := map[string]string{
 | 
			
		||||
			"name":  property.Name,
 | 
			
		||||
			"value": property.Value,
 | 
			
		||||
		}
 | 
			
		||||
		if sign {
 | 
			
		||||
			err := error(nil)
 | 
			
		||||
			obj["signature"], err = Sign(property.Value)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Printf("无法签名字符串 '%s', 原因: %s", property.Value, err.Error())
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		list = append(list, obj)
 | 
			
		||||
	}
 | 
			
		||||
	return list
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Sign(value string) (string, error) {
 | 
			
		||||
	if PrivateKey == nil {
 | 
			
		||||
		panic("未初始化私钥")
 | 
			
		||||
	}
 | 
			
		||||
	sum := sha1.Sum([]byte(value))
 | 
			
		||||
	sig, err := rsa.SignPKCS1v15(rand.Reader, PrivateKey, crypto.SHA1, sum[:])
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	return base64.StdEncoding.EncodeToString(sig), nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										37
									
								
								util/uuid_utils.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								util/uuid_utils.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,37 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022. Gardel <sunxinao@hotmail.com> and contributors
 | 
			
		||||
 *
 | 
			
		||||
 * This program is free software: you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU Affero General Public License as published by
 | 
			
		||||
 * the Free Software Foundation, either version 3 of the License, or
 | 
			
		||||
 * (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This program is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
			
		||||
 * GNU Affero General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU Affero General Public License
 | 
			
		||||
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package util
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/hex"
 | 
			
		||||
	"github.com/google/uuid"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func UnsignedString(u uuid.UUID) string {
 | 
			
		||||
	var buf [32]byte
 | 
			
		||||
	hex.Encode(buf[:], u[:])
 | 
			
		||||
	return string(buf[:])
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ToUUID(str string) (uuid.UUID, error) {
 | 
			
		||||
	return uuid.Parse(str)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func RandomUUID() string {
 | 
			
		||||
	return UnsignedString(uuid.New())
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user