首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nginx和Oauth2-proxy:在Google登录后,重定向回Oauth登录页面

Nginx和Oauth2-proxy:在Google登录后,重定向回Oauth登录页面
EN

Stack Overflow用户
提问于 2020-12-16 06:58:45
回答 3查看 3.6K关注 0票数 5

我正在尝试使用Oauth2-proxy作为我的带有Google auth的网站的网关。此时会出现Oauth登录页面,您可以单击"Sign In“进入Google登录页面,但登录后会直接重定向回Oauth登录页面。我怀疑这与cookies有关,但我不知道我错在哪里。

NGINX配置:

代码语言:javascript
复制
server {
  listen 443;
  server_name my.website.com;

  ssl on;
  ssl_certificate /etc/ssl/my.website.com.pem;
  ssl_certificate_key /etc/ssl/my.website.com.key;

  location /oauth2/ {
    proxy_pass       http://127.0.0.1:4180;
    proxy_set_header Host                    $host;
    proxy_set_header X-Real-IP               $remote_addr;
    proxy_set_header X-Scheme                $scheme;
    proxy_set_header X-Auth-Request-Redirect $request_uri;
  }
  location = /oauth2/auth {
    proxy_pass       http://127.0.0.1:4180;
    proxy_set_header Host             $host;
    proxy_set_header X-Real-IP        $remote_addr;
    proxy_set_header X-Scheme         $scheme;
    proxy_set_header Content-Length   "";
    proxy_pass_request_body           off;
  }

  location / {
    auth_request /oauth2/auth;
    error_page 401 = /oauth2/sign_in;

    # pass information via X-User and X-Email headers to backend,
    # requires running with --set-xauthrequest flag
    auth_request_set $user   $upstream_http_x_auth_request_user;
    auth_request_set $email  $upstream_http_x_auth_request_email;
    proxy_set_header X-User  $user;
    proxy_set_header X-Email $email;

    # if you enabled --pass-access-token, this will pass the token to the backend
    auth_request_set $token  $upstream_http_x_auth_request_access_token;
    proxy_set_header X-Access-Token $token;

    # if you enabled --cookie-refresh, this is needed for it to work with auth_request
    auth_request_set $auth_cookie $upstream_http_set_cookie;
    add_header Set-Cookie $auth_cookie;

    # When using the --set-authorization-header flag, some provider's cookies can exceed the 4kb
    # limit and so the OAuth2 Proxy splits these into multiple parts.
    # Nginx normally only copies the first `Set-Cookie` header from the auth_request to the respon$
    # so if your cookies are larger than 4kb, you will need to extract additional cookies manually.
    auth_request_set $auth_cookie_name_upstream_1 $upstream_cookie_auth_cookie_name_1;

    # Extract the Cookie attributes from the first Set-Cookie header and append them
    # to the second part ($upstream_cookie_* variables only contain the raw cookie content)
    if ($auth_cookie ~* "(; .*)") {
        set $auth_cookie_name_0 $auth_cookie;
        set $auth_cookie_name_1 "auth_cookie_name_1=$auth_cookie_name_upstream_1$1";
    }

    # Send both Set-Cookie headers now if there was a second part
    if ($auth_cookie_name_upstream_1) {
        add_header Set-Cookie $auth_cookie_name_0;
        add_header Set-Cookie $auth_cookie_name_1;
    }
    
    proxy_set_header   X-Forwarded-For $remote_addr;
    proxy_set_header   Host $http_host;
    proxy_pass         "http://127.0.0.1:8888"; # This is where my web server is hosted
  }
}

OAuth2-代理配置:

代码语言:javascript
复制
cookie_secret="" # Filled in with a base64 string
provider="google"
email_domains="*"
client_secret="" # Filled in with Google auth client secret
client_id="" # Filled in with Google auth client ID
cookie_secure="false" # No idea what to set this as, tried both false and true, same result
redirect_url="https://my.website.com/oauth2/auth/"
upstreams="http://127.0.0.1:8888/" # My website server

Oauth2-proxy启动命令:

代码语言:javascript
复制
~/oauth2-proxy-v6.1.1.linux-amd64/oauth2-proxy --config=/home/username/work/src/github.com/oauth2-proxy/oauth2-proxy/contrib/local-environment/oauth2-proxy.cfg

任何想法都将不胜感激!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-03-14 14:04:58

根据documentation -- Redirect -url是OAuth重定向URL,应设置为支持的回调端点,例如"https://internalapp.yourcompany.com/oauth2/callback"

/oauth2/callback在OAuth周期结束时使用的endpoint。oauth应用程序将被配置为回调url。

实际生成的url有一个state参数,其中包括要返回的原始URL。据我所知,回调从状态中检索原始的(受代理保护的) url,并将浏览器重定向到它。

票数 2
EN

Stack Overflow用户

发布于 2020-12-22 22:03:51

您将返回到oauth页面,因为您已经(在OAUT2-proxy配置中)设置了redirect_url="https://my.website.com/oauth2/auth/"

例如,将其更改为仅https://my.website.com/,即可登录到您的网站主屏幕。

票数 0
EN

Stack Overflow用户

发布于 2021-03-14 23:58:26

我接受的答案是正确的,但我也不得不做一些cookie更改,并认为我应该只发布我的最终工作代码。

我能够让它在这些变化中工作:

location /oauth2/

proxy_set_header X-Auth-Request-Redirect "https://my.website.com";

location /

auth_request_set $auth_cookie $upstream_http_set_cookie;

在Oauth2-proxy配置中(附加或替换行):

代码语言:javascript
复制
redirect_url = "https://my.website.com/oauth2/callback"
set_xauthrequest = true
upstreams = ["file:///dev/null"]
cookie_domains = [".website.com"]
cookie_secure = false
cookie_samesite = "lax"

编辑:配置文件的最终内容:

NGINX配置:

代码语言:javascript
复制
server {
  listen 443;
  server_name my.website.com;

  ssl on;
  ssl_certificate /etc/ssl/my.website.com.pem;
  ssl_certificate_key /etc/ssl/my.website.com.key;

  location /oauth2/ {
    proxy_pass       http://127.0.0.1:4180;
    proxy_set_header Host                    $host;
    proxy_set_header X-Real-IP               $remote_addr;
    proxy_set_header X-Scheme                $scheme;
    proxy_set_header X-Auth-Request-Redirect "https://my.website.com";
  }

  location = /oauth2/auth {
    proxy_pass       http://127.0.0.1:4180;
    proxy_set_header Host             $host;
    proxy_set_header X-Real-IP        $remote_addr;
    proxy_set_header X-Scheme         $scheme;
    proxy_set_header Content-Length   "";
    proxy_pass_request_body           off;
  }

  location / {
    auth_request /oauth2/auth;
    error_page 401 = /oauth2/sign_in;

    auth_request_set $user   $upstream_http_x_auth_request_user;
    auth_request_set $email  $upstream_http_x_auth_request_email;
    proxy_set_header X-User  $user;
    proxy_set_header X-Email $email;

    auth_request_set $token  $upstream_http_x_auth_request_access_token;
    proxy_set_header X-Access-Token $token;

    auth_request_set $auth_cookie $upstream_http_set_cookie;
    add_header Set-Cookie $auth_cookie;

    auth_request_set $auth_cookie_name_upstream_1 $upstream_cookie_auth_cookie_name_1;

    if ($auth_cookie ~* "(; .*)") {
        set $auth_cookie_name_0 $auth_cookie;
        set $auth_cookie_name_1 "auth_cookie_name_1=$auth_cookie_name_upstream_1$1";
    }

    if ($auth_cookie_name_upstream_1) {
        add_header Set-Cookie $auth_cookie_name_0;
        add_header Set-Cookie $auth_cookie_name_1;
    }
    
    proxy_set_header   X-Forwarded-For $remote_addr;
    proxy_set_header   Host $http_host;
    proxy_pass         "http://127.0.0.1:8888"; # This is where my web server is hosted
  }
}

OAuth2-代理配置:

代码语言:javascript
复制
cookie_secret="" # Filled in with a base64 string
provider="google"
email_domains="*"
client_secret="" # Filled in with Google auth client secret
client_id="" # Filled in with Google auth client ID
cookie_domains=[".website.com"]
cookie_secure="false"
cookie_samesite="lax"
redirect_url="https://my.website.com/oauth2/callback"
upstreams="http://127.0.0.1:8888/" # My website server
set_xauthrequest=true
upstreams=["file:///dev/null"]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65314905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档