README
¶
tokensource
Package tokensource
has a Golang OAuth2 Token Source that notifies the refresh operations.
This is useful to react to those events and store the new token in your database.
Install
import (
"libs.altipla.consulting/tokensource"
)
Basic usage
Basic usage with the HasChanged()
getter:
package main
import (
"libs.altipla.consulting/tokensource"
"golang.org/x/oauth2"
"golang.org/x/oauth2/slack"
)
func Handler(w http.ResponseWriter, r *http.Request) {
authConfig := &oauth2.Config{
ClientID: "CLIENT_ID_HERE",
ClientSecret: "CLIENT_SECRET_HERE",
Scopes: []string{"SCOPE_FOO", "SCOPE_BAR"},
Endpoint: slack.Endpoint,
RedirectURL: "https://www.example.com/oauth2/redirect",
}
token, err := authConfig.Exchange(r.Context(), r.FormValue("code"))
if err != nil {
processError(err)
return
}
storeTokenInDatabase(token)
// ------------------------------------------------------------------------------
// Everything under this comment is repeated every time you want to use the token
ts := tokensource.NewNotify(r.Context(), authConfig, token)
defer updateToken(ts)
// use ts.Client(r.Context()) to make the requests
}
func updateToken(ts *tokensource.Notify) {
if ts.HasChanged() {
token, err := ts.Token()
if err != nil {
processError(err)
return
}
storeTokenInDatabase(token)
}
}
Notification callback
Notification callback when the token updates:
package main
import (
"libs.altipla.consulting/tokensource"
"golang.org/x/oauth2"
"golang.org/x/oauth2/slack"
)
func Handler(w http.ResponseWriter, r *http.Request) {
authConfig := &oauth2.Config{
ClientID: "CLIENT_ID_HERE",
ClientSecret: "CLIENT_SECRET_HERE",
Scopes: []string{"SCOPE_FOO", "SCOPE_BAR"},
Endpoint: slack.Endpoint,
RedirectURL: "https://www.example.com/oauth2/redirect",
}
token, err := authConfig.Exchange(r.Context(), r.FormValue("code"))
if err != nil {
processError(err)
return
}
storeTokenInDatabase(token)
// ------------------------------------------------------------------------------
// Everything under this comment is repeated every time you want to use the token
ts := tokensource.NewNotifyHook(r.Context(), authConfig, token, storeTokenInDatabase)
// use ts.Client(r.Context()) to make the requests
}
Contributing
You can make pull requests or create issues in GitHub. Any code you send should be formatted using make gofmt
.
Running tests
Run the tests:
make test
License
Documentation
¶
Overview ¶
Package tokensource has a Golang OAuth2 Token Source that notifies the refresh operations.
This is useful to react to those events and store the new token in your database.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Notify ¶
Notify is an oauth2.TokenSource that tracks changes in the token to be able to store the updated token after finishing the operations.
func NewNotify ¶
NewNotify builds a new oauth2.TokenSource that alerts when a token changes when calling HasChanged() after finishing all calls.
func NewNotifyHook ¶
func NewNotifyHook(ctx context.Context, config *oauth2.Config, token *oauth2.Token, hook UpdateHook) *Notify
NewNotifyHook builds a new oauth2.TokenSource that alerts when a token changes through a hook that will be called when it happens.
func (*Notify) Client ¶
Client builds an OAuth2-authenticated HTTP client like oauth2.Config.Client does.
func (*Notify) HasChanged ¶
HasChanged returns if we have a new token different from the one passed to NewNotify.
type UpdateHook ¶
UpdateHook is a function that can be passed to the constructor that will be called when the token updates.