trafficonese / leaflet.extras2

Extra functionality for leaflet R package.
https://trafficonese.github.io/leaflet.extras2/
GNU General Public License v3.0
85 stars 20 forks source link

addWMS - authentication for title layer #54

Open Ralikutty opened 1 year ago

Ralikutty commented 1 year ago

Hi,

I am using a leaflet map in my RShiny application and would like to access an authenticated title layer. From the documentation I see that this feature is not currently supported. If there a way to add an authorisation header(bearer token authentication) to my request using the addWMS ? Or is there a workaround for the same ?

Many thanks in advance!

trafficonese commented 1 year ago

Hey, have you tried this? https://gis.stackexchange.com/a/187239

Ralikutty commented 1 year ago

Hi, Thanks for your prompt reply :) I need to use bearer token authentication which means I need to pass a bearer token in the URL instead. I have tried the solution as suggested in https://community.rstudio.com/t/r-leaflet-wms-authorization-header/52574 with no luck. I get the same error - https://drive.google.com/file/d/1gbTpzE8oj7AehhcOIoQ4actyF3k5iQwE/view?usp=share_link

kardemummabulle commented 2 months ago

Hello,

Anyone had luck finding a workaround? I feel like I tried everything with no success :( Thanks in advance!

trafficonese commented 2 months ago

I just pushed a new branch wmsheader, where you can define the header yourself.

Can you install it with: remotes::install_github("trafficonese/leaflet.extras2@wmsheader")

and then test your Layer with an app like this:

library(shiny)
library(leaflet)
library(leaflet.extras2)

LAYER <- "tiger:poi"
TOKEN <- paste0("user:strongpwd")

ui <- fluidPage(
  leafletOutput("map", height = "500px")
)
server <- function(input, output, session) {
  output$map <- renderLeaflet({
    leaflet()  %>%
      addTiles(group = "base") %>%
      addWMSHeader(baseUrl = "http://localhost:8080/geoserver/wms",
                   layers = LAYER,
                   options = leaflet::WMSTileOptions(transparent = TRUE, format = "image/png"),
                   header = list(
                     list("header" = "Authorization",
                          "value" = paste0("Basic ", base64enc::base64encode(charToRaw(TOKEN))) ),
                     list("header" = "content-type",
                          "value" = "text/plain")
                   )
      ) %>%
      addLayersControl(baseGroups = "base", overlayGroups = LAYER)

  })
}
shinyApp(ui, server)
kardemummabulle commented 1 month ago

Hi,

Thanks for the suggestion @trafficonese. Unfortunately it didn't work for me but I solved it in another way. Attaching the example below.

I downloaded plugins from [ticinum-aerospace] via cmd and saved them i Shiny’s “www” folder. And then just re-wrote the code a bit.

library(shiny)
library(leaflet)
library(htmltools)
library(httr)
library(Rcpp)

wms_headerPlugin <- htmlDependency(
  name="leaflet-wms-header",
  version="1.0.8",
  src="www/leaflet-wms-header",
  script="index.js"
)

ui=shinyUI(
  bootstrapPage(theme="slate.min.css",
                useShinyjs(),
                add_busy_bar(color="white",height="8px"),              
                # Page size & html tags
                tags$style(type="text/css","html, body {width:100%; height:95.5%}"),  
                # Leaflet map base map size                     
                leafglOutput("Map",width="100%",height="100%"), 
                tags$head(
                  wms_headerPlugin # activating the plugin here
                )
  ) 
) 

server=function(input, output, session) {
  output$Map <- renderLeaflet({
    leaflet() %>%
      addTiles(layerId="Esri.WorldImagery") %>%
      setView(lng=16,lat=62,zoom=5) %>%
      htmlwidgets::onRender("
        function(el, x) {
          var map = this;
          var wmsUrl = 'URL FOR WMS';
          var wmsOptions = {
            layers: 'LAYER',
            format: 'image/png',
            crs: L.CRS.EPSG3006,
            transparent: true
          };
          var wmsHeaders = [
            { header: 'Authorization', value: 'Basic ' + btoa('USERNAME:PASSWORD') },
            { header: 'content-type', value: 'text/plain' }
          ];
          var wmsLayer = L.TileLayer.wmsHeader(wmsUrl, wmsOptions, wmsHeaders);
          map.addLayer(wmsLayer);
        }
      ")
  })
}

shinyApp(ui=ui,server=server)
trafficonese commented 1 month ago

@kardemummabulle thanks for your input. I will have a look at the leaflet-wms-header plugin and the possibility to merge both plugins, as currently I think you won't have the getFeatureInfo functionality of addWMS.

kardemummabulle commented 1 month ago

@trafficonese thanks!

It is indeed a real struggle, I can for instance see the WMS if I render it in the main output$Map but integrating it into observe({ }) part in more complex applications gives a battle right now. And I am not winning it yet :)

Hope there is something that can be done at the package level.

kardemummabulle commented 1 week ago

@trafficonese

Hello, any luck integrating leaflet-wms-header with leaflet.extras2? :)