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

addWMS - authentication for title layer #54

Open
Ralikutty opened this issue Feb 24, 2023 · 8 comments
Open

addWMS - authentication for title layer #54

Ralikutty opened this issue Feb 24, 2023 · 8 comments
Labels
help wanted ❤️ we'd love your help!

Comments

@Ralikutty
Copy link

Ralikutty commented Feb 24, 2023

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
Copy link
Owner

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

@Ralikutty
Copy link
Author

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

@trafficonese trafficonese added the help wanted ❤️ we'd love your help! label Feb 28, 2023
@kardemummabulle
Copy link

Hello,

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

@trafficonese
Copy link
Owner

trafficonese commented Sep 7, 2024

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
Copy link

kardemummabulle commented Sep 27, 2024

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
Copy link
Owner

trafficonese commented Sep 27, 2024

@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
Copy link

@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
Copy link

@trafficonese

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted ❤️ we'd love your help!
Projects
None yet
Development

No branches or pull requests

3 participants