1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-06-23 00:40:46 +02:00
Files
.github
contrib
docs
pkg
providers
auth_test.go
azure.go
azure_test.go
bitbucket.go
bitbucket_test.go
digitalocean.go
digitalocean_test.go
facebook.go
github.go
github_test.go
gitlab.go
gitlab_test.go
google.go
google_test.go
internal_util.go
internal_util_test.go
keycloak.go
keycloak_test.go
linkedin.go
linkedin_test.go
logingov.go
logingov_test.go
nextcloud.go
nextcloud_test.go
oidc.go
oidc_test.go
provider_data.go
provider_default.go
provider_default_test.go
providers.go
.dockerignore
.gitignore
.golangci.yml
.travis.yml
CHANGELOG.md
CONTRIBUTING.md
Dockerfile
Dockerfile.arm64
Dockerfile.armv6
LICENSE
MAINTAINERS
Makefile
README.md
RELEASE.md
configure
dist.sh
env_options.go
env_options_test.go
go.mod
go.sum
htpasswd.go
htpasswd_test.go
http.go
http_test.go
logging_handler.go
logging_handler_test.go
main.go
oauthproxy.go
oauthproxy_test.go
options.go
options_test.go
string_array.go
templates.go
templates_test.go
validator.go
validator_test.go
version.go
watcher.go
watcher_unsupported.go
oauth2-proxy/providers/internal_util.go

75 lines
2.0 KiB
Go
Raw Normal View History

2015-05-12 21:48:13 -04:00
package providers
import (
"io/ioutil"
2015-05-12 21:48:13 -04:00
"net/http"
"net/url"
2019-05-24 17:08:48 +01:00
"github.com/pusher/oauth2_proxy/pkg/logger"
2019-05-24 16:55:12 +01:00
"github.com/pusher/oauth2_proxy/pkg/requests"
2015-05-12 21:48:13 -04:00
)
// 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 {
logger.Printf("error attempting to strip %s: %s", param, err)
return endpoint
}
if u.RawQuery != "" {
values, err := url.ParseQuery(u.RawQuery)
if err != nil {
logger.Printf("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
2018-11-29 14:26:41 +00:00
func validateToken(p Provider, accessToken string, header http.Header) bool {
if accessToken == "" || p.Data().ValidateURL == nil || p.Data().ValidateURL.String() == "" {
2015-05-12 21:48:13 -04:00
return false
}
endpoint := p.Data().ValidateURL.String()
2015-05-12 21:48:13 -04:00
if len(header) == 0 {
2018-11-29 14:26:41 +00:00
params := url.Values{"access_token": {accessToken}}
endpoint = endpoint + "?" + params.Encode()
2015-05-12 21:48:13 -04:00
}
2019-05-24 16:55:12 +01:00
resp, err := requests.RequestUnparsedResponse(endpoint, header)
if err != nil {
logger.Printf("GET %s", stripToken(endpoint))
logger.Printf("token validation request failed: %s", err)
2015-05-12 21:48:13 -04:00
return false
}
body, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
logger.Printf("%d GET %s %s", resp.StatusCode, stripToken(endpoint), body)
if resp.StatusCode == 200 {
return true
}
logger.Printf("token validation request failed: status %d - %s", resp.StatusCode, body)
return false
2015-05-12 21:48:13 -04:00
}