37 lines
858 B
Go
37 lines
858 B
Go
|
|
package handlers
|
||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"net/http"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/golang-jwt/jwt/v5"
|
||
|
|
)
|
||
|
|
|
||
|
|
// DebugTokenHandler genererar en debug-token
|
||
|
|
func DebugTokenHandler(secret string) http.HandlerFunc {
|
||
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
now := time.Now().Unix()
|
||
|
|
claims := jwt.MapClaims{
|
||
|
|
"sub": "3847477b-3d56-4975-9157-ae8f9ce52aa7",
|
||
|
|
"email": "erik@landvex.com",
|
||
|
|
"role": "admin",
|
||
|
|
"exp": now + 2592000,
|
||
|
|
"iat": now,
|
||
|
|
"iss": "boc-auth",
|
||
|
|
"aud": "boc",
|
||
|
|
}
|
||
|
|
|
||
|
|
token, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(secret))
|
||
|
|
if err != nil {
|
||
|
|
http.Error(w, `{"error":"token generation failed"}`, http.StatusInternalServerError)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
w.Header().Set("Content-Type", "application/json")
|
||
|
|
json.NewEncoder(w).Encode(map[string]string{
|
||
|
|
"token": token,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|