0

njs ngx_http_auth_request_module を使用しています。

私はこのようなjs関数を持っています;

function introspectAccessToken(r) {
    r.subrequest("/_oauth2_send_request",
        function(reply) {
            if (reply.status == 200) {
                r.return(204); // Token is valid, return success code
            } else {
                // need 302 here
            }
        }
    );
}

ドキュメントには、「サブリクエストが 2xx レスポンス コードを返す場合、アクセスは許可されます。401 または 403 が返される場合、アクセスは対応するエラー コードで拒否されます。サブリクエストによって返されるその他のレスポンス コードはエラーと見なされます。」http://nginx.org/en/docs/http/ngx_http_auth_request_module.html

subsequest が「else」ブロックに入った場合、ユーザーに 302 を返す必要があります。

これを達成する方法はありますか?r.return(302) を設定すると、ドキュメントにあるようにブラウザーにエラー ページが表示されます。

編集:私のnginx.conf

location /my-webclient {
    auth_request /_oauth2_token_introspection;
    root   /usr/share/nginx/html;
    rewrite ^ /hmb-profile-webclient/index.html break;
}       

location = /_oauth2_token_introspection {
    internal;
    js_content introspectAccessToken;                                       
}   

location /_oauth2_send_request {
    internal;
    proxy_method      POST;
    proxy_set_body    $cookie_jwt;
    proxy_pass http://my-specific-url;
}   

http://my-specific-url が返されます

  • 200 (jwt Cookie が有効な場合)
  • 302 (戻り場所を含む)、jwt Cookie が無効な場合
4

1 に答える 1

1

わかりました-これを行う必要さえないと思いますnjs。トークン イントロスペクション エンドポイントが NJS でビルドされておらず、トークンが有効な json であるが他の言語で実装されているかどうかを検証していない場合、この NGINX 構成は機能します。

#cache for auth_request responses

proxy_cache_path /var/cache/nginx/oauth keys_zone=token_responses:1m max_size=2m;

#Your Server
server {

  listen 80;


  location / {
    #Redirect Unauthed Request to some other location... 
    error_page 401 =302 http://localhost:9000/login;
  
    auth_request /_auth_request;
    proxy_pass http://127.0.0.1:8080;


  }

  location /_auth_request {
    internal;
    proxy_pass http://127.0.0.1:8090;
    ###place caching responses here to be more effective;
    proxy_cache           token_responses; # Enable caching
    proxy_cache_key       $cookie_jwt;    # Cache for each access token
    proxy_cache_lock      on;              # Duplicate tokens must wait
    proxy_cache_valid     200 10s;         # How long to use each response
    proxy_ignore_headers  Cache-Control Expires Set-Cookie;
  }


}

....

トークン構造を検証するなど、NJS を使用してより多くのロジックを構築したい場合は、js_set で NJS を使用するアプローチが適切でした。

js_include oauth2.js; # Location of JavaScript code

server {
    listen 80;

    location / {
        error_page 401 403 =302 http://localhost:9000/login;
        auth_request /_oauth2_token_introspection;
        proxy_pass http://my_backend;
    }

    location = /_oauth2_token_introspection {
        internal;
        js_content introspectAccessToken;                                       
    }

njs 関数

function introspectAccessToken(r) {
    r.subrequest("/_oauth2_send_request",
        function(reply) {
            if (reply.status == 200) {
                var response = JSON.parse(reply.responseBody);
                if (response.active == true) {
                    r.return(204); // Token is valid, return success code
                } else {
                    r.return(403); // Token is invalid, return forbidden code
                }
            } else {
                r.return(401); // Unexpected response, return 'auth required'
            }
        }
    );
}

error_pageディレクティブも同様に機能します。あなたのコードに基づいて、私はあなたがすでにこの記事をチェックアウトしたと思いますか?

https://www.nginx.com/blog/validating-oauth-2-0-access-tokens-nginx/

ICYMI は、このユース ケースに関する素晴らしいブログ投稿です。

于 2021-08-16T23:12:32.933 に答える