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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Token as environment variable #68

Closed
mattkerlogue opened this issue Nov 14, 2022 · 6 comments
Closed

Token as environment variable #68

mattkerlogue opened this issue Nov 14, 2022 · 6 comments
Assignees

Comments

@mattkerlogue
Copy link

First off, huge thanks and massive congratulations for getting the rtoot package off the ground! 馃帀

It's meant that I've been able to port my Twitter bot over to the botsin.space instance.

Related to #7 and #18. I've had to adopt a different approach to authentication as it runs on GitHub Actions, therefore I can't make use of an RDS hidden away deep in the system to store the token.

The approach used in {rtweet} is to use environment variables and within the project I've implemented what appears to be a working solution that mimics this approach with {rtoot}. The workflow is as follows:

  • A new function mastodon_token() which creates the same underlying list structure as auth_setup() but to which you provide the access token, type and instance. I couldn't see an internal function that did this, but let me know if there is. This relies on the user generating their own key/token from within the Mastodon API settings, but it's pretty simple for folk that would want to get this complicated (and definitely easier than the approach with the Twitter API).

  • User writes their token to an .Renviron file, either at the user-level or project-level:

MASTODON_TOKEN="yoUrM4sToD0nt0k3N"
  • User then includes the following in their code:
# to use the defaults
toot_token <- mastodon_token()

# or to be explicit
toot_token <- mastodon_token(
  access_token = Sys.getenv("MASTODON_TOKEN"),
  type = "user",
  instance = "botsin.space"
)

rtoot::post_toot(
  status = status_msg,
  media = tmp_file,
  alt_text = alt_msg,
  token = toot_token
)

I discuss in more length in this blog post about migrating the bot to Mastodon.

Happy to contribute a PR if you'd like to include in rtoot's core functionality, but probably worth some consideration of how it interacts with the existing approach to authentication.

@mattkerlogue
Copy link
Author

@matt-dray has a list of 25 twitter bots that rely on this approach to authentication with {rtweet} and it'd be great to support them to migrate to the fediverse too.

@schochastics
Copy link
Member

Bots are an obvious use case for the environment approach to tokens (Haven't thought of that yet) so we need to address this somehow. However, I am also with @JBGruber (see #18 (comment)) that it is a bit clumsy to write 3 (well technically only 2 are needed) variables as system variables. I'd prefer a solution that allows both RDS and ENV. Like: "if ENV not found, look for RDS". RDS is more accessible in my opinion than telling users to create 2 system variables. @chainsawriot thoughts? @JBGruber you wrote the auth part, any suggestions from your side?

@chainsawriot
Copy link
Collaborator

@mattkerlogue Thanks for the suggestion. As you might know, we have used different names for testing. But I think that can be easily harmonized.

https://github.com/schochastics/rtoot/blob/a24b44de80f0da182c17f0e8cd753bef21c3e216/tests/testthat/setup-rtoot.R#L4-L6

@schochastics I have another solution actually. One envvar.

require(rtoot)
#> Loading required package: rtoot
Sys.setenv(RTOOT_DEFAULT_TOKEN="aslfjkhdjkfhskjlafhsadf;public;emacs.ch")
get_token_from_envvar <- function(envvar = "RTOOT_DEFAULT_TOKEN") {
    if (Sys.getenv(envvar) == "") {
        stop("envvar not found.")
    }
    res <- strsplit(x = Sys.getenv(envvar), split = ";")[[1]]
    bearer <- list(bearer = res[1])
    bearer$type <- res[2]
    bearer$instance <- res[3]
    class(bearer) <- "rtoot_bearer"
    bearer
}

get_token_from_envvar("whatever")
#> Error in get_token_from_envvar("whatever"): envvar not found.
get_token_from_envvar("RTOOT_DEFAULT_TOKEN")
#> <mastodon bearer token> for instance: emacs.ch of type: public

Created on 2022-11-14 by the reprex package (v2.0.1)

@JBGruber
Copy link
Contributor

I thought about something in the same lines as @chainsawriot. One token that is parsed to make the list object.

I think it would make sense to keep the option and add a condition to parse the environment variable if options("rtoot_token" = "environment").

@schochastics
Copy link
Member

Sounds good to me. @chainsawriot do you want to implement this?

@chainsawriot
Copy link
Collaborator

@mattkerlogue You can now convert your token

x <- convert_token_to_envvar(token)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants