mirror of
https://github.com/pocket-id/pocket-id.git
synced 2025-12-11 15:53:04 +03:00
34 lines
785 B
Go
34 lines
785 B
Go
package middleware
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stonith404/pocket-id/backend/internal/common"
|
|
)
|
|
|
|
type CorsMiddleware struct{}
|
|
|
|
func NewCorsMiddleware() *CorsMiddleware {
|
|
return &CorsMiddleware{}
|
|
}
|
|
|
|
func (m *CorsMiddleware) Add() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
// Allow all origins for the token endpoint
|
|
if c.FullPath() == "/api/oidc/token" {
|
|
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
|
} else {
|
|
c.Writer.Header().Set("Access-Control-Allow-Origin", common.EnvConfig.AppURL)
|
|
}
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Headers", "*")
|
|
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
|
|
|
|
if c.Request.Method == "OPTIONS" {
|
|
c.AbortWithStatus(204)
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|