-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
137 lines (117 loc) · 2.84 KB
/
routes.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"encoding/json"
"io"
"net/http"
)
type GreetRes struct {
Hello string `json:"hello"`
}
func (s *APIServer) handleGreet(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(400)
w.Write([]byte("Method not supported"))
return
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(200)
res := &GreetRes{
Hello: "worlds",
}
json.NewEncoder(w).Encode(res)
}
func (s *APIServer) handleLogin(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(400)
w.Write([]byte("Method not supported 2"))
return
}
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(400)
w.Write([]byte("Failed to read body"))
return
}
payload := &LoginPayload{}
err = json.Unmarshal(bodyBytes, payload)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
res, err := s.keycloakClient.Login(r.Context(), payload.Username, payload.Password)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
rol, err := s.keycloakClient.DecodeJWT(r.Context(), res.AccessToken)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
/*
var tokenString = res.AccessToken
token, _, err := new(jwt.Parser).ParseUnverified(tokenString, jwt.MapClaims{})
if err != nil {
fmt.Println(err)
return
}
if claims, ok := token.Claims.(jwt.MapClaims); ok {
fmt.Println(claims["realm_access"])
} else {
fmt.Println(err)
}
*/
/*
rol, err := s.keycloakClient.DecodeJWT(r.Context(), res.AccessToken)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
*/
/*
_, err = s.keycloakClient.GetRoleMappingByUserID(r.Context(), res.AccessToken)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
*/
w.WriteHeader(200)
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(res)
json.NewEncoder(w).Encode(rol)
}
/* func (s *APIServer) handleRoleMapping(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(400)
w.Write([]byte("Method not supported 2"))
return
}
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(400)
w.Write([]byte("Failed to read body"))
return
}
payload := &IntrospectPayload{}
err = json.Unmarshal(bodyBytes, payload)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
res, err := s.keycloakClient.Login(r.Context(), payload.AccessToken)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(200)
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(res)
}
*/