Skip to content

Instantly share code, notes, and snippets.

@ur4ltz
Forked from areina/apitools_github_oauth.lua
Created February 5, 2021 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ur4ltz/be16f128e95e7f51172def5e7e3d52a8 to your computer and use it in GitHub Desktop.
Save ur4ltz/be16f128e95e7f51172def5e7e3d52a8 to your computer and use it in GitHub Desktop.
Apitools middleware to provide a callback endpoint for github oauth
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