Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authkey is currently not sent for WMS GetLegendGraphic requests #10727 #10765

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion web/client/components/misc/SecureImage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import React, { useEffect, useState } from 'react';
import axios from 'axios';

import { getAuthenticationMethod } from '../../utils/SecurityUtils';
import { getAuthKeyParameter, getAuthenticationMethod, getToken } from '../../utils/SecurityUtils';
import { updateUrlParams } from '../../utils/URLUtils';


const SecureImage = ({
Expand Down Expand Up @@ -42,6 +43,16 @@ const SecureImage = ({
.catch((error) => {
console.error('Error fetching image:', error);
});
} else if (authMethod === "authkey") {
const authParam = getAuthKeyParameter(src);
const token = getToken();
if (authParam && token) {
const newSrc = updateUrlParams(src, {[authParam]: token});
setImageSrc(newSrc);
} else {
setImageSrc(src);
}

} else {
setImageSrc(src);
}
Expand Down
14 changes: 14 additions & 0 deletions web/client/utils/URLUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,17 @@ export const isValidURLTemplate = (url, params, regexp = /^(http(s{0,1}):\/\/)+?
export const getDefaultUrl = (url) => {
return isArray(url) ? url[0] : url;
};

/**
* Updates the given URL by adding or updating query parameters.
*
* @param {string} url - The source URL.
* @param {Object} params - The parameters to add or update.
* @returns {string} - The updated URL with new query parameters.
*/
export function updateUrlParams(url, params) {
const parsedUrl = queryString.parseUrl(url);
const updatedQuery = { ...parsedUrl?.query, ...params };
// TODO: use stringifyUrl instead after updating `query-string`, not supported in current version
return parsedUrl?.url + '?' + queryString.stringify(updatedQuery);
}
13 changes: 12 additions & 1 deletion web/client/utils/__tests__/URLUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
import expect from 'expect';

import { urlParts, isSameUrl, sameQueryParams, isValidURL, isValidURLTemplate, getDefaultUrl } from '../URLUtils';
import { urlParts, isSameUrl, sameQueryParams, isValidURL, isValidURLTemplate, getDefaultUrl, updateUrlParams } from '../URLUtils';

const url1 = "https://demo.geo-solutions.it:443/geoserver/wfs";
const url2 = "https://demo.geo-solutions.it/geoserver/wfs";
Expand Down Expand Up @@ -179,6 +179,17 @@ describe('URLUtils', () => {

expect(getDefaultUrl(_url)).toBe(_url[0]);
});

it("update Url params", () => {
const url = 'https://my-site.com/some/path/to/resouce?query=true&passtest=true';
const updatedUrl = updateUrlParams(url, { query: false, passtest: false });
expect(updatedUrl).toBe('https://my-site.com/some/path/to/resouce?passtest=false&query=false');
});
it("add new Url param", () => {
const url = 'https://my-site.com/some/path/to/resouce';
const updatedUrl = updateUrlParams(url, { newParam: 'newValue' });
expect(updatedUrl).toBe('https://my-site.com/some/path/to/resouce?newParam=newValue');
});
});


Loading