Skip to content

Commit

Permalink
Authkey is currently not sent for WMS GetLegendGraphic requests #10727 (
Browse files Browse the repository at this point in the history
#10765)

* added authkey parameters in secure image

* fix: use query-string, add test
  • Loading branch information
rowheat02 authored Jan 27, 2025
1 parent 59fa0f4 commit fc230b6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
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');
});
});


0 comments on commit fc230b6

Please sign in to comment.