首页 > 解决方案 > 同一 Docker 网络中的 OAuth2 服务器和客户端

问题描述

我正在尝试在与 OAuth2 服务器相同的 Docker 网络中运行测试 OAuth2 客户端。这个想法是允许测试整个应用程序,而无需运行外部 OAuth2 客户端。

我遇到的问题是,传递给 OAuth2 客户端的 OAuth2 服务器端点仅在我的本地机器上的 Docker 网络之外可用(我使用简单的 nginx 代理来处理这个问题,见下文)。因此,当调用回调时客户端尝试获取令牌时,它无法解析主机名(因为客户端容器上下文中的“localhost”指的是该容器的本地网络,但我的浏览器上的“localhost”指 Docker 网络本身)。理想情况下,我想在开发过程中继续使用 nginx 作为更好的主机名的代理。

为了澄清,这是目前该过程的工作方式:

  1. 用户在浏览器中访问 app.localhost
  2. 用户被重定向到 auth.localhost 以尝试登录
  3. 用户输入他们的凭据,用户被重定向到回调
  4. 回调错误,因为它无法从 auth.localhost 正确获取访问令牌

这是我的 docker-compose,为简洁起见,删除了不相关的部分:

version: '3'
services:
  hydra:
    image: oryd/hydra:v1.0.0-beta.9
    depends_on:
      - hydra-migrate
    command:
      serve all --dangerous-force-http
    environment:
      - OAUTH2_ISSUER_URL=http://auth.localhost/
      - OAUTH2_CONSENT_URL=http://auth.localhost/consent
      - OAUTH2_LOGIN_URL=http://auth.localhost/auth/login
      - DATABASE_URL=postgres://hydra@postgres:5432/hydra?sslmode=disable
      - SYSTEM_SECRET=youReallyNeedToChangeThis
      - OAUTH2_SHARE_ERROR_DEBUG=1
      - OIDC_SUBJECT_TYPES_SUPPORTED=public,pairwise
      - OIDC_SUBJECT_TYPE_PAIRWISE_SALT=youReallyNeedToChangeThis

  nginx:
    image: nginx
    ports:
      - "80:80"
    volumes:
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - hydra
      - oauth2-test

  oauth2-test:
    build: https://github.com/chatlogs/oauth2-simple-client.git
    environment:
      - APP_URL=http://app.localhost
      - OAUTH2_URL=http://auth.localhost
      - OAUTH2_CLIENT_ID=test
      - OAUTH2_CLIENT_SECRET=secret
    depends_on:
      - hydra-create-client

这是我的 nginx.conf:

events {
  worker_connections  1024;
}

http {
  # Proxy ChatLogs Auth and Hydra OAuth server
  server {
    listen  80;
    server_name  auth.localhost;

    location ^~ / {
      proxy_pass  http://chatlogs-auth:3000;
    }

    location ^~ /oauth2 {
      proxy_pass  http://hydra:4444/oauth2;
    }
  }

  server {
    listen  80;
    server_name  app.localhost;

    location / {
      proxy_pass http://oauth2-test:3000;
    }
  }
}

这是 oauth2-test 显示的错误:

oauth2-test_1          | { Error: getaddrinfo ENOTFOUND auth.localhost auth.localhost:80
oauth2-test_1          |     at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)
oauth2-test_1          |   errno: 'ENOTFOUND',
oauth2-test_1          |   code: 'ENOTFOUND',
oauth2-test_1          |   syscall: 'getaddrinfo',
oauth2-test_1          |   hostname: 'auth.localhost',
oauth2-test_1          |   host: 'auth.localhost',
oauth2-test_1          |   port: 80,
oauth2-test_1          |   trace:
oauth2-test_1          |    [ { method: 'POST', url: 'http://auth.localhost/oauth2/token' } ],
oauth2-test_1          |   isBoom: true,
oauth2-test_1          |   isServer: true,
oauth2-test_1          |   data: null,
oauth2-test_1          |   output:
oauth2-test_1          |    { statusCode: 502,
oauth2-test_1          |      payload:
oauth2-test_1          |       { message:
oauth2-test_1          |          'Client request error: getaddrinfo ENOTFOUND auth.localhost auth.localhost:80',
oauth2-test_1          |         statusCode: 502,
oauth2-test_1          |         error: 'Bad Gateway' },
oauth2-test_1          |      headers: {} },
oauth2-test_1          |   reformat: [Function] }

任何帮助表示赞赏!

标签: dockeroauth-2.0

解决方案


将环境变量放在引号中。它正在寻找匹配服务。像http://Nginx。如果你把它放在引号中,它将在语法上是正确的。


推荐阅读