Unable to publish message on behalf of another user
I have created a custom endpoint wherein a new TKB article gets published to one of our product boards as a product advisory, which is triggered by a member of the respective product team via a custom component on an "administrative" page that I've developed.
Everything works as expected in terms of creating the message, but one of the requirements I'm attempting is for the content to be published using a generic "ProductTeam" service account, which I can't seem to get working properly.
Even when passing a valid session key for the service account in the header or as a query parameter, the message is still published by the user (i.e. me) that triggers the endpoint.
These are the two functions I wrote to handle the publishing:
<#-------------------- Function: getSessionKey -------------------->
<#-- This function attempts to authenticate as a user and return the session key -->
<#function getSessionKey userLogin userPw>
<#local sessionKey = "" />
<#local queryString = "user.login=${userLogin?url}&user.password=${userPw?url}&restapi.response_format=json" />
<#attempt>
<#local response = restadmin('/authentication/sessions/login?${queryString}') />
<#recover>
<@logging.consoleError "Failed to authenticate as the user" />
</#attempt>
<#if response?? && response.@status == "success">
<#local sessionKey = response.value />
<#else>
<@logging.consoleError "Authentication request for the user was not successful" />
</#if>
<#return sessionKey />
</#function>
<#-------------------- Function: publishNewMessage -------------------->
<#-- This function publishes a new message and returns the API response -->
<#function publishNewMessage payload sessionKey="" sessionKeyInQuery=false>
<#if sessionKey?? && sessionKey?length gt 0>
<#if sessionKeyInQuery?? && sessionKeyInQuery>
<#local messagePostCall = restBuilder()
.method("POST")
.path("/messages")
.queryParam("restapi.session_key", "${sessionKey}")
.body(payload)
.admin(false) />
<#else>
<#local messagePostCall = restBuilder()
.method("POST")
.path("/messages")
.header("li-api-session-key","${sessionKey}")
.body(payload)
.admin(false) />
</#if>
<#else>
<#local messagePostCall = restBuilder()
.method("POST")
.path("/messages")
.body(payload)
.admin(true) />
</#if>
<#local response = messagePostCall.call() />
<#return response />
</#function>
Here are some usage examples, both of which still publish the message with my own user as the author rather than the service account, despite passing its session key.
<#assign payload = {
"data": {
"type": "message",
"board": {
"id": "some-product-board"
},
"subject": "This is a product advisory",
"body": "This is the body of the message"
}
} />
<#assign sessionKey = getSessionKey(SVC_USERNAME, SVC_PWD) />
<#-- Passing the session key in the header -->
<#assign response = publishNewMessage(payload, sessionKey) />
<#-- Passing the session key in the query string -->
<#assign response = publishNewMessage(payload, sessionKey, true) />
Does anyone have any ideas regarding what I might be doing wrong? Thanks!
Hi jeffshurtliff ,
Let me reply to your last 2 questions:
Is there a way to force the directives above to select one session key over another, or is there perhaps a different directive/method to leverage instead to publish the post within the endpoint?
There is no way (to my knowledge) to pass a different session key from your freemarker template and have that override the session id of the currently authenticated user. We do allow you to pass either the Li-Api-Session-Key header or the restapi.session_key parameter to your endpoint call and it will use that session key to authenticate the user.
Is there maybe a different way that the author can be specified by the Community APIs when creating a new message? For example, if I pass "author": {"type": "user", "id": "12345"} in the payload when creating the message will that specify the author or will it be ignored?If the user you are authenticated as has the "Switch User" permission, then you can pass the "author": { "type": "user", "id": "12345" } and it should set the user for the id you've passed as message author (assuming that id maps to a valid user) instead of using the currently authenticated user as the author.
I hope that helps.
-Doug