Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User authentication using OpenID Connect #114

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

User authentication using OpenID Connect #114

wants to merge 2 commits into from

Conversation

reinkrul
Copy link
Member

No description provided.

return errors.New("auth init failure")
}

redirectUrl := a.oauth2Config.AuthCodeURL("TODO:STATE")

Check failure

Code scanning / CodeQL

Use of constant `state` value in OAuth 2.0 URL High

Using a constant
state string
to create oauth2 URLs.

Copilot Autofix AI 15 days ago

To fix the problem, we need to replace the constant state value with a unique, non-guessable value for each authentication request. This can be achieved by generating a random state value using a secure random number generator and encoding it. Additionally, we should store this state value in a way that it can be validated later during the callback to ensure it matches the original request.

  1. Generate a unique state value: Create a function to generate a random state value using crypto/rand and encode it using base64.URLEncoding.
  2. Store the state value: Store the generated state value in a cookie or session to validate it later during the callback.
  3. Validate the state value: During the callback, retrieve and validate the state value to ensure it matches the original request.
Suggested changeset 1
api/auth.go

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/api/auth.go b/api/auth.go
--- a/api/auth.go
+++ b/api/auth.go
@@ -123,3 +123,4 @@
 
-		redirectUrl := a.oauth2Config.AuthCodeURL("TODO:STATE")
+		state := generateStateOauthCookie(c.Response())
+		redirectUrl := a.oauth2Config.AuthCodeURL(state)
 		// Check redirect URL to prevent invalid/recursive redirects
@@ -141,2 +142,8 @@
 	}
+	// Validate state
+	stateCookie, err := c.Cookie("oauthstate")
+	if err != nil || stateCookie.Value != httpRequest.URL.Query().Get("state") {
+		log.Logger.Warn().Err(err).Msg("Invalid state parameter")
+		return c.NoContent(http.StatusUnauthorized)
+	}
 	// Check if the user is authenticated
@@ -179 +186,19 @@
 }
+
+func generateStateOauthCookie(w http.ResponseWriter) string {
+	b := make([]byte, 16)
+	_, err := rand.Read(b)
+	if err != nil {
+		log.Logger.Error().Err(err).Msg("Failed to generate random state")
+		return ""
+	}
+	state := base64.URLEncoding.EncodeToString(b)
+	http.SetCookie(w, &http.Cookie{
+		Name:     "oauthstate",
+		Value:    state,
+		Expires:  time.Now().Add(10 * time.Minute),
+		HttpOnly: true,
+		Path:     "/",
+	})
+	return state
+}
EOF
@@ -123,3 +123,4 @@

redirectUrl := a.oauth2Config.AuthCodeURL("TODO:STATE")
state := generateStateOauthCookie(c.Response())
redirectUrl := a.oauth2Config.AuthCodeURL(state)
// Check redirect URL to prevent invalid/recursive redirects
@@ -141,2 +142,8 @@
}
// Validate state
stateCookie, err := c.Cookie("oauthstate")
if err != nil || stateCookie.Value != httpRequest.URL.Query().Get("state") {
log.Logger.Warn().Err(err).Msg("Invalid state parameter")
return c.NoContent(http.StatusUnauthorized)
}
// Check if the user is authenticated
@@ -179 +186,19 @@
}

func generateStateOauthCookie(w http.ResponseWriter) string {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
log.Logger.Error().Err(err).Msg("Failed to generate random state")
return ""
}
state := base64.URLEncoding.EncodeToString(b)
http.SetCookie(w, &http.Cookie{
Name: "oauthstate",
Value: state,
Expires: time.Now().Add(10 * time.Minute),
HttpOnly: true,
Path: "/",
})
return state
}
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant