package util import ( "fmt" "github.com/davidebianchi/go-jsonclient" "net/http" "net/url" ) type openIdUserInfoResp struct { Name string `json:"name"` GivenName string `json:"given_name"` FamilyName string `json:"family_name"` PreferredUsername string `json:"preferred_username"` Email string `json:"email"` Picture string `json:"picture"` } func OpenIdUserInfo(openIdUserInfoURL, accessToken string) (map[string]any, error) { u, err := url.Parse(openIdUserInfoURL) if err != nil { logger.Errorf(`failed to parse openIdUserInfoURL. error: %s`, err.Error()) return nil, err } client, err := jsonclient.New(jsonclient.Options{ BaseURL: fmt.Sprintf(`%s://%s/`, u.Scheme, u.Host), Headers: map[string]string{ `Authorization`: fmt.Sprintf(`Bearer %s`, accessToken), }, }) if err != nil { logger.Errorf(`failed to create user_info client. error: %s`, err.Error()) return nil, err } req, err := client.NewRequest(http.MethodGet, u.Path, nil) if err != nil { logger.Errorf(`failed to create user_info request. error: %s`, err.Error()) return nil, err } uiResp := new(openIdUserInfoResp) resp, err := client.Do(req, uiResp) if err != nil { logger.Errorf(`failed to request user_info. error: %s`, err.Error()) return nil, err } if resp.StatusCode >= 300 { logger.Errorf(`user_info request return wrong code. code: %d, err_msg: %s`, resp.StatusCode, resp.Status) return nil, err } return map[string]any{ `userId`: uiResp.PreferredUsername, `userName`: uiResp.Name, `avatar`: uiResp.Picture, }, nil }