Spaces:
Sleeping
Sleeping
Update internal/auth/handler/auth_handler.go
Browse files
internal/auth/handler/auth_handler.go
CHANGED
|
@@ -34,49 +34,50 @@ func (rw *LoggingResponseWriter) WriteHeader(statusCode int) {
|
|
| 34 |
|
| 35 |
func Login(w http.ResponseWriter, r *http.Request) {
|
| 36 |
log.Println("Login handler triggered")
|
| 37 |
-
state, _ := utils.SecureRandom(32)
|
| 38 |
-
codeVerifier, _ := utils.SecureRandom(64)
|
| 39 |
-
codeChallenge := crypto.SHA256Base64URL(codeVerifier)
|
| 40 |
-
|
| 41 |
-
http.SetCookie(w, &http.Cookie{
|
| 42 |
-
Name: "oauth_state",
|
| 43 |
-
Value: state,
|
| 44 |
-
HttpOnly: true,
|
| 45 |
-
Secure: true,
|
| 46 |
-
SameSite: http.SameSiteNoneMode,
|
| 47 |
-
Path: "/",
|
| 48 |
-
MaxAge: 300,
|
| 49 |
-
})
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
//
|
| 74 |
-
|
| 75 |
|
| 76 |
-
//
|
| 77 |
-
|
| 78 |
-
log.Printf("Redirection failed with status code: %d", loggingRW.StatusCode)
|
| 79 |
-
}
|
| 80 |
}
|
| 81 |
|
| 82 |
func Callback(w http.ResponseWriter, r *http.Request) {
|
|
|
|
| 34 |
|
| 35 |
func Login(w http.ResponseWriter, r *http.Request) {
|
| 36 |
log.Println("Login handler triggered")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
// Generate secure random values
|
| 39 |
+
state, _ := utils.SecureRandom(32)
|
| 40 |
+
codeVerifier, _ := utils.SecureRandom(64)
|
| 41 |
+
codeChallenge := crypto.SHA256Base64URL(codeVerifier)
|
| 42 |
+
|
| 43 |
+
// Set cookies
|
| 44 |
+
http.SetCookie(w, &http.Cookie{
|
| 45 |
+
Name: "oauth_state",
|
| 46 |
+
Value: state,
|
| 47 |
+
HttpOnly: true,
|
| 48 |
+
Secure: true,
|
| 49 |
+
SameSite: http.SameSiteNoneMode,
|
| 50 |
+
Path: "/",
|
| 51 |
+
MaxAge: 300,
|
| 52 |
+
})
|
| 53 |
+
log.Println("Cookies set 1")
|
| 54 |
+
|
| 55 |
+
http.SetCookie(w, &http.Cookie{
|
| 56 |
+
Name: "pkce_verifier",
|
| 57 |
+
Value: codeVerifier,
|
| 58 |
+
HttpOnly: true,
|
| 59 |
+
Secure: true,
|
| 60 |
+
SameSite: http.SameSiteNoneMode,
|
| 61 |
+
Path: "/",
|
| 62 |
+
MaxAge: 300,
|
| 63 |
+
})
|
| 64 |
+
log.Println("Cookies set 2")
|
| 65 |
+
|
| 66 |
+
// Construct the authorization URL for Keycloak
|
| 67 |
+
authURL := fmt.Sprintf("%s/realms/%s/protocol/openid-connect/auth?client_id=%s&response_type=code&scope=openid profile email&redirect_uri=%s&state=%s&code_challenge=%s&code_challenge_method=S256",
|
| 68 |
+
os.Getenv("KEYCLOAK_URL"),
|
| 69 |
+
os.Getenv("KEYCLOAK_REALM"),
|
| 70 |
+
os.Getenv("KEYCLOAK_CLIENT_ID"),
|
| 71 |
+
url.QueryEscape(os.Getenv("KEYCLOAK_REDIRECT_URL")),
|
| 72 |
+
state,
|
| 73 |
+
codeChallenge,
|
| 74 |
+
)
|
| 75 |
|
| 76 |
+
// Log the URL being redirected to
|
| 77 |
+
log.Printf("Redirecting to URL: %s", authURL)
|
| 78 |
|
| 79 |
+
// Perform the actual redirect
|
| 80 |
+
http.Redirect(w, r, authURL, http.StatusTemporaryRedirect)
|
|
|
|
|
|
|
| 81 |
}
|
| 82 |
|
| 83 |
func Callback(w http.ResponseWriter, r *http.Request) {
|