How do I log on to JIRA in PowerShell using REST when basic authentication in plain text is blocked? - powershell

I've been trying to access our on-prem JIRA Server (configured with plain HTTP) using PowerShell Invoke-RestMethod, but I think I'm missing something.
The authentication header is created in base64
$cred = [convert]:.ToBase64String([text.encoding]::ASCII.GetBytes("account:password"))
$headers = #{Authorization = "Basic $cred"}
Using REST, I then ask for the issue (without posting any property filter to keep the request simple while I'm learning).
Invoke-RestMethod -Uri http://jiraserver:8080/jira-software/REST/agile/1.0/issue/test-1 `
-Headers $headers -ContentType "application/json" -AllowUnencryptedAuthentication
This obviously fails, as I get back a reply containing a login form
<form action="/login.jsp"
class="aui"
id="login-form"
method="post">
I do think I remember that basic authentication is no longer supported in JIRA.
How do I use OAuth in that case instead?

When working with another task that is well within the scope of AtlassianĀ“s PowerShell module JiraPS, I noticed a command called Invoke-JiraMethod.
It turns out that the module provides JIRA REST API access :)
So by setting up a JIRA session with the PowerShell module, I'll be able to use REST over an unencrypted connection.

Related

getting 203 with error when using vsts rest api

When I'm trying to create a new work item in VSTS with the POST request:
https://galilinetsky.visualstudio.com/Automatiom/_apis/wit/workitems/$Test%20Case?api-version=5.0-preview.2
I get the next response :
Microsoft Internet Explorer's Enhanced Security Configuration is
currently enabled on your environment. This enhanced level of security
prevents our web integration experiences from displaying or performing
correctly. To continue with your operation please disable this
configuration or contact your administrator.
What am I doing wrong?
The solution is to be found in a similar question: Why I get Internet Explorer enhanced security error message in Chrome if I call VSO API from Angularjs SPA?
Andy writes
the PAT has to be prefix[ed] by ":" before you base 64 encode it"
So the solution is:
Create a Personal Access Token
Add a colon (':') before it
Encode the new PAT (with the preceding colon) using Base 64
Et voila ! That PAT will no longer give you a 203 error.
It's mainly caused by the PAT format is incorrect.
Such as if I add colon : before the PAT, the REST API will return with 203.
adding on to #numeratus
This question took awhile for me to get correctly on powershell. https://www.opentechguides.com/how-to/article/azure/201/devops-rest-powershell.html helped me greatly and a resulting simplified powershell request to azure apis
#enter your token in pat token
$pat = "xxx"
# Create header with PAT
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($pat)"))
$header = #{authorization = "Basic $token"}
#enter your url in projects url
$projectsUrl = "https://feeds.dev.azure.com/"
$projects = Invoke-RestMethod -Uri $projectsUrl -Method Get -ContentType "application/json" -Headers $header

CURL script on Powershell to get a Response from HTTPS URL and also want to feed Username/Password

I am new to CURL and facing problems while writing a script to get a response from HTTPS URL.
Its failing at singleSignOn.
Basic command I am using is:
curl -UseBasicParsing https://abc.xyz.com.au -UseDefaultCredentials
Can someone please help with pointers how to force SSO on CURL while doing a check on HTTPS URL?
In Powershell Curl is really an alias for Invoke-WebRequest.
If you're new to this commandlet, start easy. Try using it with Google:
Invoke-WebRequest -Uri 'https://www.google.com'
Authentication is a little tricker. If the site uses basic authentication, you can use something like the following:
$credential= Get-Credential
Invoke-WebRequest -Uri 'https://foo.com' -Credential $credential
Depending on the application, you might have to construct the basic authentication headers yourself. There is a nice explanation of that in the post Use Invoke-WebRequest with a username and password for basic authentication on the GitHub API
If the single-sign-on piece is using redirection, that's a different animal. You can use the Headers property on the response object to find the URL you're being redirected to. Jim McNatt has a nice article talking about that.

403 Error using PowerShell 5 as Kentico 8.2 REST client

This REST request works when I load it directly in a browser in which I'm simultaneously logged into the Kentico 8.2 admin site:
https://www.example.com/rest/CMS.SettingsKey
Now, I need to return the same results using PowerShell 5. I tried various versions of the following:
$url = "https://www.example.com/rest/CMS.SettingsKey"
$httpMethod = "Get"
$credentialsBytes = [System.Text.Encoding]::UTF8.GetBytes("username:password")
$credentialsEncoded = [System.Convert]::ToBase64String($credentialsBytes)
$headers = #{}
$headers.Add("Authorization", "Basic $($credentialsEncoded)")
$settings = (Invoke-RestMethod -Uri $url -Method $httpMethod -headers $headers)
Write-Host $settings
Note that "username" and "password" are the same credentials used to log into the admin site (when the REST request is working in the browser), and the user is a global admin.
The PS snippet gives me a 403 Forbidden error. I followed this page and this page, but I can't get it to work. What am I doing wrong?
EDIT:
I enabled the REST service, but now I'm getting a 401 Unauthorized error. Again, the user is a global admin. I get the feeling the headers aren't being included in the request (or that there is a problem with the headers), because the same request works (from PowerShell) if I generate a hash and use hash parameter authentication instead of basic authentication. I tried using Fiddler a bit as suggested in comments, but I'm new to it, and I don't have time to dive in too deep right now.
Just tested it using the latest hotfix versions (9.0.32 & 8.2.48) and it works just fine.
Make sure REST is enabled for the specific site (not only the global setting)
Make sure you have <modules runAllManagedModulesForAllRequests="true"> in your web.config as described here.

Adding trigger to build config in TeamCity via REST API and PowerShell

I'm trying to add a build trigger to a build configuration in an automated fashion through PowerShell and the TeamCity 8 REST API.
Using the following question as a reference, it would appear that what I am trying to do is possible:Adding a Trigger to a build configuration in TeamCity using the REST API
But, whenever I try to add the trigger to the build, using the following code, I get a (405) Method Not Allowed error:
$triggerXML= "<trigger id=`"TriggerID`" type=`"buildDependencyTrigger`">
<properties>
<property name=`"afterSuccessfulBuildOnly`" value=`"true`"/>
<property name=`"dependsOn`" value=`"BuildID`"/>
</properties>
</trigger>"
$webclient.UploadString('http://teamcity:8111/httpAuth/app/rest/buildTypes/BuildID', "POST", $triggerXML)
Has anyone implemented this successfully using PowerShell?
Not that API, but I have scripts that automate TeamCity.
Here is a code snippet I use:
$TeamCityHostAndPort = "myteamcityserver:8111"
# authenticate with NTLM
$LoginUrl = "http://$TeamCityHostAndPort/ntlmLogin.html"
Invoke-WebRequest -Uri $LoginUrl -UseDefaultCredentials -SessionVariable TeamCitySession | Out-Null
#start backup
$StartBackupUrl = "http://$TeamCityHostAndPort/httpAuth/app/rest/server/backup?includeConfigs=true&includeDatabase=true&includeBuildLogs=true&fileName=TeamCity_Backup_"
$filename = Invoke-RestMethod -WebSession $TeamCitySession -Method Post -Uri $StartBackupUrl
notice the first call to authenticate (I disabled built-in users and stick with Windows auth) and the authenticated session passed to subsequent calls.
Invoke-RestMethod is Powershell v4.

Send login username with REST API + Powershell

I've got the following Powershell script:
$url = "http://somerandomapikeyhere#prestashop.dev/my-store/api"
$request = [System.Net.WebRequest]::Create($url)
$response = $request.GetResponse()
echo $response
which attempts to access my Prestashop API REST service.
I can access the URL from Chrome without a problem, but in PowerShell I get a 401 Unauthorized response, which is understandeable as the somerandomapikeyhere bit in the URL needs to get dealt with differently in PowerShell.
The question is, how? Any ideas / advice?
It's not powershel, but the .net class that is not able to take an url in that format. You need to provide the credentials seperate. See here or here.
But if you have powershell v3 you are better off using the build in commands for web requests or even REST requests.
Invoke-restmethod -uri 'prestashop.dev/my-store/api' -credential 'someapikey'