Skip to content
/ oauth2 Public

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

License

Notifications You must be signed in to change notification settings

udhos/oauth2

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published