Skip to content

Commit 6deb12c

Browse files
Adds option to add state postfix
1 parent b78c391 commit 6deb12c

File tree

5 files changed

+33
-14
lines changed

5 files changed

+33
-14
lines changed

.github/workflows/ci.yaml

+14-1
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,21 @@ jobs:
5252
5353
docker:
5454
runs-on: ubuntu-20.04
55+
permissions:
56+
contents: 'read'
57+
id-token: 'write'
58+
timeout-minutes: 30
5559
steps:
56-
5760
- name: Check out code
5861
uses: actions/checkout@v3
5962

63+
- name: Setup Google Cloud Auth
64+
uses: 'google-github-actions/auth@v2'
65+
with:
66+
project_id: 'meisterlabs-staging'
67+
workload_identity_provider: projects/930405717829/locations/global/workloadIdentityPools/github/providers/github-actions-provider
68+
service_account: 'meister-artifact-registry@meisterlabs-staging.iam.gserviceaccount.com'
69+
6070
- name: Set up QEMU
6171
uses: docker/setup-qemu-action@v2
6272

@@ -66,4 +76,7 @@ jobs:
6676

6777
- name: Docker Build
6878
run: |
79+
export IMAGE_TAG=$(echo 'refs/heads/24-04-additional-state' | sed 's/refs\/heads\///')
80+
echo $IMAGE_TAG
6981
make docker
82+
make docker-push

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ GOLANGCILINT ?= golangci-lint
44
BINARY := oauth2-proxy
55
VERSION ?= $(shell git describe --always --dirty --tags 2>/dev/null || echo "undefined")
66
# Allow to override image registry.
7-
REGISTRY ?= quay.io/oauth2-proxy
7+
REGISTRY=europe-west1-docker.pkg.dev/meisterlabs-staging/oauth2-proxy
88
.NOTPARALLEL:
99

1010
GO_MAJOR_VERSION = $(shell $(GO) version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f1)
@@ -61,7 +61,7 @@ docker-all: docker
6161

6262
.PHONY: docker-push
6363
docker-push:
64-
$(DOCKER_BUILDX_PUSH_X_PLATFORM) -t $(REGISTRY)/oauth2-proxy:latest -t $(REGISTRY)/oauth2-proxy:${VERSION} .
64+
$(DOCKER_BUILDX_PUSH_X_PLATFORM) -t $(REGISTRY)/oauth2-proxy:${IMAGE_TAG} -t $(REGISTRY)/oauth2-proxy:${VERSION} .
6565

6666
.PHONY: docker-push-all
6767
docker-push-all: docker-push

oauthproxy.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ type OAuthProxy struct {
9898
forceJSONErrors bool
9999
realClientIPParser ipapi.RealClientIPParser
100100
trustedIPs *ip.NetSet
101+
statePostfix string
101102

102103
sessionChain alice.Chain
103104
headersChain alice.Chain
@@ -235,6 +236,7 @@ func NewOAuthProxy(opts *options.Options, validator func(string) bool) (*OAuthPr
235236
upstreamProxy: upstreamProxy,
236237
redirectValidator: redirectValidator,
237238
appDirector: appDirector,
239+
statePostfix: opts.StatePostfix,
238240
}
239241
p.buildServeMux(opts.ProxyPrefix)
240242

@@ -787,7 +789,7 @@ func (p *OAuthProxy) doOAuthStart(rw http.ResponseWriter, req *http.Request, ove
787789
callbackRedirect := p.getOAuthRedirectURI(req)
788790
loginURL := p.provider.GetLoginURL(
789791
callbackRedirect,
790-
encodeState(csrf.HashOAuthState(), appRedirect),
792+
encodeState(csrf.HashOAuthState(), appRedirect, p.statePostfix),
791793
csrf.HashOIDCNonce(),
792794
extraParams,
793795
)
@@ -845,7 +847,8 @@ func (p *OAuthProxy) OAuthCallback(rw http.ResponseWriter, req *http.Request) {
845847

846848
csrf.ClearCookie(rw, req)
847849

848-
nonce, appRedirect, err := decodeState(req)
850+
nonce, appRedirect, _, err := decodeState(req)
851+
849852
if err != nil {
850853
logger.Errorf("Error while parsing OAuth2 state: %v", err)
851854
p.ErrorPage(rw, req, http.StatusInternalServerError, err.Error())
@@ -1185,18 +1188,20 @@ func checkAllowedEmails(req *http.Request, s *sessionsapi.SessionState) bool {
11851188

11861189
// encodedState builds the OAuth state param out of our nonce and
11871190
// original application redirect
1188-
func encodeState(nonce string, redirect string) string {
1189-
return fmt.Sprintf("%v:%v", nonce, redirect)
1191+
func encodeState(nonce string, redirect string, additional string) string {
1192+
return fmt.Sprintf("%v:%v:%v", nonce, redirect, additional)
11901193
}
11911194

11921195
// decodeState splits the reflected OAuth state response back into
11931196
// the nonce and original application redirect
1194-
func decodeState(req *http.Request) (string, string, error) {
1195-
state := strings.SplitN(req.Form.Get("state"), ":", 2)
1196-
if len(state) != 2 {
1197-
return "", "", errors.New("invalid length")
1197+
func decodeState(req *http.Request) (string, string, string, error) {
1198+
state := strings.SplitN(req.Form.Get("state"), ":", 3)
1199+
1200+
if len(state) != 3 {
1201+
return "", "", "", errors.New("invalid length")
11981202
}
1199-
return state[0], state[1], nil
1203+
1204+
return state[0], state[1], state[2], nil
12001205
}
12011206

12021207
// addHeadersForProxying adds the appropriate headers the request / response for proxying

oauthproxy_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ func (patTest *PassAccessTokenTest) getCallbackEndpoint() (httpCode int, cookie
413413
http.MethodGet,
414414
fmt.Sprintf(
415415
"/oauth2/callback?code=callback_code&state=%s",
416-
encodeState(csrf.HashOAuthState(), "%2F"),
416+
encodeState(csrf.HashOAuthState(), "", "%2F"),
417417
),
418418
strings.NewReader(""),
419419
)

pkg/apis/options/options.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Options struct {
2727
TrustedIPs []string `flag:"trusted-ip" cfg:"trusted_ips"`
2828
ForceHTTPS bool `flag:"force-https" cfg:"force_https"`
2929
RawRedirectURL string `flag:"redirect-url" cfg:"redirect_url"`
30+
StatePostfix string `flag:"state-postfix" cfg:"state_postfix"`
3031

3132
AuthenticatedEmailsFile string `flag:"authenticated-emails-file" cfg:"authenticated_emails_file"`
3233
EmailDomains []string `flag:"email-domain" cfg:"email_domains"`
@@ -151,7 +152,7 @@ func NewFlagSet() *pflag.FlagSet {
151152
flagSet.Int("redis-connection-idle-timeout", 0, "Redis connection idle timeout seconds, if Redis timeout option is non-zero, the --redis-connection-idle-timeout must be less then Redis timeout option")
152153
flagSet.String("signature-key", "", "GAP-Signature request signature key (algorithm:secretkey)")
153154
flagSet.Bool("gcp-healthchecks", false, "Enable GCP/GKE healthcheck endpoints")
154-
155+
flagSet.String("state-postfix", "", "state_postifx")
155156
flagSet.AddFlagSet(cookieFlagSet())
156157
flagSet.AddFlagSet(loggingFlagSet())
157158
flagSet.AddFlagSet(templatesFlagSet())

0 commit comments

Comments
 (0)