setup auth jwt to rather give response not redirect.
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"nfeeder/internal/db"
|
"nfeeder/internal/db"
|
||||||
@@ -17,7 +18,10 @@ import (
|
|||||||
|
|
||||||
const ISSUER = "nfeeder-app"
|
const ISSUER = "nfeeder-app"
|
||||||
|
|
||||||
var jwtKey = []byte("very_super_duper_secret")
|
type AuthResponse struct {
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
RefreshToken string `json:"refresh_token"`
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) handleRegister() http.HandlerFunc {
|
func (s *Server) handleRegister() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -44,7 +48,7 @@ func (s *Server) handleRegister() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Auto-login after reg
|
// Auto-login after reg
|
||||||
s.issueTokenAndRedirect(w, r, user.ID)
|
s.issueToken(w, r, user.ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,7 +70,7 @@ func (s *Server) handleLogin() http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s.issueTokenAndRedirect(w, r, user.ID)
|
s.issueToken(w, r, user.ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,7 +104,7 @@ func (s *Server) handleRefresh() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) issueTokenAndRedirect(w http.ResponseWriter, r *http.Request, userID int64) {
|
func (s *Server) issueToken(w http.ResponseWriter, r *http.Request, userID int64) {
|
||||||
nowTime := time.Now()
|
nowTime := time.Now()
|
||||||
jwtExpireTime := nowTime.Add(24 * time.Hour)
|
jwtExpireTime := nowTime.Add(24 * time.Hour)
|
||||||
refreshExpireTime := nowTime.Add((24 * time.Hour) * 3)
|
refreshExpireTime := nowTime.Add((24 * time.Hour) * 3)
|
||||||
@@ -113,22 +117,12 @@ func (s *Server) issueTokenAndRedirect(w http.ResponseWriter, r *http.Request, u
|
|||||||
}
|
}
|
||||||
|
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
tokenString, err := token.SignedString(jwtKey)
|
tokenString, err := token.SignedString(s.jwtSecret)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
http.SetCookie(w, &http.Cookie{
|
|
||||||
Name: "nfeeder_token",
|
|
||||||
Value: tokenString,
|
|
||||||
Path: "/",
|
|
||||||
HttpOnly: true,
|
|
||||||
Secure: false, // Set to true in prod for HTTPS
|
|
||||||
SameSite: http.SameSiteLaxMode,
|
|
||||||
Expires: jwtExpireTime,
|
|
||||||
})
|
|
||||||
|
|
||||||
// Generate refresh token
|
// Generate refresh token
|
||||||
rawToken := rand.Text()
|
rawToken := rand.Text()
|
||||||
hash := sha256.Sum256([]byte(rawToken))
|
hash := sha256.Sum256([]byte(rawToken))
|
||||||
@@ -146,15 +140,15 @@ func (s *Server) issueTokenAndRedirect(w http.ResponseWriter, r *http.Request, u
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
http.SetCookie(w, &http.Cookie{
|
resp := AuthResponse{
|
||||||
Name: "nfeeder_refresh",
|
AccessToken: tokenString,
|
||||||
Value: string(rawToken),
|
RefreshToken: rawToken,
|
||||||
Path: "/",
|
}
|
||||||
HttpOnly: true,
|
|
||||||
Secure: false, // Set to true in prod for HTTPS
|
|
||||||
SameSite: http.SameSiteLaxMode,
|
|
||||||
Expires: refreshExpireTime,
|
|
||||||
})
|
|
||||||
|
|
||||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
if err := json.NewEncoder(w).Encode(resp); err != nil {
|
||||||
|
log.Printf("json encoding failed: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package web
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -14,21 +15,37 @@ const userIDKey contextKey = "userID"
|
|||||||
|
|
||||||
func (s *Server) hasAuth(next http.Handler) http.Handler {
|
func (s *Server) hasAuth(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
cookie, err := r.Cookie("nfeeder_token")
|
var tokenString string
|
||||||
if err != nil {
|
|
||||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
// Authorization Header Bearer token
|
||||||
|
authHeader := r.Header.Get("Authorization")
|
||||||
|
if len(authHeader) > 7 && authHeader[:7] == "Bearer " {
|
||||||
|
tokenString = authHeader[7:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback to Cookie (future Web Frontend)
|
||||||
|
if tokenString == "" {
|
||||||
|
if cookie, err := r.Cookie("nfeeder_token"); err == nil {
|
||||||
|
tokenString = cookie.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if tokenString == "" {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{"error": "unauthorized"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse and validate
|
// parse and validate
|
||||||
claims := &jwt.RegisteredClaims{}
|
claims := &jwt.RegisteredClaims{}
|
||||||
token, err := jwt.ParseWithClaims(cookie.Value, claims, func(t *jwt.Token) (interface{}, error) {
|
token, err := jwt.ParseWithClaims(tokenString, claims, func(t *jwt.Token) (interface{}, error) {
|
||||||
// check alg
|
// check/confirm alg
|
||||||
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||||
return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"])
|
return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"])
|
||||||
}
|
}
|
||||||
|
|
||||||
return jwtKey, nil
|
return s.jwtSecret, nil
|
||||||
})
|
})
|
||||||
|
|
||||||
/// golang-jwt the library internally runs a Valid():
|
/// golang-jwt the library internally runs a Valid():
|
||||||
@@ -36,13 +53,17 @@ func (s *Server) hasAuth(next http.Handler) http.Handler {
|
|||||||
/// Not Before Check: It checks if the nbf (Not Before) time has passed.
|
/// Not Before Check: It checks if the nbf (Not Before) time has passed.
|
||||||
/// Issued At Check: It ensures the iat isn't in the future.
|
/// Issued At Check: It ensures the iat isn't in the future.
|
||||||
if err != nil || !token.Valid {
|
if err != nil || !token.Valid {
|
||||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{"error": "invalid or expired token"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify issuer
|
// Verify issuer
|
||||||
if claims.Issuer != ISSUER {
|
if claims.Issuer != ISSUER {
|
||||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{"error": "invalid issuer"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user