Skip to content

Commit c52d08d

Browse files
authored
Update key when needed. (#54)
* Update key when needed. * Bump vnum. * Pass team_id through to server. * Use package environment Specifically for team_id.
1 parent 2f1f83d commit c52d08d

18 files changed

+153
-28
lines changed

.Rbuildignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
^LICENSE\.md$
55
^CODE_OF_CONDUCT\.md$
66
^app\.R
7+
^apptest\.R
78
^bad_token\.R
89
^\.secret
910
^data-raw$

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: shinyslack
22
Title: Integrate Slack and Shiny
3-
Version: 0.0.0.9009
3+
Version: 0.0.0.9010
44
Authors@R:
55
person("Jon", "Harmon", , "jonthegeek@gmail.com", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0003-4781-4346"))

NAMESPACE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(check_login)
4+
export(get_shinyslack_team_id)
45
export(shinyslack_app)
5-
export(slack_shiny_ui)
66
export(user_info)
77
importFrom(rlang,"%||%")

NEWS.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
# shinyslack 0.0.0.9006
1+
# shinyslack 0.0.0.9010
22

3+
* New exported function get_shinyslack_team_id() provides the team id for the Slack workspace used for login.
4+
* Breaking: Removed slack_shiny_ui() from exports.
35
* Breaking: The site url is now automatically determined, so site_url is no longer an argument to any function.
46
* 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.
57
* Abstracted UI switcher into [{scenes}](https://github.com/shinyworks/scenes) package.

R/cookies.R

+14-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#' @return A [shiny::reactive()] which returns a logical indicating whether the
4545
#' user is logged in with proper API access.
4646
#' @export
47-
check_login <- function(team_id,
47+
check_login <- function(team_id = get_shinyslack_team_id(),
4848
session = shiny::getDefaultReactiveDomain(),
4949
shinyslack_key = Sys.getenv("SHINYSLACK_KEY")) {
5050
return(
@@ -67,3 +67,16 @@ check_login <- function(team_id,
6767
)
6868
return(.shinyslack_decrypt(cookie_token, shinyslack_key))
6969
}
70+
71+
.update_shinyslack_api_key <- function(slack_api_key,
72+
team_id,
73+
session,
74+
shinyslack_key) {
75+
if (is.null(slack_api_key)) {
76+
slack_token <- .get_slack_cookie_token(team_id, shinyslack_key, session)
77+
if (.validate_slack_token(slack_token, team_id)) {
78+
session$userData$shinyslack_api_key <- slack_token
79+
}
80+
}
81+
return(session$userData$shinyslack_api_key)
82+
}

R/encrypt.R

+8-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99
.shinyslack_encrypt <- function(string,
1010
shinyslack_key = Sys.getenv("SHINYSLACK_KEY")) {
1111
if (isTRUE(as.logical(nchar(shinyslack_key)))) {
12-
cli::cli_inform(c("shinyslack_key set.", v = "Encrypting string."))
12+
cli::cli_inform(c(
13+
"shinyslack: shinyslack_key set.",
14+
v = "Encrypting string."
15+
))
1316
string <- .sodium_encrypt(string, shinyslack_key)
1417
} else {
15-
cli::cli_warn(c("shinyslack_key not found.", x = "String not encoded."))
18+
cli::cli_warn(c(
19+
"shinyslack: shinyslack_key not found.",
20+
x = "String not encoded."
21+
))
1622
}
1723

1824
return(string)

R/shinyslack-package.R

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#' @keywords internal
2+
"_PACKAGE"
3+
4+
## usethis namespace: start
5+
## usethis namespace: end
6+
NULL
7+
8+
the <- rlang::new_environment()
9+
the$team_id <- character()

R/uis.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122

123123
if (is.null(url)) {
124124
cli::cli_abort(
125-
message = c(x = "Could not determine url.")
125+
message = c(x = "shinyslack: Could not determine url.")
126126
)
127127
}
128128

R/user.R

+16-7
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,27 @@ user_info <- function(components = c("user_id",
1818
"display_name",
1919
"pronouns",
2020
"user_name"),
21-
...,
2221
session = shiny::getDefaultReactiveDomain(),
23-
slack_api_key = session$userData$shinyslack_api_key) {
22+
slack_api_key = session$userData$shinyslack_api_key,
23+
team_id = get_shinyslack_team_id(),
24+
shinyslack_key = Sys.getenv("SHINYSLACK_KEY")) {
2425
components <- match.arg(components, several.ok = TRUE)
25-
rlang::check_dots_empty()
2626
return(
2727
shiny::reactive({
28-
basics <- slackcalls::post_slack("auth.test", token = slack_api_key)
29-
if (all(components %in% c("user_id", "user_name"))) {
30-
return(c(user_id = basics$user_id, user_name = basics$user)[components])
28+
slack_api_key <- .update_shinyslack_api_key(
29+
slack_api_key,
30+
team_id,
31+
session,
32+
shinyslack_key
33+
)
34+
if (!is.null(slack_api_key)) {
35+
basics <- slackcalls::post_slack("auth.test", token = slack_api_key)
36+
if (all(components %in% c("user_id", "user_name"))) {
37+
return(c(user_id = basics$user_id, user_name = basics$user)[components])
38+
}
39+
return(.get_more_user_info(basics$user_id, slack_api_key, components))
3140
}
32-
return(.get_more_user_info(basics$user_id, slack_api_key, components))
41+
return(NULL)
3342
})
3443
)
3544
}

R/ui_wrapper.R renamed to R/wrapper.R

+27-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ shinyslack_app <- function(ui,
1616
shinyslack_key = Sys.getenv("SHINYSLACK_KEY")) {
1717
dots <- rlang::list2(...)
1818
dots$options <- .parse_app_args(dots$options)
19+
set_shinyslack_team_id(team_id)
1920

2021
return(
2122
rlang::exec(
@@ -56,7 +57,7 @@ shinyslack_app <- function(ui,
5657
#'
5758
#' @return A function defining the UI of a Shiny app (either with login or
5859
#' without).
59-
#' @export
60+
#' @keywords internal
6061
slack_shiny_ui <- function(ui,
6162
team_id,
6263
expiration = 90,
@@ -90,3 +91,28 @@ slack_shiny_ui <- function(ui,
9091
)
9192
)
9293
}
94+
95+
#' Get the current team_id
96+
#'
97+
#' The `team_id` is set when an app is launched. In almost all cases, that value
98+
#' is the one you will want for any instances of `team_id`.
99+
#'
100+
#' @return A string representing the team_id.
101+
#' @export
102+
#'
103+
#' @examples
104+
#' # If no app is active, the team_id will be a zero-length character vector.
105+
#' get_shinyslack_team_id()
106+
#'
107+
#' set_shinyslack_team_id("T123456")
108+
#' get_shinyslack_team_id()
109+
get_shinyslack_team_id <- function() {
110+
return(the$team_id)
111+
}
112+
113+
#' @inheritParams .shared-parameters
114+
#' @rdname get_shinyslack_team_id
115+
set_shinyslack_team_id <- function(team_id) {
116+
the$team_id <- team_id
117+
return(the$team_id)
118+
}

app.R

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,20 @@ pkgload::load_all(
66
quiet = TRUE
77
)
88

9-
team_id <- "T6UC1DKJQ"
9+
team_id_errorproof <- "T6UC1DKJQ"
1010
Sys.unsetenv("SLACK_API_TOKEN")
1111

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

1414
server <- function(input, output, session) {
15-
username <- user_info(components = "display_name")
16-
15+
user_name <- user_info(components = "display_name")
1716
output$user_name <- shiny::renderText({
18-
shiny::req(check_login(team_id)())
19-
username()
17+
user_name()
2018
})
2119
}
2220

2321
shinyslack_app(
2422
ui = ui,
2523
server = server,
26-
team_id = team_id
24+
team_id = team_id_errorproof
2725
)

man/dot-parse_app_args.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/dot-shinyslack_decrypt.Rd

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/shinyslack-package.Rd

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/shinyslack_app.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/shinyslack_team_id.Rd

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/slack_shiny_ui.Rd

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/user_info.Rd

+9-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)