SDITools / adobeanalyticsr

R Client for Adobe Analytics API v2.0
Other
18 stars 9 forks source link

Adding JWT Authentication #98

Closed charlie-gallagher closed 2 years ago

charlie-gallagher commented 2 years ago

Hi! I did my best to overhaul the authentication to handle both OAuth and JWT. As an informal overview, there would now be two steps to authenticating: setting the type with aw_auth_with and then calling aw_auth to actually carry out the authentication. The results are stored in a hidden environment .adobeanalytics and internal functions retrieve the token with retrieve_aw_token.

# Assume environment variables are set up for now
aw_auth_with("oauth")  # Sets auth type for session
aw_auth()

# Alternatively, you can pass the type directly to aw_auth
aw_auth("oauth")

There's a subtle difference between these two ways of authenticating. If you set aw_auth_with() in a new session, you can start calling query functions like aw_get_metrics without authenticating. If you don't set aw_auth_with() and try to call a query function, it will throw an error because it doesn't know which method to try by default.

You can also set the caching path and filename with the functions aw_auth_path and aw_auth_name.

I added a new R6 object, AdobeJwtToken, to handle the JWT authentication. This stores the httr request object as well as the user's authentication details. As with httr::oauth_service_token(), the response is never cached, but it can be refreshed with AdobeJwtToken$refresh().

Summary

Goals

Features

Problems

Breaking changes

My goal was none, although aw_token now throws a lifecycle warning. It would be nice to have no breaking changes if possible, so please help me if I made mistakes with the OAuth parts.

Final to-dos