nagisa-kunhah commented on code in PR #1542:
URL: https://github.com/apache/dubbo-admin/pull/1542#discussion_r3927499078


##########
pkg/config/console/auth/config.go:
##########
@@ -19,26 +19,136 @@ package auth
 
 import (
        "errors"
+       "fmt"
+       "net/url"
+       "regexp"
+       "slices"
 
        "github.com/apache/dubbo-admin/pkg/config"
 )
 
-const DefaultExpirationTime = 7200
+const (
+       DefaultExpirationTime = 7200
+       DefaultSessionSecret  = "secret"
+
+       MethodPassword     = "password"
+       ProviderTypeGitHub = "github"
+       ProviderTypeOIDC   = "oidc"
+)
+
+var providerIDPattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$`)
+
+type ProviderConfig struct {
+       Type                 string   `json:"type" yaml:"type"`
+       DisplayName          string   `json:"displayName" yaml:"displayName"`
+       Issuer               string   `json:"issuer,omitempty" 
yaml:"issuer,omitempty"`
+       ClientID             string   `json:"clientId" yaml:"clientId"`
+       ClientSecret         string   `json:"clientSecret" yaml:"clientSecret"`
+       RedirectURL          string   `json:"redirectUrl" yaml:"redirectUrl"`
+       PostLoginRedirectURL string   `json:"postLoginRedirectUrl" 
yaml:"postLoginRedirectUrl"`
+       Scopes               []string `json:"scopes,omitempty" 
yaml:"scopes,omitempty"`
+}
 
 // Config AuthConfig configure the valid user and password
 type Config struct {
        config.BaseConfig
-       User           string `json:"user"`
-       Password       string `json:"password"`
-       ExpirationTime int    `json:"expirationTime"`
+       Methods             []string                  `json:"methods" 
yaml:"methods"`
+       User                string                    `json:"user" yaml:"user"`
+       Password            string                    `json:"password" 
yaml:"password"`
+       ExpirationTime      int                       `json:"expirationTime" 
yaml:"expirationTime"`
+       SessionSecret       string                    `json:"sessionSecret" 
yaml:"sessionSecret"`
+       SessionCookieSecure bool                      
`json:"sessionCookieSecure" yaml:"sessionCookieSecure"`
+       Providers           map[string]ProviderConfig 
`json:"providers,omitempty" yaml:"providers,omitempty"`
+}
+
+func (c *Config) Sanitize() {
+       c.Password = config.SanitizedValue
+       c.SessionSecret = config.SanitizedValue
+       for id, provider := range c.Providers {
+               provider.ClientSecret = config.SanitizedValue
+               c.Providers[id] = provider
+       }
 }
 
 func (c *Config) Validate() error {
-       if c.User == "" || c.Password == "" {
+       if len(c.Methods) == 0 {
+               c.Methods = []string{MethodPassword}
+       }
+       // Methods contains built-in login methods only; OAuth and OIDC are 
configured through Providers.
+       for _, method := range c.Methods {
+               if method != MethodPassword {
+                       return fmt.Errorf("auth: unsupported method %q", method)
+               }
+       }
+       if slices.Contains(c.Methods, MethodPassword) && (c.User == "" || 
c.Password == "") {
                return errors.New("auth: user or password is needed, but found 
empty")
        }
        if c.ExpirationTime <= 0 || c.ExpirationTime >= 24*60*60 {
                return errors.New("auth: expirationTime should be greater than 
0 and less than 86400")
        }
+       if c.SessionSecret == "" {
+               c.SessionSecret = DefaultSessionSecret
+       }
+       for id, provider := range c.Providers {
+               if err := validateProvider(id, &provider); err != nil {
+                       return err
+               }
+               c.Providers[id] = provider
+       }
        return nil
 }
+
+func validateProvider(id string, provider *ProviderConfig) error {
+       if !providerIDPattern.MatchString(id) {
+               return fmt.Errorf("auth: invalid provider id %q", id)
+       }
+       if provider.Type != ProviderTypeGitHub && provider.Type != 
ProviderTypeOIDC {
+               return fmt.Errorf("auth provider %q: unsupported type %q", id, 
provider.Type)
+       }
+       if provider.DisplayName == "" {
+               provider.DisplayName = id
+       }
+       if provider.ClientID == "" || provider.ClientSecret == "" {
+               return fmt.Errorf("auth provider %q: clientId and clientSecret 
are required", id)
+       }
+       redirect, err := validateHTTPURL(provider.RedirectURL)
+       if err != nil {
+               return fmt.Errorf("auth provider %q: invalid redirectUrl: %w", 
id, err)
+       }
+       expectedPath := "/api/v1/auth/providers/" + id + "/callback"
+       // The provider must return to the callback route registered for this 
provider ID.
+       if redirect.Path != expectedPath {
+               return fmt.Errorf("auth provider %q: redirectUrl must use 
callback path %q", id, expectedPath)
+       }
+       if _, err := validateHTTPURL(provider.PostLoginRedirectURL); err != nil 
{
+               return fmt.Errorf("auth provider %q: invalid 
postLoginRedirectUrl: %w", id, err)
+       }
+       switch provider.Type {
+       case ProviderTypeGitHub:
+               if len(provider.Scopes) == 0 {
+                       provider.Scopes = []string{"read:user", "user:email"}
+               }
+       case ProviderTypeOIDC:
+               if _, err := validateHTTPURL(provider.Issuer); err != nil {
+                       return fmt.Errorf("auth provider %q: invalid issuer: 
%w", id, err)
+               }

Review Comment:
   Updated. OIDC issuers and discovered authorization, token, JWKS, and 
UserInfo endpoints now require HTTPS by default. An explicit allowInsecureHTTP 
option is available for local development and tests, and release mode rejects 
configurations that enable it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to