mirror of
https://github.com/netbirdio/dex.git
synced 2026-05-22 18:43:53 -07:00
6f2e233c7a
Signed-off-by: maksim.nabokikh <max.nabokih@gmail.com>
53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
// handleUserInfo fetches user information from the provider's UserInfo endpoint.
|
|
func (s *Server) handleUserInfo(w http.ResponseWriter, r *http.Request) {
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, fmt.Sprintf("Failed to parse form: %v", err), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
accessToken := r.FormValue("access_token")
|
|
if accessToken == "" {
|
|
http.Error(w, "access_token is required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(r.Context(), http.MethodGet, s.userInfoURL, nil)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("Failed to create request: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
req.Header.Set("Authorization", "Bearer "+accessToken)
|
|
|
|
resp, err := s.client.Do(req)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("Failed to fetch userinfo: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
body, _ := io.ReadAll(resp.Body)
|
|
http.Error(w, fmt.Sprintf("UserInfo request failed: %s", string(body)), resp.StatusCode)
|
|
return
|
|
}
|
|
|
|
var userInfo map[string]any
|
|
if err := json.NewDecoder(resp.Body).Decode(&userInfo); err != nil {
|
|
http.Error(w, fmt.Sprintf("Failed to decode userinfo: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(userInfo)
|
|
}
|