@@ -67,6 +67,27 @@ func Protect(gocloakClient *gocloak.GoCloak, authRealm string, authClientId stri
67
67
return
68
68
}
69
69
70
+ userInfo , err := gocloakClient .GetUserInfo (context .Background (), accessToken , authRealm )
71
+ if err != nil {
72
+ log .Warn ().
73
+ Int ("status" , 401 ).
74
+ Str ("method" , r .Method ).
75
+ Str ("path" , r .RequestURI ).
76
+ Str ("ip" , r .RemoteAddr ).
77
+ Str ("client-id" , authClientId ).
78
+ Str ("query" , query ).
79
+ Msg ("Unauthorized" )
80
+
81
+ w .WriteHeader (401 )
82
+ json .NewEncoder (w ).Encode (errors .BadRequestError (err .Error ()))
83
+ return
84
+ }
85
+ username := * userInfo .PreferredUsername
86
+ var userClientId string = ""
87
+ if strings .Contains (username , "service-account-" ) {
88
+ userClientId = strings .ReplaceAll (username , "service-account-" , "" )
89
+ }
90
+
70
91
isTokenValid := * rptResult .Active
71
92
72
93
if ! isTokenValid {
@@ -75,7 +96,8 @@ func Protect(gocloakClient *gocloak.GoCloak, authRealm string, authClientId stri
75
96
Str ("method" , r .Method ).
76
97
Str ("path" , r .RequestURI ).
77
98
Str ("ip" , r .RemoteAddr ).
78
- Str ("client-id" , authClientId ).
99
+ Str ("username" , username ).
100
+ Str ("client-id" , userClientId ).
79
101
Str ("query" , query ).
80
102
Msg ("Unauthorized" )
81
103
@@ -114,9 +136,10 @@ func Protect(gocloakClient *gocloak.GoCloak, authRealm string, authClientId stri
114
136
Str ("method" , r .Method ).
115
137
Str ("path" , r .RequestURI ).
116
138
Str ("ip" , r .RemoteAddr ).
117
- Str ("client-id" , authClientId ).
139
+ Str ("username" , username ).
140
+ Str ("client-id" , userClientId ).
118
141
Str ("query" , query ).
119
- Msg ("Forbidden" )
142
+ Msg (err . Error () )
120
143
121
144
w .WriteHeader (403 )
122
145
json .NewEncoder (w ).Encode (errors .UnauthorizedError ())
@@ -130,7 +153,8 @@ func Protect(gocloakClient *gocloak.GoCloak, authRealm string, authClientId stri
130
153
Str ("method" , r .Method ).
131
154
Str ("path" , r .RequestURI ).
132
155
Str ("ip" , r .RemoteAddr ).
133
- Str ("client-id" , authClientId ).
156
+ Str ("username" , username ).
157
+ Str ("client-id" , userClientId ).
134
158
Str ("query" , query ).
135
159
Msg ("Bad Request" )
136
160
@@ -165,7 +189,8 @@ func Protect(gocloakClient *gocloak.GoCloak, authRealm string, authClientId stri
165
189
Str ("method" , r .Method ).
166
190
Str ("path" , r .RequestURI ).
167
191
Str ("ip" , r .RemoteAddr ).
168
- Str ("client-id" , authClientId ).
192
+ Str ("username" , username ).
193
+ Str ("client-id" , userClientId ).
169
194
Str ("query" , query ).
170
195
RawJSON ("permissions" , out ).
171
196
Msg ("OK" )
@@ -176,7 +201,8 @@ func Protect(gocloakClient *gocloak.GoCloak, authRealm string, authClientId stri
176
201
Str ("method" , r .Method ).
177
202
Str ("path" , r .RequestURI ).
178
203
Str ("ip" , r .RemoteAddr ).
179
- Str ("client-id" , authClientId ).
204
+ Str ("username" , username ).
205
+ Str ("client-id" , userClientId ).
180
206
Str ("query" , query ).
181
207
Msg (message )
182
208
0 commit comments