Skip to content

Commit

Permalink
Fix for issue #6
Browse files Browse the repository at this point in the history
  • Loading branch information
polx committed Sep 15, 2020
1 parent 92158d5 commit 6a67375
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
27 changes: 13 additions & 14 deletions src/main/resources/Office365/Groovy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class AzureAuth {
def authDomain = "login.windows.net"
def apiVersion = "1.0"

def forceDebug = true;
def forceDebug = false;

def tenant;
def azureClientID;
Expand All @@ -80,11 +80,10 @@ public class AzureAuth {
def AZURE_PARAM_ID_TOKEN = "id_token";
def AZURE_PARAM_CODE = "code";
def AUTH_PAGE = "Office365.OAuth";
def AZURE_SESSION_REDIRECT = "azure_redirect";
def REDIRECT_URI = "";


public static storedStates = new HashMap();

def debugStr = ""

def xwiki;
Expand Down Expand Up @@ -223,12 +222,12 @@ public class AzureAuth {
def qs = request.getQueryString();
if (qs!=null & qs!="")
finalRedirect += "?" + qs;
state = finalRedirect.hashCode().toString();
storedStates.put(state, finalRedirect.replaceAll("clearAuth=1",""));
finalRedirect = finalRedirect.replaceAll("clearAuth=1","");
request.getSession().setAttribute(AZURE_SESSION_REDIRECT, finalRedirect);
}

def currentURL = URLEncoder.encode(REDIRECT_URI + "?state=${state}", "UTF-8")
def redirectUrl = "${authority}/${tenant}/oauth2/authorize?response_type=code%20id_token&scope= Files.ReadWrites%20User.Read%20User.ReadWriteUser.ReadBasic.All%20Files.ReadWrite%20Files.ReadWrite.All%20Files.ReadWrite.AppFolder%20Sites.ReadWrite.All&response_mode=form_post&redirect_uri=${currentURL}&state=${state}&client_id=${azureClientID}&nonce=${UUID.randomUUID()}";
def currentURL = URLEncoder.encode(REDIRECT_URI, "UTF-8")
def redirectUrl = "${authority}/${tenant}/oauth2/authorize?response_type=code%20id_token&scope= Files.ReadWrites%20User.Read%20User.ReadWriteUser.ReadBasic.All%20Files.ReadWrite%20Files.ReadWrite.All%20Files.ReadWrite.AppFolder%20Sites.ReadWrite.All&response_mode=form_post&redirect_uri=${currentURL}&client_id=${azureClientID}&nonce=${UUID.randomUUID()}";
return redirectUrl;
}

Expand Down Expand Up @@ -335,13 +334,13 @@ public class AzureAuth {
} else {
debug("Step 2b: Got access token succesfully")
storeAuthentication(request, authResult);
if (request.state && request.state!="") {
def url = storedStates.get(request.state);
if (url!=null) {
debug("Redirecting to final destination after authorization: ${url}")
response.sendRedirect(url)
return true;
}
def session = request.getSession();
def url = session.getAttribute(AZURE_SESSION_REDIRECT);
if(url && url.length()>=0) {
debug("Redirecting to final destination after authorization: ${url}")
response.sendRedirect(url)
session.remove(AZURE_SESSION_REDIRECT)
return true;
}
}
return true;
Expand Down
9 changes: 8 additions & 1 deletion src/main/resources/Office365/Office365Macro.xml
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ if (url!=null && url!="") {

if (request.update=="1" && nb==embednb)
force = true;

if (request.embed=="1") {
if (embednb==nb) {
def embedLink = request.embedLink;
Expand All @@ -568,6 +568,13 @@ if (url!=null && url!="") {
doc.set("fileName", request.filename)
doc.set("user", xcontext.user)
doc.save("Inserting Office365 Document")
def path = request.getPathInfo();
if(path!=null && path.length()>0) {
path = path.substring(path.lastIndexOf('/'));
} else {
path = ".";
}
response.sendRedirect(path);
}
}

Expand Down

0 comments on commit 6a67375

Please sign in to comment.