Copied changes from github.com/doorkeeper-gem/doorkeeper/pull/249