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

Fix #10506 Adding possibility to fetch legends images using Bearer token #10507

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
60 changes: 40 additions & 20 deletions web/client/plugins/TOC/components/Legend.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import PropTypes from 'prop-types';
import React from 'react';

import {
addAuthenticationParameter,
addAuthenticationToSLD,
clearNilValuesForParams
clearNilValuesForParams,
getAuthenticationMethod
} from '../../../utils/SecurityUtils';
import Message from '../../../components/I18N/Message';
import SecureImage from './SecureImage';
import { randomInt } from '../../../utils/RandomUtils';

/**
Expand Down Expand Up @@ -85,23 +86,26 @@ class Legend extends React.Component {

const cleanParams = clearNilValuesForParams(layer.params);
const scale = this.getScale(props);
let query = assign({}, {
service: "WMS",
request: "GetLegendGraphic",
format: "image/png",
height: props.legendHeight,
width: props.legendWidth,
layer: layer.name,
style: layer.style || null,
version: layer.version || "1.3.0",
SLD_VERSION: "1.1.0",
LEGEND_OPTIONS: props.legendOptions
}, layer.legendParams || {},
props.language && layer.localizedLayerStyles ? {LANGUAGE: props.language} : {},
addAuthenticationToSLD(cleanParams || {}, props.layer),
cleanParams && cleanParams.SLD_BODY ? {SLD_BODY: cleanParams.SLD_BODY} : {},
scale !== null ? { SCALE: scale } : {});
addAuthenticationParameter(url, query);
let query = assign(
{},
{
service: "WMS",
request: "GetLegendGraphic",
format: "image/png",
height: props.legendHeight,
width: props.legendWidth,
layer: layer.name,
style: layer.style || null,
version: layer.version || "1.3.0",
SLD_VERSION: "1.1.0",
LEGEND_OPTIONS: props.legendOptions
},
layer.legendParams || {},
props.language && layer.localizedLayerStyles ? {LANGUAGE: props.language} : {},
addAuthenticationToSLD(cleanParams || {}, props.layer),
cleanParams && cleanParams.SLD_BODY ? {SLD_BODY: cleanParams.SLD_BODY} : {},
scale !== null ? { SCALE: scale } : {}
);

return urlUtil.format({
host: urlObj.host,
Expand All @@ -114,7 +118,23 @@ class Legend extends React.Component {
}
render() {
if (!this.state.error && this.props.layer && this.props.layer.type === "wms" && this.props.layer.url) {
return <img onError={this.onImgError} onLoad={(e) => this.validateImg(e.target)} src={this.getUrl(this.props)} style={this.props.style}/>;
const url = this.getUrl(this.props);
const authMethod = getAuthenticationMethod(url);
return (
authMethod === "bearer" ?
<SecureImage
onError={this.onImgError}
onLoad={(e) => this.validateImg(e.target)}
src={url}
style={this.props.style}
/> :
<img
onError={this.onImgError}
onLoad={(e) => this.validateImg(e.target)}
src={url}
style={this.props.style}
/>
);
}
return <Message msgId="layerProperties.legenderror" />;
}
Expand Down
66 changes: 66 additions & 0 deletions web/client/plugins/TOC/components/SecureImage.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright 2024, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

import React, { useEffect, useState } from 'react';
import { connect } from 'react-redux';
import { createSelector } from 'reselect';

import { securityTokenSelector } from '../../../selectors/security';

const SecureImage = connect(
createSelector(
securityTokenSelector,
(token) => {
return { token };
}
),
{}
)(({
alt,
src,
token,
...props
}) => {
const [imageSrc, setImageSrc] = useState('');

useEffect(() => {
const fetchImage = async() => {
try {
const response = await fetch(src, {
headers: {
'Authorization': `Bearer ${token}`
}
});
if (response.ok) {
const blob = await response.blob();
const imageUrl = URL.createObjectURL(blob);
setImageSrc(imageUrl);
} else {
console.error('Failed to fetch image:', response.statusText);
}
} catch (error) {
console.error('Error fetching image:', error);
}
};

fetchImage();

// Clean up the URL object when the component unmounts
return () => {
if (imageSrc) {
URL.revokeObjectURL(imageSrc);
}
};
}, [src, token]);

return (
<img src={imageSrc} alt={alt} {...props} />
);
});

export default SecureImage;
Loading