tokensource

package
v1.39.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2019 License: MIT Imports: 4 Imported by: 2

README

tokensource

GoDoc

Golang OAuth2 Token Source that notifies the refresh operations.

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
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Notify

type Notify struct {
	*sync.Mutex
	// contains filtered or unexported fields
}

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

func NewNotify(ctx context.Context, config *oauth2.Config, token *oauth2.Token) *Notify

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

func (notify *Notify) Client(ctx context.Context) *http.Client

Client builds an OAuth2-authenticated HTTP client like oauth2.Config.Client does.

func (*Notify) HasChanged

func (notify *Notify) HasChanged() bool

HasChanged returns if we have a new token different from the one passed to NewNotify.

func (*Notify) Token

func (notify *Notify) Token() (*oauth2.Token, error)

Token implements oauth2.TokenSource returning a refreshed token if needed. Any update will be registered to make HasChanged() return true. It is threadsafe as the library requires.

type UpdateHook

type UpdateHook func(token *oauth2.Token) error

UpdateHook is a function that can be passed to the constructor that will be called when the token updates.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL