oauth2-proxy: Error redeeming code during OAuth2 callback: token exchange failed: oauth2: cannot fetch token: 401
Expected Behavior
Use of Oauth2Proxy to initiate and finalize OpenID Authentication flow to access Web resources within Kubernetes using NGINX Ingress Controller.
Current Behavior
Authentication flow fails during callback call with error:
[oauthproxy.go:723] Error redeeming code during OAuth2 callback: token exchange failed: oauth2: cannot fetch token: 401
The following is observed in the browser:

As mentioned Oauth2 Proxy is giving us the error:

Steps to Reproduce (for bugs)
Using multiple versions of Oauth2Proxy, but the lates tested was 7.2.0, runing on Kubernetes 1.19.13 with the ingress nginx controller 1.1.0. I have the following deployment configuration for Oauth2 Proxy:
apiVersion: apps/v1
kind: Deployment
metadata:
name: oauth2-proxy
namespace: admin2
labels:
app.kubernetes.io/name: cxm-dex-oauth
cxm.iac: admin
k8s-app: oauth2-proxy
spec:
replicas: 1
selector:
matchLabels:
k8s-app: oauth2-proxy
template:
metadata:
labels:
cxm.iac: admin
k8s-app: oauth2-proxy
spec:
containers:
- name: oauth2-proxy
image: 'quay.io/oauth2-proxy/oauth2-proxy:v7.2.0'
args:
- '--cookie-domain=".domain.com"'
- '--whitelist-domain=".domain.com"'
- '--provider=oidc'
- '--provider-display-name=''oauth2-proxy'''
- '--email-domain=*'
- '--client-id=clientid'
- '--client-secret=''secret'''
- '--errors-to-info-log=true'
- '--http-address=0.0.0.0:4180'
- '--standard-logging=true'
- '--auth-logging=true'
- '--request-logging=true'
- '--upstream=static://'
- '--redirect-url=https://prometheus.domain.com/oauth2/callback'
- '--oidc-issuer-url=https://idp.com'
- '--cookie-secret=secret'
ports:
- containerPort: 4180
protocol: TCP
imagePullPolicy: IfNotPresent
restartPolicy: Always
imagePullSecrets:
- name: docker-registry
The ingress artifacts are defined as follow:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: prometheus-oauth2
namespace: admin2
annotations:
cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
ingressClassName: nginx
tls:
- hosts:
- "*.domain.com"
secretName: tls
rules:
- host: prometheus.domain.com
http:
paths:
- path: /oauth2
pathType: Prefix
backend:
service:
name: oauthproxy-service
port:
number: 4180
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: prometheus
namespace: observability
annotations:
cert-manager.io/cluster-issuer: letsencrypt-prod
nginx.ingress.kubernetes.io/auth-url: "https://$host/oauth2/auth"
nginx.ingress.kubernetes.io/auth-signin: "https://$host/oauth2/start?rd=$escaped_request_uri"
nginx.ingress.kubernetes.io/auth-response-headers: Authorization
nginx.ingress.kubernetes.io/proxy-buffer-size: "64k"
nginx.ingress.kubernetes.io/configuration-snippet: |
auth_request_set $name_upstream_1 $upstream_cookie__oauth2_proxy_1;
access_by_lua_block {
if ngx.var.name_upstream_1 ~= "" then
ngx.header["Set-Cookie"] = "_oauth2_proxy_1=" .. ngx.var.name_upstream_1 .. ngx.var.auth_cookie:match("(; .*)")
end
}
spec:
ingressClassName: nginx
tls:
- hosts:
- "*.domain.com"
secretName: tls
rules:
- host: prometheus.domain.com
http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
serviceName: prometheus-k8s
servicePort: 9090
We are using SAP IaS to provide the OpenID services, more information about it here
- Version used:
Oauth2Proxy 7.2.0 Kubernetes 1.19.13 Nginx controller 1.1.0
Thanks,
Zareh
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 16 (2 by maintainers)
Commits related to this issue
- fix: Control gateway-api Listener with global.enableGatewayAPI in Helm (#1461) — committed to Jing-ze/oauth2-proxy by hanxiantao 2 years ago
For anyone out there coming from google with the related issue:
If you are using Keycloak, try disabling the “Use Refresh Token” in your client configuration. This has solved the issue in my case.
I’ve fixed my problem by setting these parameters in oauth –cookie-csrf-per-request=true –cookie-csrf-expire=5m
I am having this issue too, successful login but no token. I turned off
Use Refresh Tokens… this has been driving me crazy for days. Anyone have a solution yet?error in oauth2:
error in keycloak:
Still running into the same issue with github. Successful auth still causes error.
From the browser console a code has been returned to OAuth2 with any error. 401 doesn’t come from the identity provider.