38 lines
710 B
Go
38 lines
710 B
Go
|
package utils
|
||
|
|
||
|
import (
|
||
|
"github.com/golang-jwt/jwt/v5"
|
||
|
"net/http"
|
||
|
jwt2 "sonarqube-badge/security/jwt"
|
||
|
"sonarqube-badge/store"
|
||
|
)
|
||
|
|
||
|
func GetToken(req *http.Request) (*jwt.Token, error) {
|
||
|
cookie, err := req.Cookie("jwt-token")
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
token, err := jwt2.VerifyToken(cookie.Value, req.Context())
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return token, nil
|
||
|
}
|
||
|
|
||
|
func CreateJWTCookie(user *store.User, req *http.Request) (*http.Cookie, error) {
|
||
|
token, err := jwt2.CreateToken(*user, req.Context())
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
cookie := http.Cookie{
|
||
|
Name: "jwt-token",
|
||
|
Value: token,
|
||
|
Path: "/",
|
||
|
Secure: true,
|
||
|
HttpOnly: true,
|
||
|
}
|
||
|
return &cookie, nil
|
||
|
}
|