OpenID Authentication cURL to YAML and JavaScript

Mindwatering Incorporated

Author: Tripp W Black

Created: 11/12 at 04:40 PM

 

Category:
Linux
RH AAP

Issue:
Convert OpenID curl command to YAML for AAP or JavaScript for NodeJS or vRO:


Adapted cURL code from OpenID documentation:
$ curl \
--request POST \
--data "grant_type=client_credentials" \
--data "client_id=myClient" \
--data "client_secret=reallygoodpassword" \
--data "scope=write" \
"https://openam.mindwatering.net:8443/as/oauth2/realms/root/authorization.oauth2"



YAML Code Equivalent:
- name: 'OpenID Authentication to authorization.oath2'
ansible.builtin.uri:
url: ''https://openam.mindwatering.net:8443/as/oauth2/realms/root/authorization.oauth2'
method: POST
body:
grant_type: client_credentials
client_id: myClient
client_secret: reallygoodpassword
scope: write
body_format: form-urlencoded
headers:
Content-Type: application/x-www-form-urlencoded
return_content: true
status_code: [200, 201, 202, 302]
register: apiLoginResults
- name: Printout Result
debug: msg="apiLoginResult- '{{ apiLoginResults }}'"



JavaScript Fetch:
fetch('https://openam.mindwatering.net:8443/as/oauth2/realms/root/authorization.oauth2', {
method: 'POST',
body: new URLSearchParams({
'grant_type': 'client_credentials',
'client_id': 'myClient',
'client_secret': 'reallygoodpassword',
'scope': 'write'
})
});



JavaScript XHR:
const data = new URLSearchParams({
'grant_type': 'client_credentials',
'client_id': 'myClient',
'client_secret': 'reallygoodpassword',
'scope': 'write'
});

let xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open('POST', 'openam.mindwatering.net:8443/as/oauth2/realms/root/authorization.oauth2');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

xhr.onload = function() {
console.log(xhr.response);
};

xhr.send(data);



JavaScript / NodeJS HTTP:
import https from 'https';

const options = {
hostname: 'openam.mindwatering.net:8443',
path: '//as/oauth2/realms/root/authorization.oauth2',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};

const req = https.request(options, function (res) {
const chunks = [];

res.on('data', function (chunk) {
chunks.push(chunk);
});

res.on('end', function () {
const body = Buffer.concat(chunks);
console.log(body.toString());
});
});

req.write(new URLSearchParams({
'grant_type': 'client_credentials',
'client_id': 'myClient',
'client_secret': 'reallygoodpassword',
'scope': 'write'
}).toString());
req.end();



JavaScript / NodeJS Request:
(deprecated)
var request = require('request');

var headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};

var dataString = 'grant_type=client_credentials&client_id=myClient&client_secret=reallygoodpassword&scope=write';

var options = {
url: 'https://'openam.mindwatering.net:8443/as/oauth2/realms/root/authorization.oauth2',
method: 'POST',
headers: headers,
body: dataString
};

function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}

request(options, callback);


________________


Token Endpoint Request:
$ curl \
-H "Authorization: BASIC myClient:reallygoodpassword | base64" \
--request POST \
"https://openam.mindwatering.net:8443/as/oauth2/realms/root/token.oauth2?grant_type=client_credentials"



Ansible Request:
- name: 'OpenID Authentication to token.oath2'
ansible.builtin.uri:
url: 'https://openam.mindwatering.net:8443/as/oauth2/realms/root/token.oauth2?grant_type=client_credentials'
method: POST
headers:
Content-Type: "application/x-www-form-urlencoded"
Authorization: 'BASIC myClient:reallygoodpassword | base64'
status_code: [200, 201, 202, 302]
return_content: true
register: apiLoginResult

- name: access_token
debug: msg="apiLoginResult.json.access_token- '{{ apiLoginResult.json.access_token }}'"

- name: cookies
debug: msg="apiLoginResult.cookies- '{{ apiLoginResult.cookies }}'"

- name: cookies.PF
debug: msg="apiLoginResult.cookies.PF- '{{ apiLoginResult.cookies.PF }}'"


Notes:
Pass the access_token and the cookies.PF value as headers to the Apigee/OpenID endpoint configured to the backend (reverse proxy back-end app server)


Do something like:
- name Update Info
ansible.builtin.uri:
url: "{{ restBaseURI }}{{ restEndPointOPPath }}"
method: POST
headers:
Cookie: "{{ apiLoginResult.cookies.PF }}"
Authorization: "Bearer {{ apiLoginResult.json.access_token }}"
body_format: json
body: "{{ restBodyPayload }}"
status_code: [200, 201, 202, 203]
return_content: true
register: apiResult

- name: result
debug: msg="apiResult- '{{ apiResult }}'"




previous page