Skip to content

Commit

Permalink
add /logout path to delete cookies (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo-ruth authored Nov 30, 2021
1 parent 03d4eb0 commit 4147247
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
19 changes: 19 additions & 0 deletions proxy/logoutHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package proxy

import (
_ "embed" //embed web resources for login page
"fmt"
"net/http"
)

// logoutGetHandler delete auth cookies
func logoutGetHandler(w http.ResponseWriter, r *http.Request) {

// Call token cookie deletion helper
err := deleteTokenCookie(w, r)
if err != nil {
fmt.Printf("deleting token cookie: %s\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
1 change: 1 addition & 0 deletions proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func Server(loginURL, guestClusterName, proxyURL string) error {
r.HandleFunc("/*", proxyHandler(proxyURL))
r.Get("/login", loginGetHandler)
r.Post("/login", loginPostHandler(loginURL, guestClusterName))
r.Get("/logout", logoutGetHandler)

// Serve requests
return http.ListenAndServe(":8080", r)
Expand Down
22 changes: 22 additions & 0 deletions proxy/tokenHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -55,6 +56,27 @@ func getTokenCookie(r *http.Request) (string, error) {
return token, nil
}

func deleteTokenCookie(w http.ResponseWriter, r *http.Request) error {

// Delete proxy_auth_token_parts cookie
http.SetCookie(w, &http.Cookie{Name: "proxy_auth_token_parts", Value: "", MaxAge: 0})

// Compile regex to extract token parts cookies
cookieRegex, err := regexp.Compile("proxy_auth_token_.*")
if err != nil {
return fmt.Errorf("compiling proxy auth token regex: %s\n", err)
}

// List token parts cookies
for _, cookie := range r.Cookies() {
if cookieRegex.MatchString(cookie.Name) {
http.SetCookie(w, &http.Cookie{Name: cookie.Name, Value: "", MaxAge: 0})
}
}

return nil
}

// tokenExpired checks if JWT token is expired
func tokenExpired(rawToken string) (bool, error) {
// Split Header/Payload/Signature parts of JWT token
Expand Down

0 comments on commit 4147247

Please sign in to comment.