This repository has been archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
94 lines (77 loc) · 2.46 KB
/
model.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package sessioninfo
import (
"fmt"
"net/http"
"strings"
"github.com/labstack/echo/v4"
"github.com/worldline-go/auth/claims"
"github.com/worldline-go/turna/pkg/server/middlewares/session"
"github.com/worldline-go/turna/pkg/server/model"
)
type Information struct {
// Values list to store in the cookie like "preferred_username", "given_name", "family_name", "sid", "azp", "aud"
Values []string `cfg:"values"`
// Custom map to store in the cookie.
Custom map[string]interface{} `cfg:"custom"`
// Roles to store in the cookie as []string.
Roles bool `cfg:"roles"`
// Scopes to store in the cookie as []string.
Scopes bool `cfg:"scopes"`
}
func (m *Info) Info(c echo.Context) error {
// get session middleware
sessionM := session.GlobalRegistry.Get(m.SessionMiddleware)
if sessionM == nil {
return c.JSON(http.StatusInternalServerError, model.MetaData{Error: "session middleware not found"})
}
// check if token exist in store
v64 := ""
if v, err := sessionM.GetStore().Get(c.Request(), sessionM.GetCookieName(c)); !v.IsNew && err == nil {
// add the access token to the request
v64, _ = v.Values[session.TokenKey].(string)
} else {
if err != nil {
return c.JSON(http.StatusInternalServerError, model.MetaData{Error: err.Error()})
}
// cookie not found
return c.JSON(http.StatusNotFound, model.MetaData{Error: "cookie not found"})
}
// check if token is valid
token, err := session.ParseToken64(v64)
if err != nil {
return c.JSON(http.StatusForbidden, model.MetaData{Error: fmt.Sprintf("cannot parse token: %v", err)})
}
// check if token is valid
claim := claims.Custom{}
if _, err := sessionM.Action.Token.GetKeyFunc().ParseWithClaims(token.AccessToken, &claim); err != nil {
return c.JSON(http.StatusForbidden, model.MetaData{Error: err.Error()})
}
// return the claims
totalLen := len(m.Information.Values) + len(m.Information.Custom)
if m.Information.Roles {
totalLen++
}
if m.Information.Scopes {
totalLen++
}
response := make(map[string]interface{}, totalLen)
for _, v := range m.Information.Values {
if claim, ok := claim.Map[v]; ok {
response[v] = claim
}
}
for k, v := range m.Information.Custom {
response[k] = v
}
if m.Information.Roles {
roles := make([]string, 0, len(claim.RoleSet))
for role := range claim.RoleSet {
roles = append(roles, role)
}
response["roles"] = roles
}
if m.Information.Scopes {
response["scopes"] = strings.Fields(claim.Scope)
}
return c.JSON(http.StatusOK, response)
}