CI: minor changement for CI passed

master
fbonhomm 2019-09-15 16:31:06 +02:00
parent f976539084
commit bd6f4a1075
16 changed files with 349 additions and 313 deletions

View File

@ -1,7 +1,7 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
* License: MIT
*/
package controllers
@ -28,10 +28,10 @@ func AuthLogin(c *gin.Context) {
if err = services.Db.First(&user).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
} else if err = user.Compare(c.PostForm("password")); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error() })
} else if accessToken, err = libs.GenerateAccessToken(user); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
} else if refreshToken, err = libs.GenerateRefreshToken(user); err != nil {
} else if accessToken, err = libs.GenerateAccessToken(&user); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
} else if refreshToken, err = libs.GenerateRefreshToken(&user); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusOK, gin.H{
@ -45,16 +45,19 @@ func AuthLogin(c *gin.Context) {
func AuthRefresh(c *gin.Context) {
var accessToken string
var refreshToken string
var token map[string]string
var err error
var user = models.User{}
token, _ := GetToken(c)
if token, err = GetToken(c); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
if err = services.Db.First(&user, token["id"]).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
} else if accessToken, err = libs.GenerateAccessToken(user); err != nil {
} else if accessToken, err = libs.GenerateAccessToken(&user); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
} else if refreshToken, err = libs.GenerateRefreshToken(user); err != nil {
} else if refreshToken, err = libs.GenerateRefreshToken(&user); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusOK, gin.H{

View File

@ -1,15 +1,16 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
* License: MIT
*/
package controllers
import (
"github.com/gin-gonic/gin"
"net/http"
"github.com/gin-gonic/gin"
"github.com/fbonhomm/api-go/source/models"
"github.com/fbonhomm/api-go/source/services"
)
@ -23,30 +24,30 @@ func UserCreate(c *gin.Context) {
}
if err := services.Db.Create(&user).Error; err != nil {
c.JSON(http.StatusConflict, gin.H{ "error": err.Error() })
c.JSON(http.StatusConflict, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusCreated, gin.H{ "item": user })
c.JSON(http.StatusCreated, gin.H{"item": user})
}
}
// UserGetId
func UserGetId(c *gin.Context) {
// UserGetID
func UserGetID(c *gin.Context) {
user := models.User{}
if err := services.Db.First(&user, c.Param("id")).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error() })
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusOK, gin.H{ "item": user })
c.JSON(http.StatusOK, gin.H{"item": user})
}
}
// UserDeleteId
func UserDeleteId(c *gin.Context) {
// UserDeleteID
func UserDeleteID(c *gin.Context) {
user := models.User{}
if err := services.Db.First(&user, c.Param("id")).Delete(&user).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error() })
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusOK, gin.H{ "item": user })
c.JSON(http.StatusOK, gin.H{"item": user})
}
}

View File

@ -1,25 +1,24 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
* License: MIT
*/
package controllers
import (
"errors"
"github.com/gin-gonic/gin"
)
// GetToken
func GetToken(c *gin.Context) (info map[string]string, err error) {
func GetToken(c *gin.Context) (map[string]string, error) {
token, ok := c.Get("Token")
if ok == false {
err = errors.New("token is not found")
} else {
info = token.(map[string]string)
if !ok {
return nil, errors.New("token is not found")
}
return info, err
return token.(map[string]string), nil
}

View File

@ -1,12 +1,13 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
* License: MIT
*/
package libs
import (
"errors"
"time"
"github.com/dgrijalva/jwt-go"
@ -16,10 +17,16 @@ import (
)
// GenerateAccessToken
func GenerateAccessToken(user models.User) (accessToken string, err error) {
token := jwt.New(jwt.SigningMethodES256)
func GenerateAccessToken(user *models.User) (accessToken string, err error) {
var ok bool
var token *jwt.Token
var claims jwt.MapClaims
claims := token.Claims.(jwt.MapClaims)
token = jwt.New(jwt.SigningMethodES256)
if claims, ok = token.Claims.(jwt.MapClaims); !ok {
return accessToken, errors.New("access token is not valid")
}
claims["id"] = user.ID
claims["name"] = user.Name
claims["email"] = user.Email

View File

@ -1,23 +1,32 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
* License: MIT
*/
package libs
import (
"errors"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/fbonhomm/api-go/source/models"
"github.com/fbonhomm/api-go/source/services"
"time"
)
// GenerateRefreshToken
func GenerateRefreshToken(user models.User) (refreshToken string, err error) {
token := jwt.New(jwt.SigningMethodES384)
func GenerateRefreshToken(user *models.User) (refreshToken string, err error) {
var ok bool
var token *jwt.Token
var claims jwt.MapClaims
claims := token.Claims.(jwt.MapClaims)
token = jwt.New(jwt.SigningMethodES384)
if claims, ok = token.Claims.(jwt.MapClaims); !ok {
return refreshToken, errors.New("refresh token is not valid")
}
claims["id"] = user.ID
claims["exp"] = time.Now().Add(time.Hour * 24).Unix()

View File

@ -1,15 +1,16 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
* License: MIT
*/
package libs
import (
"errors"
"github.com/gin-gonic/gin"
"strings"
"github.com/gin-gonic/gin"
)
// GetToken
@ -18,7 +19,7 @@ func GetToken(c *gin.Context) (string, error) {
var splitToken = strings.Split(reqToken, "Bearer")
if len(splitToken) != 2 {
return "", errors.New("Token not provided.")
return "", errors.New("token not provided")
}
return strings.TrimSpace(splitToken[1]), nil

View File

@ -1,39 +1,41 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
* License: MIT
*/
package middlewares
import (
"fmt"
"net/http"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"github.com/fbonhomm/api-go/source/libs"
"github.com/fbonhomm/api-go/source/services"
"github.com/gin-gonic/gin"
"net/http"
)
func Auth(c *gin.Context) {
tokenString, err := libs.GetToken(c)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{ "error": err })
c.JSON(http.StatusUnauthorized, gin.H{"error": err})
c.Abort()
return
}
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodECDSA); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return services.PublicKeyAccess, nil
})
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{ "error": "Token not conform." })
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token not conform."})
c.Abort()
return
}
@ -41,7 +43,7 @@ func Auth(c *gin.Context) {
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
c.Set("Token", claims)
} else {
c.JSON(http.StatusUnauthorized, gin.H{ "error": "Token not conform." })
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token not conform."})
c.Abort()
}
}
@ -51,14 +53,14 @@ func AuthRefresh(c *gin.Context) {
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodECDSA); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return services.PublicKeyRefresh, nil
})
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{ "error": "Token not conform." })
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token not conform."})
c.Abort()
return
}
@ -71,7 +73,7 @@ func AuthRefresh(c *gin.Context) {
}
c.Set("Token", decode)
} else {
c.JSON(http.StatusUnauthorized, gin.H{ "error": "Token not conform." })
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token not conform."})
c.Abort()
}
}

View File

@ -1,7 +1,7 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
* License: MIT
*/
package models
@ -11,11 +11,6 @@ import (
"golang.org/x/crypto/bcrypt"
)
// ID uint `gorm:"primary_key"`
// CreatedAt time.Time
// UpdatedAt time.Time
// DeletedAt *time.Time `sql:"index"`
type User struct {
gorm.Model
Name string `gorm:"type:varchar(50);not null" json:"name"`

View File

@ -1,16 +1,17 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
* License: MIT
*/
package routers
import (
"github.com/gin-gonic/gin"
"github.com/fbonhomm/api-go/source/controllers"
"github.com/fbonhomm/api-go/source/middlewares"
"github.com/fbonhomm/api-go/source/validators"
"github.com/gin-gonic/gin"
)
// main

View File

@ -1,7 +1,7 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
* License: MIT
*/
package routers
@ -33,4 +33,3 @@ func RouterInitialize() *gin.Engine {
return routerEngine
}

View File

@ -1,16 +1,17 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
* License: MIT
*/
package routers
import (
"github.com/gin-gonic/gin"
"github.com/fbonhomm/api-go/source/controllers"
"github.com/fbonhomm/api-go/source/middlewares"
"github.com/fbonhomm/api-go/source/validators"
"github.com/gin-gonic/gin"
)
// main
@ -18,6 +19,6 @@ func User(router *gin.Engine) {
route := router.Group("/users")
route.POST("", middlewares.Auth, validators.ValidateUserCreate, controllers.UserCreate)
route.GET("/:id", middlewares.Auth, validators.ValidateUserGetId, controllers.UserGetId)
route.DELETE("/:id", middlewares.Auth, validators.ValidateUserDeleteId, controllers.UserDeleteId)
route.GET("/:id", middlewares.Auth, validators.ValidateUserGetID, controllers.UserGetID)
route.DELETE("/:id", middlewares.Auth, validators.ValidateUserDeleteID, controllers.UserDeleteID)
}

View File

@ -1,18 +1,21 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
* License: MIT
*/
package services
import (
"fmt"
"github.com/fbonhomm/api-go/source/models"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"log"
"os"
"github.com/jinzhu/gorm"
// initialize dialects
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/fbonhomm/api-go/source/models"
)
var Db *gorm.DB

View File

@ -1,39 +1,56 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
* License: MIT
*/
package services
import (
"log"
"os"
"crypto/ecdsa"
"io/ioutil"
"os"
"github.com/dgrijalva/jwt-go"
)
var PublicKeyAccess *ecdsa.PublicKey
var PrivateKeyAccess *ecdsa.PrivateKey
var PublicKeyRefresh *ecdsa.PublicKey
var PrivateKeyRefresh *ecdsa.PrivateKey
var err error
func Jwt() {
var tmp []byte
var err error
var root = os.Getenv("ROOT") + "/source/services"
tmp, err = ioutil.ReadFile(root + "/jwt/access.public.pem")
PublicKeyAccess, _ = jwt.ParseECPublicKeyFromPEM(tmp)
if tmp, err = ioutil.ReadFile(root + "/jwt/access.public.pem"); err != nil {
log.Fatal(err)
}
if PublicKeyAccess, err = jwt.ParseECPublicKeyFromPEM(tmp); err != nil {
log.Fatal(err)
}
tmp, _ = ioutil.ReadFile(root + "/jwt/access.private.pem")
PrivateKeyAccess, _ = jwt.ParseECPrivateKeyFromPEM(tmp)
if tmp, err = ioutil.ReadFile(root + "/jwt/access.private.pem"); err != nil {
log.Fatal(err)
}
if PrivateKeyAccess, err = jwt.ParseECPrivateKeyFromPEM(tmp); err != nil {
log.Fatal(err)
}
tmp, _ = ioutil.ReadFile(root + "/jwt/refresh.public.pem")
PublicKeyRefresh, _ = jwt.ParseECPublicKeyFromPEM(tmp)
if tmp, err = ioutil.ReadFile(root + "/jwt/refresh.public.pem"); err != nil {
log.Fatal(err)
}
if PublicKeyRefresh, err = jwt.ParseECPublicKeyFromPEM(tmp); err != nil {
log.Fatal(err)
}
tmp, _ = ioutil.ReadFile(root + "/jwt/refresh.private.pem")
PrivateKeyRefresh, _ = jwt.ParseECPrivateKeyFromPEM(tmp)
if tmp, err = ioutil.ReadFile(root + "/jwt/refresh.private.pem"); err != nil {
log.Fatal(err)
}
if PrivateKeyRefresh, err = jwt.ParseECPrivateKeyFromPEM(tmp); err != nil {
log.Fatal(err)
}
}

View File

@ -1,7 +1,7 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
* License: MIT
*/
package validators
@ -20,7 +20,6 @@ type authRefresh struct {
RefreshToken string `form:"refresh_token" binding:"required,min=100,max=1000"`
}
// ValidateAuthLogin
func ValidateAuthLogin(c *gin.Context) {
var v authLogin

View File

@ -1,7 +1,7 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
* License: MIT
*/
package validators
@ -19,15 +19,14 @@ type userCreate struct {
Password string `form:"password" binding:"required,min=8,max=50"`
}
type userGetId struct {
Id uint `uri:"id" binding:"required,min=0,max=9999"`
type userGetID struct {
ID uint `uri:"id" binding:"required,min=0,max=9999"`
}
type userDeleteId struct {
Id uint `uri:"id" binding:"required,min=0,max=9999"`
type userDeleteID struct {
ID uint `uri:"id" binding:"required,min=0,max=9999"`
}
// ValidateUserCreate
func ValidateUserCreate(c *gin.Context) {
var v userCreate
@ -37,18 +36,18 @@ func ValidateUserCreate(c *gin.Context) {
}
}
// ValidateUserGetId
func ValidateUserGetId(c *gin.Context) {
var v userGetId
// ValidateUserGetID
func ValidateUserGetID(c *gin.Context) {
var v userGetID
if err := c.ShouldBindUri(&v); err != nil {
errorHandling(c, err.Error())
}
}
// ValidateUserDeleteId
func ValidateUserDeleteId(c *gin.Context) {
var v userDeleteId
// ValidateUserDeleteID
func ValidateUserDeleteID(c *gin.Context) {
var v userDeleteID
if err := c.ShouldBindUri(&v); err != nil {
errorHandling(c, err.Error())

View File

@ -1,7 +1,7 @@
/**
* Created by fbonhomm
* Email: flo-github@outlook.fr
* Licence: MIT
* License: MIT
*/
package validators
@ -13,6 +13,6 @@ import (
)
func errorHandling(c *gin.Context, msg string) {
c.JSON(http.StatusBadRequest, gin.H{ "error": msg })
c.JSON(http.StatusBadRequest, gin.H{"error": msg})
c.Abort()
}