mirror of
https://github.com/pocket-id/pocket-id.git
synced 2026-08-24 21:17:31 +00:00
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: Elias Schneider <login@eliasschneider.com>
56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type CorsMiddleware struct{}
|
|
|
|
func NewCorsMiddleware() *CorsMiddleware {
|
|
return &CorsMiddleware{}
|
|
}
|
|
|
|
func (m *CorsMiddleware) Add() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
path := c.FullPath()
|
|
if path == "" {
|
|
// The router doesn't map preflight requests, so we need to use the raw URL path
|
|
path = c.Request.URL.Path
|
|
}
|
|
|
|
if !isCorsPath(path) {
|
|
c.Next()
|
|
return
|
|
}
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
|
c.Writer.Header().Set("Access-Control-Allow-Headers", "Authorization")
|
|
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST")
|
|
|
|
// Preflight request
|
|
if c.Request.Method == http.MethodOptions {
|
|
c.AbortWithStatus(204)
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
func isCorsPath(path string) bool {
|
|
switch path {
|
|
case "/api/oidc/token",
|
|
"/api/oidc/userinfo",
|
|
"/api/oidc/end-session",
|
|
"/api/oidc/introspect",
|
|
"/.well-known/jwks.json",
|
|
"/.well-known/openid-configuration",
|
|
"/.well-known/oauth-authorization-server":
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|