Extract loginSuccess to login.go
This commit is contained in:
parent
aa055e944e
commit
bd1b3416b4
33
login.go
33
login.go
@ -2,14 +2,21 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/dgrijalva/jwt-go"
|
"github.com/dgrijalva/jwt-go"
|
||||||
"gopkg.in/gin-gonic/gin.v1"
|
"gopkg.in/gin-gonic/gin.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
expiration = 72
|
||||||
|
secret = "uditapbzuditagscwxuqdflgzpbu´ßiaefnlmzeßtrubiadern"
|
||||||
|
authCookie = "authentication"
|
||||||
|
)
|
||||||
|
|
||||||
func verifyLogin(c *gin.Context) bool {
|
func verifyLogin(c *gin.Context) bool {
|
||||||
tokenString, err := c.Cookie("authentication")
|
tokenString, err := c.Cookie(authCookie)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -22,7 +29,7 @@ func verifyLogin(c *gin.Context) bool {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if !verifyToken(c, token, err) {
|
if !verifyToken(c, token, err) {
|
||||||
c.SetCookie("authentication", "", -1, "", "", false, false)
|
c.SetCookie(authCookie, "", -1, "", "", false, false)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,3 +52,25 @@ func verifyToken(c *gin.Context, token *jwt.Token, err error) bool {
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
29
main.go
29
main.go
@ -2,17 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/dgrijalva/jwt-go"
|
|
||||||
"gopkg.in/gin-gonic/gin.v1"
|
"gopkg.in/gin-gonic/gin.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
expiration = 72
|
|
||||||
secret = "uditapbzuditagscwxuqdflgzpbu´ßiaefnlmzeßtrubiadern"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
|
|
||||||
@ -74,25 +67,5 @@ func loginPost(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create token
|
loginSuccess(c, username, "Jan Bader")
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
|
||||||
"usr": "jan",
|
|
||||||
"name": "Jan Bader",
|
|
||||||
"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("authentication", t, maxAge, "", "", false, true)
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, map[string]string{
|
|
||||||
"token": t,
|
|
||||||
})
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user