Skip to content

Commit

Permalink
add basic auth support for map server
Browse files Browse the repository at this point in the history
Signed-off-by: Benstone Zhang <[email protected]>
  • Loading branch information
benstonezhang committed Nov 26, 2023
1 parent f97c91c commit e79ee72
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 10 deletions.
3 changes: 2 additions & 1 deletion css/adminSettings.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#graphhopperURL,
#graphhopperAPIKEY,
#mapboxAPIKEY,
#maplibreStreetStyleURL
#maplibreStreetStyleURL,
#maplibreStreetStyleAuth
{
width: 400px;
}
3 changes: 2 additions & 1 deletion lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ private function addCsp($response): void {
'osrmCarURL',
'osrmFootURL',
'graphhopperURL',
'maplibreStreetStyleURL'
'maplibreStreetStyleURL',
'maplibreStreetStyleAuth'
];
foreach ($urlKeys as $key) {
$url = $this->config->getAppValue('maps', $key);
Expand Down
1 change: 1 addition & 0 deletions lib/Controller/PublicUtilsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public function getOptionsValues(): DataResponse {
'graphhopperAPIKEY',
'mapboxAPIKEY',
'maplibreStreetStyleURL',
'maplibreStreetStyleAuth',
'graphhopperURL'
];
foreach ($settingsKeys as $k) {
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/UtilsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public function getOptionsValues($myMapId=null): DataResponse {
'graphhopperAPIKEY',
'mapboxAPIKEY',
'maplibreStreetStyleURL',
'maplibreStreetStyleAuth',
'graphhopperURL'
];
foreach ($settingsKeys as $k) {
Expand All @@ -175,6 +176,7 @@ public function setRoutingSettings($values): DataResponse {
'graphhopperAPIKEY',
'mapboxAPIKEY',
'maplibreStreetStyleURL',
'maplibreStreetStyleAuth',
'graphhopperURL'
];
foreach ($values as $k=>$v) {
Expand Down
1 change: 1 addition & 0 deletions lib/Settings/AdminSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function getForm() {
'graphhopperAPIKEY',
'mapboxAPIKEY',
'maplibreStreetStyleURL',
'maplibreStreetStyleAuth',
'graphhopperURL'
];
$parameters = [];
Expand Down
10 changes: 6 additions & 4 deletions src/adminSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ $(document).ready(function() {
'input#graphhopperAPIKEY, ' +
'#osrmDEMO, ' +
'input#mapboxAPIKEY, ' +
'input#maplibreStreetStyleURL', function(e) {
var value = $(this).val();
setMapsRoutingSettings($(this).attr('id'), value);
});
'input#maplibreStreetStyleURL, ' +
'input#maplibreStreetStyleAuth',
function(e) {
var value = $(this).val();
setMapsRoutingSettings($(this).attr('id'), value);
});
$('body').on('change', '#osrmDEMO', function(e) {
var value = $(this).is(':checked') ? '1' : '0';
setMapsRoutingSettings($(this).attr('id'), value);
Expand Down
24 changes: 22 additions & 2 deletions src/components/Map.vue
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ import 'mapbox-gl/dist/mapbox-gl'
import 'mapbox-gl/dist/mapbox-gl.css'
import 'mapbox-gl-leaflet/leaflet-mapbox-gl'
import '@maplibre/maplibre-gl-leaflet'
import ResourceType from 'maplibre-gl'
import {
baseLayersByName,
overlayLayersByName,
Expand Down Expand Up @@ -784,18 +785,37 @@ export default {
if ((gl !== null) &&
('maplibreStreetStyleURL' in this.optionValues && this.optionValues.maplibreStreetStyleURL !== '')) {
let token = null
if ('maplibreStreetStyleAuth' in this.optionValues && this.optionValues.maplibreStreetStyleAuth !== '') {
token = this.optionValues.maplibreStreetStyleAuth
}
// wrapper to make tile layer component correctly pass arguments
L.myMaplibreGL = (url, options) => {
if (token !== null) {
token = 'Basic ' + btoa(token)
const oldTransform = options.transformRequest
options.transformRequest = (url, resourceType) => {
const param = oldTransform?.() || {}
param.url = param.url || url
if (resourceType === ResourceType.Tile) {
param.type = 'arrayBuffer'
}
param.headers = param.headers || {}
param.headers.Authorization = token
return param
}
}
return new L.maplibreGL(options)
}
this.allBaseLayers = {}
Object.keys(baseLayersByName).forEach(id => {
if (id === 'Open Street Map') {
let layer = Object.assign({}, baseLayersByName[id]);
const layer = Object.assign({}, baseLayersByName[id])
delete layer.url
layer.tileLayerClass = L.myMaplibreGL
layer.options = Object.assign({}, layer.options);
layer.options = Object.assign({}, layer.options)
layer.options.style = this.optionValues.maplibreStreetStyleURL
layer.options.minZoom = 0
layer.options.maxZoom = 22
Expand Down
6 changes: 4 additions & 2 deletions templates/adminSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@
</div><br/>

<h3><a href="https://maplibre.org/" title="<?php p($l->t('MapLibre Website')) ?>" target="_blank"><?php p($l->t('MapLibre settings')); ?></a></h3>
<p><?php p($l->t('Set the URL of style.json for OpenStreetMap Vector Tiles with MapLibre-GL-JS')); ?></p>
<p><?php p($l->t('Set the URL and Basic Authorization of style.json for OpenStreetMap Vector Tiles with MapLibre-GL-JS.')); ?></p>
<p><?php p($l->t('Leave empty to disable.')); ?></p><br/>
<div id="maplibre_street">
<label for="maplibreStreetStyleURL"><?php p($l->t('MapLibre Street style URL')); ?></label><br/>
<input id="maplibreStreetStyleURL" type="text" value="<?php if (isset($_['maplibreStreetStyleURL'])) p($_['maplibreStreetStyleURL']); ?>"/>
<input id="maplibreStreetStyleURL" type="text" value="<?php if (isset($_['maplibreStreetStyleURL'])) p($_['maplibreStreetStyleURL']); ?>"/><br/>
<label for="maplibreStreetStyleAuth"><?php p($l->t('Basic Authorization if required. Format is "user:password"')); ?></label><br/>
<input id="maplibreStreetStyleAuth" type="text" value="<?php if (isset($_['maplibreStreetStyleAuth'])) p($_['maplibreStreetStyleAuth']); ?>"/>
</div><br/>
</div>

0 comments on commit e79ee72

Please sign in to comment.