1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

oAuth2+nginx:後ろのシステムにリクエストヘッダでユーザIDを渡す

Posted at

の続き

環境は同じくWindowsServer2012

nginxでoauth2-proxyを使う事で単純にGoogleでログインはできたが、
その先のシステムが独自にロール等を管理している場合でも使えるようにユーザID、というかgmailのアドレスを渡したい。

ここを見ると

--pass-user-headers   pass X-Forwarded-User and X-Forwarded-Email information to upstream (default true)

とdefault trueなので、リクエストヘッダのX-Forward-UserかEmailに何らかのデータが載っているのかをnode.js+Express上でreq.headersを表示して確認したが、そのままだと存在していないようなので、ネットで検索しながら設定。

oauth2-proxy側の設定ファイルには、tokenは必須ではないが、以下を設定に追加

## headers
pass_user_headers = true
pass_host_header = true
pass_access_token = true
set_xauthrequest = true

nginx側にはこんな感じでproxy_set_headerをしてやる

location /node/ {
    auth_request /oauth2/auth;
    proxy_pass http://localhost:3000/;
    auth_request_set $user   $upstream_http_x_auth_request_user;
    auth_request_set $email  $upstream_http_x_auth_request_email;
    proxy_set_header X-Auth_User  $user;
    proxy_set_header X-Auth_Email $email;
}

これでnodejs側でreqから以下のようなIDとEmailが取れた。

  'x-auth_user': '102016090607545600000',
  'x-auth_email': 'mymail@gmail.com',

このx-auth_emailをユーザIDとして利用する事で、システム独自で認可処理を持っている場合でも扱えるはず。
(手元のJavaのシステムはリクエストヘッダをユーザIDとして扱える、というか扱えるように昔コードを書いたので、やろうと思えばいける)

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?