Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,457,006
Community Members
 
Community Events
176
Community Groups

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

Edited

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

After a couple of hours and some significant help from @Sayed Faqir 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.

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

Atlassian Community Events