Revision | b55bd837b4e3b9e3f9be65e9bc85f3a035371789 (tree) |
---|---|
Time | 2020-05-14 11:57:47 |
Author | Francisco Reyes <francisco@nats...> |
Commiter | Francisco Reyes |
basic authe examples
@@ -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 | +} |
@@ -26,7 +26,7 @@ | ||
26 | 26 | // Echo instance |
27 | 27 | e := echo.New() |
28 | 28 | |
29 | - g := e.Group("/admin") | |
29 | + g := e.Group("") | |
30 | 30 | g.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) { |
31 | 31 | if username == "joe" && password == "secret" { |
32 | 32 | return true, nil |
@@ -34,17 +34,13 @@ | ||
34 | 34 | return false, nil |
35 | 35 | })) |
36 | 36 | // Route => handler |
37 | - e.GET("/", func(c echo.Context) error { | |
37 | + g.GET("/", func(c echo.Context) error { | |
38 | 38 | return c.HTML(http.StatusOK, "root") |
39 | 39 | }) |
40 | - g.GET("", func(c echo.Context) error { | |
40 | + g.GET("/admin", func(c echo.Context) error { | |
41 | 41 | return c.HTML(http.StatusOK, "admin") |
42 | 42 | }) |
43 | 43 | |
44 | - g.GET("/internal", func(c echo.Context) error { | |
45 | - return c.HTML(http.StatusOK, "internal") | |
46 | - }) | |
47 | - | |
48 | 44 | // Start server |
49 | 45 | e.Logger.Fatal(e.Start(":1323")) |
50 | 46 | } |
@@ -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 | +} |