1414# ' # first, create the yml file using use_auth0() function
1515# '
1616# ' if (interactive()) {
17+ # '
1718# ' # ui.R file
1819# ' library(shiny)
1920# ' library(auth0)
2728# ' options(shiny.port = 8080)
2829# ' shiny::runApp()
2930# ' }
31+ # '
3032# ' }
3133# ' @export
3234auth0_ui <- function (ui , info ) {
@@ -36,53 +38,39 @@ auth0_ui <- function(ui, info) {
3638 } else {
3739 if (missing(info )) info <- auth0_info()
3840 function (req ) {
39- verify <- has_auth_code(
40- shiny :: parseQueryString(req $ QUERY_STRING ),
41- info $ state
42- )
41+ verify <- has_auth_code(shiny :: parseQueryString(req $ QUERY_STRING ), info $ state )
4342 if (! verify ) {
4443 if (grepl(" error=unauthorized" , req $ QUERY_STRING )) {
4544 redirect <- sprintf(" location.replace(\" %s\" );" , logout_url())
4645 shiny :: tags $ script(shiny :: HTML(redirect ))
4746 } else {
47+
4848 params <- shiny :: parseQueryString(req $ QUERY_STRING )
4949 params $ code <- NULL
5050 params $ state <- NULL
5151
52- # Reconstruct the query string
53- query <- paste(
52+ query_params <- paste(
5453 mapply(paste , names(params ), params , MoreArgs = list (sep = " =" )),
55- collapse = " &"
56- )
57- query <- if (query != " " ) paste0(" ?" , query ) else " "
58-
59- # Preserve the original path (req$PATH_INFO) in the redirect URI
60- if (grepl(" 127.0.0.1" , req $ HTTP_HOST )) {
61- redirect_uri <- paste0(
62- " http://" ,
63- gsub(" 127.0.0.1" , " localhost" , req $ HTTP_HOST ),
64- req $ PATH_INFO ,
65- query
66- )
54+ collapse = " &" )
55+ query <- if (query_params != " " ) paste0(" /?" , query_params ) else " /"
56+ if (! is.null(info $ remote_url ) && info $ remote_url != " " && ! getOption(" auth0_local" )) {
57+ redirect_uri <- paste0(info $ remote_url , query )
6758 } else {
68- redirect_uri <- paste0(
69- " http://" ,
70- req $ HTTP_HOST ,
71- req $ PATH_INFO ,
72- query
73- )
59+ if (grepl(" 127.0.0.1" , req $ HTTP_HOST )) {
60+ redirect_uri <- paste0(" http://" , gsub(" 127.0.0.1" , " localhost" , req $ HTTP_HOST ), query )
61+ } else {
62+ redirect_uri <- paste0(" http://" , req $ HTTP_HOST , query )
63+ }
7464 }
7565 redirect_uri <<- redirect_uri
76-
77- # Generate the Auth0 authorization URL
78- query_extra <- if (is.null(info $ audience )) list () else
79- list (audience = info $ audience )
66+ query_extra <- if (! is.null(info $ audience ) || ! is.null(info $ extra_params )) {
67+ c(info $ extra_params , list (audience = info $ audience ))
68+ } else {
69+ NULL
70+ }
8071 url <- httr :: oauth2.0_authorize_url(
81- info $ api ,
82- info $ app(redirect_uri ),
83- scope = info $ scope ,
84- state = info $ state ,
85- query_extra = query_extra
72+ info $ api , info $ app(redirect_uri ), scope = info $ scope , state = info $ state ,
73+ query_extra = query_extra
8674 )
8775 redirect <- sprintf(" location.replace(\" %s\" );" , url )
8876 shiny :: tags $ script(shiny :: HTML(redirect ))
@@ -103,21 +91,20 @@ auth0_ui <- function(ui, info) {
10391# ' @param server the shiny server function.
10492# ' @param info object returned from [auth0_info]. If not informed,
10593# ' will try to find the `_auth0.yml` and create it automatically.
94+ # ' @param remove_callback_params whether to remove the `code` and `state` query.
10695# '
10796# ' @export
108- auth0_server <- function (server , info ) {
97+ auth0_server <- function (server , info , remove_callback_params = TRUE ) {
10998 disable <- getOption(" auth0_disable" )
11099 if (! is.null(disable ) && disable ) {
111100 server
112101 } else {
113102 if (missing(info )) info <- auth0_info()
114103 function (input , output , session ) {
115- shiny :: isolate(auth0_server_verify(
116- session ,
117- info $ app ,
118- info $ api ,
119- info $ state
120- ))
104+ shiny :: isolate(auth0_server_verify(session , info $ app , info $ api , info $ state ))
105+ if (remove_callback_params ) {
106+ auth0_remove_callback_params(session )
107+ }
121108 shiny :: observeEvent(input [[" ._auth0logout_" ]], logout())
122109 server(input , output , session )
123110 }
@@ -132,6 +119,8 @@ auth0_server <- function(server, info) {
132119# ' @param ui an ordinary UI object to create shiny apps.
133120# ' @param server an ordinary server object to create shiny apps.
134121# ' @param config_file path to YAML configuration file.
122+ # ' @param remove_callback_params whether to remove the `code` and `state` query
123+ # ' parameters from the URL after successful authentication. Defaults to `TRUE`.
135124# ' @param ... Other arguments passed on to [shiny::shinyApp()].
136125# '
137126# ' @details
@@ -147,7 +136,8 @@ auth0_server <- function(server, info) {
147136# ' disable auth0 temporarily.
148137# '
149138# ' @export
150- shinyAppAuth0 <- function (ui , server , config_file = NULL , ... ) {
139+ shinyAppAuth0 <- function (ui , server , config_file = NULL , remove_callback_params = TRUE , ... ) {
140+
151141 disable <- getOption(" auth0_disable" )
152142 if (! is.null(disable ) && disable ) {
153143 shiny :: shinyApp(ui , server , ... )
@@ -156,7 +146,11 @@ shinyAppAuth0 <- function(ui, server, config_file = NULL, ...) {
156146 config_file <- auth0_find_config_file()
157147 }
158148 info <- auth0_info(config_file )
159- shiny :: shinyApp(auth0_ui(ui , info ), auth0_server(server , info ), ... )
149+ shiny :: shinyApp(
150+ auth0_ui(ui , info ),
151+ auth0_server(server , info , remove_callback_params ),
152+ ...
153+ )
160154 }
161155}
162156
@@ -171,10 +165,8 @@ shinyAppAuth0 <- function(ui, server, config_file = NULL, ...) {
171165# '
172166# ' @export
173167shinyAuth0App <- function (ui , server , config_file = NULL ) {
174- warning(
175- " `shinyAuth0App()` is soft-deprecated as of auth0 0.1.2." ,
176- " Please use `shinyAppAuth0()` instead."
177- )
168+ warning(" `shinyAuth0App()` is soft-deprecated as of auth0 0.1.2." ,
169+ " Please use `shinyAppAuth0()` instead." )
178170 shinyAppAuth0(ui , server , config_file )
179171}
180172
@@ -221,12 +213,14 @@ shinyAuth0App <- function(ui, server, config_file = NULL) {
221213# ' }
222214# ' shinyAuth0App(ui, server, config_file)
223215# ' }
216+ # '
224217# ' }
225218# '
219+ # '
226220# ' @export
227221auth0_logout_url <- function (config_file = NULL , redirect_js = TRUE ) {
228- stop(
229- " `auth0_logout_url()` is deprecated. " ,
230- " See `?logoutButton()` to add a logout button in auth0 apps."
231- )
222+
223+ stop( " `auth0_logout_url()` is deprecated. " ,
224+ " See `?logoutButton()` to add a logout button in auth0 apps." )
225+
232226}
0 commit comments