NTLM Authentication in groovy (Scriptrunner -> Sharepoint-API)

Chris Schneider
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 25, 2022

Dear Community,

I am trying to make a REST call from a groovy script to a sharepoint API. Unfortunately, I struggle at the authentication, as it should use NTLM.

How do I specify, that the authentication method should be NTLM and not basic or bearer token?

 

This is my current code, which delivers a "401" response form the sharepoint.

 

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*
import groovy.json.JsonSlurper
import net.sf.json.groovy.JsonSlurper
import groovyx.net.http.Method

def http = new HTTPBuilder('http://base.url')

http.request(POST, JSON) {
requestContentType = ContentType.JSON
headers."Accept-Encoding" = "gzip, deflate, br"
headers."Accept" = "application/json;odata=verbose"
headers."Content-Type" = "application/json;odata=verbose"
headers."Connection" = "keep-alive"


body = [username: 'user', password: 'password']

response.success = { resp, JSON ->

return JSON

}

response.failure = { resp ->
return "Request failed with status ${resp.status}"

}
}

 

(Scriptrunner does not validate the code, whereas JWME just runs it fine and delivers the expreded "401".)

1 answer

1 accepted

0 votes
Answer accepted
Chris Schneider
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 31, 2022

After a couple of hours and some significant help from @Sayed Bares (Appfire) and @Ram Kumar Aravindakshan _Adaptavist_  (Adaptavist), I was able to successfully authenticate against our sharepoint:

 

//step 1: NTLM authentication to sharepoint

import groovyx.net.http.HTTPBuilder
import groovyx.net.http.RESTClient

 

def baseURL = 'server-url'
def sharepointAPI = 'resource_path'
def domain = 'xxx'
def username = 'yyy'
def password = 'zzz'


def restClient = new RESTClient( baseURL )
restClient.auth.ntlm(username, password, '', domain)

def response = restClient.post(
path: sharepointAPI,
contentType: "application/json;odata=verbose",
headers: ['Connection': 'keep-alive', 'Accept': 'application/json;odata=verbose'])


response.getData()



//step 2: parse JSON -> "FormDigestValue"

 

The response looks like this (JWME (Appfire)):

{d={GetContextWebInformation={__metadata={type=SP.ContextWebInformation}, FormDigestTimeoutSeconds=3600, FormDigestValue=0xB6F2D8BEAE813C24C386D8E24...4067630E1393782C0E,31 Jan 2022 14:43:52 -0000, LibraryVersion=16.0.5266.1000, SiteFullUrl=https://server-url.local, SupportedSchemaVersions={__metadata={type=Collection(Edm.String)}, results=[14.0.0.0, 15.0.0.0]}, WebFullUrl=https://server-url.local/help-desk/it}}}

 

The response looks like this (Scriptrunner):

[d:[GetContextWebInformation:[__metadata:[type:SP.ContextWebInformation], FormDigestTimeoutSeconds:3600, FormDigestValue:0xB6F2D8BEAE813C24C386D8E24...4067630E1393782C0E,31 Jan 2022 14:43:52 -0000, LibraryVersion:16.0.5266.1000, SiteFullUrl:https://server-url.local, SupportedSchemaVersions:[__metadata:[type:Collection(Edm.String)], results:[14.0.0.0, 15.0.0.0]], WebFullUrl:https://server-url.local/help-desk/it]]]

 

 

Now, I need to figure out, how to parse the response and get the value "FormDigestValue" into a variable.

Chris Schneider
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 31, 2022

The response is already JSON and Groovy parsed it into a LazyMap, which is Groovy's internal representation of a JSON object.

response.data.d.GetContextWebInformation.FormDigestValue

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events