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:

image

As mentioned Oauth2 Proxy is giving us the error:

image

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

Most upvoted comments

For anyone out there coming from google with the related issue:

Error redeeming code during OAuth2 callback: token exchange failed: oauth2: cannot fetch token: 400 Bad Request
Response: {"error":"invalid_grant","error_description":"Code not valid"}

If you are using Keycloak, try disabling the “Use Refresh Token” in your client configuration. This has solved the issue in my case.

image

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:

[oauthproxy.go:823] Error redeeming code during OAuth2 callback: token exchange failed: oauth2: cannot fetch token: 401 Unauthorized
Response: {"error":"unauthorized_client","error_description":"Invalid client or Invalid client credentials"}

error in keycloak:

WARN  [org.keycloak.events] (executor-thread-30) type=CODE_TO_TOKEN_ERROR, realmId=xxxxxxx, clientId=oauth2_proxy, userId=null, ipAddress=192.168.x.x, error=invalid_client_credentials, grant_type=authorization_code

Still running into the same issue with github. Successful auth still causes error.

[2022/08/23 13:06:36] [oauthproxy.go:768] Error redeeming code during OAuth2 callback: error performing request: Post "https://github.com/login/oauth/access_token": dial tcp: i/o timeout
10.244.4.173:40858 - [redacted] - - [2022/08/23 13:06:06] [redacted] GET - "/oauth2/callback?code=[redacted]&state=[redacted]is%3A%2F" HTTP/1.1 "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36" 500 2837 30.001
10.244.4.173:40858 - [redacted] - - [2022/08/23 13:06:36] [redacted] GET - "/oauth2/auth" HTTP/1.1 "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36" 401 13 0.000

From the browser console a code has been returned to OAuth2 with any error. 401 doesn’t come from the identity provider.