forked from oauth2-proxy/oauth2-proxy
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description Adding support for `/sign_out_all_sessions`. /sign_out_all_sessions endpoint will remove the current session and make a POST request to IAM, configured via `OAUTH2_PROXY_BACKEND_LOGOUT_ALL_SESSIONS_URL` env, to invalidate all the tokens and sessions. This will not invalidate other user sessions. Once the tokens and sessions are invalidated, after the refresh token period defined on the `OAUTH2_PROXY_COOKIE_REFRESH` env, OAuth will fail to refresh the access token and clear that session. related to: - philips-internal/pics-foundation-envoy#105 - philips-internal/pics#3006 [AB#1579962](https://tfsemea1.ta.philips.com/tfs/TPC_Region11/0839b845-d626-4499-94ae-563a86a88d0a/_workitems/edit/1579962) ## Motivation and Context Possibility for signing out on all devices. ## How Has This Been Tested? Integrated locally with PICS by running binary. Docs [here](https://github.com/philips-internal/pics/blob/main/src/services/Oauth2Proxy/docs/development.md). ## Checklist: - [x] Add OAUTH2_PROXY_BACKEND_LOGOUT_ALL_SESSIONS_URL env - [x] Add /sign_out_all_sessions endpoint - [x] Remove other user sessions when tokens are invalid
- Loading branch information
Showing
10 changed files
with
174 additions
and
26 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
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,59 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger" | ||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/requests" | ||
) | ||
|
||
const ( | ||
picsSignOutAllDevicesPath = "/sign_out_all_sessions" | ||
) | ||
|
||
func PicsSignOutAllSessions(backendLogoutAllSessionsURL string, introspectClaims string, accessToken string) (resp requests.Result, err error) { | ||
userID, err := getUserID(introspectClaims) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting userID from instrospect claims: %v", err) | ||
} | ||
|
||
backendLogoutURL := strings.ReplaceAll(backendLogoutAllSessionsURL, "{user_id}", userID) | ||
resp = requests.New(backendLogoutURL). | ||
WithMethod("POST"). | ||
SetHeader("Authorization", "Bearer "+accessToken). | ||
SetHeader("API-Version", "1"). | ||
SetHeader("Accept", "application/json"). | ||
Do() | ||
|
||
if resp.Error() != nil { | ||
return nil, fmt.Errorf("error logging out from IAM: %v", err) | ||
} | ||
|
||
return resp, err | ||
} | ||
|
||
func getUserID(introspectClaims string) (string, error) { | ||
decodedClaims, err := base64.StdEncoding.DecodeString(introspectClaims) | ||
if err != nil { | ||
logger.Errorf("error decoding claims: %v", err) | ||
return "", err | ||
} | ||
|
||
var claims map[string]interface{} | ||
err = json.Unmarshal(decodedClaims, &claims) | ||
if err != nil { | ||
logger.Errorf("error unmarshalling claims: %v", err) | ||
return "", err | ||
} | ||
|
||
userID, ok := claims["sub"].(string) | ||
if !ok { | ||
logger.Errorf("error extracting 'sub' from claims") | ||
return "", err | ||
} | ||
|
||
return userID, nil | ||
} |
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,55 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func createIntrospectClaims() string { | ||
claims := map[string]interface{}{ | ||
"sub": "1234567890", | ||
} | ||
claimsBytes, err := json.Marshal(claims) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
return base64.StdEncoding.EncodeToString(claimsBytes) | ||
} | ||
|
||
func Test_PicsSignOutAllSessionsReturnsErrorWhenUserIDIsNotFound(t *testing.T) { | ||
_, err := PicsSignOutAllSessions("http://localhost:8080/test", "", "") | ||
|
||
assert.Error(t, err) | ||
} | ||
|
||
func Test_getUserID(t *testing.T) { | ||
introspectClaims := createIntrospectClaims() | ||
userID, err := getUserID(introspectClaims) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, "1234567890", userID) | ||
} | ||
|
||
func Test_PicsSignOutAllSessionsReturns200Ok(t *testing.T) { | ||
introspectClaims := createIntrospectClaims() | ||
accessToken := "validAccessToken" | ||
|
||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, "Bearer "+accessToken, r.Header.Get("Authorization")) | ||
assert.Equal(t, "1", r.Header.Get("API-Version")) | ||
assert.Equal(t, "application/json", r.Header.Get("Accept")) | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
defer server.Close() | ||
|
||
resp, err := PicsSignOutAllSessions(server.URL+"/{user_id}", introspectClaims, accessToken) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode()) | ||
} |
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