learnifymedhub commited on
Commit
6301200
·
verified ·
1 Parent(s): 458780b

Update internal/auth/handler/auth_handler.go

Browse files
Files changed (1) hide show
  1. internal/auth/handler/auth_handler.go +41 -40
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
- http.SetCookie(w, &http.Cookie{
52
- Name: "pkce_verifier",
53
- Value: codeVerifier,
54
- HttpOnly: true,
55
- Secure: true,
56
- SameSite: http.SameSiteNoneMode,
57
- Path: "/",
58
- MaxAge: 300,
59
- })
60
- 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",
61
- os.Getenv("KEYCLOAK_URL"),
62
- os.Getenv("KEYCLOAK_REALM"),
63
- os.Getenv("KEYCLOAK_CLIENT_ID"),
64
- url.QueryEscape(os.Getenv("KEYCLOAK_REDIRECT_URL")),
65
- state,
66
- codeChallenge,
67
- )
68
-
69
- http.Redirect(w, r, authURL, http.StatusTemporaryRedirect)
70
-
71
- log.Printf("Redirecting to URL: %s", authURL)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
- // Perform the redirect
74
- http.Redirect(loggingRW, r, authURL, http.StatusTemporaryRedirect)
75
 
76
- // Check if redirect status code was 3xx (redirection)
77
- if loggingRW.StatusCode != http.StatusTemporaryRedirect {
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) {