Finish cleaning dependencies
This commit is contained in:
70
jwt/login.go
70
jwt/login.go
@ -2,25 +2,44 @@ package jwt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.javil.eu/jacob1123/budgeteer"
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"gopkg.in/gin-gonic/gin.v1"
|
||||
)
|
||||
|
||||
// TokenVerifier verifies Tokens
|
||||
type TokenVerifier struct {
|
||||
}
|
||||
type Token struct {
|
||||
username string
|
||||
name string
|
||||
expiry float64
|
||||
}
|
||||
|
||||
const (
|
||||
expiration = 72
|
||||
secret = "uditapbzuditagscwxuqdflgzpbu´ßiaefnlmzeßtrubiadern"
|
||||
authCookie = "authentication"
|
||||
)
|
||||
|
||||
func verifyLogin(c *gin.Context) (jwt.MapClaims, error) {
|
||||
tokenString, err := c.Cookie(authCookie)
|
||||
// CreateToken creates a new token from username and name
|
||||
func (tv *TokenVerifier) CreateToken(username string, name string) (string, error) {
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
"usr": username,
|
||||
"name": name,
|
||||
"exp": time.Now().Add(time.Hour * expiration).Unix(),
|
||||
})
|
||||
|
||||
// Generate encoded token and send it as response.
|
||||
t, err := token.SignedString([]byte(secret))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func (tv *TokenVerifier) VerifyToken(tokenString string) (budgeteer.Token, error) {
|
||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
|
||||
@ -28,17 +47,20 @@ func verifyLogin(c *gin.Context) (jwt.MapClaims, error) {
|
||||
return []byte(secret), nil
|
||||
})
|
||||
if err != nil {
|
||||
c.SetCookie(authCookie, "", -1, "", "", false, false)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
claims, err := verifyToken(token)
|
||||
if err != nil {
|
||||
c.SetCookie(authCookie, "", -1, "", "", false, false)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return claims, nil
|
||||
tkn := &Token{
|
||||
username: claims["usr"].(string),
|
||||
name: claims["name"].(string),
|
||||
expiry: claims["exp"].(float64),
|
||||
}
|
||||
return tkn, nil
|
||||
}
|
||||
|
||||
func verifyToken(token *jwt.Token) (jwt.MapClaims, error) {
|
||||
@ -58,28 +80,14 @@ func verifyToken(token *jwt.Token) (jwt.MapClaims, error) {
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
func loginSuccess(c *gin.Context, username string, name string) {
|
||||
// Create token
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
"usr": username,
|
||||
"name": name,
|
||||
"exp": time.Now().Add(time.Hour * expiration).Unix(),
|
||||
})
|
||||
|
||||
// Generate encoded token and send it as response.
|
||||
t, err := token.SignedString([]byte(secret))
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
maxAge := (int)((expiration * time.Hour).Seconds())
|
||||
c.SetCookie(authCookie, t, maxAge, "", "", false, true)
|
||||
|
||||
c.JSON(http.StatusOK, map[string]string{
|
||||
"token": t,
|
||||
})
|
||||
func (t *Token) GetName() string {
|
||||
return t.name
|
||||
}
|
||||
|
||||
func clearLogin(c *gin.Context) {
|
||||
c.SetCookie(authCookie, "", -1, "", "", false, true)
|
||||
func (t *Token) GetUsername() string {
|
||||
return t.username
|
||||
}
|
||||
|
||||
func (t *Token) GetExpiry() float64 {
|
||||
return t.expiry
|
||||
}
|
||||
|
Reference in New Issue
Block a user