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にモンキーパッチを当てる。
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環境のみ有効にするといったアプローチを取るのが無難。