forked from cloudwego/hertz-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add csrf example (cloudwego#60)
* feat: add csrf example * feat: add license to each file and update dependencies * fix: Handling possible errors in the error log * fix: Use two paragraphs * doc: add links for csrf example * fix: add some comments to explain the different meanings corresponding to different return values. * fix: fix conflict * fix: fix conflict * fix: add license to each file and replaced obsolete function * style: use gofumpt Co-authored-by: kinggo <[email protected]>
- Loading branch information
1 parent
cb30d52
commit 67590ad
Showing
13 changed files
with
378 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# csrf | ||
|
||
You can learn about how to use csrf for hertz: | ||
|
||
* [default](./default): This is using csrf by default | ||
* [custom_secret](./custom_secret): How to set the secret to generate a csrf-token | ||
* [custom_errorFunc](./custom_errorfunc): How to customize error handling functions | ||
* [custom_ignoreMethods](./custom_ignoremethods): How to set up custom methods that do not need to be protected | ||
* [custom_keyLookup](./custom_keylookup): How to customize extractor by setting keyLookup | ||
* [custom_extractor](./custom_extractor): How to customize extractor directly | ||
* [custom_next](./custom_next): How to skip the csrf middleware in certain situations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2022 CloudWeGo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/cloudwego/hertz/pkg/app" | ||
"github.com/cloudwego/hertz/pkg/app/server" | ||
"github.com/hertz-contrib/csrf" | ||
"github.com/hertz-contrib/sessions" | ||
"github.com/hertz-contrib/sessions/cookie" | ||
) | ||
|
||
var ( | ||
errMissingHeader = errors.New("[CSRF] missing csrf token in header") | ||
errMissingQuery = errors.New("[CSRF] missing csrf token in query") | ||
errMissingParam = errors.New("[CSRF] missing csrf token in param") | ||
errMissingForm = errors.New("[CSRF] missing csrf token in form") | ||
errMissingSalt = errors.New("[CSRF] missing salt") | ||
errInvalidToken = errors.New("[CSRF] invalid token") | ||
) | ||
|
||
// myErrFunc is executed when an error occurs in csrf middleware. | ||
func myErrFunc(_ context.Context, ctx *app.RequestContext) { | ||
err := ctx.Errors.Last() | ||
switch err { | ||
case errMissingForm, errMissingParam, errMissingHeader, errMissingQuery: | ||
ctx.String(http.StatusBadRequest, err.Error()) // extract csrf-token failed | ||
case errMissingSalt: | ||
fmt.Println(err.Error()) | ||
ctx.String(http.StatusInternalServerError, err.Error()) // get salt failed,which is unexpected | ||
case errInvalidToken: | ||
ctx.String(http.StatusBadRequest, err.Error()) // csrf-token is invalid | ||
} | ||
ctx.Abort() | ||
} | ||
|
||
func main() { | ||
h := server.Default() | ||
|
||
store := cookie.NewStore([]byte("store")) | ||
h.Use(sessions.New("csrf-session", store)) | ||
h.Use(csrf.New(csrf.WithErrorFunc(myErrFunc))) | ||
|
||
h.GET("/protected", func(c context.Context, ctx *app.RequestContext) { | ||
ctx.String(200, csrf.GetToken(ctx)) | ||
}) | ||
h.POST("/protected", func(c context.Context, ctx *app.RequestContext) { | ||
ctx.String(200, "CSRF token is valid") | ||
}) | ||
|
||
h.Spin() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2022 CloudWeGo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/cloudwego/hertz/pkg/app" | ||
"github.com/cloudwego/hertz/pkg/app/server" | ||
"github.com/hertz-contrib/csrf" | ||
"github.com/hertz-contrib/sessions" | ||
"github.com/hertz-contrib/sessions/cookie" | ||
) | ||
|
||
func myExtractor(_ context.Context, ctx *app.RequestContext) (string, error) { | ||
token := ctx.FormValue("csrf-token") | ||
if token == nil { | ||
return "", errors.New("missing token in form-data") // get csrf-token from form-data failed | ||
} | ||
return string(token), nil | ||
} | ||
|
||
func main() { | ||
h := server.Default() | ||
|
||
store := cookie.NewStore([]byte("secret")) | ||
h.Use(sessions.New("csrf-session", store)) | ||
h.Use(csrf.New(csrf.WithExtractor(myExtractor))) | ||
|
||
h.GET("/protected", func(c context.Context, ctx *app.RequestContext) { | ||
ctx.String(200, csrf.GetToken(ctx)) | ||
}) | ||
h.POST("/protected", func(c context.Context, ctx *app.RequestContext) { | ||
ctx.String(200, "CSRF token is valid") | ||
}) | ||
|
||
h.Spin() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2022 CloudWeGo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cloudwego/hertz/pkg/app" | ||
"github.com/cloudwego/hertz/pkg/app/server" | ||
"github.com/hertz-contrib/csrf" | ||
"github.com/hertz-contrib/sessions" | ||
"github.com/hertz-contrib/sessions/cookie" | ||
) | ||
|
||
func main() { | ||
h := server.Default() | ||
|
||
store := cookie.NewStore([]byte("secret")) | ||
h.Use(sessions.New("csrf-session", store)) | ||
h.Use(csrf.New(csrf.WithIgnoredMethods([]string{"GET", "HEAD", "TRACE"}))) | ||
|
||
h.GET("/protected", func(c context.Context, ctx *app.RequestContext) { | ||
ctx.String(200, csrf.GetToken(ctx)) | ||
}) | ||
|
||
h.OPTIONS("/protected", func(c context.Context, ctx *app.RequestContext) { | ||
ctx.String(200, "success") | ||
}) | ||
h.Spin() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2022 CloudWeGo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cloudwego/hertz/pkg/app" | ||
"github.com/cloudwego/hertz/pkg/app/server" | ||
"github.com/hertz-contrib/csrf" | ||
"github.com/hertz-contrib/sessions" | ||
"github.com/hertz-contrib/sessions/cookie" | ||
) | ||
|
||
func main() { | ||
h := server.Default() | ||
|
||
store := cookie.NewStore([]byte("store")) | ||
h.Use(sessions.New("csrf-session", store)) | ||
h.Use(csrf.New(csrf.WithKeyLookUp("form:csrf"))) | ||
|
||
h.GET("/protected", func(c context.Context, ctx *app.RequestContext) { | ||
ctx.String(200, csrf.GetToken(ctx)) | ||
}) | ||
h.POST("/protected", func(c context.Context, ctx *app.RequestContext) { | ||
ctx.String(200, "CSRF token is valid") | ||
}) | ||
|
||
h.Spin() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2022 CloudWeGo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cloudwego/hertz/pkg/app" | ||
"github.com/cloudwego/hertz/pkg/app/server" | ||
"github.com/hertz-contrib/csrf" | ||
"github.com/hertz-contrib/sessions" | ||
"github.com/hertz-contrib/sessions/cookie" | ||
) | ||
|
||
func isPostMethod(_ context.Context, ctx *app.RequestContext) bool { | ||
if string(ctx.Method()) == "POST" { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
func main() { | ||
h := server.Default() | ||
|
||
store := cookie.NewStore([]byte("store")) | ||
h.Use(sessions.New("csrf-session", store)) | ||
|
||
// skip csrf middleware when request method is post | ||
h.Use(csrf.New(csrf.WithNext(isPostMethod))) | ||
|
||
h.POST("/protected", func(c context.Context, ctx *app.RequestContext) { | ||
ctx.String(200, "success even no csrf-token in header") | ||
}) | ||
h.Spin() | ||
} |
Oops, something went wrong.