77 "fmt"
88 "net"
99 "net/http"
10+ "net/url"
1011 "os"
1112 "os/signal"
1213 "strings"
@@ -51,8 +52,8 @@ func main() {
5152
5253 log .Info ("Starting token service" )
5354
54- http .HandleFunc ("/token/ " , tokenHandler )
55- http .HandleFunc ("/loginstatus/ " , authorizedHandler )
55+ http .HandleFunc ("/token" , tokenHandler )
56+ http .HandleFunc ("/loginstatus" , authorizedHandler )
5657 http .HandleFunc ("/healthz" , healthHandler )
5758
5859 // Listen for SIGTERM
@@ -96,22 +97,22 @@ func healthHandler(w http.ResponseWriter, r *http.Request) {
9697}
9798
9899func tokenHandler (w http.ResponseWriter , r * http.Request ) {
99- userID := r .URL .Path [len ("/token/" ):]
100100 switch r .Method {
101101 case http .MethodPost :
102- handleStore (w , r , userID )
102+ handleStore (w , r )
103103 case http .MethodPut :
104- handleUpdate (w , r , userID )
104+ handleUpdate (w , r )
105105 case http .MethodGet :
106- handleGet (w , r , userID )
106+ handleGet (w , r )
107107 case http .MethodDelete :
108- handleDelete (w , r , userID )
108+ handleDelete (w , r )
109109 default :
110110 w .WriteHeader (http .StatusMethodNotAllowed )
111111 }
112112}
113113
114114type StoreRequest struct {
115+ UserID string `json:"user_id"`
115116 AccessToken string `json:"access_token"`
116117 RefreshToken string `json:"refresh_token"`
117118 IDToken string `json:"id_token"`
@@ -121,21 +122,21 @@ type StoreRequest struct {
121122 ClientSecret string `json:"client_secret"`
122123}
123124
124- func handleStore (w http.ResponseWriter , r * http.Request , userID string ) {
125- _ , exists := store .tokens [userID ]
126- if exists {
127- log .WithField ("user_id" , userID ).Warn ("Token store requested but existing token found" )
128- http .Error (w , "User tokens already exists. Use PUT to update a token" , 409 )
129- return
130- }
131-
125+ func handleStore (w http.ResponseWriter , r * http.Request ) {
132126 var req StoreRequest
133127 if err := json .NewDecoder (r .Body ).Decode (& req ); err != nil {
134128 log .WithError (err ).Warn ("Invalid store request" )
135129 http .Error (w , err .Error (), 400 )
136130 return
137131 }
138132
133+ _ , exists := store .tokens [req .UserID ]
134+ if exists {
135+ log .WithField ("user_id" , req .UserID ).Warn ("Token store requested but existing token found" )
136+ http .Error (w , "User tokens already exists. Use PUT to update a token" , 409 )
137+ return
138+ }
139+
139140 provider , err := oidc .NewProvider (ctx , req .Issuer )
140141 if err != nil {
141142 http .Error (w , err .Error (), 400 )
@@ -159,26 +160,26 @@ func handleStore(w http.ResponseWriter, r *http.Request, userID string) {
159160 Expiry : time .Unix (req .Expiry , 0 ),
160161 }
161162
162- storeToken (userID , token , req .IDToken , oauthConfig , req .Issuer )
163+ storeToken (req . UserID , token , req .IDToken , oauthConfig , req .Issuer )
163164
164165 w .WriteHeader (http .StatusCreated )
165166}
166167
167- func handleUpdate (w http.ResponseWriter , r * http.Request , userID string ) {
168- _ , exists := store .tokens [userID ]
169- if ! exists {
170- log .WithField ("user_id" , userID ).Warn ("Token update requested but no existing token found" )
171- http .Error (w , "User tokens not found. Use POST to create a token" , 404 )
172- return
173- }
174-
168+ func handleUpdate (w http.ResponseWriter , r * http.Request ) {
175169 var req StoreRequest
176170 if err := json .NewDecoder (r .Body ).Decode (& req ); err != nil {
177171 log .WithError (err ).Warn ("Invalid update request" )
178172 http .Error (w , err .Error (), 400 )
179173 return
180174 }
181175
176+ _ , exists := store .tokens [req .UserID ]
177+ if ! exists {
178+ log .WithField ("user_id" , req .UserID ).Warn ("Token update requested but no existing token found" )
179+ http .Error (w , "User tokens not found. Use POST to create a token" , 404 )
180+ return
181+ }
182+
182183 provider , err := oidc .NewProvider (ctx , req .Issuer )
183184 if err != nil {
184185 http .Error (w , err .Error (), 400 )
@@ -202,7 +203,7 @@ func handleUpdate(w http.ResponseWriter, r *http.Request, userID string) {
202203 Expiry : time .Unix (req .Expiry , 0 ),
203204 }
204205
205- storeToken (userID , token , req .IDToken , oauthConfig , req .Issuer )
206+ storeToken (req . UserID , token , req .IDToken , oauthConfig , req .Issuer )
206207
207208 w .WriteHeader (http .StatusOK )
208209}
@@ -220,7 +221,21 @@ func storeToken(userID string, token *oauth2.Token, idToken string, config *oaut
220221 log .WithField ("user_id" , userID ).Info ("Stored token" )
221222}
222223
223- func handleGet (w http.ResponseWriter , _ * http.Request , userID string ) {
224+ func handleGet (w http.ResponseWriter , r * http.Request ) {
225+ // Get the userID from the query parameter
226+ encodedID := r .URL .Query ().Get ("id" )
227+ if encodedID == "" {
228+ http .Error (w , "Missing 'id' query parameter" , http .StatusBadRequest )
229+ return
230+ }
231+
232+ // URL-decode the userID
233+ userID , err := url .QueryUnescape (encodedID )
234+ if err != nil {
235+ http .Error (w , "Invalid 'id' query parameter" , http .StatusBadRequest )
236+ return
237+ }
238+
224239 entry , err := getEntry (userID )
225240 if err != nil {
226241 log .WithField ("user_id" , userID ).Warn ("Token not found" )
@@ -241,7 +256,21 @@ func handleGet(w http.ResponseWriter, _ *http.Request, userID string) {
241256 })
242257}
243258
244- func handleDelete (w http.ResponseWriter , _ * http.Request , userID string ) {
259+ func handleDelete (w http.ResponseWriter , r * http.Request ) {
260+ // Get the userID from the query parameter
261+ encodedID := r .URL .Query ().Get ("id" )
262+ if encodedID == "" {
263+ http .Error (w , "Missing 'id' query parameter" , http .StatusBadRequest )
264+ return
265+ }
266+
267+ // URL-decode the userID
268+ userID , err := url .QueryUnescape (encodedID )
269+ if err != nil {
270+ http .Error (w , "Invalid 'id' query parameter" , http .StatusBadRequest )
271+ return
272+ }
273+
245274 store .mu .Lock ()
246275 delete (store .tokens , userID )
247276 store .mu .Unlock ()
@@ -256,7 +285,19 @@ func authorizedHandler(w http.ResponseWriter, r *http.Request) {
256285 return
257286 }
258287
259- userID := r .URL .Path [len ("/loginstatus/" ):]
288+ // Get the userID from the query parameter
289+ encodedID := r .URL .Query ().Get ("id" )
290+ if encodedID == "" {
291+ http .Error (w , "Missing 'id' query parameter" , http .StatusBadRequest )
292+ return
293+ }
294+
295+ // URL-decode the userID
296+ userID , err := url .QueryUnescape (encodedID )
297+ if err != nil {
298+ http .Error (w , "Invalid 'id' query parameter" , http .StatusBadRequest )
299+ return
300+ }
260301
261302 entry , err := getEntry (userID )
262303 if err != nil {
0 commit comments