-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.go
More file actions
41 lines (33 loc) · 1.07 KB
/
main.go
File metadata and controls
41 lines (33 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/baba2k/echo-keycloak"
"github.com/dgrijalva/jwt-go"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func main() {
e := echo.New()
e.HideBanner = true
e.Use(middleware.Logger())
e.Use(middleware.Recover())
restricted := e.Group("/restricted",
// init echo-keycloak middleware with keycloak host and realm
keycloak.Keycloak("http://localhost:8080", "test"))
restricted.GET("", func(c echo.Context) error {
token := c.Get("user").(*jwt.Token)
claims := token.Claims.(*jwt.MapClaims)
prettyJSONClaims, _ := json.MarshalIndent(claims, "", " ")
return c.String(http.StatusOK, fmt.Sprintf(
"Hello, User! Your claims are:\n%+v\n", string(prettyJSONClaims)))
})
restricted.GET("/admin", func(c echo.Context) error {
return c.String(http.StatusOK,
fmt.Sprintf("Hello, Admin! Your roles are: %+v\n",
c.Get("roles").([]string)))
// init echo-keycloak-roles middleware with role "admin"
}, keycloak.KeycloakRoles([]string{"admin"}))
e.Logger.Fatal(e.Start(":8080"))
}