mirror of
https://github.com/kataras/iris.git
synced 2026-01-07 20:17:05 +00:00
Implement feature request: http://support.iris-go.com/d/29-mark-cookie-for-session-as-secure
Example:
app := iris.New()
app.Adapt(httprouter.New())
// IMPORTANT
cookieName := "mycustomsessionid"
// AES only supports key sizes of 16, 24 or 32 bytes.
// You either need to provide exactly that amount or you derive the key
from what you type in.
hashKey := []byte("the-big-and-secret-fash-key-here")
blockKey := []byte("lot-secret-of-characters-big-too")
secureCookie := securecookie.New(hashKey, blockKey)
app.Adapt(sessions.New(sessions.Config{
Cookie: cookieName,
Encode: secureCookie.Encode,
Decode: secureCookie.Decode,
}))
Former-commit-id: 6fe5ce6cb834d55862242e08405fad4e721caa5b
This commit is contained in:
43
iris.go
43
iris.go
@@ -814,6 +814,7 @@ func (s *Framework) Regex(pairParamExpr ...string) HandlerFunc {
|
||||
ctx.EmitError(StatusInternalServerError)
|
||||
}
|
||||
|
||||
// just to check if router is adapted.
|
||||
wp := s.policies.RouterReversionPolicy.WildcardPath
|
||||
if wp == nil {
|
||||
s.Log(ProdMode, "regex cannot be used when a router policy is missing\n"+errRouterIsMissing.Format(s.Config.VHost).Error())
|
||||
@@ -826,6 +827,8 @@ func (s *Framework) Regex(pairParamExpr ...string) HandlerFunc {
|
||||
"paramName2, expression2. The len should be %2==0")
|
||||
return srvErr
|
||||
}
|
||||
|
||||
// we do compile first to reduce the performance cost at serve time.
|
||||
pairs := make(map[string]*regexp.Regexp, len(pairParamExpr)/2)
|
||||
|
||||
for i := 0; i < len(pairParamExpr)-1; i++ {
|
||||
@@ -843,18 +846,7 @@ func (s *Framework) Regex(pairParamExpr ...string) HandlerFunc {
|
||||
// return the middleware
|
||||
return func(ctx *Context) {
|
||||
for k, v := range pairs {
|
||||
pathPart := ctx.Param(k)
|
||||
if pathPart == "" {
|
||||
// take care, the router already
|
||||
// does the param validations
|
||||
// so if it's empty here it means that
|
||||
// the router has label it as optional.
|
||||
// so we skip it, and continue to the next.
|
||||
continue
|
||||
}
|
||||
// the improtant thing:
|
||||
// if the path part didn't match with the relative exp, then fire status not found.
|
||||
if !v.MatchString(pathPart) {
|
||||
if !ctx.ParamValidate(v, k) {
|
||||
ctx.EmitError(StatusNotFound)
|
||||
return
|
||||
}
|
||||
@@ -864,6 +856,33 @@ func (s *Framework) Regex(pairParamExpr ...string) HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Framework) RegexSingle(paramName string, expr string, onFail HandlerFunc) HandlerFunc {
|
||||
|
||||
// just to check if router is adapted.
|
||||
wp := s.policies.RouterReversionPolicy.WildcardPath
|
||||
if wp == nil {
|
||||
s.Log(ProdMode, "regex cannot be used when a router policy is missing\n"+errRouterIsMissing.Format(s.Config.VHost).Error())
|
||||
return onFail
|
||||
}
|
||||
|
||||
// we do compile first to reduce the performance cost at serve time.
|
||||
r, err := regexp.Compile(expr)
|
||||
if err != nil {
|
||||
s.Log(ProdMode, "regex '"+expr+"' failed. Trace: "+err.Error())
|
||||
return onFail
|
||||
}
|
||||
|
||||
// return the middleware
|
||||
return func(ctx *Context) {
|
||||
if !ctx.ParamValidate(r, paramName) {
|
||||
onFail(ctx)
|
||||
return
|
||||
}
|
||||
// otherwise continue to the next handler...
|
||||
ctx.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// RouteParam returns a named parameter as each router defines named path parameters.
|
||||
// For example, with the httprouter(: as named param symbol):
|
||||
// userid should return :userid.
|
||||
|
||||
Reference in New Issue
Block a user