fix bug - oidc scopes
This commit is contained in:
+20
-19
@@ -29,7 +29,6 @@ type OidcEndpoint struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type OauthService struct {
|
type OauthService struct {
|
||||||
OidcEndpoint *OidcEndpoint
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type GithubUserdata struct {
|
type GithubUserdata struct {
|
||||||
@@ -93,7 +92,6 @@ type OidcUserdata struct {
|
|||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
VerifiedEmail bool `json:"email_verified"`
|
VerifiedEmail bool `json:"email_verified"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Picture string `json:"picture"`
|
|
||||||
PrefferedUsername string `json:"preffered_username"`
|
PrefferedUsername string `json:"preffered_username"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +155,7 @@ func (os *OauthService) BeginAuth(op string) (error error, code, url string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Method to fetch OIDC configuration dynamically
|
// Method to fetch OIDC configuration dynamically
|
||||||
func (os *OauthService) FetchOIDCConfig(issuer string) error {
|
func FetchOidcConfig(issuer string) (error, OidcEndpoint) {
|
||||||
configURL := strings.TrimSuffix(issuer, "/") + "/.well-known/openid-configuration"
|
configURL := strings.TrimSuffix(issuer, "/") + "/.well-known/openid-configuration"
|
||||||
|
|
||||||
// Get the HTTP client (with or without proxy based on configuration)
|
// Get the HTTP client (with or without proxy based on configuration)
|
||||||
@@ -165,21 +163,20 @@ func (os *OauthService) FetchOIDCConfig(issuer string) error {
|
|||||||
|
|
||||||
resp, err := client.Get(configURL)
|
resp, err := client.Get(configURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("failed to fetch OIDC configuration")
|
return errors.New("failed to fetch OIDC configuration"), OidcEndpoint{}
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return errors.New("OIDC configuration not found")
|
return errors.New("OIDC configuration not found, status code: %d"), OidcEndpoint{}
|
||||||
}
|
}
|
||||||
|
|
||||||
var endpoint OidcEndpoint
|
var endpoint OidcEndpoint
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&endpoint); err != nil {
|
if err := json.NewDecoder(resp.Body).Decode(&endpoint); err != nil {
|
||||||
return errors.New("failed to parse OIDC configuration")
|
return errors.New("failed to parse OIDC configuration"), OidcEndpoint{}
|
||||||
}
|
}
|
||||||
|
|
||||||
os.OidcEndpoint = &endpoint
|
return nil, endpoint
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetOauthConfig retrieves the OAuth2 configuration based on the provider type
|
// GetOauthConfig retrieves the OAuth2 configuration based on the provider type
|
||||||
@@ -234,24 +231,22 @@ func (os *OauthService) getOidcConfig() (error, *oauth2.Config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set scopes
|
// Set scopes
|
||||||
scopes := g.Scopes
|
scopes := strings.TrimSpace(g.Scopes)
|
||||||
if scopes == "" {
|
if scopes == "" {
|
||||||
scopes = "openid,profile,email"
|
scopes = "openid,profile,email"
|
||||||
}
|
}
|
||||||
scopeList := strings.Split(scopes, ",")
|
scopeList := strings.Split(scopes, ",")
|
||||||
|
err, endpoint := FetchOidcConfig(g.Issuer)
|
||||||
// Fetch OIDC configuration
|
if err != nil {
|
||||||
if err := os.FetchOIDCConfig(g.Issuer); err != nil {
|
|
||||||
return err, nil
|
return err, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, &oauth2.Config{
|
return nil, &oauth2.Config{
|
||||||
ClientID: g.ClientId,
|
ClientID: g.ClientId,
|
||||||
ClientSecret: g.ClientSecret,
|
ClientSecret: g.ClientSecret,
|
||||||
RedirectURL: g.RedirectUrl,
|
RedirectURL: g.RedirectUrl,
|
||||||
Endpoint: oauth2.Endpoint{
|
Endpoint: oauth2.Endpoint{
|
||||||
AuthURL: os.OidcEndpoint.AuthURL,
|
AuthURL: endpoint.AuthURL,
|
||||||
TokenURL: os.OidcEndpoint.TokenURL,
|
TokenURL: endpoint.TokenURL,
|
||||||
},
|
},
|
||||||
Scopes: scopeList,
|
Scopes: scopeList,
|
||||||
}
|
}
|
||||||
@@ -363,7 +358,6 @@ func (os *OauthService) OidcCallback(code string) (error error, userData *OidcUs
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err, nil
|
return err, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用代理配置创建 HTTP 客户端
|
// 使用代理配置创建 HTTP 客户端
|
||||||
httpClient := getHTTPClientWithProxy()
|
httpClient := getHTTPClientWithProxy()
|
||||||
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, httpClient)
|
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, httpClient)
|
||||||
@@ -377,7 +371,14 @@ func (os *OauthService) OidcCallback(code string) (error error, userData *OidcUs
|
|||||||
|
|
||||||
// 使用带有代理的 HTTP 客户端获取用户信息
|
// 使用带有代理的 HTTP 客户端获取用户信息
|
||||||
client := oauthConfig.Client(ctx, token)
|
client := oauthConfig.Client(ctx, token)
|
||||||
resp, err := client.Get(os.OidcEndpoint.UserInfo)
|
g := os.InfoByOp(model.OauthTypeOidc)
|
||||||
|
err, endpoint := FetchOidcConfig(g.Issuer)
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Warn("failed fetching OIDC configuration: ", err)
|
||||||
|
error = errors.New("FetchOidcConfigError")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resp, err := client.Get(endpoint.UserInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.Logger.Warn("failed getting user info: ", err)
|
global.Logger.Warn("failed getting user info: ", err)
|
||||||
error = errors.New("GetOauthUserInfoError")
|
error = errors.New("GetOauthUserInfoError")
|
||||||
@@ -413,8 +414,8 @@ func (os *OauthService) BindGoogleUser(email, username string, userId uint) erro
|
|||||||
return os.BindOauthUser(model.OauthTypeGoogle, email, username, userId)
|
return os.BindOauthUser(model.OauthTypeGoogle, email, username, userId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (os *OauthService) BindOidcUser(openid, username string, userId uint) error {
|
func (os *OauthService) BindOidcUser(sub, username string, userId uint) error {
|
||||||
return os.BindOauthUser(model.OauthTypeOidc, openid, username, userId)
|
return os.BindOauthUser(model.OauthTypeOidc, sub, username, userId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (os *OauthService) BindOauthUser(thirdType, openid, username string, userId uint) error {
|
func (os *OauthService) BindOauthUser(thirdType, openid, username string, userId uint) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user