且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过 OKTA 从后端服务向 SharePoint 进行身份验证

更新时间:2023-12-01 08:24:46

有可能.

这就是我所做的.1) 从 Okta 获取您的 sessionToken.为此,您需要一个 okta 授权令牌.

Here is what I did. 1) Get your sessionToken from Okta. You'll need an okta authorization token for that.

2) 做一个 HttpGet(sharepointEmbeddedLink + "?onetimetoken=" + sessionToken)还要添加这个标题: new BasicHeader(AUTHORIZATION, String.format("SSWS %s", OKTA_AUTHORIZATION_TOKEN);

2) Do a HttpGet(sharepointEmbeddedLink + "?onetimetoken=" + sessionToken) Also add this header: new BasicHeader(AUTHORIZATION, String.format("SSWS %s", OKTA_AUTHORIZATION_TOKEN);

3) 接下来,您必须解析 html 响应并获取 SAML 参数:WRESULT、WCTX、WA

3) Next you'll have to parse the html response and get the SAML Arguments: WRESULT, WCTX, WA

4) 接下来执行此操作 - 取这 3 个并以application/x-www-form-urlencoded"格式创建一个字符串.它将类似于wa=wsign1.0&wctx=somevalue&wresult=somevalue".

4) Next do this - take those 3 and create a string in this format "application/x-www-form-urlencoded". It will be something like this "wa=wsign1.0&wctx=somevalue&wresult=somevalue".

        byte[] out = theStringAbove.getBytes;
        int length = out.length;

        URL url = new URL("https://login.microsoftonline.com/login.srf");
        URLConnection con = url.openConnection();
        HttpURLConnection http = (HttpURLConnection) con;

        http.setRequestMethod("POST"); // PUT is another valid option
        http.setDoOutput(true);
        http.setInstanceFollowRedirects(true);
        http.setFixedLengthStreamingMode(length);
        http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        http.setRequestProperty("User-agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.215 Safari/535.1");
        http.connect();
        http.getOutputStream().write(out);

5) 您将在响应中包含 saml 令牌.您将不得不再次解析 html 文件.

5) You'll have the saml Token in the response. You'll have to parse an html file again.

6) 您将在第 3 步或第 4 步中获得共享点 siteUrl,然后执行此操作 :)

6) You'll get the sharepoint siteUrl in step3 or 4 and do this next :)

    HttpPost httpPost = new HttpPost(siteUrl + "_forms/default.aspx?wa=wsignin1.0");
    byte[] utf8TokenStringBytes = ("t=" + samlToken).getBytes(StandardCharsets.UTF_8);
    HttpEntity entity = new ByteArrayEntity(utf8TokenStringBytes);
    httpPost.setEntity(entity);
    httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    httpPost.setHeader("User-agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.215 Safari/535.1");

    HttpResponse response = httpclient.execute(httpPost, httpContext);

如果一切正常,您将拥有一些可以使用的 cookie 标头:D

If everyting is ok, you'll have some cookie headers that you can use :D