Reputation: 1
I have an application in which the user's token is updated with new data when logging in. While doing the autotests, I get access tokens through the keycloak api and send them along with the requests. But for a valid token, I need to log in to the system via the api. The question is, which query should I use and does such a query even exist?
I searched for a lot of information in the keycloak documentation and in various articles, but they all refer only to the request for access-token. Also, I know that there is a domain/realms/realm/login-actions/authenticate request, but there are rather unclear parameters in the payload. I'm not sure if this is what I need. If so, can you tell me what should be in the payload?
Upvotes: 0
Views: 498
Reputation: 9390
Setting Events Config
PUT /admin/realms/{realm}/events/config
Getting Events Config
GET /admin/realms/{realm}/events/config
Getting Events
GET /admin/realms/{realm}/events
In here
get-events.js
const axios = require('axios');
const getMasterToken = async (userName, password) => {
try {
const resp = await axios.post(
'http://localhost:8080/realms/master/protocol/openid-connect/token',
new URLSearchParams({
'client_id': 'admin-cli',
'username': userName,
'password': password,
'grant_type': 'password'
})
);
return resp.data.access_token;
} catch (err) {
console.error(err);
}
};
const getEventConfig = async (token, realmName) => {
try {
const config = {
headers: { Authorization: `Bearer ${token}` }
};
const url = `http://localhost:8080/admin/realms/${realmName}/events/config`;
const response = await axios.get(url, config);
if (response.status === 200) {
return response.data;
} else {
console.error(`Failed to fetch client: Server responded with status ${response.status}`);
return null;
}
} catch (error) {
if (error.response) {
console.error('Server responded with status:', error.response.status, 'Response data:', error.response.data);
} else if (error.request) {
console.error('No response received:', error.request);
} else {
console.error('Error setting up request:', error.message);
}
return null;
}
};
const setEventConfig = async (token, realmName, data) => {
try {
const config = {
headers: { Authorization: `Bearer ${token}` }
};
const url = `http://localhost:8080/admin/realms/${realmName}/events/config`;
const response = await axios.put(url, data, config);
if (response.status === 204) {
console.log('Configuration updated successfully');
} else {
console.log('Unexpected response status:', response.status);
}
} catch (error) {
console.error('Error updating configuration:', error);
}
};
const getEvents = async (token, realmName) => {
try {
const config = {
headers: { Authorization: `Bearer ${token}` }
};
const url = `http://localhost:8080/admin/realms/${realmName}/events`;
const response = await axios.get(url, config);
if (response.status === 200) {
return response.data;
} else {
console.error(`Failed to fetch client: Server responded with status ${response.status}`);
return null;
}
} catch (error) {
if (error.response) {
console.error('Server responded with status:', error.response.status, 'Response data:', error.response.data);
} else if (error.request) {
console.error('No response received:', error.request);
} else {
console.error('Error setting up request:', error.message);
}
return null;
}
};
(async () => {
const masterToken = await getMasterToken('admin', 'admin');
const realmName = 'master';
let eventConfig = await getEventConfig(masterToken, realmName);
// Enable event config
eventConfig.eventsEnabled = true;
eventConfig.eventsExpiration = 600;
await setEventConfig(masterToken, realmName, eventConfig);
eventConfig = await getEventConfig(masterToken, realmName);
const events = await getEvents(masterToken, realmName);
console.log(JSON.stringify(events, null, 4));
})();
npm install axios
node get-events.js
[
{
"time": 1715375313887,
"type": "LOGIN",
"realmId": "99f7bcf2-2465-4285-977c-a0f5fdf485d6",
"clientId": "admin-cli",
"userId": "83093157-7c61-410c-95de-c3fac5ace9c5",
"sessionId": "b418cfda-961d-41aa-afa1-3140861c3492",
"ipAddress": "172.25.0.1",
"details": {
"auth_method": "openid-connect",
"token_id": "489c263e-bf46-4dfa-8515-5f271526f07e",
"grant_type": "password",
"refresh_token_type": "Refresh",
"scope": "profile email",
"refresh_token_id": "104648f7-bccd-435d-8021-d24ddda8ccba",
"client_auth_method": "client-secret",
"username": "admin"
}
},
{
"time": 1715375087671,
"type": "LOGIN",
"realmId": "99f7bcf2-2465-4285-977c-a0f5fdf485d6",
"clientId": "admin-cli",
"userId": "83093157-7c61-410c-95de-c3fac5ace9c5",
"sessionId": "9e94b599-ff01-4941-b34b-a25ff2a222a3",
"ipAddress": "172.25.0.1",
"details": {
"auth_method": "openid-connect",
"token_id": "f3404361-064e-437e-bb10-31302ea07bde",
"grant_type": "password",
"refresh_token_type": "Refresh",
"scope": "profile email",
"refresh_token_id": "85126c11-1a27-4350-9e64-2e34c158c9c7",
"client_auth_method": "client-secret",
"username": "admin"
}
},
{
"time": 1715375028759,
"type": "LOGIN",
"realmId": "99f7bcf2-2465-4285-977c-a0f5fdf485d6",
"clientId": "admin-cli",
"userId": "83093157-7c61-410c-95de-c3fac5ace9c5",
"sessionId": "b96c3b4e-49c7-4971-a112-714b8bb94154",
"ipAddress": "172.25.0.1",
"details": {
"auth_method": "openid-connect",
"token_id": "f28bda0f-4d51-459b-8e0b-a124ba3dcc98",
"grant_type": "password",
"refresh_token_type": "Refresh",
"scope": "profile email",
"refresh_token_id": "ba5b25bd-018c-4e93-8877-be093955db4f",
"client_auth_method": "client-secret",
"username": "admin"
}
},
{
"time": 1715374085047,
"type": "LOGIN",
"realmId": "99f7bcf2-2465-4285-977c-a0f5fdf485d6",
"clientId": "admin-cli",
"userId": "83093157-7c61-410c-95de-c3fac5ace9c5",
"sessionId": "74fff1e9-c123-4fe6-a770-89633440e2d4",
"ipAddress": "172.25.0.1",
"details": {
"auth_method": "openid-connect",
"token_id": "fcc7294f-e3ba-47ca-8e52-0ad0f82855f8",
"grant_type": "password",
"refresh_token_type": "Refresh",
"scope": "profile email",
"refresh_token_id": "c1a5efaf-b0d2-4822-a4e6-03b1f0a85cd4",
"client_auth_method": "client-secret",
"username": "admin"
}
},
{
"time": 1715374014186,
"type": "LOGIN",
"realmId": "99f7bcf2-2465-4285-977c-a0f5fdf485d6",
"clientId": "admin-cli",
"userId": "83093157-7c61-410c-95de-c3fac5ace9c5",
"sessionId": "02e0e456-4627-4230-b3cc-6c27c604039e",
"ipAddress": "172.25.0.1",
"details": {
"auth_method": "openid-connect",
"token_id": "1fb3bb8c-5ba6-4a91-8ec1-b82566d93e75",
"grant_type": "password",
"refresh_token_type": "Refresh",
"scope": "profile email",
"refresh_token_id": "b2cdfb79-d9c5-424b-b223-cd77ee2fa8cf",
"client_auth_method": "client-secret",
"username": "admin"
}
}
]
In here
Upvotes: 0