forked from higress-group/oauth2-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
134 additions
and
34 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,83 @@ | ||
package providers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/Jing-ze/oauth2-proxy/pkg/util" | ||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper" | ||
) | ||
|
||
// stripToken is a helper function to obfuscate "access_token" | ||
// query parameters | ||
func stripToken(endpoint string) string { | ||
return stripParam("access_token", endpoint) | ||
} | ||
|
||
// stripParam generalizes the obfuscation of a particular | ||
// query parameter - typically 'access_token' or 'client_secret' | ||
// The parameter's second half is replaced by '...' and returned | ||
// as part of the encoded query parameters. | ||
// If the target parameter isn't found, the endpoint is returned | ||
// unmodified. | ||
func stripParam(param, endpoint string) string { | ||
u, err := url.Parse(endpoint) | ||
if err != nil { | ||
util.Logger.Errorf("error attempting to strip %s: %s", param, err) | ||
return endpoint | ||
} | ||
|
||
if u.RawQuery != "" { | ||
values, err := url.ParseQuery(u.RawQuery) | ||
if err != nil { | ||
util.Logger.Errorf("error attempting to strip %s: %s", param, err) | ||
return u.String() | ||
} | ||
|
||
if val := values.Get(param); val != "" { | ||
values.Set(param, val[:(len(val)/2)]+"...") | ||
u.RawQuery = values.Encode() | ||
return u.String() | ||
} | ||
} | ||
|
||
return endpoint | ||
} | ||
|
||
// validateToken returns true if token is valid | ||
func validateToken(ctx context.Context, p Provider, accessToken string, header http.Header, client wrapper.HttpClient, callback func(args ...interface{}), timeout uint32) (bool, bool) { | ||
if accessToken == "" || p.Data().ValidateURL == nil || p.Data().ValidateURL.String() == "" { | ||
return false, false | ||
} | ||
endpoint := p.Data().ValidateURL.String() | ||
if len(header) == 0 { | ||
params := url.Values{"access_token": {accessToken}} | ||
if hasQueryParams(endpoint) { | ||
endpoint = endpoint + "&" + params.Encode() | ||
} else { | ||
endpoint = endpoint + "?" + params.Encode() | ||
} | ||
} | ||
|
||
client.Get(endpoint, headers, []byte{}, func(statusCode int, responseHeaders http.Header, responseBody []byte) { | ||
util.Logger.Debugf("%d GET %s %s", statusCode, stripToken(endpoint), responseBody) | ||
if statusCode == 200 { | ||
callback() | ||
} else { | ||
util.SendError(fmt.Sprintf("token validation request failed: status %d - %s", result.StatusCode(), result.Body()), nil, http.StatusInternalServerError) | ||
} | ||
}, timeout) | ||
return true, true | ||
} | ||
|
||
// hasQueryParams check if URL has query parameters | ||
func hasQueryParams(endpoint string) bool { | ||
endpointURL, err := url.Parse(endpoint) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return len(endpointURL.RawQuery) != 0 | ||
} |
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