Commit MetaInfo

Revisionb55bd837b4e3b9e3f9be65e9bc85f3a035371789 (tree)
Time2020-05-14 11:57:47
AuthorFrancisco Reyes <francisco@nats...>
CommiterFrancisco Reyes

Log Message

basic authe examples

Change Summary

Incremental Difference

diff -r 04a0d4bd0c74 -r b55bd837b4e3 basicauth/entire-site.go
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/basicauth/entire-site.go Wed May 13 22:57:47 2020 -0400
@@ -0,0 +1,50 @@
1+/*
2+Copyright 2020 Francisco Reyes
3+
4+Licensed under the Apache License, Version 2.0 (the "License");
5+you may not use this file except in compliance with the License.
6+You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+Unless required by applicable law or agreed to in writing, software
11+distributed under the License is distributed on an "AS IS" BASIS,
12+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+See the License for the specific language governing permissions and
14+limitations under the License.
15+*/
16+
17+
18+// Secure entire site behind basic auth
19+
20+package main
21+
22+import (
23+ "github.com/labstack/echo"
24+ "github.com/labstack/echo/middleware"
25+ "net/http"
26+)
27+
28+func main() {
29+ // Echo instance
30+ e := echo.New()
31+
32+ // define a group for the entire site
33+ g := e.Group("")
34+ g.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
35+ if username == "joe" && password == "secret" {
36+ return true, nil
37+ }
38+ return false, nil
39+ }))
40+ // Route => handler
41+ g.GET("/", func(c echo.Context) error {
42+ return c.HTML(http.StatusOK, "root")
43+ })
44+ g.GET("/admin", func(c echo.Context) error {
45+ return c.HTML(http.StatusOK, "admin")
46+ })
47+
48+ // Start server
49+ e.Logger.Fatal(e.Start(":1323"))
50+}
diff -r 04a0d4bd0c74 -r b55bd837b4e3 basicauth/main.go
--- a/basicauth/main.go Tue May 12 21:06:04 2020 -0400
+++ b/basicauth/main.go Wed May 13 22:57:47 2020 -0400
@@ -26,7 +26,7 @@
2626 // Echo instance
2727 e := echo.New()
2828
29- g := e.Group("/admin")
29+ g := e.Group("")
3030 g.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
3131 if username == "joe" && password == "secret" {
3232 return true, nil
@@ -34,17 +34,13 @@
3434 return false, nil
3535 }))
3636 // Route => handler
37- e.GET("/", func(c echo.Context) error {
37+ g.GET("/", func(c echo.Context) error {
3838 return c.HTML(http.StatusOK, "root")
3939 })
40- g.GET("", func(c echo.Context) error {
40+ g.GET("/admin", func(c echo.Context) error {
4141 return c.HTML(http.StatusOK, "admin")
4242 })
4343
44- g.GET("/internal", func(c echo.Context) error {
45- return c.HTML(http.StatusOK, "internal")
46- })
47-
4844 // Start server
4945 e.Logger.Fatal(e.Start(":1323"))
5046 }
diff -r 04a0d4bd0c74 -r b55bd837b4e3 basicauth/section-only.go
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/basicauth/section-only.go Wed May 13 22:57:47 2020 -0400
@@ -0,0 +1,56 @@
1+/*
2+Copyright 2020 Francisco Reyes
3+
4+Licensed under the Apache License, Version 2.0 (the "License");
5+you may not use this file except in compliance with the License.
6+You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+Unless required by applicable law or agreed to in writing, software
11+distributed under the License is distributed on an "AS IS" BASIS,
12+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+See the License for the specific language governing permissions and
14+limitations under the License.
15+*/
16+
17+
18+// Secure entire site behind basic auth
19+
20+package main
21+
22+import (
23+ "github.com/labstack/echo"
24+ "github.com/labstack/echo/middleware"
25+ "net/http"
26+)
27+
28+func main() {
29+ // Echo instance
30+ e := echo.New()
31+
32+ // define a group for path /admin and under
33+ g := e.Group("/admin")
34+ g.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
35+ if username == "joe" && password == "secret" {
36+ return true, nil
37+ }
38+ return false, nil
39+ }))
40+ // use default object insteD group - won't be authenticated
41+ e.GET("/", func(c echo.Context) error {
42+ return c.HTML(http.StatusOK, "root")
43+ })
44+ // authenticate through basic auth for /admin
45+ g.GET("", func(c echo.Context) error {
46+ return c.HTML(http.StatusOK, "admin")
47+ })
48+
49+ // authenticate through basic auth for /admin/second_level
50+ g.GET("/second_level", func(c echo.Context) error {
51+ return c.HTML(http.StatusOK, "admin")
52+ })
53+
54+ // Start server
55+ e.Logger.Fatal(e.Start(":1323"))
56+}
Show on old repository browser