Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improving token config and create bindingJSON utils #7

Merged
merged 2 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions config/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

import (
"github.com/joho/godotenv"
"os"
)

type TokenENV struct {
JWTSecret string
}

type TokenENVInterface interface {
BuildTokenConfig() error
}

func (te *TokenENV) BuildTokenConfig() (*TokenENV, error) {
// load .env file
if err := godotenv.Load(".env"); err != nil {
return nil, err
}

return &TokenENV{
JWTSecret: os.Getenv("JWT_SECRET"),
}, nil
}
10 changes: 5 additions & 5 deletions controllers/testing/test_handler.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package testing

import (
"github.com/divisi-developer-poros/poros-web-backend/models/response"
"github.com/divisi-developer-poros/poros-web-backend/models/token"
"github.com/divisi-developer-poros/poros-web-backend/utils/response"
"github.com/gin-gonic/gin"
"net/http"
)

type Cobs struct {
Res response.Response
Token token.JWTToken
Res response.Response
Token token.JWTToken
}

type TestInterface interface {
Expand All @@ -31,8 +31,8 @@ type UserTesting struct {

func (cobs *Cobs) Login(c *gin.Context) {
var userCobs UserTesting
if err := c.BindJSON(&userCobs); err != nil {
cobs.Res.CustomResponse(c, "Content-Type", "application/json", "error", "failed when parsing data", http.StatusBadRequest, nil)
if err := c.ShouldBindJSON(c); err != nil {
cobs.Res.CustomResponse(c, "Content-Type", "application/json", "error", "failed when binding data", http.StatusBadRequest, nil)
return
}

Expand Down
4 changes: 2 additions & 2 deletions middleware/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package middleware

import (
"fmt"
"github.com/divisi-developer-poros/poros-web-backend/models/response"
jt "github.com/divisi-developer-poros/poros-web-backend/models/token"
"github.com/divisi-developer-poros/poros-web-backend/utils/response"
"github.com/gin-gonic/gin"
"net/http"
"strings"
)

type TokenMiddleware struct {
ResponseEntity response.Response
JWT jt.JWTToken
JWT jt.JWTToken
}

func (tm *TokenMiddleware) AuthorizeToken(c *gin.Context) {
Expand Down
12 changes: 0 additions & 12 deletions models/response/model.go

This file was deleted.

9 changes: 5 additions & 4 deletions models/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package token
import (
"errors"
"github.com/dgrijalva/jwt-go"
"os"
"github.com/divisi-developer-poros/poros-web-backend/config"
"time"
)

var EnvironmentToken config.TokenENV

func (jt *JWTToken) GenerateToken(userName string, userType int) (string, error) {
return jwt.NewWithClaims(jwt.SigningMethodHS256, &JWTToken{
Username: userName,
Expand All @@ -16,16 +18,15 @@ func (jt *JWTToken) GenerateToken(userName string, userType int) (string, error)
Issuer: "poros",
IssuedAt: time.Now().Unix(),
},
}).SignedString([]byte(os.Getenv("JWT_SECRET")))
}).SignedString([]byte(EnvironmentToken.JWTSecret))
}

func (jt *JWTToken) TokenValidation(encodedToken string) (*jwt.Token, error) {
return jwt.Parse(encodedToken, func(token *jwt.Token) (interface{}, error) {
//return signingKey, nil
if _, valid := token.Method.(*jwt.SigningMethodHMAC); !valid {
return nil, errors.New("invalid token")
} else {
return []byte(os.Getenv("JWT_SECRET")), nil
return []byte(EnvironmentToken.JWTSecret), nil
}
})
}
9 changes: 9 additions & 0 deletions models/response/response.go → utils/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ package response

import "github.com/gin-gonic/gin"

type Response struct{}

type ResInterface interface {
CustomResponse(c *gin.Context,
key, value, status, message string,
httpStatus int,
data interface{})
}

func (r *Response) CustomResponse(c *gin.Context,
key, value, status, message string,
httpStatus int,
Expand Down