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

Update key when needed. #54

Merged
merged 4 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
^LICENSE\.md$
^CODE_OF_CONDUCT\.md$
^app\.R
^apptest\.R
^bad_token\.R
^\.secret
^data-raw$
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: shinyslack
Title: Integrate Slack and Shiny
Version: 0.0.0.9009
Version: 0.0.0.9010
Authors@R:
person("Jon", "Harmon", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-4781-4346"))
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(check_login)
export(get_shinyslack_team_id)
export(shinyslack_app)
export(slack_shiny_ui)
export(user_info)
importFrom(rlang,"%||%")
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# shinyslack 0.0.0.9006
# shinyslack 0.0.0.9010

* New exported function get_shinyslack_team_id() provides the team id for the Slack workspace used for login.
* Breaking: Removed slack_shiny_ui() from exports.
* Breaking: The site url is now automatically determined, so site_url is no longer an argument to any function.
* Breaking: The "real" ui is no longer automatically wrapped in a cookie handler. Many UIs that use shinyslack won't need to deal with cookies in the normal app, so we shouldn't add extra, unnecessary javascript.
* Abstracted UI switcher into [{scenes}](https://github.com/shinyworks/scenes) package.
Expand Down
15 changes: 14 additions & 1 deletion R/cookies.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#' @return A [shiny::reactive()] which returns a logical indicating whether the
#' user is logged in with proper API access.
#' @export
check_login <- function(team_id,
check_login <- function(team_id = get_shinyslack_team_id(),
session = shiny::getDefaultReactiveDomain(),
shinyslack_key = Sys.getenv("SHINYSLACK_KEY")) {
return(
Expand All @@ -67,3 +67,16 @@ check_login <- function(team_id,
)
return(.shinyslack_decrypt(cookie_token, shinyslack_key))
}

.update_shinyslack_api_key <- function(slack_api_key,
team_id,
session,
shinyslack_key) {
if (is.null(slack_api_key)) {
slack_token <- .get_slack_cookie_token(team_id, shinyslack_key, session)
if (.validate_slack_token(slack_token, team_id)) {
session$userData$shinyslack_api_key <- slack_token
}
}
return(session$userData$shinyslack_api_key)
}
10 changes: 8 additions & 2 deletions R/encrypt.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
.shinyslack_encrypt <- function(string,
shinyslack_key = Sys.getenv("SHINYSLACK_KEY")) {
if (isTRUE(as.logical(nchar(shinyslack_key)))) {
cli::cli_inform(c("shinyslack_key set.", v = "Encrypting string."))
cli::cli_inform(c(
"shinyslack: shinyslack_key set.",
v = "Encrypting string."
))
string <- .sodium_encrypt(string, shinyslack_key)
} else {
cli::cli_warn(c("shinyslack_key not found.", x = "String not encoded."))
cli::cli_warn(c(
"shinyslack: shinyslack_key not found.",
x = "String not encoded."
))
}

return(string)
Expand Down
9 changes: 9 additions & 0 deletions R/shinyslack-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#' @keywords internal
"_PACKAGE"

## usethis namespace: start
## usethis namespace: end
NULL

the <- rlang::new_environment()
the$team_id <- character()
2 changes: 1 addition & 1 deletion R/uis.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@

if (is.null(url)) {
cli::cli_abort(
message = c(x = "Could not determine url.")
message = c(x = "shinyslack: Could not determine url.")
)
}

Expand Down
23 changes: 16 additions & 7 deletions R/user.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,27 @@ user_info <- function(components = c("user_id",
"display_name",
"pronouns",
"user_name"),
...,
session = shiny::getDefaultReactiveDomain(),
slack_api_key = session$userData$shinyslack_api_key) {
slack_api_key = session$userData$shinyslack_api_key,
team_id = get_shinyslack_team_id(),
shinyslack_key = Sys.getenv("SHINYSLACK_KEY")) {
components <- match.arg(components, several.ok = TRUE)
rlang::check_dots_empty()
return(
shiny::reactive({
basics <- slackcalls::post_slack("auth.test", token = slack_api_key)
if (all(components %in% c("user_id", "user_name"))) {
return(c(user_id = basics$user_id, user_name = basics$user)[components])
slack_api_key <- .update_shinyslack_api_key(
slack_api_key,
team_id,
session,
shinyslack_key
)
if (!is.null(slack_api_key)) {
basics <- slackcalls::post_slack("auth.test", token = slack_api_key)
if (all(components %in% c("user_id", "user_name"))) {
return(c(user_id = basics$user_id, user_name = basics$user)[components])
}
return(.get_more_user_info(basics$user_id, slack_api_key, components))
}
return(.get_more_user_info(basics$user_id, slack_api_key, components))
return(NULL)
})
)
}
Expand Down
28 changes: 27 additions & 1 deletion R/ui_wrapper.R → R/wrapper.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ shinyslack_app <- function(ui,
shinyslack_key = Sys.getenv("SHINYSLACK_KEY")) {
dots <- rlang::list2(...)
dots$options <- .parse_app_args(dots$options)
set_shinyslack_team_id(team_id)

return(
rlang::exec(
Expand Down Expand Up @@ -56,7 +57,7 @@ shinyslack_app <- function(ui,
#'
#' @return A function defining the UI of a Shiny app (either with login or
#' without).
#' @export
#' @keywords internal
slack_shiny_ui <- function(ui,
team_id,
expiration = 90,
Expand Down Expand Up @@ -90,3 +91,28 @@ slack_shiny_ui <- function(ui,
)
)
}

#' Get the current team_id
#'
#' The `team_id` is set when an app is launched. In almost all cases, that value
#' is the one you will want for any instances of `team_id`.
#'
#' @return A string representing the team_id.
#' @export
#'
#' @examples
#' # If no app is active, the team_id will be a zero-length character vector.
#' get_shinyslack_team_id()
#'
#' set_shinyslack_team_id("T123456")
#' get_shinyslack_team_id()
get_shinyslack_team_id <- function() {
return(the$team_id)
}

#' @inheritParams .shared-parameters
#' @rdname get_shinyslack_team_id
set_shinyslack_team_id <- function(team_id) {
the$team_id <- team_id
return(the$team_id)
}
10 changes: 4 additions & 6 deletions app.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ pkgload::load_all(
quiet = TRUE
)

team_id <- "T6UC1DKJQ"
team_id_errorproof <- "T6UC1DKJQ"
Sys.unsetenv("SLACK_API_TOKEN")

ui <- shiny::fluidPage(shiny::textOutput("user_name"))

server <- function(input, output, session) {
username <- user_info(components = "display_name")

user_name <- user_info(components = "display_name")
output$user_name <- shiny::renderText({
shiny::req(check_login(team_id)())
username()
user_name()
})
}

shinyslack_app(
ui = ui,
server = server,
team_id = team_id
team_id = team_id_errorproof
)
2 changes: 1 addition & 1 deletion man/dot-parse_app_args.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/dot-shinyslack_decrypt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/shinyslack-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/shinyslack_app.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions man/shinyslack_team_id.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/slack_shiny_ui.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions man/user_info.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.