Created
April 23, 2014 16:01
-
-
Save areina/11221309 to your computer and use it in GitHub Desktop.
Apitools middleware to provide a callback endpoint for github oauth
This file contains 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
return function(request, next_middleware) | |
local CLIENT_ID = "foo" | |
local CLIENT_SECRET = "bar" | |
local APP_URL = "http://localhost:9000/#overview" | |
if request.uri == '/callback' then | |
local session_code = request.args["code"] | |
local url = "https://github.com/login/oauth/access_token" | |
local request_body = { | |
client_id = CLIENT_ID, | |
client_secret = CLIENT_SECRET, | |
code = session_code | |
} | |
local request_args = { | |
method = "POST", | |
url = url, | |
headers = { | |
["User-Agent"] = "Brainslug-App", | |
Accept = "application/json" | |
} | |
} | |
local result_body, result_status, result_headers = http.simple(request_args, request_body) | |
-- extract the token and granted scopes | |
local access_token = json.decode(result_body)['access_token'] | |
local redirect_location = APP_URL .. "?access_token=" .. access_token | |
local redirect_code = 302 | |
return { | |
status = redirect_code, | |
headers = { | |
Location = redirect_location | |
}, | |
body = "" | |
} | |
else | |
return next_middleware() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment