oauth2

module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: MIT

README

license Go Report Card Go Reference

oauth2

https://github.com/udhos/oauth2 implements the oauth2 client_credentials flow with singleflight and plugable cache interface.

Created by gh-md-toc

Features

  • oauth2 client_credentials flow.
  • plugable cache.
  • default memory cache.
  • filesystem cache.
  • testing-only error cache.
  • redis cache.
  • singleflight.
  • debug logs.

Usage

import "github.com/udhos/oauth2/clientcredentials"
import "github.com/udhos/oauth2/cache/rediscache"

cache, errRedis := rediscache.New("localhost:6379::my-cache-key")
if errRedis != nil {
    log.Fatalf("redis: %v", errRedis)
}

options := clientcredentials.Options{
    TokenURL:     "https://token-server/token",
    ClientID:     "client-id",
    ClientSecret: "client-secret",
    Scope:        "scope1 scope2",
    HTTPClient:   http.DefaultClient,
    Cache:        cache,
}

client := clientcredentials.New(options)

req, errReq := http.NewRequestWithContext(context.TODO(), "GET", "https://server/resource", nil)
if errReq != nil {
    log.Fatalf("request: %v", errReq)
}

resp, errDo := client.Do(req)
if errDo != nil {
    log.Fatalf("do: %v", errDo)
}
defer resp.Body.Close()

Example client

See cmd/oauth2-client-example/main.go.

Test with example client

Test using this token server: https://oauth.tools/collection/1599045253169-GHF

go install github.com/udhos/oauth2/cmd/oauth2-client-example@latest

oauth2-client-example -tokenURL https://login-demo.curity.io/oauth/v2/oauth-token -clientID demo-backend-client -clientSecret MJlO3binatD9jk1

oauth2-client-example -tokenURL https://login-demo.curity.io/oauth/v2/oauth-token -clientID demo-backend-client -clientSecret MJlO3binatD9jk1 -cache file:/tmp/cache

oauth2-client-example -tokenURL https://login-demo.curity.io/oauth/v2/oauth-token -clientID demo-backend-client -clientSecret MJlO3binatD9jk1 -cache error

oauth2-client-example -tokenURL https://login-demo.curity.io/oauth/v2/oauth-token -clientID demo-backend-client -clientSecret MJlO3binatD9jk1 -cache redis:localhost:6379::

oauth2-client-example -tokenURL https://login-demo.curity.io/oauth/v2/oauth-token -clientID demo-backend-client -clientSecret MJlO3binatD9jk1 -cache redis:localhost:6379::oauth2-client-example

Test singleflight with example client

Run token server at: http://localhost:8080/oauth/token

Run server at: http://localhost:8000/v1/hello

Cache error makes sure every request retrieves a new token: -cache error.

  1. Send requests with singlefligh:
oauth2-client-example -tokenURL http://localhost:8080/oauth/token -targetURL http://localhost:8000/v1/hello -cache error -interval 0 -concurrent -count 10
  1. Send requests WITHOUT singlefligh:
oauth2-client-example -tokenURL http://localhost:8080/oauth/token -targetURL http://localhost:8000/v1/hello -cache error -interval 0 -concurrent -count 10 -disableSingleflight

Test caches

Set the cache with the env var CACHE, then run the tests.

# Test file cache
export CACHE=file:/tmp/cache
go test -race ./...

# Test redis cache
./run-redis-local.sh
export CACHE=redis:localhost:6379::oauth2-client-example
go test -race ./...

Development

git clone https://github.com/udhos/oauth2
cd oauth2
./build.sh

References

Cache token / transport confusion

Directories

Path Synopsis
Package cache provides cache implementations.
Package cache provides cache implementations.
errorcache
Package errorcache implements a cache.
Package errorcache implements a cache.
filecache
Package filecache implements a cache.
Package filecache implements a cache.
rediscache
Package rediscache implements a cache.
Package rediscache implements a cache.
Package clientcredentials helps with oauth2 client-credentials flow.
Package clientcredentials helps with oauth2 client-credentials flow.
cmd
oauth2-client-example
Package main implements the tool.
Package main implements the tool.
Package token implements Token.
Package token implements Token.

Jump to

Keyboard shortcuts

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