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

[Fixes #973] Improve LOCKDOWN_GEONODE #972

Merged
merged 5 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 21 additions & 1 deletion geonode_mapstore_client/client/js/api/geonode/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,28 @@ import { getConfigProp } from '@mapstore/framework/utils/ConfigUtils';
* @name api.geonode.user
*/


export const addApiTokenIfNeeded = (url) => {
const geoNodePageConfig = window.__GEONODE_CONFIG__ || {};
const apikey = geoNodePageConfig.apikey || null;

/*
In case of LOCKDOWN_MODE in geonode, we need to check if the search page
contains an APIKEY. This is required because otherwise the endpoint
will always raise an error due the missing auth. In this way if the
main call provide an apikey, we can proceed with the login
*/
var _url = url;
if (apikey) {
_url += '?apikey=' + apikey;
}

return _url;
};

export const getUserInfo = () => {
const { endpointV1 = '/api' } = getConfigProp('geoNodeApi') || {};
return axios.get(`${endpointV1}/o/v4/userinfo`)
var url = `${endpointV1}/o/v4/userinfo`;
return axios.get(addApiTokenIfNeeded(url))
.then(({ data }) => data);
};
15 changes: 12 additions & 3 deletions geonode_mapstore_client/client/js/api/geonode/v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,22 @@ function addCountToLabel(name, count) {
// some fields such as search_fields does not support the array notation `key[]=value1&key[]=value2`
// this function will parse all values included array in the `key=value1&key=value2` format
function addQueryString(requestUrl, params) {
if (!params) {

const geoNodePageConfig = window.__GEONODE_CONFIG__ || {};
const geonodeApiKey = geoNodePageConfig.apikey || null;

var _params = params;

if (geonodeApiKey) {
_params.search_fields = geonodeApiKey;
}
if (!_params) {
return requestUrl;
}
const queryString = Object.keys(params)
const queryString = Object.keys(_params)
.reduce((str, key, idx) => {
const start = idx === 0 ? '?' : '&';
const values = castArray(params[key]);
const values = castArray(_params[key]);
if (values.length > 1) {
return str + values.reduce((valStr, value, jdx) => {
return valStr + (jdx === 0 ? start : '&') + key + '=' + value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% load client_lib_tags %}
{% load base_tags %}
{% load get_menu_json %}
{% load apikey %}
{% comment %}
app and map configuration need to be normalized
{% endcomment %}
Expand All @@ -12,6 +13,8 @@

{% get_menu_json 'CARDS_MENU' as CARDS_MENU %}
{{ CARDS_MENU|json_script:"menu-CARDS_MENU" }}
{% generate_proxyurl PROXY_URL|default:"/proxy/?url=" request as UPDATED_PROXY_URL %}
{% retrieve_apikey request as user_apikey %}

<script>
(function(){
Expand Down Expand Up @@ -62,7 +65,7 @@
const siteName = '{{ SITE_NAME }}' || 'Geonode';
const geoServerPublicLocation = '{{ GEOSERVER_PUBLIC_LOCATION }}' || '';
const isMobile = '{{ request.user_agent.is_mobile }}' === 'True' ? true : false;

window.__GEONODE_CONFIG__ = {
languageCode: '{{ LANGUAGE_CODE }}',
languages: languages,
Expand All @@ -71,10 +74,11 @@
resourceType: '{{ resource.resource_type|default:"" }}',
isEmbed: isEmbed,
pluginsConfigKey: pluginsConfigKey,
apikey: "{%if user_apikey %}{{user_apikey}}{% else %}{% endif %}",
pluginsConfigPatchRules: pluginsConfigPatchRules,
localConfig: {
proxyUrl: {
url: '{{ PROXY_URL|default:"/proxy/?url=" }}',
url: '{{UPDATED_PROXY_URL|safe }}',
useCORS: []
},
extensionsFolder: extensionsFolder,
Expand Down
27 changes: 27 additions & 0 deletions geonode_mapstore_client/templatetags/apikey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import logging
from urllib.parse import urlparse

from django.conf import settings
from geonode.base.auth import extract_user_from_headers, get_auth_token

from django import template

logger = logging.getLogger(__name__)
register = template.Library()


@register.simple_tag()
def generate_proxyurl(_url, request):
if request:
apikey = request.GET.get('apikey')
if apikey:
pproxyurl = urlparse(_url)
proxyurl = f'{pproxyurl.path}?apikey={apikey}&{pproxyurl.query}'
return proxyurl
return _url


@register.simple_tag()
def retrieve_apikey(request):
if settings.ENABLE_APIKEY_LOGIN:
return get_auth_token(request.user) or None