Last active
August 2, 2016 11:35
-
-
Save mudge/2a786e033579c3baf6a14d9f5e396aea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# If you're using Rails, add this file to app/middleware/microsoft_hyperlink_library_middleware.rb | |
# And add the following to your config/application.rb (note the string class name): | |
# | |
# config.middleware.use "MicrosoftHyperlinkLibraryMiddleware" | |
# | |
# This will fix the awful bug described in https://support.microsoft.com/en-gb/kb/899927 where | |
# links from PowerPoint or Word that require an authenticated session continually go to your | |
# log in page even when a user is authenticated. | |
# | |
# The workaround is to rewrite any server-side HTTP redirects to client-side redirects | |
# using the <meta http-equiv="refresh"> tag for any requests from Microsoft Office products. | |
class MicrosoftHyperlinkLibraryMiddleware | |
attr_reader :app | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
status, header, body = app.call(env) | |
return meta_refresh(header) if redirect?(status) && microsoft_office_request?(env) | |
[status, header, body] | |
end | |
private | |
def redirect?(status) | |
status >= 300 && status < 400 | |
end | |
def microsoft_office_request?(env) | |
request = Rack::Request.new(env) | |
user_agent = request.user_agent.to_s | |
user_agent.include?(' PowerPoint/') || user_agent.include?(' Word/') | |
end | |
def meta_refresh(header) | |
url = header['Location'] | |
[ | |
200, | |
{ 'Content-Type' => 'text/html' }, | |
[%(<!DOCTYPE html><title>Refresh</title><meta http-equiv="refresh" content="0; url=#{url}">)] | |
] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment