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

Use token source instead of non-refreshable token #151

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

andig
Copy link

@andig andig commented Jan 26, 2024

Description

Authenticate using oauth2 token sources instead of tokens. Authentication is handled using an http.Transport rather than passing authHeaders around. Creation of the token source could be further simplified by implementing #24. Happy to provide a PR, too.

If you want to take this one step further, one could also replace the token source by an http client and leave the entire setup to the consumer to also address #23.

Fixes #29

Type of change

Please select all options that apply to this change:

  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Bug fix (non-breaking change which fixes an issue)
  • Documentation update

Checklist:

Confirm you have completed the following steps:

  • My code follows the style of this project.
  • I have performed a self-review of my code.
  • I have made corresponding updates to the documentation.
  • I have added/updated unit tests to cover my changes.

Copy link

@derekargueta derekargueta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks awesome, thanks!

If you want to take this one step further, one could also replace the token source by an http client and leave the entire setup to the consumer to also address #23.

this would be a great next step afterwards

@sethterashima
Copy link
Collaborator

Thanks for the PR. Specifying the http.Client will have other useful applications.

APIs that require the Client to handle OAuth need to document this requirement.

@@ -114,7 +114,11 @@ func (p *oauthPayload) domain() string {

// New returns an [Account] that can be used to fetch a [vehicle.Vehicle].
// Optional userAgent can be passed in - otherwise it will be generated from code
func New(oauthToken, userAgent string) (*Account, error) {
func New(ts oauth2.TokenSource, userAgent string) (*Account, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently tokens are invalidated immediately after another refresh token get issued. I'm afraid that building this functionality is going to create downstream errors for folks who manages their tokens cross app/manually

Copy link
Author

@andig andig Feb 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be fair, this PR is from before that change. Actually, I think this makes this PR even more important. How you build your token source is up to you.

@andig
Copy link
Author

andig commented Feb 3, 2024

@sethterashima remaining compile errors are in config/example. I really wonder how to handle config. Specifying a short-lived token one time makes even less sense now than before.

If this PR is a good idea depends on how you look at accounts: if that's a short-lived thing that should be regenerated all the time or something long-lived that requires a source of refreshed tokens?


wakeLock sync.Mutex
lastPoke time.Time
}

// NewConnection creates a Connection.
func NewConnection(vin string, authHeader, serverURL, userAgent string) *Connection {
func NewConnection(vin string, client *http.Client, serverURL, userAgent string) *Connection {
Copy link
Collaborator

@sethterashima sethterashima Feb 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requirements for client need to be clearly documented, ideally with an example of setting up a token source. Alternatively, we could create a NewConnectionWithClient(client *http.Client, vin string ...) *Connection function (with documentation + example).

This could be mirrored in a account.NewWithClient(...) function.

@sethterashima
Copy link
Collaborator

sethterashima commented Feb 13, 2024

The initial release was focused around the proxy, with the aim of creating a drop-in replacement for existing 3P apps, and this informed the API design around tokens. I think having account "just work" without worrying about refresh tokens would be an improvement.

An extra, reverse-compatible RefreshTokenName field in Config should work.

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

Successfully merging this pull request may close these issues.

Proposal: create Account from refreshing oauth2.TokenSource
4 participants