Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CORS AllowOriginFunc documentation and recipe #164

Merged
merged 1 commit into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions cookbook/cors/origin-func/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"net/http"
"regexp"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)

var (
users = []string{"Joe", "Veer", "Zion"}
)

func getUsers(c echo.Context) error {
return c.JSON(http.StatusOK, users)
}

// allowOrigin takes the origin as an argument and returns true if the origin
// is allowed or false otherwise.
func allowOrigin(origin string) (bool, error) {
// In this example we use a regular expression but we can imagine various
// kind of custom logic. For example, an external datasource could be used
// to maintain the list of allowed origins.
return regexp.MatchString(`^https:\/\/labstack\.(net|com)$`, origin)
}

func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())

// CORS restricted with a custom function to allow origins
// and with the GET, PUT, POST or DELETE methods allowed.
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOriginFunc: allowOrigin,
AllowMethods: []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete},
}))

e.GET("/api/users", getUsers)

e.Logger.Fatal(e.Start(":1323"))
}
File renamed without changes.
11 changes: 9 additions & 2 deletions website/content/cookbook/cors.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ description = "CORS recipe for Echo"
parent = "cookbook"
+++

## Server
## Server using a list of allowed origins

`server.go`

{{< embed "cors/server.go" >}}
{{< embed "cors/origin-list/server.go" >}}

## Server using a custom function to allow origins

`server.go`

{{< embed "cors/origin-func/server.go" >}}

## [Source Code]({{< source "cors" >}})

## Maintainers

- [vishr](https://github.com/vishr)
- [curvegrid](https://github.com/curvegrid)
lammel marked this conversation as resolved.
Show resolved Hide resolved
21 changes: 14 additions & 7 deletions website/content/middleware/cors.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,41 @@ CORSConfig struct {

// AllowOrigin defines a list of origins that may access the resource.
// Optional. Default value []string{"*"}.
AllowOrigins []string `json:"allow_origins"`
AllowOrigins []string `yaml:"allow_origins"`
lammel marked this conversation as resolved.
Show resolved Hide resolved

// AllowOriginFunc is a custom function to validate the origin. It takes the
// origin as an argument and returns true if allowed or false otherwise. If
// an error is returned, it is returned by the handler. If this option is
// set, AllowOrigins is ignored.
// Optional.
AllowOriginFunc func(origin string) (bool, error) `yaml:"allow_origin_func"`

// AllowMethods defines a list methods allowed when accessing the resource.
// This is used in response to a preflight request.
// Optional. Default value DefaultCORSConfig.AllowMethods.
AllowMethods []string `json:"allow_methods"`
AllowMethods []string `yaml:"allow_methods"`

// AllowHeaders defines a list of request headers that can be used when
// making the actual request. This in response to a preflight request.
// making the actual request. This is in response to a preflight request.
// Optional. Default value []string{}.
AllowHeaders []string `json:"allow_headers"`
AllowHeaders []string `yaml:"allow_headers"`

// AllowCredentials indicates whether or not the response to the request
// can be exposed when the credentials flag is true. When used as part of
// a response to a preflight request, this indicates whether or not the
// actual request can be made using credentials.
// Optional. Default value false.
AllowCredentials bool `json:"allow_credentials"`
AllowCredentials bool `yaml:"allow_credentials"`

// ExposeHeaders defines a whitelist headers that clients are allowed to
// access.
// Optional. Default value []string{}.
ExposeHeaders []string `json:"expose_headers"`
ExposeHeaders []string `yaml:"expose_headers"`

// MaxAge indicates how long (in seconds) the results of a preflight request
// can be cached.
// Optional. Default value 0.
MaxAge int `json:"max_age"`
MaxAge int `yaml:"max_age"`
}
```

Expand Down