1
0

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-proxyをwindowsで利用する

Posted at

SNS系のログイン情報を使うにはoauth2-proxyにWebサーバ、もしくはapacheならmod_auth_openidcで受けるのがクラウドのサービスとかを使わないのであれば一般的、と調べた結果思った。
(Apacheのモジュールも新しいのが出ているのかもしれないが調べてない)

今回は受けが広そうなoauth2-proxyを試してみる。
※認証と認可が云々、oauthよりopenIDの方がとか、セッション維持の管理はここでは置いておく

oauth2-proxy

仕組みはこの辺りを見ると色々書いてある。

バイナリは
https://github.com/oauth2-proxy/oauth2-proxy/releases
にある。
この前のnginxをwindowsで立ち上げたのと、事例が少なそうなのでWindowsでやってみる事にした。
oauth2-proxy-v7.1.3.windows-amd64.tar.gz
x64ではなくAMD64表記なのか、と思いつつダウンロードして7zipで展開。
とりあえずC:\temp\oauth2-proxy7に配置
拡張子を.exeにしてやってプロンプトからそのまま実行すると

C:\temp\oauth2-proxy7>oauth2-proxy
[2021/10/21 11:04:55] [main.go:54] invalid configuration:
  missing setting: cookie-secret
  provider missing setting: client-id
  missing setting: client-secret or client-secret-file
  missing setting for email validation: email-domain or authenticated-emails-file required.
      use email-domain=* to authorize all email addresses

と出るので設定ファイル等が足りない事が分かり、実行そのものは出来ていそうな雰囲気。

Auth0が幅広く扱えるらしいが、とりあえず自分用テストなので今回は利用しない。

Linuxだと動作はoauth2_proxy.cfgというファイルを配置云々の記事は出ているが、Windowsの場合が分からないので
公式ドキュメントをとりあえず見ると
起動パラメータが載っている。
パラメータでも色々指定可能だが、これで設定ファイルの場所を指定する事で動かすのが良さそう。

事前準備でGoogle API ConsoleでoAuth2の設定を作成する。

  • 同意画面設定 でとりあえず外部で作成して必須項目を埋める
  • スコープはとりあえず空白
  • テストユーザはテストするアドレスを追加
  • OAuth クライアント ID の作成からウェブアプリケーションで追加
  • JavaScriptにURL、リダイレクトにURL/oauth2/callback 今回URLはlocalhost:8082にした

設定ファイルを書き換えて、oauth2-proxy --config=C:\temp\oauth2-proxy7\oauth2_proxy.cfg

## <addr>:<port> to listen on for HTTP/HTTPS clients
http_address = "127.0.0.1:4180"

## the OAuth Redirect URL.
redirect_url = "http://localhost:8082/oauth2/callback"

## the http url(s) of the upstream endpoint. If multiple, routing is based on path
upstreams = [
    "http://localhost:8082/"
]

## Authenticated Email Addresses File (one email per line)
authenticated_emails_file = "C:\\temp\\oauth2-proxy7\\authenticated_emails_file"

## Cookie Settings
cookie_name = "_oauth2_proxy"
cookie_secret = "secretcookietest"

authenticated_emails_fileにはログインに使うgmailのアドレスを記述

nginx

nginx -V コマンドでhttp_auth_request_moduleが入っているかを確認。(入ってた)
locationに以下を設定してnginxを再起動、画面にアクセスするとoAuth2Proxyのログイン画面が出る。

nginx.conf
location / {
    auth_request /oauth2/auth;
    error_page 401 = /oauth2/sign_in;
    root   C:\inetpub\wwwroot;
    index  index.html index.htm;
}

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;
    # nginx auth_request includes headers but not body
    proxy_set_header Content-Length   "";
    proxy_pass_request_body           off;
}

Chromeでログインすると
cookie "_oauth2_proxy" not present, removing session
のエラー
Also, this problem appears only in Chrome. My app works fine when accessed with firefox, and no errors are being generated
のような報告がここで上がっているので一旦IEで試すと
error performing request: Post "https://www.googleapis.com/oauth2/v3/token": dial tcp 142.250.206.234:443: i/o timeout
HTTPS_PROXYをセットするとエラーが変わって403で返ってきたので前進。
この状態でChromeで試すとページが表示出来た。IEはサポート外とすれば良いので、とりあえずWindowsでのnginxとの連携は出来た。

システムで使う場合にはユーザに応じたロールの取得が必要だが、独自で持っている場合はX-Forwarded-Userを利用していけそうなので時間があれば確認しておきたいところ。

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?