@@ -62,31 +62,42 @@ func IsValidPassword(password string) (bool, string) {
6262// It prefers strong authentication methods (e.g., OAuth) over weaker ones (e.g., bearer tokens).
6363// It logs an error and returns "unknown" if no valid credentials are provided.
6464func DetermineAuthMethod (authConfig AuthConfig ) (string , error ) {
65- validClientID , clientIDErrMsg := IsValidClientID (authConfig .ClientID )
66- validClientSecret , clientSecretErrMsg := IsValidClientSecret (authConfig .ClientSecret )
67- validUsername , usernameErrMsg := IsValidUsername (authConfig .Username )
68- validPassword , passwordErrMsg := IsValidPassword (authConfig .Password )
65+ // Initialize validation flags as true
66+ validClientID , validClientSecret , validUsername , validPassword := true , true , true , true
67+ clientIDErrMsg , clientSecretErrMsg , usernameErrMsg , passwordErrMsg := "" , "" , "" , ""
6968
70- if validClientID && validClientSecret {
71- return "oauth" , nil
69+ // Validate ClientID and ClientSecret for OAuth if provided
70+ if authConfig .ClientID != "" || authConfig .ClientSecret != "" {
71+ validClientID , clientIDErrMsg = IsValidClientID (authConfig .ClientID )
72+ validClientSecret , clientSecretErrMsg = IsValidClientSecret (authConfig .ClientSecret )
73+ // If both ClientID and ClientSecret are valid, use OAuth
74+ if validClientID && validClientSecret {
75+ return "oauth" , nil
76+ }
7277 }
7378
74- if validUsername && validPassword {
75- return "bearer" , nil
79+ // Validate Username and Password for Bearer if OAuth is not valid or not provided
80+ if authConfig .Username != "" || authConfig .Password != "" {
81+ validUsername , usernameErrMsg = IsValidUsername (authConfig .Username )
82+ validPassword , passwordErrMsg = IsValidPassword (authConfig .Password )
83+ // If both Username and Password are valid, use Bearer
84+ if validUsername && validPassword {
85+ return "bearer" , nil
86+ }
7687 }
7788
78- // Construct error message
89+ // Construct an error message if any of the provided fields are invalid
7990 errorMsg := "No valid credentials provided."
80- if ! validClientID {
91+ if ! validClientID && authConfig . ClientID != "" {
8192 errorMsg += " " + clientIDErrMsg
8293 }
83- if ! validClientSecret {
94+ if ! validClientSecret && authConfig . ClientSecret != "" {
8495 errorMsg += " " + clientSecretErrMsg
8596 }
86- if ! validUsername {
97+ if ! validUsername && authConfig . Username != "" {
8798 errorMsg += " " + usernameErrMsg
8899 }
89- if ! validPassword {
100+ if ! validPassword && authConfig . Password != "" {
90101 errorMsg += " " + passwordErrMsg
91102 }
92103
0 commit comments