0
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 5 years have passed since last update.

doorkeeper gemでCallback URLにワイルドカードを利用できるようにする

Last updated at Posted at 2017-07-22

RFC 6749で定義されているOAuth 2.0の仕様ではワイルドカードを用いてはいけないことになっている。

The redirection endpoint URI MUST be an absolute URI as defined by [RFC3986] Section 4.3. The endpoint URI MAY include an "application/x-www-form-urlencoded" formatted (per Appendix B) query component ([RFC3986] Section 3.4), which MUST be retained when adding additional query parameters. The endpoint URI MUST NOT include a fragment component.
https://tools.ietf.org/html/rfc6749#section-3.1.2

導入は自己責任で。

TL;DR

initializerでdoorkeeper gemにモンキーパッチを当てる。

config/initializers/monkey_patches/doorkeeper.rb
module URICheckerExtension
  def matches?(url, client_url)
    return true if wildcard_matches?(url, client_url)
    super
  end

  def wildcard?(url)
    !/^\*\./.match(url.host).nil?
  end

  def wildcard_matches?(url, client_url)
    url = as_uri(url)
    client_url = as_uri(client_url)
    url.query = nil

    return false unless wildcard?(client_url)
    url.host.sub(/^.+?\./, '') == client_url.host.sub(/^\*\./, '') && url.path == client_url.path
  end
end

module Doorkeeper::OAuth::Helpers::URIChecker
  class << self
    prepend URICheckerExtension
  end
end

Callback URLに https://*.example.com を指定した場合、これにマッチするURLはValidationが通るようになる。

Motivation

社内PaaSなどでOAuthを利用するアプリケーションがポコポコ立ち上がる環境を想定したとき、一々作成されたURLを認可サーバに登録するのは面倒くさいし生産性が下がる。

アプリケーションごとにある程度のルールを設けつつこれを解消したかったので、Redirect URLにワイルドカードを利用できるようにした。

もちろんセキュリティの問題もあるので、表向きには出さずにQA環境のみ有効にするといったアプローチを取るのが無難。

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