fixed
This commit is contained in:
1 parent
4194cef934
commit
363127fe9e
3350 files changed
+1284220
-1
No files matched your search
Executable
+48
@@ -0,0 +1,48 @@
|
||||
package appresult
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
var (
|
||||
ErrMissingParam = NewAppError(nil, "missing param", "SE-00001")
|
||||
ErrNotFound = NewAppError(nil, "not found", "SE-00003")
|
||||
ErrInternalServer = NewAppError(nil, "internal server error", "SE-00004")
|
||||
ErrNotAcceptable = NewAppError(nil, "Not acceptable", "SE-00007")
|
||||
ErrCheckPasswordLimitFull = NewAppError(nil, "ErrCheckPasswordLimitFull", "SE-00008")
|
||||
ErrUpdatePassword = NewAppError(nil, "missing password", "SE-00010")
|
||||
ErrCheckEmail = NewAppError(nil, "Please enter a valid email address", "SE-00011")
|
||||
ErrAccount = NewAppError(nil, "This account has already been registered", "SE-00015")
|
||||
)
|
||||
|
||||
type AppError struct {
|
||||
Status bool `json:"status"`
|
||||
Err error `json:"err,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Code string `json:"code,omitempty"`
|
||||
}
|
||||
|
||||
func (e *AppError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
func (e *AppError) Unwrap() error { return e.Err }
|
||||
|
||||
func (e *AppError) Marshal() []byte {
|
||||
marshal, err := json.Marshal(e)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return marshal
|
||||
}
|
||||
|
||||
func NewAppError(err error, message, code string) *AppError {
|
||||
return &AppError{
|
||||
Status: false,
|
||||
Err: err,
|
||||
Message: message,
|
||||
Code: code,
|
||||
}
|
||||
}
|
||||
|
||||
func systemError(err error) *AppError {
|
||||
return NewAppError(err, "internal system error", "SE-000")
|
||||
}
|
||||
Executable
+78
@@ -0,0 +1,78 @@
|
||||
package appresult
|
||||
|
||||
import (
|
||||
"bankline/internal/config"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type UnsignedResponse struct {
|
||||
Message interface{} `json:"message"`
|
||||
}
|
||||
|
||||
type appHandler func(w http.ResponseWriter, r *http.Request) error
|
||||
|
||||
func HeaderContentTypeJson() (string, string) {
|
||||
return "Content-Type", "application/json"
|
||||
}
|
||||
func AccessControlAllow() (string, string) {
|
||||
return "Access-Control-Allow-Origin", "*"
|
||||
}
|
||||
|
||||
func respondWithError(c *gin.Context, code int, message interface{}) {
|
||||
c.AbortWithStatusJSON(code, gin.H{"error": message})
|
||||
}
|
||||
|
||||
func JwtTokenCheck() gin.HandlerFunc {
|
||||
|
||||
return func(c *gin.Context) {
|
||||
token := c.GetHeader("Authorization")
|
||||
|
||||
if token == "" {
|
||||
respondWithError(c, 403, "Authorization header is missing")
|
||||
return
|
||||
}
|
||||
|
||||
cfg := config.GetConfig()
|
||||
claims, err := TokenClaims(token, cfg.JwtKey)
|
||||
if err != nil {
|
||||
respondWithError(c, 401, "Token is expired")
|
||||
return
|
||||
}
|
||||
|
||||
isAdmin := claims["admin"].(bool)
|
||||
if isAdmin == false {
|
||||
respondWithError(c, 403, "Unauthorized")
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("id", fmt.Sprint(claims["id"]))
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func TokenClaims(token, secretKey string) (jwt.MapClaims, error) {
|
||||
decoded, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
|
||||
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return []byte(secretKey), nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("err", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
claims, ok := decoded.Claims.(jwt.MapClaims)
|
||||
|
||||
if !ok {
|
||||
// TODO tokenin omrini test etmeli
|
||||
return nil, ErrInternalServer
|
||||
}
|
||||
|
||||
return claims, nil
|
||||
}
|
||||
Executable
+42
@@ -0,0 +1,42 @@
|
||||
package appresult
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
var (
|
||||
Success = NewAppSuccess("Success !!!", "SS-10000", nil)
|
||||
SuccessRegister = NewAppSuccess("You don't have an account.Please sign up", "SS-11000", nil)
|
||||
SuccessLogin = NewAppSuccess("you have an account !!!", "SS-12000", nil)
|
||||
)
|
||||
|
||||
type AppSuccess struct {
|
||||
Status bool `json:"status"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Code string `json:"code,omitempty"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func (s *AppSuccess) Error() string {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (s *AppSuccess) Success() string {
|
||||
return s.Message
|
||||
}
|
||||
|
||||
func (s *AppSuccess) Marshal() []byte {
|
||||
marshal, err := json.Marshal(s)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return marshal
|
||||
}
|
||||
|
||||
func NewAppSuccess(message, code string, data interface{}) *AppSuccess {
|
||||
return &AppSuccess{
|
||||
Status: true,
|
||||
Message: message,
|
||||
Code: code,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user