Skip to content

Instantly share code, notes, and snippets.

@opn
Last active August 5, 2017 09:01
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 opn/ea239fc1f9644ef6cb38bdbc1831dc41 to your computer and use it in GitHub Desktop.
Save opn/ea239fc1f9644ef6cb38bdbc1831dc41 to your computer and use it in GitHub Desktop.
This is the Livecode IDE's oAuth2 handler that is found in stack "com.livecode.script-library.oauth2" in the file "oauth2.livecodescript".
command OAuth2 pAuthURL, pTokenURL, pClientID, pClientSecret, pScopes, pPort, pParams
local tURL
put pAuthURL & "?response_type=code" into tURL
put "&client_id=" & urlEncode(pClientID) after tURL
if pScopes is not empty then
put "&scope=" & urlEncode(pScopes) after tURL
end if
local tUniqueRef
put uuid() into tUniqueRef
put "&state=" & tUniqueRef after tURL
local tKey
repeat for each key tKey in pParams
put "&" & tKey & "=" & urlEncode(pParams[tKey]) after tURL
end repeat
put "&redirect_uri=" & urlEncode(kRedirectURL & ":" & pPort & "/") after tURL
local tBrowserRect
if the environment is not "mobile" then
reset the templateStack
create invisible stack tUniqueRef
set the title of stack tUniqueRef to "Authenticate"
local tOldDefault
put the defaultStack into tOldDefault
set the defaultStack to tUniqueRef
set the resizable of stack tUniqueRef to false
set the width of stack tUniqueRef to 640
set the height of stack tUniqueRef to 600
put 0,0,640,600 into tBrowserRect
else
put the effective working screenrect into tBrowserRect
end if
create widget tUniqueRef as "com.livecode.widget.browser"
set the rect of widget tUniqueRef to tBrowserRect
set the url of widget tUniqueRef to tURL
local tOldInterface
put the defaultNetworkInterface into tOldInterface
set the defaultNetworkInterface to "127.0.0.1"
accept connections on port pPort with message "__NewConnection"
set the defaultNetworkInterface to tOldInterface
put pPort into sUniqueRefs[tUniqueRef]
if the environment is not "mobile" then
close stack tUniqueRef
set the style of stack tUniqueRef to "modal"
set the visible of stack tUniqueRef to true
set the destroyStack of stack tUniqueRef to true
set the defaultStack to tOldDefault
if the clickStack is not empty and the visible of the clickStack then
-- seems to require `stack` in the syntax rather than an expression
sheet tUniqueRef in stack (the short name of the clickStack)
else if the visible of the defaultStack then
sheet tUniqueRef
else
set the location of stack tUniqueRef to the screenLoc
modal tUniqueRef
end if
else
wait while exists(widget tUniqueRef) with messages
end if
local tResult
put the dialogData into tResult
if tResult["code"] is not empty then
local tParams
put "grant_type=authorization_code" into tParams
put "&client_id=" & urlEncode(pClientID) after tParams
put "&client_secret=" & urlEncode(pClientSecret) after tParams
put "&code=" & urlEncode(tResult["code"]) after tParams
put "&redirect_uri=" & urlEncode(kRedirectURL & ":" & pPort & "/") after tParams
local tResponse
set the httpHeaders to "Accept: application/json"
post tParams to pTokenURL
put JSONToArray(it) into tResponse
return tResponse for value
else
return tResult["error"] for error
end if
end OAuth2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment