<?xml version="1.0" encoding="utf-8"?>
<luaAutoStarters>
<luaScript name="Automatic Token Request">
<code><![CDATA[
-- Variables that depend on Microsofts API
local refreshUrl = "https://login.microsoftonline.com/organizations/oauth2/v2.0/token"
local content = "client_id=[Client-ID]&scope=presence.read&redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient&grant_type=refresh_token&refresh_token="
-- Configure the timing. Tokens last for 3599 seconds so the overall refresh-time must be lower.
local refreshInterval = 20 -- check every 20 seconds if there is something new to do
local refreshCountdownMax = 150 -- once authentication was successful, wait 150 times 20 seconds until refresh
local refreshCountdown = 0 -- current live-ticker decremented once every 20 seconds, new credentials are fetched when it reaches 0
-- Analyzes the response from Microsoft to the request of (new) tokens
local function analyzeResponse(responseCode, responseBody, responseHeaders)
if responseCode == 200 and responseBody then
values = json.eval(responseBody)
debug.log("Teams Automatic Token Refresh: updating tokens")
persisted["msTeamsToken"] = values["access_token"]
persisted["msTeamsRefresh"] = values["refresh_token"]
refreshCountdown = refreshCountdownMax
else
debug.log("Teams Automatic Token Refresh: failed to receive tokens", "e")
refreshCountdown = 0
end
shared["msTeamsReAuthNeeded"] = "false"
end
local function refreshLoop()
if persisted["msTeamsToken"] and persisted["msTeamsRefresh"] then
refreshCountdown = refreshCountdown - 1
if shared["msTeamsReAuthNeeded"] and shared["msTeamsReAuthNeeded"] == "true" then
debug.log("Teams Automatic Token Refresh: other scripts failed to authenticate -> trigger refresh at "..tostring(refreshCountdown), "e")
refreshCountdown = 0
end
if refreshCountdown <= 0 then
debug.log("Teams Automatic Token Refresh: inquire token update", "d")
http.post(refreshUrl, analyzeResponse, content..persisted["msTeamsRefresh"])
end
else
debug.log("Teams Automatic Token Refresh: not yet valid", "i")
refreshCountdown = 0
end
time.callbackIn(refreshLoop, refreshInterval)
end
-- start refreshing tokens
refreshLoop()
]]></code>
</luaScript>
<luaScript name="Get User-Code">
<code><![CDATA[
-- Variables that depend on Microsofts API
local tokenRequestUrl = "https://login.microsoftonline.com/organizations/oauth2/v2.0/token"
local tokenRequestContent ="client_id=[Client-ID]&scope=presence.read&redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient&grant_type=authorization_code&code="
-- Analyze the response from Microsoft to the request of tokens
local function analyzeTokenResponse(responseCode, responseBody, responseHeaders)
if responseBody then
debug.log("Teams Get User-Code got response: "..responseBody)
end
if responseCode == 200 and responseBody then
values = json.eval(responseBody)
debug.log("Teams Get User-Code storing access_token: "..values["access_token"])
debug.log("Teams Get User-Code storing refresh_token: "..values["refresh_token"])
persisted["msTeamsToken"] = values["access_token"]
persisted["msTeamsRefresh"] = values["refresh_token"]
end
end
-- Get a code out of the query parameters of the given URL and calls Microsofts servers to receive the tokens with the code
local function analyzeRequest(requestUrl, requestBody, requestHeader, requestVariables)
x1, x2 = string.find(requestVariables["query_string"], "code=")
y1, y2 = string.find(requestVariables["query_string"], "&session_state")
debug.log("Teams Get User-Code got request to start: "..x1.." - "..x2.." - "..y1.." - "..y2)
if x1 and y2 then
code = string.sub(requestVariables["query_string"], x2+1, y1-1)
http.post(tokenRequestUrl,analyzeTokenResponse,tokenRequestContent..code)
end
end
-- Listen to the address <Phone-IP>/api/v1/exec/getTeamsCode and gives the request to "analyzeRequest"
http.listen("getTeamsCode", analyzeRequest, true)]]></code>
</luaScript>
</luaAutoStarters>