forked from oauth2-proxy/oauth2-proxy
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SSO - Refresh hsdpamcookie to allow signle sign on between other Phil…
…ips products (#49) <!--- Provide a general summary of your changes in the Title above --> ## Description Users with different tabs, applications etc will now have their cookie refreshed so when they open other tabs that have other applications, these applications will now be authenticated. Example: I'm doing logging for 1 hour, when I open reporting, hsp reporting will ask for login due to expired cookie. Now this is no longer happer https://www.hsdp.io/documentation/identity-and-access-management-iam/api-documents/resource-reference-api/oauth2-api#/Session%20Refresh/refreshSessionUsingGET New settings introduced: `OAUTH2_PROXY_OIDC_ENABLE_COOKIE_REFRESH` default false `OAUTH2_PROXY_OIDC_COOKIE_REFRESH_NAME` default 'hsdpamcookie' ## Motivation and Context <!--- Why is this change required? What problem does it solve? --> <!--- If it fixes an open issue, please link to the issue here. --> ## How Has This Been Tested? <!--- Please describe in detail how you tested your changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> Tested locally ## Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [ ] My change requires a change to the documentation or CHANGELOG. - [ ] I have updated the documentation/CHANGELOG accordingly. - [ ] I have created a feature (non-master) branch for my PR.
- Loading branch information
Showing
11 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,5 @@ _testmain.go | |
# vi Dockerfile.dev | ||
# docker build -f Dockerfile.dev . | ||
Dockerfile.dev | ||
|
||
obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package middleware | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/justinas/alice" | ||
middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware" | ||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger" | ||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/requests" | ||
) | ||
|
||
type CookieRefreshOptions struct { | ||
IssuerURL string | ||
CookieRefreshName string | ||
} | ||
|
||
func NewCookieRefresh(opts *CookieRefreshOptions) alice.Constructor { | ||
cr := &cookieRefresh{ | ||
HTTPClient: &http.Client{}, | ||
IssuerURL: opts.IssuerURL, | ||
CookieRefreshName: opts.CookieRefreshName, | ||
} | ||
return cr.refreshCookie | ||
} | ||
|
||
type cookieRefresh struct { | ||
HTTPClient *http.Client | ||
IssuerURL string | ||
CookieRefreshName string | ||
} | ||
|
||
func (cr *cookieRefresh) refreshCookie(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { | ||
scope := middlewareapi.GetRequestScope(req) | ||
if scope.Session == nil || !scope.Session.SessionJustRefreshed { | ||
next.ServeHTTP(rw, req) | ||
return | ||
} | ||
|
||
cookie, err := req.Cookie(cr.CookieRefreshName) | ||
if err != nil { | ||
logger.Errorf("SSO Cookie Refresher - Could find '%s' cookie in the request: %v", cr.CookieRefreshName, err) | ||
return | ||
} | ||
resp := requests.New(fmt.Sprintf("%s/session/refresh", cr.IssuerURL)). | ||
WithContext(req.Context()). | ||
WithMethod("GET"). | ||
SetHeader("api-version", "1"). | ||
SetHeader("Cookie", fmt.Sprintf("%s=%s", cr.CookieRefreshName, cookie.Value)). | ||
Do() | ||
|
||
if resp.StatusCode() != http.StatusNoContent { | ||
bodyString := string(resp.Body()) | ||
logger.Errorf("SSO Cookie Refresher - Could not refresh the '%s' cookie due to status and content: %v - %v", cr.CookieRefreshName, resp.StatusCode(), bodyString) | ||
return | ||
} | ||
|
||
logger.Printf("SSO Cookie Refresher - Cookie '%s' refreshed", cr.CookieRefreshName) | ||
next.ServeHTTP(rw, req) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters